blob: 57784c21eec8705468776fef6cd32a5ebeba37ab [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
77 // Anything can be extended to ppc long double.
78 if (VT == MVT::ppcf128)
79 return true;
80
81 // PPC long double cannot be shrunk to anything though.
82 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
83 return false;
84
Dale Johannesenf04afdb2007-08-30 00:23:21 +000085 // convert modifies in place, so make a copy.
86 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000087 return Val2.convert(*MVTToAPFloatSemantics(VT),
88 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000089}
90
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000094
Evan Chenga8df1662006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000098 // Look through a bit convert.
99 if (N->getOpcode() == ISD::BIT_CONVERT)
100 N = N->getOperand(0).Val;
101
Evan Chenga8df1662006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000103
104 unsigned i = 0, e = N->getNumOperands();
105
106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
109
110 // Do not accept an all-undef vector.
111 if (i == e) return false;
112
113 // Do not accept build_vectors that aren't all constants or which have non-~0
114 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000115 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000116 if (isa<ConstantSDNode>(NotZero)) {
117 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
118 return false;
119 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000120 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
121 convertToAPInt().isAllOnesValue())
122 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000123 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000124 return false;
125
126 // Okay, we have at least one ~0 value, check to see if the rest match or are
127 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000128 for (++i; i != e; ++i)
129 if (N->getOperand(i) != NotZero &&
130 N->getOperand(i).getOpcode() != ISD::UNDEF)
131 return false;
132 return true;
133}
134
135
Evan Cheng4a147842006-03-26 09:50:58 +0000136/// isBuildVectorAllZeros - Return true if the specified node is a
137/// BUILD_VECTOR where all of the elements are 0 or undef.
138bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000139 // Look through a bit convert.
140 if (N->getOpcode() == ISD::BIT_CONVERT)
141 N = N->getOperand(0).Val;
142
Evan Cheng4a147842006-03-26 09:50:58 +0000143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000144
145 unsigned i = 0, e = N->getNumOperands();
146
147 // Skip over all of the undef values.
148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
149 ++i;
150
151 // Do not accept an all-undef vector.
152 if (i == e) return false;
153
154 // Do not accept build_vectors that aren't all constants or which have non-~0
155 // elements.
156 SDOperand Zero = N->getOperand(i);
157 if (isa<ConstantSDNode>(Zero)) {
158 if (!cast<ConstantSDNode>(Zero)->isNullValue())
159 return false;
160 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000162 return false;
163 } else
164 return false;
165
166 // Okay, we have at least one ~0 value, check to see if the rest match or are
167 // undefs.
168 for (++i; i != e; ++i)
169 if (N->getOperand(i) != Zero &&
170 N->getOperand(i).getOpcode() != ISD::UNDEF)
171 return false;
172 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000173}
174
Evan Chengefec7512008-02-18 23:04:32 +0000175/// isScalarToVector - Return true if the specified node is a
176/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
177/// element is not an undef.
178bool ISD::isScalarToVector(const SDNode *N) {
179 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
180 return true;
181
182 if (N->getOpcode() != ISD::BUILD_VECTOR)
183 return false;
184 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
185 return false;
186 unsigned NumElems = N->getNumOperands();
187 for (unsigned i = 1; i < NumElems; ++i) {
188 SDOperand V = N->getOperand(i);
189 if (V.getOpcode() != ISD::UNDEF)
190 return false;
191 }
192 return true;
193}
194
195
Evan Chengbb81d972008-01-31 09:59:15 +0000196/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengfc718542008-02-04 23:10:38 +0000197/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Chengbb81d972008-01-31 09:59:15 +0000198/// is 0).
199bool ISD::isDebugLabel(const SDNode *N) {
200 SDOperand Zero;
201 if (N->getOpcode() == ISD::LABEL)
202 Zero = N->getOperand(2);
203 else if (N->isTargetOpcode() &&
204 N->getTargetOpcode() == TargetInstrInfo::LABEL)
205 // Chain moved to last operand.
206 Zero = N->getOperand(1);
207 else
208 return false;
209 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
210}
211
Chris Lattnerc3aae252005-01-07 07:46:32 +0000212/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
213/// when given the operation for (X op Y).
214ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
215 // To perform this operation, we just need to swap the L and G bits of the
216 // operation.
217 unsigned OldL = (Operation >> 2) & 1;
218 unsigned OldG = (Operation >> 1) & 1;
219 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
220 (OldL << 1) | // New G bit
221 (OldG << 2)); // New L bit.
222}
223
224/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
225/// 'op' is a valid SetCC operation.
226ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
227 unsigned Operation = Op;
228 if (isInteger)
229 Operation ^= 7; // Flip L, G, E bits, but not U.
230 else
231 Operation ^= 15; // Flip all of the condition bits.
232 if (Operation > ISD::SETTRUE2)
233 Operation &= ~8; // Don't let N and U bits get set.
234 return ISD::CondCode(Operation);
235}
236
237
238/// isSignedOp - For an integer comparison, return 1 if the comparison is a
239/// signed operation and 2 if the result is an unsigned comparison. Return zero
240/// if the operation does not depend on the sign of the input (setne and seteq).
241static int isSignedOp(ISD::CondCode Opcode) {
242 switch (Opcode) {
243 default: assert(0 && "Illegal integer setcc operation!");
244 case ISD::SETEQ:
245 case ISD::SETNE: return 0;
246 case ISD::SETLT:
247 case ISD::SETLE:
248 case ISD::SETGT:
249 case ISD::SETGE: return 1;
250 case ISD::SETULT:
251 case ISD::SETULE:
252 case ISD::SETUGT:
253 case ISD::SETUGE: return 2;
254 }
255}
256
257/// getSetCCOrOperation - Return the result of a logical OR between different
258/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
259/// returns SETCC_INVALID if it is not possible to represent the resultant
260/// comparison.
261ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
262 bool isInteger) {
263 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
264 // Cannot fold a signed integer setcc with an unsigned integer setcc.
265 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000266
Chris Lattnerc3aae252005-01-07 07:46:32 +0000267 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000268
Chris Lattnerc3aae252005-01-07 07:46:32 +0000269 // If the N and U bits get set then the resultant comparison DOES suddenly
270 // care about orderedness, and is true when ordered.
271 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000272 Op &= ~16; // Clear the U bit if the N bit is set.
273
274 // Canonicalize illegal integer setcc's.
275 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
276 Op = ISD::SETNE;
277
Chris Lattnerc3aae252005-01-07 07:46:32 +0000278 return ISD::CondCode(Op);
279}
280
281/// getSetCCAndOperation - Return the result of a logical AND between different
282/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
283/// function returns zero if it is not possible to represent the resultant
284/// comparison.
285ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
286 bool isInteger) {
287 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
288 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000289 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000290
291 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000292 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
293
294 // Canonicalize illegal integer setcc's.
295 if (isInteger) {
296 switch (Result) {
297 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000298 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
299 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
300 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
301 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000302 }
303 }
304
305 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000306}
307
Chris Lattnerb48da392005-01-23 04:39:44 +0000308const TargetMachine &SelectionDAG::getTarget() const {
309 return TLI.getTargetMachine();
310}
311
Jim Laskey58b968b2005-08-17 20:08:02 +0000312//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000313// SDNode Profile Support
314//===----------------------------------------------------------------------===//
315
Jim Laskeydef69b92006-10-27 23:52:51 +0000316/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
317///
Jim Laskey583bd472006-10-27 23:46:08 +0000318static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
319 ID.AddInteger(OpC);
320}
321
322/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
323/// solely with their pointer.
324void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
325 ID.AddPointer(VTList.VTs);
326}
327
Jim Laskeydef69b92006-10-27 23:52:51 +0000328/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
329///
Jim Laskey583bd472006-10-27 23:46:08 +0000330static void AddNodeIDOperands(FoldingSetNodeID &ID,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000331 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000332 for (; NumOps; --NumOps, ++Ops) {
333 ID.AddPointer(Ops->Val);
334 ID.AddInteger(Ops->ResNo);
335 }
Jim Laskey583bd472006-10-27 23:46:08 +0000336}
337
Jim Laskey583bd472006-10-27 23:46:08 +0000338static void AddNodeIDNode(FoldingSetNodeID &ID,
339 unsigned short OpC, SDVTList VTList,
340 const SDOperand *OpList, unsigned N) {
341 AddNodeIDOpcode(ID, OpC);
342 AddNodeIDValueTypes(ID, VTList);
343 AddNodeIDOperands(ID, OpList, N);
344}
345
Roman Levenstein9cac5252008-04-16 16:15:27 +0000346
Jim Laskeydef69b92006-10-27 23:52:51 +0000347/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
348/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000349static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
350 AddNodeIDOpcode(ID, N->getOpcode());
351 // Add the return value info.
352 AddNodeIDValueTypes(ID, N->getVTList());
353 // Add the operand info.
354 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
355
356 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000357 switch (N->getOpcode()) {
358 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000359 case ISD::ARG_FLAGS:
360 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
361 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000362 case ISD::TargetConstant:
363 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000364 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000365 break;
366 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000367 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000368 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000370 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000371 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000372 case ISD::GlobalAddress:
373 case ISD::TargetGlobalTLSAddress:
374 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
376 ID.AddPointer(GA->getGlobal());
377 ID.AddInteger(GA->getOffset());
378 break;
379 }
380 case ISD::BasicBlock:
381 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
382 break;
383 case ISD::Register:
384 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
385 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000386 case ISD::SRCVALUE:
387 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
388 break;
389 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000390 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000391 ID.AddPointer(MO.getValue());
392 ID.AddInteger(MO.getFlags());
393 ID.AddInteger(MO.getOffset());
394 ID.AddInteger(MO.getSize());
395 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000396 break;
397 }
398 case ISD::FrameIndex:
399 case ISD::TargetFrameIndex:
400 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
401 break;
402 case ISD::JumpTable:
403 case ISD::TargetJumpTable:
404 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
405 break;
406 case ISD::ConstantPool:
407 case ISD::TargetConstantPool: {
408 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
409 ID.AddInteger(CP->getAlignment());
410 ID.AddInteger(CP->getOffset());
411 if (CP->isMachineConstantPoolEntry())
412 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
413 else
414 ID.AddPointer(CP->getConstVal());
415 break;
416 }
417 case ISD::LOAD: {
418 LoadSDNode *LD = cast<LoadSDNode>(N);
419 ID.AddInteger(LD->getAddressingMode());
420 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000421 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000422 ID.AddInteger(LD->getAlignment());
423 ID.AddInteger(LD->isVolatile());
424 break;
425 }
426 case ISD::STORE: {
427 StoreSDNode *ST = cast<StoreSDNode>(N);
428 ID.AddInteger(ST->getAddressingMode());
429 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000430 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000431 ID.AddInteger(ST->getAlignment());
432 ID.AddInteger(ST->isVolatile());
433 break;
434 }
Jim Laskey583bd472006-10-27 23:46:08 +0000435 }
436}
437
438//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000439// SelectionDAG Class
440//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000441
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000442/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000443/// SelectionDAG.
444void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000445 // Create a dummy node (which is not added to allnodes), that adds a reference
446 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000447 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000448
Chris Lattner190a4182006-08-04 17:45:20 +0000449 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000450
Chris Lattner190a4182006-08-04 17:45:20 +0000451 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000452 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000453 if (I->use_empty())
454 DeadNodes.push_back(I);
455
456 // Process the worklist, deleting the nodes and adding their uses to the
457 // worklist.
458 while (!DeadNodes.empty()) {
459 SDNode *N = DeadNodes.back();
460 DeadNodes.pop_back();
461
462 // Take the node out of the appropriate CSE map.
463 RemoveNodeFromCSEMaps(N);
464
465 // Next, brutally remove the operand list. This is safe to do, as there are
466 // no cycles in the graph.
467 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000468 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000469 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000470
471 // Now that we removed this operand, see if there are no uses of it left.
472 if (Operand->use_empty())
473 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000474 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000475 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000476 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000477 }
Chris Lattner190a4182006-08-04 17:45:20 +0000478 N->OperandList = 0;
479 N->NumOperands = 0;
480
481 // Finally, remove N itself.
482 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000483 }
484
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000485 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000486 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000487}
488
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000489void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000490 SmallVector<SDNode*, 16> DeadNodes;
491 DeadNodes.push_back(N);
492
493 // Process the worklist, deleting the nodes and adding their uses to the
494 // worklist.
495 while (!DeadNodes.empty()) {
496 SDNode *N = DeadNodes.back();
497 DeadNodes.pop_back();
498
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000499 if (UpdateListener)
500 UpdateListener->NodeDeleted(N);
501
Evan Cheng130a6472006-10-12 20:34:05 +0000502 // Take the node out of the appropriate CSE map.
503 RemoveNodeFromCSEMaps(N);
504
505 // Next, brutally remove the operand list. This is safe to do, as there are
506 // no cycles in the graph.
507 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000508 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000509 Operand->removeUser(std::distance(N->op_begin(), I), N);
Evan Cheng130a6472006-10-12 20:34:05 +0000510
511 // Now that we removed this operand, see if there are no uses of it left.
512 if (Operand->use_empty())
513 DeadNodes.push_back(Operand);
514 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000515 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000516 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000517 }
Evan Cheng130a6472006-10-12 20:34:05 +0000518 N->OperandList = 0;
519 N->NumOperands = 0;
520
521 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000522 AllNodes.erase(N);
523 }
524}
525
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000526void SelectionDAG::DeleteNode(SDNode *N) {
527 assert(N->use_empty() && "Cannot delete a node that is not dead!");
528
529 // First take this out of the appropriate CSE map.
530 RemoveNodeFromCSEMaps(N);
531
Chris Lattner1e111c72005-09-07 05:37:01 +0000532 // Finally, remove uses due to operands of this node, remove from the
533 // AllNodes list, and delete the node.
534 DeleteNodeNotInCSEMaps(N);
535}
536
537void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
538
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000539 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000540 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000541
542 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000543 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000544 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000545 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000546 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000547 }
Chris Lattner65113b22005-11-08 22:07:03 +0000548 N->OperandList = 0;
549 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000550
551 delete N;
552}
553
Chris Lattner149c58c2005-08-16 18:17:10 +0000554/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
555/// correspond to it. This is useful when we're about to delete or repurpose
556/// the node. We don't want future request for structurally identical nodes
557/// to return N anymore.
558void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000559 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000560 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000561 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000562 case ISD::STRING:
563 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
564 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000565 case ISD::CONDCODE:
566 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
567 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000568 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000569 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
570 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000571 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000572 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000573 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000574 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000575 Erased =
576 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000577 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000578 case ISD::VALUETYPE: {
579 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
580 if (MVT::isExtendedVT(VT)) {
581 Erased = ExtendedValueTypeNodes.erase(VT);
582 } else {
583 Erased = ValueTypeNodes[VT] != 0;
584 ValueTypeNodes[VT] = 0;
585 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000586 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000587 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000588 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000589 // Remove it from the CSE Map.
590 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000591 break;
592 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000593#ifndef NDEBUG
594 // Verify that the node was actually in one of the CSE maps, unless it has a
595 // flag result (which cannot be CSE'd) or is one of the special cases that are
596 // not subject to CSE.
597 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000598 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000599 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000600 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000601 assert(0 && "Node is not in map!");
602 }
603#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000604}
605
Chris Lattner8b8749f2005-08-17 19:00:20 +0000606/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
607/// has been taken out and modified in some way. If the specified node already
608/// exists in the CSE maps, do not modify the maps, but return the existing node
609/// instead. If it doesn't exist, add it and return null.
610///
611SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
612 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000613 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000614 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000615
Chris Lattner9f8cc692005-12-19 22:21:21 +0000616 // Check that remaining values produced are not flags.
617 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
618 if (N->getValueType(i) == MVT::Flag)
619 return 0; // Never CSE anything that produces a flag.
620
Chris Lattnera5682852006-08-07 23:03:03 +0000621 SDNode *New = CSEMap.GetOrInsertNode(N);
622 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000623 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000624}
625
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000626/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
627/// were replaced with those specified. If this node is never memoized,
628/// return null, otherwise return a pointer to the slot it would take. If a
629/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000630SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
631 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000632 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000633 return 0; // Never add these nodes.
634
635 // Check that remaining values produced are not flags.
636 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
637 if (N->getValueType(i) == MVT::Flag)
638 return 0; // Never CSE anything that produces a flag.
639
Chris Lattner63e3f142007-02-04 07:28:00 +0000640 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000641 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000642 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000643 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000644}
645
646/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
647/// were replaced with those specified. If this node is never memoized,
648/// return null, otherwise return a pointer to the slot it would take. If a
649/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000650SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
651 SDOperand Op1, SDOperand Op2,
652 void *&InsertPos) {
653 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
654 return 0; // Never add these nodes.
655
656 // Check that remaining values produced are not flags.
657 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
658 if (N->getValueType(i) == MVT::Flag)
659 return 0; // Never CSE anything that produces a flag.
660
Chris Lattner63e3f142007-02-04 07:28:00 +0000661 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000662 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000663 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000664 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
665}
666
667
668/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
669/// were replaced with those specified. If this node is never memoized,
670/// return null, otherwise return a pointer to the slot it would take. If a
671/// node already exists with these operands, the slot will be non-null.
672SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000673 SDOperandPtr Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000674 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000675 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000676 return 0; // Never add these nodes.
677
678 // Check that remaining values produced are not flags.
679 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
680 if (N->getValueType(i) == MVT::Flag)
681 return 0; // Never CSE anything that produces a flag.
682
Jim Laskey583bd472006-10-27 23:46:08 +0000683 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000684 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000685
Evan Cheng9629aba2006-10-11 01:47:58 +0000686 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
687 ID.AddInteger(LD->getAddressingMode());
688 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000689 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000690 ID.AddInteger(LD->getAlignment());
691 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000692 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
693 ID.AddInteger(ST->getAddressingMode());
694 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000695 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000696 ID.AddInteger(ST->getAlignment());
697 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000698 }
Jim Laskey583bd472006-10-27 23:46:08 +0000699
Chris Lattnera5682852006-08-07 23:03:03 +0000700 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000701}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000702
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000703
Chris Lattner78ec3112003-08-11 14:57:33 +0000704SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000705 while (!AllNodes.empty()) {
706 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000707 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000708 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000709 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000710 }
Chris Lattner65113b22005-11-08 22:07:03 +0000711 N->OperandList = 0;
712 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000713 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000714 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000715}
716
Chris Lattner0f2287b2005-04-13 02:38:18 +0000717SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000718 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000719 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
720 MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000721 return getNode(ISD::AND, Op.getValueType(), Op,
722 getConstant(Imm, Op.getValueType()));
723}
724
Chris Lattner36ce6912005-11-29 06:21:05 +0000725SDOperand SelectionDAG::getString(const std::string &Val) {
726 StringSDNode *&N = StringNodes[Val];
727 if (!N) {
728 N = new StringSDNode(Val);
729 AllNodes.push_back(N);
730 }
731 return SDOperand(N, 0);
732}
733
Chris Lattner61b09412006-08-11 21:01:22 +0000734SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Dan Gohman6394b092008-02-08 22:59:30 +0000735 MVT::ValueType EltVT =
736 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
737
738 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
739}
740
741SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000742 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000743
744 MVT::ValueType EltVT =
745 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000746
Dan Gohman6394b092008-02-08 22:59:30 +0000747 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
748 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000749
750 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000751 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000752 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000753 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000754 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000755 SDNode *N = NULL;
756 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
757 if (!MVT::isVector(VT))
758 return SDOperand(N, 0);
759 if (!N) {
760 N = new ConstantSDNode(isT, Val, EltVT);
761 CSEMap.InsertNode(N, IP);
762 AllNodes.push_back(N);
763 }
764
765 SDOperand Result(N, 0);
766 if (MVT::isVector(VT)) {
767 SmallVector<SDOperand, 8> Ops;
768 Ops.assign(MVT::getVectorNumElements(VT), Result);
769 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
770 }
771 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000772}
773
Chris Lattner0bd48932008-01-17 07:00:52 +0000774SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
775 return getConstant(Val, TLI.getPointerTy(), isTarget);
776}
777
778
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000779SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000780 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000781 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000782
Dan Gohman7f321562007-06-25 16:23:39 +0000783 MVT::ValueType EltVT =
784 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000785
Chris Lattnerd8658612005-02-17 20:17:32 +0000786 // Do the map lookup using the actual bit pattern for the floating point
787 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
788 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000789 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000790 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000791 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000792 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000793 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000794 SDNode *N = NULL;
795 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
796 if (!MVT::isVector(VT))
797 return SDOperand(N, 0);
798 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000799 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000800 CSEMap.InsertNode(N, IP);
801 AllNodes.push_back(N);
802 }
803
Dan Gohman7f321562007-06-25 16:23:39 +0000804 SDOperand Result(N, 0);
805 if (MVT::isVector(VT)) {
806 SmallVector<SDOperand, 8> Ops;
807 Ops.assign(MVT::getVectorNumElements(VT), Result);
808 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
809 }
810 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000811}
812
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000813SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
814 bool isTarget) {
815 MVT::ValueType EltVT =
816 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
817 if (EltVT==MVT::f32)
818 return getConstantFP(APFloat((float)Val), VT, isTarget);
819 else
820 return getConstantFP(APFloat(Val), VT, isTarget);
821}
822
Chris Lattnerc3aae252005-01-07 07:46:32 +0000823SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000824 MVT::ValueType VT, int Offset,
825 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000826 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000827
828 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
829 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000830 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000831 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
832 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
833 }
834
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000835 if (GVar && GVar->isThreadLocal())
836 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
837 else
838 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000839
Jim Laskey583bd472006-10-27 23:46:08 +0000840 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000841 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000842 ID.AddPointer(GV);
843 ID.AddInteger(Offset);
844 void *IP = 0;
845 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
846 return SDOperand(E, 0);
847 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
848 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000849 AllNodes.push_back(N);
850 return SDOperand(N, 0);
851}
852
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000853SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
854 bool isTarget) {
855 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000856 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000857 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000858 ID.AddInteger(FI);
859 void *IP = 0;
860 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
861 return SDOperand(E, 0);
862 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
863 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000864 AllNodes.push_back(N);
865 return SDOperand(N, 0);
866}
867
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000868SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
869 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000870 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000871 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000872 ID.AddInteger(JTI);
873 void *IP = 0;
874 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
875 return SDOperand(E, 0);
876 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
877 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000878 AllNodes.push_back(N);
879 return SDOperand(N, 0);
880}
881
Evan Chengb8973bd2006-01-31 22:23:14 +0000882SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000883 unsigned Alignment, int Offset,
884 bool isTarget) {
885 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000886 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000887 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000888 ID.AddInteger(Alignment);
889 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000890 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000891 void *IP = 0;
892 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
893 return SDOperand(E, 0);
894 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
895 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000896 AllNodes.push_back(N);
897 return SDOperand(N, 0);
898}
899
Chris Lattnerc3aae252005-01-07 07:46:32 +0000900
Evan Chengd6594ae2006-09-12 21:00:35 +0000901SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
902 MVT::ValueType VT,
903 unsigned Alignment, int Offset,
904 bool isTarget) {
905 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000906 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000907 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000908 ID.AddInteger(Alignment);
909 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000910 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000911 void *IP = 0;
912 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
913 return SDOperand(E, 0);
914 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
915 CSEMap.InsertNode(N, IP);
916 AllNodes.push_back(N);
917 return SDOperand(N, 0);
918}
919
920
Chris Lattnerc3aae252005-01-07 07:46:32 +0000921SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000922 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000923 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000924 ID.AddPointer(MBB);
925 void *IP = 0;
926 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
927 return SDOperand(E, 0);
928 SDNode *N = new BasicBlockSDNode(MBB);
929 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000930 AllNodes.push_back(N);
931 return SDOperand(N, 0);
932}
933
Duncan Sands276dcbd2008-03-21 09:14:45 +0000934SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
935 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000936 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000937 ID.AddInteger(Flags.getRawBits());
938 void *IP = 0;
939 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
940 return SDOperand(E, 0);
941 SDNode *N = new ARG_FLAGSSDNode(Flags);
942 CSEMap.InsertNode(N, IP);
943 AllNodes.push_back(N);
944 return SDOperand(N, 0);
945}
946
Chris Lattner15e4b012005-07-10 00:07:11 +0000947SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000948 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000949 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000950
Duncan Sandsf411b832007-10-17 13:49:58 +0000951 SDNode *&N = MVT::isExtendedVT(VT) ?
952 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
953
954 if (N) return SDOperand(N, 0);
955 N = new VTSDNode(VT);
956 AllNodes.push_back(N);
957 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000958}
959
Chris Lattnerc3aae252005-01-07 07:46:32 +0000960SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
961 SDNode *&N = ExternalSymbols[Sym];
962 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000963 N = new ExternalSymbolSDNode(false, Sym, VT);
964 AllNodes.push_back(N);
965 return SDOperand(N, 0);
966}
967
Chris Lattner809ec112006-01-28 10:09:25 +0000968SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
969 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000970 SDNode *&N = TargetExternalSymbols[Sym];
971 if (N) return SDOperand(N, 0);
972 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000973 AllNodes.push_back(N);
974 return SDOperand(N, 0);
975}
976
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000977SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
978 if ((unsigned)Cond >= CondCodeNodes.size())
979 CondCodeNodes.resize(Cond+1);
980
Chris Lattner079a27a2005-08-09 20:40:02 +0000981 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000982 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000983 AllNodes.push_back(CondCodeNodes[Cond]);
984 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000985 return SDOperand(CondCodeNodes[Cond], 0);
986}
987
Chris Lattner0fdd7682005-08-30 22:38:38 +0000988SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000989 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000990 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000991 ID.AddInteger(RegNo);
992 void *IP = 0;
993 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
994 return SDOperand(E, 0);
995 SDNode *N = new RegisterSDNode(RegNo, VT);
996 CSEMap.InsertNode(N, IP);
997 AllNodes.push_back(N);
998 return SDOperand(N, 0);
999}
1000
Dan Gohman69de1932008-02-06 22:27:42 +00001001SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001002 assert((!V || isa<PointerType>(V->getType())) &&
1003 "SrcValue is not a pointer?");
1004
Jim Laskey583bd472006-10-27 23:46:08 +00001005 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001006 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001007 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001008
Chris Lattner61b09412006-08-11 21:01:22 +00001009 void *IP = 0;
1010 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1011 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001012
1013 SDNode *N = new SrcValueSDNode(V);
1014 CSEMap.InsertNode(N, IP);
1015 AllNodes.push_back(N);
1016 return SDOperand(N, 0);
1017}
1018
Dan Gohman36b5c132008-04-07 19:35:22 +00001019SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001020 const Value *v = MO.getValue();
1021 assert((!v || isa<PointerType>(v->getType())) &&
1022 "SrcValue is not a pointer?");
1023
1024 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001025 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001026 ID.AddPointer(v);
1027 ID.AddInteger(MO.getFlags());
1028 ID.AddInteger(MO.getOffset());
1029 ID.AddInteger(MO.getSize());
1030 ID.AddInteger(MO.getAlignment());
1031
1032 void *IP = 0;
1033 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1034 return SDOperand(E, 0);
1035
1036 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001037 CSEMap.InsertNode(N, IP);
1038 AllNodes.push_back(N);
1039 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001040}
1041
Chris Lattner37ce9df2007-10-15 17:47:20 +00001042/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1043/// specified value type.
1044SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1045 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1046 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1047 const Type *Ty = MVT::getTypeForValueType(VT);
1048 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1049 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1050 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1051}
1052
1053
Chris Lattner51dabfb2006-10-14 00:41:01 +00001054SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1055 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056 // These setcc operations always fold.
1057 switch (Cond) {
1058 default: break;
1059 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001060 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001062 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001063
1064 case ISD::SETOEQ:
1065 case ISD::SETOGT:
1066 case ISD::SETOGE:
1067 case ISD::SETOLT:
1068 case ISD::SETOLE:
1069 case ISD::SETONE:
1070 case ISD::SETO:
1071 case ISD::SETUO:
1072 case ISD::SETUEQ:
1073 case ISD::SETUNE:
1074 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1075 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001077
Chris Lattner67255a12005-04-07 18:14:58 +00001078 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001079 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001080 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001081 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001082
Chris Lattnerc3aae252005-01-07 07:46:32 +00001083 switch (Cond) {
1084 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001085 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1086 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001087 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1088 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1089 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1090 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1091 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1092 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1093 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1094 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001095 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096 }
Chris Lattner67255a12005-04-07 18:14:58 +00001097 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001098 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001099 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001100 // No compile time operations on this type yet.
1101 if (N1C->getValueType(0) == MVT::ppcf128)
1102 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001103
1104 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001106 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001107 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1108 return getNode(ISD::UNDEF, VT);
1109 // fall through
1110 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1111 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1112 return getNode(ISD::UNDEF, VT);
1113 // fall through
1114 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001115 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001116 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1117 return getNode(ISD::UNDEF, VT);
1118 // fall through
1119 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1120 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1121 return getNode(ISD::UNDEF, VT);
1122 // fall through
1123 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1124 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1125 return getNode(ISD::UNDEF, VT);
1126 // fall through
1127 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001128 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001129 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1130 return getNode(ISD::UNDEF, VT);
1131 // fall through
1132 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001133 R==APFloat::cmpEqual, VT);
1134 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1135 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1136 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1137 R==APFloat::cmpEqual, VT);
1138 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1139 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1140 R==APFloat::cmpLessThan, VT);
1141 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1142 R==APFloat::cmpUnordered, VT);
1143 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1144 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 }
1146 } else {
1147 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001148 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001149 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001150 }
1151
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001152 // Could not fold it.
1153 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001154}
1155
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001156/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1157/// use this predicate to simplify operations downstream.
1158bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1159 unsigned BitWidth = Op.getValueSizeInBits();
1160 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1161}
1162
Dan Gohmanea859be2007-06-22 14:59:07 +00001163/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1164/// this predicate to simplify operations downstream. Mask is known to be zero
1165/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001166bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001167 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001168 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001169 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1170 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1171 return (KnownZero & Mask) == Mask;
1172}
1173
1174/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1175/// known to be either zero or one and return them in the KnownZero/KnownOne
1176/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1177/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001178void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001179 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001180 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001181 unsigned BitWidth = Mask.getBitWidth();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001182 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1183 "Mask size mismatches value type size!");
1184
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001185 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001186 if (Depth == 6 || Mask == 0)
1187 return; // Limit search depth.
1188
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001189 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001190
1191 switch (Op.getOpcode()) {
1192 case ISD::Constant:
1193 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001194 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001195 KnownZero = ~KnownOne & Mask;
1196 return;
1197 case ISD::AND:
1198 // If either the LHS or the RHS are Zero, the result is zero.
1199 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001200 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1201 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001202 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1203 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1204
1205 // Output known-1 bits are only known if set in both the LHS & RHS.
1206 KnownOne &= KnownOne2;
1207 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1208 KnownZero |= KnownZero2;
1209 return;
1210 case ISD::OR:
1211 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001212 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1213 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001214 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1215 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1216
1217 // Output known-0 bits are only known if clear in both the LHS & RHS.
1218 KnownZero &= KnownZero2;
1219 // Output known-1 are known to be set if set in either the LHS | RHS.
1220 KnownOne |= KnownOne2;
1221 return;
1222 case ISD::XOR: {
1223 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1224 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1225 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1226 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1227
1228 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001229 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001230 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1231 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1232 KnownZero = KnownZeroOut;
1233 return;
1234 }
1235 case ISD::SELECT:
1236 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1237 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1238 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1239 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1240
1241 // Only known if known in both the LHS and RHS.
1242 KnownOne &= KnownOne2;
1243 KnownZero &= KnownZero2;
1244 return;
1245 case ISD::SELECT_CC:
1246 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1247 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1248 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1249 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1250
1251 // Only known if known in both the LHS and RHS.
1252 KnownOne &= KnownOne2;
1253 KnownZero &= KnownZero2;
1254 return;
1255 case ISD::SETCC:
1256 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001257 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1258 BitWidth > 1)
1259 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001260 return;
1261 case ISD::SHL:
1262 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1263 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001264 unsigned ShAmt = SA->getValue();
1265
1266 // If the shift count is an invalid immediate, don't do anything.
1267 if (ShAmt >= BitWidth)
1268 return;
1269
1270 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001271 KnownZero, KnownOne, Depth+1);
1272 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001273 KnownZero <<= ShAmt;
1274 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001275 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001276 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001277 }
1278 return;
1279 case ISD::SRL:
1280 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1281 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001282 unsigned ShAmt = SA->getValue();
1283
Dan Gohmand4cf9922008-02-26 18:50:50 +00001284 // If the shift count is an invalid immediate, don't do anything.
1285 if (ShAmt >= BitWidth)
1286 return;
1287
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001288 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001289 KnownZero, KnownOne, Depth+1);
1290 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001291 KnownZero = KnownZero.lshr(ShAmt);
1292 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001293
Dan Gohman72d2fd52008-02-13 22:43:25 +00001294 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001295 KnownZero |= HighBits; // High bits known zero.
1296 }
1297 return;
1298 case ISD::SRA:
1299 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001300 unsigned ShAmt = SA->getValue();
1301
Dan Gohmand4cf9922008-02-26 18:50:50 +00001302 // If the shift count is an invalid immediate, don't do anything.
1303 if (ShAmt >= BitWidth)
1304 return;
1305
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001306 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001307 // If any of the demanded bits are produced by the sign extension, we also
1308 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001309 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1310 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001311 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001312
1313 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1314 Depth+1);
1315 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001316 KnownZero = KnownZero.lshr(ShAmt);
1317 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001318
1319 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001320 APInt SignBit = APInt::getSignBit(BitWidth);
1321 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001322
Dan Gohmanca93a432008-02-20 16:30:17 +00001323 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001324 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001325 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001326 KnownOne |= HighBits; // New bits are known one.
1327 }
1328 }
1329 return;
1330 case ISD::SIGN_EXTEND_INREG: {
1331 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001332 unsigned EBits = MVT::getSizeInBits(EVT);
Dan Gohmanea859be2007-06-22 14:59:07 +00001333
1334 // Sign extension. Compute the demanded bits in the result that are not
1335 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001336 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001337
Dan Gohman977a76f2008-02-13 22:28:48 +00001338 APInt InSignBit = APInt::getSignBit(EBits);
1339 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001340
1341 // If the sign extended bits are demanded, we know that the sign
1342 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001343 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001344 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001345 InputDemandedBits |= InSignBit;
1346
1347 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1348 KnownZero, KnownOne, Depth+1);
1349 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1350
1351 // If the sign bit of the input is known set or clear, then we know the
1352 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001353 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001354 KnownZero |= NewBits;
1355 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001356 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001357 KnownOne |= NewBits;
1358 KnownZero &= ~NewBits;
1359 } else { // Input sign bit unknown
1360 KnownZero &= ~NewBits;
1361 KnownOne &= ~NewBits;
1362 }
1363 return;
1364 }
1365 case ISD::CTTZ:
1366 case ISD::CTLZ:
1367 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001368 unsigned LowBits = Log2_32(BitWidth)+1;
1369 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1370 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001371 return;
1372 }
1373 case ISD::LOAD: {
1374 if (ISD::isZEXTLoad(Op.Val)) {
1375 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001376 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohman977a76f2008-02-13 22:28:48 +00001377 unsigned MemBits = MVT::getSizeInBits(VT);
1378 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001379 }
1380 return;
1381 }
1382 case ISD::ZERO_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001383 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1384 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001385 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1386 APInt InMask = Mask;
1387 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001388 KnownZero.trunc(InBits);
1389 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001390 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001391 KnownZero.zext(BitWidth);
1392 KnownOne.zext(BitWidth);
1393 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001394 return;
1395 }
1396 case ISD::SIGN_EXTEND: {
1397 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001398 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001399 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001400 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1401 APInt InMask = Mask;
1402 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001403
1404 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001405 // bit is demanded. Temporarily set this bit in the mask for our callee.
1406 if (NewBits.getBoolValue())
1407 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001408
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001409 KnownZero.trunc(InBits);
1410 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001411 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1412
1413 // Note if the sign bit is known to be zero or one.
1414 bool SignBitKnownZero = KnownZero.isNegative();
1415 bool SignBitKnownOne = KnownOne.isNegative();
1416 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1417 "Sign bit can't be known to be both zero and one!");
1418
1419 // If the sign bit wasn't actually demanded by our caller, we don't
1420 // want it set in the KnownZero and KnownOne result values. Reset the
1421 // mask and reapply it to the result values.
1422 InMask = Mask;
1423 InMask.trunc(InBits);
1424 KnownZero &= InMask;
1425 KnownOne &= InMask;
1426
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001427 KnownZero.zext(BitWidth);
1428 KnownOne.zext(BitWidth);
1429
Dan Gohman977a76f2008-02-13 22:28:48 +00001430 // If the sign bit is known zero or one, the top bits match.
1431 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001432 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001433 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001434 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001435 return;
1436 }
1437 case ISD::ANY_EXTEND: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001438 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1439 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001440 APInt InMask = Mask;
1441 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001442 KnownZero.trunc(InBits);
1443 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001444 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001445 KnownZero.zext(BitWidth);
1446 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001447 return;
1448 }
1449 case ISD::TRUNCATE: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001450 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1451 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman977a76f2008-02-13 22:28:48 +00001452 APInt InMask = Mask;
1453 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001454 KnownZero.zext(InBits);
1455 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001456 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001457 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001458 KnownZero.trunc(BitWidth);
1459 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001460 break;
1461 }
1462 case ISD::AssertZext: {
1463 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001464 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001465 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1466 KnownOne, Depth+1);
1467 KnownZero |= (~InMask) & Mask;
1468 return;
1469 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001470 case ISD::FGETSIGN:
1471 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001472 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001473 return;
1474
Dan Gohmanea859be2007-06-22 14:59:07 +00001475 case ISD::ADD: {
1476 // If either the LHS or the RHS are Zero, the result is zero.
1477 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1478 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1479 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1480 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1481
1482 // Output known-0 bits are known if clear or set in both the low clear bits
1483 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1484 // low 3 bits clear.
Dan Gohman977a76f2008-02-13 22:28:48 +00001485 unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(),
1486 KnownZero2.countTrailingOnes());
Dan Gohmanea859be2007-06-22 14:59:07 +00001487
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001488 KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1489 KnownOne = APInt(BitWidth, 0);
Dan Gohmanea859be2007-06-22 14:59:07 +00001490 return;
1491 }
1492 case ISD::SUB: {
1493 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1494 if (!CLHS) return;
1495
1496 // We know that the top bits of C-X are clear if X contains less bits
1497 // than C (i.e. no wrap-around can happen). For example, 20-X is
1498 // positive if we can prove that X is >= 0 and < 16.
Dan Gohman977a76f2008-02-13 22:28:48 +00001499 if (CLHS->getAPIntValue().isNonNegative()) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001500 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1501 // NLZ can't be BitWidth with no sign bit
Chris Lattner423be622008-02-14 18:48:56 +00001502 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001503 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1504
1505 // If all of the MaskV bits are known to be zero, then we know the output
1506 // top bits are zero, because we now know that the output is from [0-C].
1507 if ((KnownZero & MaskV) == MaskV) {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001508 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1509 // Top bits known zero.
1510 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1511 KnownOne = APInt(BitWidth, 0); // No one bits known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001512 } else {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001513 KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known.
Dan Gohmanea859be2007-06-22 14:59:07 +00001514 }
1515 }
1516 return;
1517 }
1518 default:
1519 // Allow the target to implement this method for its nodes.
1520 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1521 case ISD::INTRINSIC_WO_CHAIN:
1522 case ISD::INTRINSIC_W_CHAIN:
1523 case ISD::INTRINSIC_VOID:
1524 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1525 }
1526 return;
1527 }
1528}
1529
1530/// ComputeNumSignBits - Return the number of times the sign bit of the
1531/// register is replicated into the other bits. We know that at least 1 bit
1532/// is always equal to the sign bit (itself), but other cases can give us
1533/// information. For example, immediately after an "SRA X, 2", we know that
1534/// the top 3 bits are all equal to each other, so we return 3.
1535unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1536 MVT::ValueType VT = Op.getValueType();
1537 assert(MVT::isInteger(VT) && "Invalid VT!");
1538 unsigned VTBits = MVT::getSizeInBits(VT);
1539 unsigned Tmp, Tmp2;
1540
1541 if (Depth == 6)
1542 return 1; // Limit search depth.
1543
1544 switch (Op.getOpcode()) {
1545 default: break;
1546 case ISD::AssertSext:
1547 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1548 return VTBits-Tmp+1;
1549 case ISD::AssertZext:
1550 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1551 return VTBits-Tmp;
1552
1553 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001554 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1555 // If negative, return # leading ones.
1556 if (Val.isNegative())
1557 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001558
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001559 // Return # leading zeros.
1560 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001561 }
1562
1563 case ISD::SIGN_EXTEND:
1564 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1565 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1566
1567 case ISD::SIGN_EXTEND_INREG:
1568 // Max of the input and what this extends.
1569 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1570 Tmp = VTBits-Tmp+1;
1571
1572 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1573 return std::max(Tmp, Tmp2);
1574
1575 case ISD::SRA:
1576 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1577 // SRA X, C -> adds C sign bits.
1578 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1579 Tmp += C->getValue();
1580 if (Tmp > VTBits) Tmp = VTBits;
1581 }
1582 return Tmp;
1583 case ISD::SHL:
1584 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1585 // shl destroys sign bits.
1586 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1587 if (C->getValue() >= VTBits || // Bad shift.
1588 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1589 return Tmp - C->getValue();
1590 }
1591 break;
1592 case ISD::AND:
1593 case ISD::OR:
1594 case ISD::XOR: // NOT is handled here.
1595 // Logical binary ops preserve the number of sign bits.
1596 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1597 if (Tmp == 1) return 1; // Early out.
1598 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1599 return std::min(Tmp, Tmp2);
1600
1601 case ISD::SELECT:
1602 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1603 if (Tmp == 1) return 1; // Early out.
1604 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1605 return std::min(Tmp, Tmp2);
1606
1607 case ISD::SETCC:
1608 // If setcc returns 0/-1, all bits are sign bits.
1609 if (TLI.getSetCCResultContents() ==
1610 TargetLowering::ZeroOrNegativeOneSetCCResult)
1611 return VTBits;
1612 break;
1613 case ISD::ROTL:
1614 case ISD::ROTR:
1615 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1616 unsigned RotAmt = C->getValue() & (VTBits-1);
1617
1618 // Handle rotate right by N like a rotate left by 32-N.
1619 if (Op.getOpcode() == ISD::ROTR)
1620 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1621
1622 // If we aren't rotating out all of the known-in sign bits, return the
1623 // number that are left. This handles rotl(sext(x), 1) for example.
1624 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1625 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1626 }
1627 break;
1628 case ISD::ADD:
1629 // Add can have at most one carry bit. Thus we know that the output
1630 // is, at worst, one more bit than the inputs.
1631 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1632 if (Tmp == 1) return 1; // Early out.
1633
1634 // Special case decrementing a value (ADD X, -1):
1635 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1636 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001637 APInt KnownZero, KnownOne;
1638 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001639 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1640
1641 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1642 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001643 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001644 return VTBits;
1645
1646 // If we are subtracting one from a positive number, there is no carry
1647 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001648 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001649 return Tmp;
1650 }
1651
1652 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1653 if (Tmp2 == 1) return 1;
1654 return std::min(Tmp, Tmp2)-1;
1655 break;
1656
1657 case ISD::SUB:
1658 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1659 if (Tmp2 == 1) return 1;
1660
1661 // Handle NEG.
1662 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001663 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001664 APInt KnownZero, KnownOne;
1665 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001666 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1667 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1668 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001669 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001670 return VTBits;
1671
1672 // If the input is known to be positive (the sign bit is known clear),
1673 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001674 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001675 return Tmp2;
1676
1677 // Otherwise, we treat this like a SUB.
1678 }
1679
1680 // Sub can have at most one carry bit. Thus we know that the output
1681 // is, at worst, one more bit than the inputs.
1682 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1683 if (Tmp == 1) return 1; // Early out.
1684 return std::min(Tmp, Tmp2)-1;
1685 break;
1686 case ISD::TRUNCATE:
1687 // FIXME: it's tricky to do anything useful for this, but it is an important
1688 // case for targets like X86.
1689 break;
1690 }
1691
1692 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1693 if (Op.getOpcode() == ISD::LOAD) {
1694 LoadSDNode *LD = cast<LoadSDNode>(Op);
1695 unsigned ExtType = LD->getExtensionType();
1696 switch (ExtType) {
1697 default: break;
1698 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001699 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001700 return VTBits-Tmp+1;
1701 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001702 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001703 return VTBits-Tmp;
1704 }
1705 }
1706
1707 // Allow the target to implement this method for its nodes.
1708 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1709 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1710 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1711 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1712 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1713 if (NumBits > 1) return NumBits;
1714 }
1715
1716 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1717 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001718 APInt KnownZero, KnownOne;
1719 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001720 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1721
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001722 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001723 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001724 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001725 Mask = KnownOne;
1726 } else {
1727 // Nothing known.
1728 return 1;
1729 }
1730
1731 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1732 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001733 Mask = ~Mask;
1734 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001735 // Return # leading zeros. We use 'min' here in case Val was zero before
1736 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001737 return std::min(VTBits, Mask.countLeadingZeros());
Dan Gohmanea859be2007-06-22 14:59:07 +00001738}
1739
Chris Lattner51dabfb2006-10-14 00:41:01 +00001740
Evan Chenga844bde2008-02-02 04:07:54 +00001741bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1742 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1743 if (!GA) return false;
1744 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1745 if (!GV) return false;
1746 MachineModuleInfo *MMI = getMachineModuleInfo();
1747 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1748}
1749
1750
Chris Lattnerc3aae252005-01-07 07:46:32 +00001751/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001752///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001753SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001754 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001755 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001756 void *IP = 0;
1757 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1758 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001759 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001760 CSEMap.InsertNode(N, IP);
1761
1762 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001763 return SDOperand(N, 0);
1764}
1765
Chris Lattnerc3aae252005-01-07 07:46:32 +00001766SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1767 SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001768 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001769 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001770 const APInt &Val = C->getAPIntValue();
1771 unsigned BitWidth = MVT::getSizeInBits(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001772 switch (Opcode) {
1773 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001774 case ISD::SIGN_EXTEND:
1775 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001776 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001777 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001778 case ISD::TRUNCATE:
1779 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001780 case ISD::UINT_TO_FP:
1781 case ISD::SINT_TO_FP: {
1782 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001783 // No compile time operations on this type.
1784 if (VT==MVT::ppcf128)
1785 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001786 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1787 (void)apf.convertFromAPInt(Val,
1788 Opcode==ISD::SINT_TO_FP,
1789 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001790 return getConstantFP(apf, VT);
1791 }
Chris Lattner94683772005-12-23 05:30:37 +00001792 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001793 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001794 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001795 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001796 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001797 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001798 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001799 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001800 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001801 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001802 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001803 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001804 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001805 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001806 }
1807 }
1808
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001809 // Constant fold unary operations with a floating point constant operand.
1810 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1811 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001812 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001813 switch (Opcode) {
1814 case ISD::FNEG:
1815 V.changeSign();
1816 return getConstantFP(V, VT);
1817 case ISD::FABS:
1818 V.clearSign();
1819 return getConstantFP(V, VT);
1820 case ISD::FP_ROUND:
1821 case ISD::FP_EXTEND:
1822 // This can return overflow, underflow, or inexact; we don't care.
1823 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001824 (void)V.convert(*MVTToAPFloatSemantics(VT),
1825 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001826 return getConstantFP(V, VT);
1827 case ISD::FP_TO_SINT:
1828 case ISD::FP_TO_UINT: {
1829 integerPart x;
1830 assert(integerPartWidth >= 64);
1831 // FIXME need to be more flexible about rounding mode.
1832 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1833 Opcode==ISD::FP_TO_SINT,
1834 APFloat::rmTowardZero);
1835 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1836 break;
1837 return getConstant(x, VT);
1838 }
1839 case ISD::BIT_CONVERT:
1840 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1841 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1842 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1843 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001844 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001845 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001846 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001847 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001848
1849 unsigned OpOpcode = Operand.Val->getOpcode();
1850 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001851 case ISD::TokenFactor:
Dan Gohmanb91c89d2008-04-14 18:43:25 +00001852 case ISD::MERGE_VALUES:
1853 return Operand; // Factor or merge of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00001854 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001855 case ISD::FP_EXTEND:
1856 assert(MVT::isFloatingPoint(VT) &&
1857 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001858 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001859 if (Operand.getOpcode() == ISD::UNDEF)
1860 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001861 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001862 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001863 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1864 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001865 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001866 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1867 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001868 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1869 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1870 break;
1871 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001872 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1873 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001874 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001875 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1876 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001877 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001878 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001879 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001880 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001881 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1882 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001883 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001884 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1885 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001886 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1887 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1888 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1889 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001890 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001891 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1892 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001893 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001894 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1895 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001896 if (OpOpcode == ISD::TRUNCATE)
1897 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001898 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1899 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001900 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001901 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1902 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001903 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001904 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1905 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001906 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1907 else
1908 return Operand.Val->getOperand(0);
1909 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001910 break;
Chris Lattner35481892005-12-23 00:16:34 +00001911 case ISD::BIT_CONVERT:
1912 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001913 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001914 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001915 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001916 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1917 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001918 if (OpOpcode == ISD::UNDEF)
1919 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001920 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001921 case ISD::SCALAR_TO_VECTOR:
1922 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001923 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001924 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00001925 if (OpOpcode == ISD::UNDEF)
1926 return getNode(ISD::UNDEF, VT);
1927 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1928 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1929 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1930 Operand.getConstantOperandVal(1) == 0 &&
1931 Operand.getOperand(0).getValueType() == VT)
1932 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00001933 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001934 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001935 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1936 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001937 Operand.Val->getOperand(0));
1938 if (OpOpcode == ISD::FNEG) // --X -> X
1939 return Operand.Val->getOperand(0);
1940 break;
1941 case ISD::FABS:
1942 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1943 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1944 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001945 }
1946
Chris Lattner43247a12005-08-25 19:12:10 +00001947 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001948 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001949 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001950 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001951 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001952 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001953 void *IP = 0;
1954 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1955 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001956 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001957 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001958 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001959 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001960 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001961 AllNodes.push_back(N);
1962 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001963}
1964
Chris Lattner36019aa2005-04-18 03:48:41 +00001965
1966
Chris Lattnerc3aae252005-01-07 07:46:32 +00001967SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1968 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001969 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1970 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001971 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001972 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001973 case ISD::TokenFactor:
1974 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1975 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001976 // Fold trivial token factors.
1977 if (N1.getOpcode() == ISD::EntryToken) return N2;
1978 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001979 break;
Chris Lattner76365122005-01-16 02:23:22 +00001980 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001981 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1982 N1.getValueType() == VT && "Binary operator types must match!");
1983 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1984 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001985 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001986 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001987 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1988 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001989 break;
Chris Lattner76365122005-01-16 02:23:22 +00001990 case ISD::OR:
1991 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001992 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1993 N1.getValueType() == VT && "Binary operator types must match!");
1994 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1995 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00001996 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001997 return N1;
1998 break;
Chris Lattner76365122005-01-16 02:23:22 +00001999 case ISD::UDIV:
2000 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002001 case ISD::MULHU:
2002 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002003 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
2004 // fall through
2005 case ISD::ADD:
2006 case ISD::SUB:
2007 case ISD::MUL:
2008 case ISD::SDIV:
2009 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002010 case ISD::FADD:
2011 case ISD::FSUB:
2012 case ISD::FMUL:
2013 case ISD::FDIV:
2014 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002015 assert(N1.getValueType() == N2.getValueType() &&
2016 N1.getValueType() == VT && "Binary operator types must match!");
2017 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002018 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2019 assert(N1.getValueType() == VT &&
2020 MVT::isFloatingPoint(N1.getValueType()) &&
2021 MVT::isFloatingPoint(N2.getValueType()) &&
2022 "Invalid FCOPYSIGN!");
2023 break;
Chris Lattner76365122005-01-16 02:23:22 +00002024 case ISD::SHL:
2025 case ISD::SRA:
2026 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002027 case ISD::ROTL:
2028 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002029 assert(VT == N1.getValueType() &&
2030 "Shift operators return type must be the same as their first arg");
2031 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002032 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002033 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002034 case ISD::FP_ROUND_INREG: {
2035 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2036 assert(VT == N1.getValueType() && "Not an inreg round!");
2037 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2038 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002039 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2040 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002041 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002042 break;
2043 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002044 case ISD::FP_ROUND:
2045 assert(MVT::isFloatingPoint(VT) &&
2046 MVT::isFloatingPoint(N1.getValueType()) &&
2047 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2048 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002049 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002050 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002051 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002052 case ISD::AssertZext: {
2053 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2054 assert(VT == N1.getValueType() && "Not an inreg extend!");
2055 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2056 "Cannot *_EXTEND_INREG FP types");
2057 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2058 "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002059 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002060 break;
2061 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002062 case ISD::SIGN_EXTEND_INREG: {
2063 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2064 assert(VT == N1.getValueType() && "Not an inreg extend!");
2065 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2066 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002067 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2068 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002069 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002070
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002071 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002072 APInt Val = N1C->getAPIntValue();
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002073 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman6c231502008-02-29 01:47:35 +00002074 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002075 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002076 return getConstant(Val, VT);
2077 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002078 break;
2079 }
2080 case ISD::EXTRACT_VECTOR_ELT:
2081 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2082
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002083 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2084 if (N1.getOpcode() == ISD::UNDEF)
2085 return getNode(ISD::UNDEF, VT);
2086
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002087 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2088 // expanding copies of large vectors from registers.
2089 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2090 N1.getNumOperands() > 0) {
2091 unsigned Factor =
2092 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2093 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2094 N1.getOperand(N2C->getValue() / Factor),
2095 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2096 }
2097
2098 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2099 // expanding large vector constants.
2100 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2101 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002102
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002103 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2104 // operations are lowered to scalars.
2105 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2106 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2107 if (IEC == N2C)
2108 return N1.getOperand(1);
2109 else
2110 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2111 }
2112 break;
2113 case ISD::EXTRACT_ELEMENT:
2114 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002115 assert(!MVT::isVector(N1.getValueType()) &&
2116 MVT::isInteger(N1.getValueType()) &&
2117 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2118 "EXTRACT_ELEMENT only applies to integers!");
2119
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002120 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2121 // 64-bit integers into 32-bit parts. Instead of building the extract of
2122 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2123 if (N1.getOpcode() == ISD::BUILD_PAIR)
2124 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002125
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002126 // EXTRACT_ELEMENT of a constant int is also very common.
2127 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Dan Gohman4c931fc2008-03-24 16:38:05 +00002128 unsigned ElementSize = MVT::getSizeInBits(VT);
2129 unsigned Shift = ElementSize * N2C->getValue();
2130 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2131 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002132 }
2133 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002134 case ISD::EXTRACT_SUBVECTOR:
2135 if (N1.getValueType() == VT) // Trivial extraction.
2136 return N1;
2137 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002138 }
2139
2140 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002141 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002142 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002143 switch (Opcode) {
2144 case ISD::ADD: return getConstant(C1 + C2, VT);
2145 case ISD::SUB: return getConstant(C1 - C2, VT);
2146 case ISD::MUL: return getConstant(C1 * C2, VT);
2147 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002148 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002149 break;
2150 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002151 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002152 break;
2153 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002154 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002155 break;
2156 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002157 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002158 break;
2159 case ISD::AND : return getConstant(C1 & C2, VT);
2160 case ISD::OR : return getConstant(C1 | C2, VT);
2161 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002162 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002163 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2164 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2165 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2166 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002167 default: break;
2168 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002169 } else { // Cannonicalize constant to RHS if commutative
2170 if (isCommutativeBinOp(Opcode)) {
2171 std::swap(N1C, N2C);
2172 std::swap(N1, N2);
2173 }
2174 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002175 }
2176
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002177 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002178 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2179 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002180 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002181 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2182 // Cannonicalize constant to RHS if commutative
2183 std::swap(N1CFP, N2CFP);
2184 std::swap(N1, N2);
2185 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002186 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2187 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002188 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002189 case ISD::FADD:
2190 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002191 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002192 return getConstantFP(V1, VT);
2193 break;
2194 case ISD::FSUB:
2195 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2196 if (s!=APFloat::opInvalidOp)
2197 return getConstantFP(V1, VT);
2198 break;
2199 case ISD::FMUL:
2200 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2201 if (s!=APFloat::opInvalidOp)
2202 return getConstantFP(V1, VT);
2203 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002204 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002205 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2206 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2207 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002208 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002209 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002210 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2211 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2212 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002213 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002214 case ISD::FCOPYSIGN:
2215 V1.copySign(V2);
2216 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002217 default: break;
2218 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002219 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002220 }
Chris Lattner62b57722006-04-20 05:39:12 +00002221
2222 // Canonicalize an UNDEF to the RHS, even over a constant.
2223 if (N1.getOpcode() == ISD::UNDEF) {
2224 if (isCommutativeBinOp(Opcode)) {
2225 std::swap(N1, N2);
2226 } else {
2227 switch (Opcode) {
2228 case ISD::FP_ROUND_INREG:
2229 case ISD::SIGN_EXTEND_INREG:
2230 case ISD::SUB:
2231 case ISD::FSUB:
2232 case ISD::FDIV:
2233 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002234 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002235 return N1; // fold op(undef, arg2) -> undef
2236 case ISD::UDIV:
2237 case ISD::SDIV:
2238 case ISD::UREM:
2239 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002240 case ISD::SRL:
2241 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002242 if (!MVT::isVector(VT))
2243 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2244 // For vectors, we can't easily build an all zero vector, just return
2245 // the LHS.
2246 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002247 }
2248 }
2249 }
2250
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002251 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002252 if (N2.getOpcode() == ISD::UNDEF) {
2253 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002254 case ISD::XOR:
2255 if (N1.getOpcode() == ISD::UNDEF)
2256 // Handle undef ^ undef -> 0 special case. This is a common
2257 // idiom (misuse).
2258 return getConstant(0, VT);
2259 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002260 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002261 case ISD::ADDC:
2262 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002263 case ISD::SUB:
2264 case ISD::FADD:
2265 case ISD::FSUB:
2266 case ISD::FMUL:
2267 case ISD::FDIV:
2268 case ISD::FREM:
2269 case ISD::UDIV:
2270 case ISD::SDIV:
2271 case ISD::UREM:
2272 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002273 return N2; // fold op(arg1, undef) -> undef
2274 case ISD::MUL:
2275 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002276 case ISD::SRL:
2277 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002278 if (!MVT::isVector(VT))
2279 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2280 // For vectors, we can't easily build an all zero vector, just return
2281 // the LHS.
2282 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002283 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002284 if (!MVT::isVector(VT))
2285 return getConstant(MVT::getIntVTBitMask(VT), VT);
2286 // For vectors, we can't easily build an all one vector, just return
2287 // the LHS.
2288 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002289 case ISD::SRA:
2290 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002291 }
2292 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002293
Chris Lattner27e9b412005-05-11 18:57:39 +00002294 // Memoize this node if possible.
2295 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002296 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002297 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002298 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002299 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002300 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002301 void *IP = 0;
2302 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2303 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002304 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002305 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002306 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002307 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002308 }
2309
Chris Lattnerc3aae252005-01-07 07:46:32 +00002310 AllNodes.push_back(N);
2311 return SDOperand(N, 0);
2312}
2313
Chris Lattnerc3aae252005-01-07 07:46:32 +00002314SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2315 SDOperand N1, SDOperand N2, SDOperand N3) {
2316 // Perform various simplifications.
2317 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2318 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002319 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002320 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002321 // Use FoldSetCC to simplify SETCC's.
2322 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002323 if (Simp.Val) return Simp;
2324 break;
2325 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002326 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002327 if (N1C) {
2328 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002329 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002330 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002331 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002332 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002333
2334 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002335 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002336 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002337 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002338 if (N2C->getValue()) // Unconditional branch
2339 return getNode(ISD::BR, MVT::Other, N1, N3);
2340 else
2341 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002342 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002343 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002344 case ISD::VECTOR_SHUFFLE:
2345 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2346 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2347 N3.getOpcode() == ISD::BUILD_VECTOR &&
2348 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2349 "Illegal VECTOR_SHUFFLE node!");
2350 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002351 case ISD::BIT_CONVERT:
2352 // Fold bit_convert nodes from a type to themselves.
2353 if (N1.getValueType() == VT)
2354 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002355 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002356 }
2357
Chris Lattner43247a12005-08-25 19:12:10 +00002358 // Memoize node if it doesn't produce a flag.
2359 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002360 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002361 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002362 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002363 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002364 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002365 void *IP = 0;
2366 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2367 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002368 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002369 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002370 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002371 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002372 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002373 AllNodes.push_back(N);
2374 return SDOperand(N, 0);
2375}
2376
2377SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002378 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002379 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002380 SDOperand Ops[] = { N1, N2, N3, N4 };
2381 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002382}
2383
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002384SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2385 SDOperand N1, SDOperand N2, SDOperand N3,
2386 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002387 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2388 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002389}
2390
Dan Gohman707e0182008-04-12 04:36:06 +00002391/// getMemsetValue - Vectorized representation of the memset value
2392/// operand.
2393static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
2394 SelectionDAG &DAG) {
2395 MVT::ValueType CurVT = VT;
2396 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
2397 uint64_t Val = C->getValue() & 255;
2398 unsigned Shift = 8;
2399 while (CurVT != MVT::i8) {
2400 Val = (Val << Shift) | Val;
2401 Shift <<= 1;
2402 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
2403 }
2404 return DAG.getConstant(Val, VT);
2405 } else {
2406 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2407 unsigned Shift = 8;
2408 while (CurVT != MVT::i8) {
2409 Value =
2410 DAG.getNode(ISD::OR, VT,
2411 DAG.getNode(ISD::SHL, VT, Value,
2412 DAG.getConstant(Shift, MVT::i8)), Value);
2413 Shift <<= 1;
2414 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
2415 }
2416
2417 return Value;
2418 }
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002419}
2420
Dan Gohman707e0182008-04-12 04:36:06 +00002421/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2422/// used when a memcpy is turned into a memset when the source is a constant
2423/// string ptr.
2424static SDOperand getMemsetStringVal(MVT::ValueType VT,
2425 SelectionDAG &DAG,
2426 const TargetLowering &TLI,
2427 std::string &Str, unsigned Offset) {
2428 uint64_t Val = 0;
2429 unsigned MSB = MVT::getSizeInBits(VT) / 8;
2430 if (TLI.isLittleEndian())
2431 Offset = Offset + MSB - 1;
2432 for (unsigned i = 0; i != MSB; ++i) {
2433 Val = (Val << 8) | (unsigned char)Str[Offset];
2434 Offset += TLI.isLittleEndian() ? -1 : 1;
2435 }
2436 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002437}
2438
Dan Gohman707e0182008-04-12 04:36:06 +00002439/// getMemBasePlusOffset - Returns base and offset node for the
2440static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2441 SelectionDAG &DAG) {
2442 MVT::ValueType VT = Base.getValueType();
2443 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2444}
2445
2446/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2447/// to replace the memset / memcpy is below the threshold. It also returns the
2448/// types of the sequence of memory ops to perform memset / memcpy.
2449static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
2450 unsigned Limit, uint64_t Size,
2451 unsigned Align,
2452 const TargetLowering &TLI) {
2453 MVT::ValueType VT;
2454
2455 if (TLI.allowsUnalignedMemoryAccesses()) {
2456 VT = MVT::i64;
2457 } else {
2458 switch (Align & 7) {
2459 case 0:
2460 VT = MVT::i64;
2461 break;
2462 case 4:
2463 VT = MVT::i32;
2464 break;
2465 case 2:
2466 VT = MVT::i16;
2467 break;
2468 default:
2469 VT = MVT::i8;
2470 break;
2471 }
2472 }
2473
2474 MVT::ValueType LVT = MVT::i64;
2475 while (!TLI.isTypeLegal(LVT))
2476 LVT = (MVT::ValueType)((unsigned)LVT - 1);
2477 assert(MVT::isInteger(LVT));
2478
2479 if (VT > LVT)
2480 VT = LVT;
2481
2482 unsigned NumMemOps = 0;
2483 while (Size != 0) {
2484 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2485 while (VTSize > Size) {
2486 VT = (MVT::ValueType)((unsigned)VT - 1);
2487 VTSize >>= 1;
2488 }
2489 assert(MVT::isInteger(VT));
2490
2491 if (++NumMemOps > Limit)
2492 return false;
2493 MemOps.push_back(VT);
2494 Size -= VTSize;
2495 }
2496
2497 return true;
2498}
2499
2500static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2501 SDOperand Chain, SDOperand Dst,
2502 SDOperand Src, uint64_t Size,
2503 unsigned Align,
2504 bool AlwaysInline,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002505 const Value *DstSV, uint64_t DstOff,
2506 const Value *SrcSV, uint64_t SrcOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002507 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2508
2509 // Expand memcpy to a series of store ops if the size operand falls below
2510 // a certain threshold.
2511 std::vector<MVT::ValueType> MemOps;
2512 uint64_t Limit = -1;
2513 if (!AlwaysInline)
2514 Limit = TLI.getMaxStoresPerMemcpy();
2515 if (!MeetsMaxMemopRequirement(MemOps, Limit, Size, Align, TLI))
2516 return SDOperand();
2517
2518 SmallVector<SDOperand, 8> OutChains;
2519
2520 unsigned NumMemOps = MemOps.size();
2521 unsigned SrcDelta = 0;
2522 GlobalAddressSDNode *G = NULL;
2523 std::string Str;
2524 bool CopyFromStr = false;
2525
2526 if (Src.getOpcode() == ISD::GlobalAddress)
2527 G = cast<GlobalAddressSDNode>(Src);
2528 else if (Src.getOpcode() == ISD::ADD &&
2529 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2530 Src.getOperand(1).getOpcode() == ISD::Constant) {
2531 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2532 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2533 }
2534 if (G) {
2535 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
2536 if (GV && GV->isConstant()) {
2537 Str = GV->getStringValue(false);
2538 if (!Str.empty()) {
2539 CopyFromStr = true;
2540 SrcOff += SrcDelta;
2541 }
2542 }
2543 }
2544
2545 for (unsigned i = 0; i < NumMemOps; i++) {
2546 MVT::ValueType VT = MemOps[i];
2547 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2548 SDOperand Value, Store;
2549
2550 if (CopyFromStr) {
2551 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
2552 Store =
2553 DAG.getStore(Chain, Value,
2554 getMemBasePlusOffset(Dst, DstOff, DAG),
2555 DstSV, DstOff);
2556 } else {
2557 Value = DAG.getLoad(VT, Chain,
2558 getMemBasePlusOffset(Src, SrcOff, DAG),
2559 SrcSV, SrcOff, false, Align);
2560 Store =
2561 DAG.getStore(Chain, Value,
2562 getMemBasePlusOffset(Dst, DstOff, DAG),
2563 DstSV, DstOff, false, Align);
2564 }
2565 OutChains.push_back(Store);
2566 SrcOff += VTSize;
2567 DstOff += VTSize;
2568 }
2569
2570 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2571 &OutChains[0], OutChains.size());
2572}
2573
2574static SDOperand getMemsetStores(SelectionDAG &DAG,
2575 SDOperand Chain, SDOperand Dst,
2576 SDOperand Src, uint64_t Size,
2577 unsigned Align,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002578 const Value *DstSV, uint64_t DstOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002579 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2580
2581 // Expand memset to a series of load/store ops if the size operand
2582 // falls below a certain threshold.
2583 std::vector<MVT::ValueType> MemOps;
2584 if (!MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(),
2585 Size, Align, TLI))
2586 return SDOperand();
2587
2588 SmallVector<SDOperand, 8> OutChains;
2589
2590 unsigned NumMemOps = MemOps.size();
2591 for (unsigned i = 0; i < NumMemOps; i++) {
2592 MVT::ValueType VT = MemOps[i];
2593 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2594 SDOperand Value = getMemsetValue(Src, VT, DAG);
2595 SDOperand Store = DAG.getStore(Chain, Value,
2596 getMemBasePlusOffset(Dst, DstOff, DAG),
2597 DstSV, DstOff);
2598 OutChains.push_back(Store);
2599 DstOff += VTSize;
2600 }
2601
2602 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2603 &OutChains[0], OutChains.size());
2604}
2605
2606SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002607 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002608 unsigned Align, bool AlwaysInline,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002609 const Value *DstSV, uint64_t DstOff,
2610 const Value *SrcSV, uint64_t SrcOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002611
2612 // Check to see if we should lower the memcpy to loads and stores first.
2613 // For cases within the target-specified limits, this is the best choice.
2614 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2615 if (ConstantSize) {
2616 // Memcpy with size zero? Just return the original chain.
2617 if (ConstantSize->isNullValue())
2618 return Chain;
2619
2620 SDOperand Result =
2621 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2622 Align, false, DstSV, DstOff, SrcSV, SrcOff);
2623 if (Result.Val)
2624 return Result;
2625 }
2626
2627 // Then check to see if we should lower the memcpy with target-specific
2628 // code. If the target chooses to do this, this is the next best.
2629 SDOperand Result =
2630 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2631 AlwaysInline,
2632 DstSV, DstOff, SrcSV, SrcOff);
2633 if (Result.Val)
2634 return Result;
2635
2636 // If we really need inline code and the target declined to provide it,
2637 // use a (potentially long) sequence of loads and stores.
2638 if (AlwaysInline) {
2639 assert(ConstantSize && "AlwaysInline requires a constant size!");
2640 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2641 ConstantSize->getValue(), Align, true,
2642 DstSV, DstOff, SrcSV, SrcOff);
2643 }
2644
2645 // Emit a library call.
2646 TargetLowering::ArgListTy Args;
2647 TargetLowering::ArgListEntry Entry;
2648 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2649 Entry.Node = Dst; Args.push_back(Entry);
2650 Entry.Node = Src; Args.push_back(Entry);
2651 Entry.Node = Size; Args.push_back(Entry);
2652 std::pair<SDOperand,SDOperand> CallResult =
2653 TLI.LowerCallTo(Chain, Type::VoidTy,
2654 false, false, false, CallingConv::C, false,
2655 getExternalSymbol("memcpy", TLI.getPointerTy()),
2656 Args, *this);
2657 return CallResult.second;
2658}
2659
2660SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2661 SDOperand Src, SDOperand Size,
2662 unsigned Align,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002663 const Value *DstSV, uint64_t DstOff,
2664 const Value *SrcSV, uint64_t SrcOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002665
2666 // TODO: Optimize small memmove cases with simple loads and stores,
2667 // ensuring that all loads precede all stores. This can cause severe
2668 // register pressure, so targets should be careful with the size limit.
2669
2670 // Then check to see if we should lower the memmove with target-specific
2671 // code. If the target chooses to do this, this is the next best.
2672 SDOperand Result =
2673 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
2674 DstSV, DstOff, SrcSV, SrcOff);
2675 if (Result.Val)
2676 return Result;
2677
2678 // Emit a library call.
2679 TargetLowering::ArgListTy Args;
2680 TargetLowering::ArgListEntry Entry;
2681 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2682 Entry.Node = Dst; Args.push_back(Entry);
2683 Entry.Node = Src; Args.push_back(Entry);
2684 Entry.Node = Size; Args.push_back(Entry);
2685 std::pair<SDOperand,SDOperand> CallResult =
2686 TLI.LowerCallTo(Chain, Type::VoidTy,
2687 false, false, false, CallingConv::C, false,
2688 getExternalSymbol("memmove", TLI.getPointerTy()),
2689 Args, *this);
2690 return CallResult.second;
2691}
2692
2693SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2694 SDOperand Src, SDOperand Size,
2695 unsigned Align,
Dan Gohman29e4bdb2008-04-14 17:55:48 +00002696 const Value *DstSV, uint64_t DstOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002697
2698 // Check to see if we should lower the memset to stores first.
2699 // For cases within the target-specified limits, this is the best choice.
2700 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2701 if (ConstantSize) {
2702 // Memset with size zero? Just return the original chain.
2703 if (ConstantSize->isNullValue())
2704 return Chain;
2705
2706 SDOperand Result =
2707 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
2708 DstSV, DstOff);
2709 if (Result.Val)
2710 return Result;
2711 }
2712
2713 // Then check to see if we should lower the memset with target-specific
2714 // code. If the target chooses to do this, this is the next best.
2715 SDOperand Result =
2716 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
2717 DstSV, DstOff);
2718 if (Result.Val)
2719 return Result;
2720
2721 // Emit a library call.
2722 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2723 TargetLowering::ArgListTy Args;
2724 TargetLowering::ArgListEntry Entry;
2725 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2726 Args.push_back(Entry);
2727 // Extend or truncate the argument to be an i32 value for the call.
2728 if (Src.getValueType() > MVT::i32)
2729 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2730 else
2731 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2732 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2733 Args.push_back(Entry);
2734 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2735 Args.push_back(Entry);
2736 std::pair<SDOperand,SDOperand> CallResult =
2737 TLI.LowerCallTo(Chain, Type::VoidTy,
2738 false, false, false, CallingConv::C, false,
2739 getExternalSymbol("memset", TLI.getPointerTy()),
2740 Args, *this);
2741 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002742}
2743
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002744SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002745 SDOperand Ptr, SDOperand Cmp,
2746 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002747 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002748 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2749 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002750 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002751 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002752 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2753 ID.AddInteger((unsigned int)VT);
2754 void* IP = 0;
2755 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2756 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002757 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002758 CSEMap.InsertNode(N, IP);
2759 AllNodes.push_back(N);
2760 return SDOperand(N, 0);
2761}
2762
2763SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002764 SDOperand Ptr, SDOperand Val,
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002765 MVT::ValueType VT) {
2766 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2767 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002768 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002769 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002770 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002771 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2772 ID.AddInteger((unsigned int)VT);
2773 void* IP = 0;
2774 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2775 return SDOperand(E, 0);
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002776 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002777 CSEMap.InsertNode(N, IP);
2778 AllNodes.push_back(N);
2779 return SDOperand(N, 0);
2780}
2781
Duncan Sands14ea39c2008-03-27 20:23:40 +00002782SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00002783SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
2784 MVT::ValueType VT, SDOperand Chain,
2785 SDOperand Ptr, SDOperand Offset,
2786 const Value *SV, int SVOffset, MVT::ValueType EVT,
2787 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002788 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2789 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002790 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002791 Ty = MVT::getTypeForValueType(VT);
2792 } else if (SV) {
2793 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2794 assert(PT && "Value for load must be a pointer");
2795 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00002796 }
Dan Gohman575e2f42007-06-04 15:49:41 +00002797 assert(Ty && "Could not get type information for load");
2798 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2799 }
Evan Cheng466685d2006-10-09 20:57:25 +00002800
Duncan Sands14ea39c2008-03-27 20:23:40 +00002801 if (VT == EVT) {
2802 ExtType = ISD::NON_EXTLOAD;
2803 } else if (ExtType == ISD::NON_EXTLOAD) {
2804 assert(VT == EVT && "Non-extending load from different memory type!");
2805 } else {
2806 // Extending load.
2807 if (MVT::isVector(VT))
2808 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2809 else
2810 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2811 "Should only be an extending load, not truncating!");
2812 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2813 "Cannot sign/zero extend a FP/Vector load!");
2814 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2815 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00002816 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00002817
2818 bool Indexed = AM != ISD::UNINDEXED;
2819 assert(Indexed || Offset.getOpcode() == ISD::UNDEF &&
2820 "Unindexed load with an offset!");
2821
2822 SDVTList VTs = Indexed ?
2823 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
2824 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002825 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002826 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002827 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00002828 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002829 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002830 ID.AddInteger(Alignment);
2831 ID.AddInteger(isVolatile);
2832 void *IP = 0;
2833 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2834 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002835 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
2836 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002837 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002838 AllNodes.push_back(N);
2839 return SDOperand(N, 0);
2840}
2841
Duncan Sands14ea39c2008-03-27 20:23:40 +00002842SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2843 SDOperand Chain, SDOperand Ptr,
2844 const Value *SV, int SVOffset,
2845 bool isVolatile, unsigned Alignment) {
2846 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002847 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
2848 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002849}
2850
2851SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2852 SDOperand Chain, SDOperand Ptr,
2853 const Value *SV,
2854 int SVOffset, MVT::ValueType EVT,
2855 bool isVolatile, unsigned Alignment) {
2856 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00002857 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
2858 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00002859}
2860
Evan Cheng144d8f02006-11-09 17:55:04 +00002861SDOperand
2862SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2863 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002864 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002865 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2866 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00002867 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
2868 LD->getChain(), Base, Offset, LD->getSrcValue(),
2869 LD->getSrcValueOffset(), LD->getMemoryVT(),
2870 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00002871}
2872
Jeff Cohend41b30d2006-11-05 19:31:28 +00002873SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002874 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002875 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002876 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002877
Dan Gohman575e2f42007-06-04 15:49:41 +00002878 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2879 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002880 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002881 Ty = MVT::getTypeForValueType(VT);
2882 } else if (SV) {
2883 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2884 assert(PT && "Value for store must be a pointer");
2885 Ty = PT->getElementType();
2886 }
2887 assert(Ty && "Could not get type information for store");
2888 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2889 }
Evan Chengad071e12006-10-05 22:57:11 +00002890 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002891 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002892 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002893 FoldingSetNodeID ID;
2894 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002895 ID.AddInteger(ISD::UNINDEXED);
2896 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002897 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002898 ID.AddInteger(Alignment);
2899 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002900 void *IP = 0;
2901 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2902 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002903 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002904 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002905 CSEMap.InsertNode(N, IP);
2906 AllNodes.push_back(N);
2907 return SDOperand(N, 0);
2908}
2909
Jeff Cohend41b30d2006-11-05 19:31:28 +00002910SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002911 SDOperand Ptr, const Value *SV,
2912 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002913 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002914 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002915
2916 if (VT == SVT)
2917 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002918
Duncan Sandsaf47b112007-10-16 09:56:48 +00002919 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2920 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002921 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2922 "Can't do FP-INT conversion!");
2923
Dan Gohman575e2f42007-06-04 15:49:41 +00002924 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2925 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002926 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002927 Ty = MVT::getTypeForValueType(VT);
2928 } else if (SV) {
2929 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2930 assert(PT && "Value for store must be a pointer");
2931 Ty = PT->getElementType();
2932 }
2933 assert(Ty && "Could not get type information for store");
2934 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2935 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002936 SDVTList VTs = getVTList(MVT::Other);
2937 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002938 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002939 FoldingSetNodeID ID;
2940 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002941 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002942 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002943 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002944 ID.AddInteger(Alignment);
2945 ID.AddInteger(isVolatile);
2946 void *IP = 0;
2947 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2948 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002949 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002950 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002951 CSEMap.InsertNode(N, IP);
2952 AllNodes.push_back(N);
2953 return SDOperand(N, 0);
2954}
2955
Evan Cheng144d8f02006-11-09 17:55:04 +00002956SDOperand
2957SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2958 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002959 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2960 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2961 "Store is already a indexed store!");
2962 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2963 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2964 FoldingSetNodeID ID;
2965 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2966 ID.AddInteger(AM);
2967 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002968 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002969 ID.AddInteger(ST->getAlignment());
2970 ID.AddInteger(ST->isVolatile());
2971 void *IP = 0;
2972 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2973 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002974 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002975 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002976 ST->getSrcValue(), ST->getSrcValueOffset(),
2977 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002978 CSEMap.InsertNode(N, IP);
2979 AllNodes.push_back(N);
2980 return SDOperand(N, 0);
2981}
2982
Nate Begemanacc398c2006-01-25 18:21:52 +00002983SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2984 SDOperand Chain, SDOperand Ptr,
2985 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002986 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002987 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002988}
2989
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002990SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00002991 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002992 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002993 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002994 case 1: return getNode(Opcode, VT, Ops[0]);
2995 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2996 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002997 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002998 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002999
Chris Lattneref847df2005-04-09 03:27:28 +00003000 switch (Opcode) {
3001 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003002 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003003 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003004 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3005 "LHS and RHS of condition must have same type!");
3006 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3007 "True and False arms of SelectCC must have same type!");
3008 assert(Ops[2].getValueType() == VT &&
3009 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003010 break;
3011 }
3012 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003013 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003014 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3015 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003016 break;
3017 }
Chris Lattneref847df2005-04-09 03:27:28 +00003018 }
3019
Chris Lattner385328c2005-05-14 07:42:29 +00003020 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003021 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003022 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003023 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003024 FoldingSetNodeID ID;
3025 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003026 void *IP = 0;
3027 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3028 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003029 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003030 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003031 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003032 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003033 }
Chris Lattneref847df2005-04-09 03:27:28 +00003034 AllNodes.push_back(N);
3035 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003036}
3037
Chris Lattner89c34632005-05-14 06:20:26 +00003038SDOperand SelectionDAG::getNode(unsigned Opcode,
3039 std::vector<MVT::ValueType> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003040 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003041 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3042 Ops, NumOps);
3043}
3044
3045SDOperand SelectionDAG::getNode(unsigned Opcode,
3046 const MVT::ValueType *VTs, unsigned NumVTs,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003047 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003048 if (NumVTs == 1)
3049 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003050 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3051}
3052
3053SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003054 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003055 if (VTList.NumVTs == 1)
3056 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003057
Chris Lattner5f056bf2005-07-10 01:55:33 +00003058 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003059 // FIXME: figure out how to safely handle things like
3060 // int foo(int x) { return 1 << (x & 255); }
3061 // int bar() { return foo(256); }
3062#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003063 case ISD::SRA_PARTS:
3064 case ISD::SRL_PARTS:
3065 case ISD::SHL_PARTS:
3066 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003067 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003068 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3069 else if (N3.getOpcode() == ISD::AND)
3070 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3071 // If the and is only masking out bits that cannot effect the shift,
3072 // eliminate the and.
3073 unsigned NumBits = MVT::getSizeInBits(VT)*2;
3074 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3075 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3076 }
3077 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003078#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003079 }
Chris Lattner89c34632005-05-14 06:20:26 +00003080
Chris Lattner43247a12005-08-25 19:12:10 +00003081 // Memoize the node unless it returns a flag.
3082 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003083 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003084 FoldingSetNodeID ID;
3085 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003086 void *IP = 0;
3087 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3088 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003089 if (NumOps == 1)
3090 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3091 else if (NumOps == 2)
3092 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3093 else if (NumOps == 3)
3094 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3095 else
3096 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003097 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003098 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003099 if (NumOps == 1)
3100 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3101 else if (NumOps == 2)
3102 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3103 else if (NumOps == 3)
3104 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3105 else
3106 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003107 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003108 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003109 return SDOperand(N, 0);
3110}
3111
Dan Gohman08ce9762007-10-08 15:49:58 +00003112SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003113 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003114}
3115
3116SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3117 SDOperand N1) {
3118 SDOperand Ops[] = { N1 };
3119 return getNode(Opcode, VTList, Ops, 1);
3120}
3121
3122SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3123 SDOperand N1, SDOperand N2) {
3124 SDOperand Ops[] = { N1, N2 };
3125 return getNode(Opcode, VTList, Ops, 2);
3126}
3127
3128SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3129 SDOperand N1, SDOperand N2, SDOperand N3) {
3130 SDOperand Ops[] = { N1, N2, N3 };
3131 return getNode(Opcode, VTList, Ops, 3);
3132}
3133
3134SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3135 SDOperand N1, SDOperand N2, SDOperand N3,
3136 SDOperand N4) {
3137 SDOperand Ops[] = { N1, N2, N3, N4 };
3138 return getNode(Opcode, VTList, Ops, 4);
3139}
3140
3141SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3142 SDOperand N1, SDOperand N2, SDOperand N3,
3143 SDOperand N4, SDOperand N5) {
3144 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3145 return getNode(Opcode, VTList, Ops, 5);
3146}
3147
Chris Lattner70046e92006-08-15 17:46:01 +00003148SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003149 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003150}
3151
Chris Lattner70046e92006-08-15 17:46:01 +00003152SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00003153 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3154 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003155 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003156 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003157 }
3158 std::vector<MVT::ValueType> V;
3159 V.push_back(VT1);
3160 V.push_back(VT2);
3161 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003162 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003163}
Chris Lattner70046e92006-08-15 17:46:01 +00003164SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
3165 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003166 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003167 E = VTList.end(); I != E; ++I) {
3168 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3169 (*I)[2] == VT3)
3170 return makeVTList(&(*I)[0], 3);
3171 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003172 std::vector<MVT::ValueType> V;
3173 V.push_back(VT1);
3174 V.push_back(VT2);
3175 V.push_back(VT3);
3176 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003177 return makeVTList(&(*VTList.begin())[0], 3);
3178}
3179
3180SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
3181 switch (NumVTs) {
3182 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003183 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003184 case 2: return getVTList(VTs[0], VTs[1]);
3185 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3186 default: break;
3187 }
3188
3189 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3190 E = VTList.end(); I != E; ++I) {
3191 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3192
3193 bool NoMatch = false;
3194 for (unsigned i = 2; i != NumVTs; ++i)
3195 if (VTs[i] != (*I)[i]) {
3196 NoMatch = true;
3197 break;
3198 }
3199 if (!NoMatch)
3200 return makeVTList(&*I->begin(), NumVTs);
3201 }
3202
3203 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
3204 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003205}
3206
3207
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003208/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3209/// specified operands. If the resultant node already exists in the DAG,
3210/// this does not modify the specified node, instead it returns the node that
3211/// already exists. If the resultant node does not exist in the DAG, the
3212/// input node is returned. As a degenerate case, if you specify the same
3213/// input operands as the node already has, the input node is returned.
3214SDOperand SelectionDAG::
3215UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3216 SDNode *N = InN.Val;
3217 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3218
3219 // Check to see if there is no change.
3220 if (Op == N->getOperand(0)) return InN;
3221
3222 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003223 void *InsertPos = 0;
3224 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3225 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003226
3227 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003228 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003229 RemoveNodeFromCSEMaps(N);
3230
3231 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003232 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003233 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003234 N->OperandList[0].setUser(N);
3235 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003236
3237 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003238 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003239 return InN;
3240}
3241
3242SDOperand SelectionDAG::
3243UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3244 SDNode *N = InN.Val;
3245 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3246
3247 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003248 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3249 return InN; // No operands changed, just return the input node.
3250
3251 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003252 void *InsertPos = 0;
3253 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3254 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003255
3256 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003257 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003258 RemoveNodeFromCSEMaps(N);
3259
3260 // Now we update the operands.
3261 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003262 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003263 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003264 N->OperandList[0].setUser(N);
3265 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003266 }
3267 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003268 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003269 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003270 N->OperandList[1].setUser(N);
3271 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003272 }
3273
3274 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003275 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003276 return InN;
3277}
3278
3279SDOperand SelectionDAG::
3280UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003281 SDOperand Ops[] = { Op1, Op2, Op3 };
3282 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003283}
3284
3285SDOperand SelectionDAG::
3286UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3287 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003288 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3289 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003290}
3291
3292SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003293UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3294 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003295 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3296 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003297}
3298
Chris Lattner809ec112006-01-28 10:09:25 +00003299SDOperand SelectionDAG::
Roman Levenstein9cac5252008-04-16 16:15:27 +00003300UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003301 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003302 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003303 "Update with wrong number of operands");
3304
3305 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003306 bool AnyChange = false;
3307 for (unsigned i = 0; i != NumOps; ++i) {
3308 if (Ops[i] != N->getOperand(i)) {
3309 AnyChange = true;
3310 break;
3311 }
3312 }
3313
3314 // No operands changed, just return the input node.
3315 if (!AnyChange) return InN;
3316
3317 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003318 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003319 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003320 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003321
3322 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003323 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003324 RemoveNodeFromCSEMaps(N);
3325
3326 // Now we update the operands.
3327 for (unsigned i = 0; i != NumOps; ++i) {
3328 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003329 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003330 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003331 N->OperandList[i].setUser(N);
3332 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003333 }
3334 }
3335
3336 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003337 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003338 return InN;
3339}
3340
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003341/// MorphNodeTo - This frees the operands of the current node, resets the
3342/// opcode, types, and operands to the specified value. This should only be
3343/// used by the SelectionDAG class.
3344void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3345 const SDOperand *Ops, unsigned NumOps) {
3346 NodeType = Opc;
3347 ValueList = L.VTs;
3348 NumValues = L.NumVTs;
3349
3350 // Clear the operands list, updating used nodes to remove this from their
3351 // use list.
3352 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003353 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003354
3355 // If NumOps is larger than the # of operands we currently have, reallocate
3356 // the operand list.
3357 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003358 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003359 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003360 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003361 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003362 OperandsNeedDelete = true;
3363 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003364
3365 // Assign the new operands.
3366 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003367
3368 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3369 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003370 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003371 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003372 N->addUser(i, this);
3373 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003374 }
3375}
Chris Lattner149c58c2005-08-16 18:17:10 +00003376
3377/// SelectNodeTo - These are used for target selectors to *mutate* the
3378/// specified node to have the specified return type, Target opcode, and
3379/// operands. Note that target opcodes are stored as
3380/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003381///
3382/// Note that SelectNodeTo returns the resultant node. If there is already a
3383/// node of the specified opcode and operands, it returns that node instead of
3384/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003385SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3386 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003387 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003388 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00003389 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003390 void *IP = 0;
3391 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003392 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003393
Chris Lattner7651fa42005-08-24 23:00:29 +00003394 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003395
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003396 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003397
Chris Lattner4a283e92006-08-11 18:38:11 +00003398 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003399 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003400}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003401
Evan Cheng95514ba2006-08-26 08:00:10 +00003402SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3403 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003404 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003405 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003406 SDOperand Ops[] = { Op1 };
3407
Jim Laskey583bd472006-10-27 23:46:08 +00003408 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003409 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003410 void *IP = 0;
3411 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003412 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003413
Chris Lattner149c58c2005-08-16 18:17:10 +00003414 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003415 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003416 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003417 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003418}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003419
Evan Cheng95514ba2006-08-26 08:00:10 +00003420SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3421 MVT::ValueType VT, SDOperand Op1,
3422 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003423 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003424 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003425 SDOperand Ops[] = { Op1, Op2 };
3426
Jim Laskey583bd472006-10-27 23:46:08 +00003427 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003428 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003429 void *IP = 0;
3430 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003431 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003432
Chris Lattner149c58c2005-08-16 18:17:10 +00003433 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003434
Chris Lattner63e3f142007-02-04 07:28:00 +00003435 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003436
Chris Lattnera5682852006-08-07 23:03:03 +00003437 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003438 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003439}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003440
Evan Cheng95514ba2006-08-26 08:00:10 +00003441SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3442 MVT::ValueType VT, SDOperand Op1,
3443 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003444 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003445 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003446 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003447 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003448 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003449 void *IP = 0;
3450 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003451 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003452
Chris Lattner149c58c2005-08-16 18:17:10 +00003453 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003454
Chris Lattner63e3f142007-02-04 07:28:00 +00003455 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003456
Chris Lattnera5682852006-08-07 23:03:03 +00003457 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003458 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003459}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003460
Evan Cheng95514ba2006-08-26 08:00:10 +00003461SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003462 MVT::ValueType VT, SDOperandPtr Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003463 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003464 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003465 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003466 FoldingSetNodeID ID;
3467 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003468 void *IP = 0;
3469 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003470 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003471
Chris Lattner6b09a292005-08-21 18:49:33 +00003472 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003473 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003474
Chris Lattnera5682852006-08-07 23:03:03 +00003475 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003476 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003477}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003478
Evan Cheng95514ba2006-08-26 08:00:10 +00003479SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3480 MVT::ValueType VT1, MVT::ValueType VT2,
3481 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003482 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003483 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003484 SDOperand Ops[] = { Op1, Op2 };
3485 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003486 void *IP = 0;
3487 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003488 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003489
Chris Lattner0fb094f2005-11-19 01:44:53 +00003490 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003491 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003492 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003493 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003494}
3495
Evan Cheng95514ba2006-08-26 08:00:10 +00003496SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3497 MVT::ValueType VT1, MVT::ValueType VT2,
3498 SDOperand Op1, SDOperand Op2,
3499 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003500 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003501 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003502 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003503 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003504 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003505 void *IP = 0;
3506 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003507 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003508
Chris Lattner0fb094f2005-11-19 01:44:53 +00003509 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003510
Chris Lattner63e3f142007-02-04 07:28:00 +00003511 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003512 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003513 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003514}
3515
Chris Lattner0fb094f2005-11-19 01:44:53 +00003516
Evan Cheng6ae46c42006-02-09 07:15:23 +00003517/// getTargetNode - These are used for target selectors to create a new node
3518/// with specified return type(s), target opcode, and operands.
3519///
3520/// Note that getTargetNode returns the resultant node. If there is already a
3521/// node of the specified opcode and operands, it returns that node instead of
3522/// the current one.
3523SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3524 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3525}
3526SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3527 SDOperand Op1) {
3528 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3529}
3530SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3531 SDOperand Op1, SDOperand Op2) {
3532 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3533}
3534SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003535 SDOperand Op1, SDOperand Op2,
3536 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003537 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3538}
3539SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003540 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003541 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003542}
3543SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003544 MVT::ValueType VT2) {
3545 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3546 SDOperand Op;
3547 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3548}
3549SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003550 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003551 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003552 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003553}
3554SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003555 MVT::ValueType VT2, SDOperand Op1,
3556 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003557 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003558 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003559 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003560}
3561SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003562 MVT::ValueType VT2, SDOperand Op1,
3563 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003564 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003565 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003566 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003567}
Evan Cheng694481e2006-08-27 08:08:54 +00003568SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3569 MVT::ValueType VT2,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003570 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003571 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003572 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003573}
3574SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3575 MVT::ValueType VT2, MVT::ValueType VT3,
3576 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003577 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003578 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003579 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003580}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003581SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3582 MVT::ValueType VT2, MVT::ValueType VT3,
3583 SDOperand Op1, SDOperand Op2,
3584 SDOperand Op3) {
3585 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3586 SDOperand Ops[] = { Op1, Op2, Op3 };
3587 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3588}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003589SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003590 MVT::ValueType VT2, MVT::ValueType VT3,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003591 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003592 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3593 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003594}
Evan Cheng05e69c12007-09-12 23:39:49 +00003595SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3596 MVT::ValueType VT2, MVT::ValueType VT3,
3597 MVT::ValueType VT4,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003598 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng05e69c12007-09-12 23:39:49 +00003599 std::vector<MVT::ValueType> VTList;
3600 VTList.push_back(VT1);
3601 VTList.push_back(VT2);
3602 VTList.push_back(VT3);
3603 VTList.push_back(VT4);
3604 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3605 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3606}
Evan Cheng39305cf2007-10-05 01:10:49 +00003607SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3608 std::vector<MVT::ValueType> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003609 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng39305cf2007-10-05 01:10:49 +00003610 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3611 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3612 Ops, NumOps).Val;
3613}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003614
Evan Cheng08b11732008-03-22 01:55:50 +00003615/// getNodeIfExists - Get the specified node if it's already available, or
3616/// else return NULL.
3617SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003618 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003619 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3620 FoldingSetNodeID ID;
3621 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3622 void *IP = 0;
3623 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3624 return E;
3625 }
3626 return NULL;
3627}
3628
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003629
Evan Cheng99157a02006-08-07 22:13:29 +00003630/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003631/// This can cause recursive merging of nodes in the DAG.
3632///
Chris Lattner11d049c2008-02-03 03:35:22 +00003633/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003634///
Chris Lattner11d049c2008-02-03 03:35:22 +00003635void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003636 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003637 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003638 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003639 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003640 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003641
Chris Lattner8b8749f2005-08-17 19:00:20 +00003642 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003643 SDNode::use_iterator UI = From->use_begin();
3644 SDNode *U = UI->getUser();
3645
Chris Lattner8b8749f2005-08-17 19:00:20 +00003646 // This node is about to morph, remove its old self from the CSE maps.
3647 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003648 int operandNum = 0;
3649 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3650 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003651 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003652 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003653 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003654 I->setUser(U);
3655 To.Val->addUser(operandNum, U);
3656 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003657
3658 // Now that we have modified U, add it back to the CSE maps. If it already
3659 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003660 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003661 ReplaceAllUsesWith(U, Existing, UpdateListener);
3662 // U is now dead. Inform the listener if it exists and delete it.
3663 if (UpdateListener)
3664 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003665 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003666 } else {
3667 // If the node doesn't already exist, we updated it. Inform a listener if
3668 // it exists.
3669 if (UpdateListener)
3670 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003671 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003672 }
3673}
3674
3675/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3676/// This can cause recursive merging of nodes in the DAG.
3677///
3678/// This version assumes From/To have matching types and numbers of result
3679/// values.
3680///
Chris Lattner1e111c72005-09-07 05:37:01 +00003681void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003682 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003683 assert(From != To && "Cannot replace uses of with self");
3684 assert(From->getNumValues() == To->getNumValues() &&
3685 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003686 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003687 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3688 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003689
3690 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003691 SDNode::use_iterator UI = From->use_begin();
3692 SDNode *U = UI->getUser();
3693
Chris Lattner8b52f212005-08-26 18:36:28 +00003694 // This node is about to morph, remove its old self from the CSE maps.
3695 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003696 int operandNum = 0;
3697 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3698 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003699 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003700 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003701 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003702 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003703 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003704
Chris Lattner8b8749f2005-08-17 19:00:20 +00003705 // Now that we have modified U, add it back to the CSE maps. If it already
3706 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003707 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003708 ReplaceAllUsesWith(U, Existing, UpdateListener);
3709 // U is now dead. Inform the listener if it exists and delete it.
3710 if (UpdateListener)
3711 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003712 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003713 } else {
3714 // If the node doesn't already exist, we updated it. Inform a listener if
3715 // it exists.
3716 if (UpdateListener)
3717 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003718 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003719 }
3720}
3721
Chris Lattner8b52f212005-08-26 18:36:28 +00003722/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3723/// This can cause recursive merging of nodes in the DAG.
3724///
3725/// This version can replace From with any result values. To must match the
3726/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003727void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003728 SDOperandPtr To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003729 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003730 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003731 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003732
3733 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003734 SDNode::use_iterator UI = From->use_begin();
3735 SDNode *U = UI->getUser();
3736
Chris Lattner7b2880c2005-08-24 22:44:39 +00003737 // This node is about to morph, remove its old self from the CSE maps.
3738 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003739 int operandNum = 0;
3740 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3741 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003742 if (I->getVal() == From) {
3743 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003744 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003745 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003746 I->setUser(U);
3747 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003748 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003749
Chris Lattner7b2880c2005-08-24 22:44:39 +00003750 // Now that we have modified U, add it back to the CSE maps. If it already
3751 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003752 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003753 ReplaceAllUsesWith(U, Existing, UpdateListener);
3754 // U is now dead. Inform the listener if it exists and delete it.
3755 if (UpdateListener)
3756 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003757 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003758 } else {
3759 // If the node doesn't already exist, we updated it. Inform a listener if
3760 // it exists.
3761 if (UpdateListener)
3762 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003763 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003764 }
3765}
3766
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003767namespace {
3768 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3769 /// any deleted nodes from the set passed into its constructor and recursively
3770 /// notifies another update listener if specified.
3771 class ChainedSetUpdaterListener :
3772 public SelectionDAG::DAGUpdateListener {
3773 SmallSetVector<SDNode*, 16> &Set;
3774 SelectionDAG::DAGUpdateListener *Chain;
3775 public:
3776 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3777 SelectionDAG::DAGUpdateListener *chain)
3778 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00003779
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003780 virtual void NodeDeleted(SDNode *N) {
3781 Set.remove(N);
3782 if (Chain) Chain->NodeDeleted(N);
3783 }
3784 virtual void NodeUpdated(SDNode *N) {
3785 if (Chain) Chain->NodeUpdated(N);
3786 }
3787 };
3788}
3789
Chris Lattner012f2412006-02-17 21:58:01 +00003790/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3791/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003792/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00003793void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003794 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00003795 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003796
Chris Lattner012f2412006-02-17 21:58:01 +00003797 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003798 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003799 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00003800 return;
3801 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003802
3803 if (From.use_empty()) return;
3804
Chris Lattnercf5640b2007-02-04 00:14:31 +00003805 // Get all of the users of From.Val. We want these in a nice,
3806 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003807 SmallSetVector<SDNode*, 16> Users;
3808 for (SDNode::use_iterator UI = From.Val->use_begin(),
3809 E = From.Val->use_end(); UI != E; ++UI) {
3810 SDNode *User = UI->getUser();
3811 if (!Users.count(User))
3812 Users.insert(User);
3813 }
Chris Lattner012f2412006-02-17 21:58:01 +00003814
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003815 // When one of the recursive merges deletes nodes from the graph, we need to
3816 // make sure that UpdateListener is notified *and* that the node is removed
3817 // from Users if present. CSUL does this.
3818 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00003819
Chris Lattner012f2412006-02-17 21:58:01 +00003820 while (!Users.empty()) {
3821 // We know that this user uses some value of From. If it is the right
3822 // value, update it.
3823 SDNode *User = Users.back();
3824 Users.pop_back();
3825
Chris Lattner01d029b2007-10-15 06:10:22 +00003826 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00003827 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00003828 for (; Op != E; ++Op)
3829 if (*Op == From) break;
3830
3831 // If there are no matches, the user must use some other result of From.
3832 if (Op == E) continue;
3833
3834 // Okay, we know this user needs to be updated. Remove its old self
3835 // from the CSE maps.
3836 RemoveNodeFromCSEMaps(User);
3837
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003838 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00003839 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003840 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003841 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00003842 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003843 Op->setUser(User);
3844 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00003845 }
3846 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003847
3848 // Now that we have modified User, add it back to the CSE maps. If it
3849 // already exists there, recursively merge the results together.
3850 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003851 if (!Existing) {
3852 if (UpdateListener) UpdateListener->NodeUpdated(User);
3853 continue; // Continue on to next user.
3854 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003855
3856 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003857 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00003858 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003859 // can cause deletion of nodes that used the old value. To handle this, we
3860 // use CSUL to remove them from the Users set.
3861 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00003862
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003863 // User is now dead. Notify a listener if present.
3864 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00003865 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003866 }
3867}
3868
Evan Chenge6f35d82006-08-01 08:20:41 +00003869/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3870/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003871unsigned SelectionDAG::AssignNodeIds() {
3872 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003873 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3874 SDNode *N = I;
3875 N->setNodeId(Id++);
3876 }
3877 return Id;
3878}
3879
Evan Chenge6f35d82006-08-01 08:20:41 +00003880/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003881/// based on their topological order. It returns the maximum id and a vector
3882/// of the SDNodes* in assigned order by reference.
3883unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003884 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003885 std::vector<unsigned> InDegree(DAGSize);
3886 std::vector<SDNode*> Sources;
3887
3888 // Use a two pass approach to avoid using a std::map which is slow.
3889 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003890 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3891 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003892 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003893 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003894 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003895 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003896 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003897 }
3898
Evan Cheng99157a02006-08-07 22:13:29 +00003899 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003900 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003901 SDNode *N = Sources.back();
3902 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003903 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003904 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003905 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00003906 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003907 if (Degree == 0)
3908 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003909 }
3910 }
3911
Evan Chengc384d6c2006-08-02 22:00:34 +00003912 // Second pass, assign the actual topological order as node ids.
3913 Id = 0;
3914 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3915 TI != TE; ++TI)
3916 (*TI)->setNodeId(Id++);
3917
3918 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003919}
3920
3921
Evan Cheng091cba12006-07-27 06:39:06 +00003922
Jim Laskey58b968b2005-08-17 20:08:02 +00003923//===----------------------------------------------------------------------===//
3924// SDNode Class
3925//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003926
Chris Lattner917d2c92006-07-19 00:00:37 +00003927// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003928void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003929void UnarySDNode::ANCHOR() {}
3930void BinarySDNode::ANCHOR() {}
3931void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003932void HandleSDNode::ANCHOR() {}
3933void StringSDNode::ANCHOR() {}
3934void ConstantSDNode::ANCHOR() {}
3935void ConstantFPSDNode::ANCHOR() {}
3936void GlobalAddressSDNode::ANCHOR() {}
3937void FrameIndexSDNode::ANCHOR() {}
3938void JumpTableSDNode::ANCHOR() {}
3939void ConstantPoolSDNode::ANCHOR() {}
3940void BasicBlockSDNode::ANCHOR() {}
3941void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00003942void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003943void RegisterSDNode::ANCHOR() {}
3944void ExternalSymbolSDNode::ANCHOR() {}
3945void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00003946void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003947void VTSDNode::ANCHOR() {}
3948void LoadSDNode::ANCHOR() {}
3949void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003950void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003951
Chris Lattner48b85922007-02-04 02:41:42 +00003952HandleSDNode::~HandleSDNode() {
3953 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003954 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003955}
3956
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003957GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3958 MVT::ValueType VT, int o)
3959 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003960 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003961 // Thread Local
3962 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3963 // Non Thread Local
3964 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3965 getSDVTList(VT)), Offset(o) {
3966 TheGlobal = const_cast<GlobalValue*>(GA);
3967}
Chris Lattner48b85922007-02-04 02:41:42 +00003968
Dan Gohman36b5c132008-04-07 19:35:22 +00003969/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00003970/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00003971MachineMemOperand LSBaseSDNode::getMemOperand() const {
Dan Gohman69de1932008-02-06 22:27:42 +00003972 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3973 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00003974 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
3975 MachineMemOperand::MOStore;
3976 if (IsVolatile) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00003977
3978 // Check if the load references a frame index, and does not have
3979 // an SV attached.
3980 const FrameIndexSDNode *FI =
3981 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3982 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00003983 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
3984 FI->getIndex(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00003985 else
Dan Gohman36b5c132008-04-07 19:35:22 +00003986 return MachineMemOperand(getSrcValue(), Flags,
3987 getSrcValueOffset(), Size, Alignment);
Dan Gohman69de1932008-02-06 22:27:42 +00003988}
3989
Jim Laskey583bd472006-10-27 23:46:08 +00003990/// Profile - Gather unique data for the node.
3991///
3992void SDNode::Profile(FoldingSetNodeID &ID) {
3993 AddNodeIDNode(ID, this);
3994}
3995
Chris Lattnera3255112005-11-08 23:30:28 +00003996/// getValueTypeList - Return a pointer to the specified value type.
3997///
Dan Gohman547ca532008-02-08 03:26:46 +00003998const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003999 if (MVT::isExtendedVT(VT)) {
4000 static std::set<MVT::ValueType> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004001 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004002 } else {
4003 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
4004 VTs[VT] = VT;
4005 return &VTs[VT];
4006 }
Chris Lattnera3255112005-11-08 23:30:28 +00004007}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004008
Chris Lattner5c884562005-01-12 18:37:47 +00004009/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4010/// indicated value. This method ignores uses of other values defined by this
4011/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004012bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004013 assert(Value < getNumValues() && "Bad value!");
4014
4015 // If there is only one value, this is easy.
4016 if (getNumValues() == 1)
4017 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004018 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004019
Evan Cheng4ee62112006-02-05 06:29:23 +00004020 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004021
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004022 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004023
Roman Levensteindc1adac2008-04-07 10:06:32 +00004024 // TODO: Only iterate over uses of a given value of the node
4025 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4026 if (*UI == TheValue) {
4027 if (NUses == 0)
4028 return false;
4029 --NUses;
4030 }
Chris Lattner5c884562005-01-12 18:37:47 +00004031 }
4032
4033 // Found exactly the right number of uses?
4034 return NUses == 0;
4035}
4036
4037
Evan Cheng33d55952007-08-02 05:29:38 +00004038/// hasAnyUseOfValue - Return true if there are any use of the indicated
4039/// value. This method ignores uses of other values defined by this operation.
4040bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4041 assert(Value < getNumValues() && "Bad value!");
4042
Dan Gohman30359592008-01-29 13:02:09 +00004043 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004044
4045 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4046
4047 SmallPtrSet<SDNode*, 32> UsersHandled;
4048
Roman Levensteindc1adac2008-04-07 10:06:32 +00004049 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4050 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004051 if (User->getNumOperands() == 1 ||
4052 UsersHandled.insert(User)) // First time we've seen this?
4053 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4054 if (User->getOperand(i) == TheValue) {
4055 return true;
4056 }
4057 }
4058
4059 return false;
4060}
4061
4062
Evan Cheng917be682008-03-04 00:41:45 +00004063/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004064///
Evan Cheng917be682008-03-04 00:41:45 +00004065bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004066 bool Seen = false;
4067 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004068 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004069 if (User == this)
4070 Seen = true;
4071 else
4072 return false;
4073 }
4074
4075 return Seen;
4076}
4077
Evan Chenge6e97e62006-11-03 07:31:32 +00004078/// isOperand - Return true if this node is an operand of N.
4079///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004080bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004081 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4082 if (*this == N->getOperand(i))
4083 return true;
4084 return false;
4085}
4086
Evan Cheng917be682008-03-04 00:41:45 +00004087bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004088 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004089 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004090 return true;
4091 return false;
4092}
Evan Cheng4ee62112006-02-05 06:29:23 +00004093
Chris Lattner572dee72008-01-16 05:49:24 +00004094/// reachesChainWithoutSideEffects - Return true if this operand (which must
4095/// be a chain) reaches the specified operand without crossing any
4096/// side-effecting instructions. In practice, this looks through token
4097/// factors and non-volatile loads. In order to remain efficient, this only
4098/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004099bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004100 unsigned Depth) const {
4101 if (*this == Dest) return true;
4102
4103 // Don't search too deeply, we just want to be able to see through
4104 // TokenFactor's etc.
4105 if (Depth == 0) return false;
4106
4107 // If this is a token factor, all inputs to the TF happen in parallel. If any
4108 // of the operands of the TF reach dest, then we can do the xform.
4109 if (getOpcode() == ISD::TokenFactor) {
4110 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4111 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4112 return true;
4113 return false;
4114 }
4115
4116 // Loads don't have side effects, look through them.
4117 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4118 if (!Ld->isVolatile())
4119 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4120 }
4121 return false;
4122}
4123
4124
Evan Chengc5fc57d2006-11-03 03:05:24 +00004125static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004126 SmallPtrSet<SDNode *, 32> &Visited) {
4127 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004128 return;
4129
4130 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4131 SDNode *Op = N->getOperand(i).Val;
4132 if (Op == P) {
4133 found = true;
4134 return;
4135 }
4136 findPredecessor(Op, P, found, Visited);
4137 }
4138}
4139
Evan Cheng917be682008-03-04 00:41:45 +00004140/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004141/// is either an operand of N or it can be reached by recursively traversing
4142/// up the operands.
4143/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004144bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004145 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004146 bool found = false;
4147 findPredecessor(N, this, found, Visited);
4148 return found;
4149}
4150
Evan Chengc5484282006-10-04 00:56:09 +00004151uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4152 assert(Num < NumOperands && "Invalid child # of SDNode!");
4153 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4154}
4155
Reid Spencer577cc322007-04-01 07:32:19 +00004156std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004157 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004158 default:
4159 if (getOpcode() < ISD::BUILTIN_OP_END)
4160 return "<<Unknown DAG Node>>";
4161 else {
Evan Cheng72261582005-12-20 06:22:03 +00004162 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004163 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004164 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004165 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004166
Evan Cheng72261582005-12-20 06:22:03 +00004167 TargetLowering &TLI = G->getTargetLoweringInfo();
4168 const char *Name =
4169 TLI.getTargetNodeName(getOpcode());
4170 if (Name) return Name;
4171 }
4172
4173 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004174 }
4175
Evan Cheng27b7db52008-03-08 00:58:38 +00004176 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004177 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004178 case ISD::ATOMIC_LCS: return "AtomicLCS";
4179 case ISD::ATOMIC_LAS: return "AtomicLAS";
4180 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004181 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004182 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004183 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004184 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004185 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004186 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004187 case ISD::AssertSext: return "AssertSext";
4188 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004189
4190 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004191 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004192 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004193 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004194 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004195
4196 case ISD::Constant: return "Constant";
4197 case ISD::ConstantFP: return "ConstantFP";
4198 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004199 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004200 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004201 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004202 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004203 case ISD::RETURNADDR: return "RETURNADDR";
4204 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004205 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004206 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4207 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004208 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004209 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004210 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004211 case ISD::INTRINSIC_WO_CHAIN: {
4212 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4213 return Intrinsic::getName((Intrinsic::ID)IID);
4214 }
4215 case ISD::INTRINSIC_VOID:
4216 case ISD::INTRINSIC_W_CHAIN: {
4217 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004218 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004219 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004220
Chris Lattnerb2827b02006-03-19 00:52:58 +00004221 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004222 case ISD::TargetConstant: return "TargetConstant";
4223 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004224 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004225 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004226 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004227 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004228 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004229 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004230
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004231 case ISD::CopyToReg: return "CopyToReg";
4232 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004233 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004234 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004235 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00004236 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00004237 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004238 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004239 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004240 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004241
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004242 // Unary operators
4243 case ISD::FABS: return "fabs";
4244 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004245 case ISD::FSQRT: return "fsqrt";
4246 case ISD::FSIN: return "fsin";
4247 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004248 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004249 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004250
4251 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004252 case ISD::ADD: return "add";
4253 case ISD::SUB: return "sub";
4254 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004255 case ISD::MULHU: return "mulhu";
4256 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004257 case ISD::SDIV: return "sdiv";
4258 case ISD::UDIV: return "udiv";
4259 case ISD::SREM: return "srem";
4260 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004261 case ISD::SMUL_LOHI: return "smul_lohi";
4262 case ISD::UMUL_LOHI: return "umul_lohi";
4263 case ISD::SDIVREM: return "sdivrem";
4264 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004265 case ISD::AND: return "and";
4266 case ISD::OR: return "or";
4267 case ISD::XOR: return "xor";
4268 case ISD::SHL: return "shl";
4269 case ISD::SRA: return "sra";
4270 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004271 case ISD::ROTL: return "rotl";
4272 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004273 case ISD::FADD: return "fadd";
4274 case ISD::FSUB: return "fsub";
4275 case ISD::FMUL: return "fmul";
4276 case ISD::FDIV: return "fdiv";
4277 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004278 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004279 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004280
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004281 case ISD::SETCC: return "setcc";
4282 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004283 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004284 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004285 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004286 case ISD::CONCAT_VECTORS: return "concat_vectors";
4287 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004288 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004289 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004290 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004291 case ISD::ADDC: return "addc";
4292 case ISD::ADDE: return "adde";
4293 case ISD::SUBC: return "subc";
4294 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004295 case ISD::SHL_PARTS: return "shl_parts";
4296 case ISD::SRA_PARTS: return "sra_parts";
4297 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004298
4299 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4300 case ISD::INSERT_SUBREG: return "insert_subreg";
4301
Chris Lattner7f644642005-04-28 21:44:03 +00004302 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004303 case ISD::SIGN_EXTEND: return "sign_extend";
4304 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004305 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004306 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004307 case ISD::TRUNCATE: return "truncate";
4308 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004309 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004310 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004311 case ISD::FP_EXTEND: return "fp_extend";
4312
4313 case ISD::SINT_TO_FP: return "sint_to_fp";
4314 case ISD::UINT_TO_FP: return "uint_to_fp";
4315 case ISD::FP_TO_SINT: return "fp_to_sint";
4316 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004317 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004318
4319 // Control flow instructions
4320 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004321 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004322 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004323 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004324 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004325 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004326 case ISD::CALLSEQ_START: return "callseq_start";
4327 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004328
4329 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004330 case ISD::LOAD: return "load";
4331 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004332 case ISD::VAARG: return "vaarg";
4333 case ISD::VACOPY: return "vacopy";
4334 case ISD::VAEND: return "vaend";
4335 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004336 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004337 case ISD::EXTRACT_ELEMENT: return "extract_element";
4338 case ISD::BUILD_PAIR: return "build_pair";
4339 case ISD::STACKSAVE: return "stacksave";
4340 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004341 case ISD::TRAP: return "trap";
4342
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004343 // Bit manipulation
4344 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004345 case ISD::CTPOP: return "ctpop";
4346 case ISD::CTTZ: return "cttz";
4347 case ISD::CTLZ: return "ctlz";
4348
Chris Lattner36ce6912005-11-29 06:21:05 +00004349 // Debug info
4350 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004351 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004352
Duncan Sands36397f52007-07-27 12:58:54 +00004353 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004354 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004355
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004356 case ISD::CONDCODE:
4357 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004358 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004359 case ISD::SETOEQ: return "setoeq";
4360 case ISD::SETOGT: return "setogt";
4361 case ISD::SETOGE: return "setoge";
4362 case ISD::SETOLT: return "setolt";
4363 case ISD::SETOLE: return "setole";
4364 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004365
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004366 case ISD::SETO: return "seto";
4367 case ISD::SETUO: return "setuo";
4368 case ISD::SETUEQ: return "setue";
4369 case ISD::SETUGT: return "setugt";
4370 case ISD::SETUGE: return "setuge";
4371 case ISD::SETULT: return "setult";
4372 case ISD::SETULE: return "setule";
4373 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004374
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004375 case ISD::SETEQ: return "seteq";
4376 case ISD::SETGT: return "setgt";
4377 case ISD::SETGE: return "setge";
4378 case ISD::SETLT: return "setlt";
4379 case ISD::SETLE: return "setle";
4380 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004381 }
4382 }
4383}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004384
Evan Cheng144d8f02006-11-09 17:55:04 +00004385const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004386 switch (AM) {
4387 default:
4388 return "";
4389 case ISD::PRE_INC:
4390 return "<pre-inc>";
4391 case ISD::PRE_DEC:
4392 return "<pre-dec>";
4393 case ISD::POST_INC:
4394 return "<post-inc>";
4395 case ISD::POST_DEC:
4396 return "<post-dec>";
4397 }
4398}
4399
Duncan Sands276dcbd2008-03-21 09:14:45 +00004400std::string ISD::ArgFlagsTy::getArgFlagsString() {
4401 std::string S = "< ";
4402
4403 if (isZExt())
4404 S += "zext ";
4405 if (isSExt())
4406 S += "sext ";
4407 if (isInReg())
4408 S += "inreg ";
4409 if (isSRet())
4410 S += "sret ";
4411 if (isByVal())
4412 S += "byval ";
4413 if (isNest())
4414 S += "nest ";
4415 if (getByValAlign())
4416 S += "byval-align:" + utostr(getByValAlign()) + " ";
4417 if (getOrigAlign())
4418 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4419 if (getByValSize())
4420 S += "byval-size:" + utostr(getByValSize()) + " ";
4421 return S + ">";
4422}
4423
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004424void SDNode::dump() const { dump(0); }
4425void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004426 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004427
4428 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004429 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004430 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004431 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004432 else
Bill Wendling832171c2006-12-07 20:04:42 +00004433 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00004434 }
Bill Wendling832171c2006-12-07 20:04:42 +00004435 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004436
Bill Wendling832171c2006-12-07 20:04:42 +00004437 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004438 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004439 if (i) cerr << ", ";
4440 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004441 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004442 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004443 }
4444
Evan Chengce254432007-12-11 02:08:35 +00004445 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4446 SDNode *Mask = getOperand(2).Val;
4447 cerr << "<";
4448 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4449 if (i) cerr << ",";
4450 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4451 cerr << "u";
4452 else
4453 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4454 }
4455 cerr << ">";
4456 }
4457
Chris Lattnerc3aae252005-01-07 07:46:32 +00004458 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004459 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004460 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004461 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4462 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4463 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4464 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4465 else {
4466 cerr << "<APFloat(";
4467 CSDN->getValueAPF().convertToAPInt().dump();
4468 cerr << ")>";
4469 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004470 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004471 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004472 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004473 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004474 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004475 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004476 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004477 else
Bill Wendling832171c2006-12-07 20:04:42 +00004478 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004479 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004480 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004481 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004482 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004483 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004484 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004485 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004486 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004487 else
Bill Wendling832171c2006-12-07 20:04:42 +00004488 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004489 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004490 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004491 else
Bill Wendling832171c2006-12-07 20:04:42 +00004492 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004493 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004494 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004495 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4496 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004497 cerr << LBB->getName() << " ";
4498 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004499 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004500 if (G && R->getReg() &&
4501 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004502 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004503 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004504 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004505 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004506 } else if (const ExternalSymbolSDNode *ES =
4507 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004508 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004509 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4510 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004511 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004512 else
Dan Gohman69de1932008-02-06 22:27:42 +00004513 cerr << "<null>";
4514 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4515 if (M->MO.getValue())
4516 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4517 else
4518 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004519 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4520 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004521 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004522 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004523 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004524 const Value *SrcValue = LD->getSrcValue();
4525 int SrcOffset = LD->getSrcValueOffset();
4526 cerr << " <";
4527 if (SrcValue)
4528 cerr << SrcValue;
4529 else
4530 cerr << "null";
4531 cerr << ":" << SrcOffset << ">";
4532
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004533 bool doExt = true;
4534 switch (LD->getExtensionType()) {
4535 default: doExt = false; break;
4536 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004537 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004538 break;
4539 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004540 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004541 break;
4542 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004543 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004544 break;
4545 }
4546 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004547 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004548
Evan Cheng144d8f02006-11-09 17:55:04 +00004549 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004550 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004551 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004552 if (LD->isVolatile())
4553 cerr << " <volatile>";
4554 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004555 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004556 const Value *SrcValue = ST->getSrcValue();
4557 int SrcOffset = ST->getSrcValueOffset();
4558 cerr << " <";
4559 if (SrcValue)
4560 cerr << SrcValue;
4561 else
4562 cerr << "null";
4563 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004564
4565 if (ST->isTruncatingStore())
4566 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004567 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004568
4569 const char *AM = getIndexedModeName(ST->getAddressingMode());
4570 if (*AM)
4571 cerr << " " << AM;
4572 if (ST->isVolatile())
4573 cerr << " <volatile>";
4574 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004575 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004576}
4577
Chris Lattnerde202b32005-11-09 23:47:37 +00004578static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004579 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4580 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004581 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004582 else
Bill Wendling832171c2006-12-07 20:04:42 +00004583 cerr << "\n" << std::string(indent+2, ' ')
4584 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004585
Chris Lattnerea946cd2005-01-09 20:38:33 +00004586
Bill Wendling832171c2006-12-07 20:04:42 +00004587 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004588 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004589}
4590
Chris Lattnerc3aae252005-01-07 07:46:32 +00004591void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004592 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004593 std::vector<const SDNode*> Nodes;
4594 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4595 I != E; ++I)
4596 Nodes.push_back(I);
4597
Chris Lattner49d24712005-01-09 20:26:36 +00004598 std::sort(Nodes.begin(), Nodes.end());
4599
4600 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004601 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004602 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004603 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004604
Jim Laskey26f7fa72006-10-17 19:33:52 +00004605 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004606
Bill Wendling832171c2006-12-07 20:04:42 +00004607 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004608}
4609
Evan Chengd6594ae2006-09-12 21:00:35 +00004610const Type *ConstantPoolSDNode::getType() const {
4611 if (isMachineConstantPoolEntry())
4612 return Val.MachineCPVal->getType();
4613 return Val.ConstVal->getType();
4614}