blob: ab222c4f2faa14b7dbd59a3dd72cc681728edac1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG class.
11//
12//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013#include "llvm/CodeGen/SelectionDAG.h"
14#include "llvm/Constants.h"
Evan Cheng833501d2008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/GlobalVariable.h"
18#include "llvm/Intrinsics.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Assembly/Writer.h"
Dan Gohmane8b391e2008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner53f5aee2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Dan Gohman3d015562009-01-22 21:58:43 +000030#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Bill Wendling5db7ffb2008-09-30 21:22:07 +000033#include "llvm/Support/CommandLine.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000034#include "llvm/Support/MathExtras.h"
35#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/ADT/SetVector.h"
37#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsa9810f32007-10-16 09:56:48 +000038#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/ADT/SmallVector.h"
40#include "llvm/ADT/StringExtras.h"
41#include <algorithm>
42#include <cmath>
43using namespace llvm;
44
45/// makeVTList - Return an instance of the SDVTList struct initialized with the
46/// specified members.
Duncan Sands92c43912008-06-06 12:08:01 +000047static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 SDVTList Res = {VTs, NumVTs};
49 return Res;
50}
51
Duncan Sands92c43912008-06-06 12:08:01 +000052static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
53 switch (VT.getSimpleVT()) {
Chris Lattnerd037d482008-03-05 06:48:13 +000054 default: assert(0 && "Unknown FP format");
55 case MVT::f32: return &APFloat::IEEEsingle;
56 case MVT::f64: return &APFloat::IEEEdouble;
57 case MVT::f80: return &APFloat::x87DoubleExtended;
58 case MVT::f128: return &APFloat::IEEEquad;
59 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
60 }
61}
62
Chris Lattner7bcb18f2008-02-03 06:49:24 +000063SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
64
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065//===----------------------------------------------------------------------===//
66// ConstantFPSDNode Class
67//===----------------------------------------------------------------------===//
68
69/// isExactlyValue - We don't rely on operator== working on double values, as
70/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
71/// As such, this method can be used to do an exact bit-for-bit comparison of
72/// two floating point values.
Dale Johannesenc53301c2007-08-26 01:18:27 +000073bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanc1f3a072008-09-12 18:08:03 +000074 return getValueAPF().bitwiseIsEqual(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075}
76
Duncan Sands92c43912008-06-06 12:08:01 +000077bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenbbe2b702007-08-30 00:23:21 +000078 const APFloat& Val) {
Duncan Sands92c43912008-06-06 12:08:01 +000079 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerd037d482008-03-05 06:48:13 +000080
Dale Johannesen90c25d12008-04-20 18:23:46 +000081 // PPC long double cannot be converted to any other type.
82 if (VT == MVT::ppcf128 ||
83 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerd037d482008-03-05 06:48:13 +000084 return false;
85
Dale Johannesenbbe2b702007-08-30 00:23:21 +000086 // convert modifies in place, so make a copy.
87 APFloat Val2 = APFloat(Val);
Dale Johannesen6e547b42008-10-09 23:00:39 +000088 bool losesInfo;
89 (void) Val2.convert(*MVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
90 &losesInfo);
91 return !losesInfo;
Dale Johannesenbbe2b702007-08-30 00:23:21 +000092}
93
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094//===----------------------------------------------------------------------===//
95// ISD Namespace
96//===----------------------------------------------------------------------===//
97
98/// isBuildVectorAllOnes - Return true if the specified node is a
99/// BUILD_VECTOR where all of the elements are ~0 or undef.
100bool ISD::isBuildVectorAllOnes(const SDNode *N) {
101 // Look through a bit convert.
102 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greif1c80d112008-08-28 21:40:38 +0000103 N = N->getOperand(0).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
105 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
106
107 unsigned i = 0, e = N->getNumOperands();
108
109 // Skip over all of the undef values.
110 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
111 ++i;
112
113 // Do not accept an all-undef vector.
114 if (i == e) return false;
115
116 // Do not accept build_vectors that aren't all constants or which have non-~0
117 // elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000118 SDValue NotZero = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 if (isa<ConstantSDNode>(NotZero)) {
120 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
121 return false;
122 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman161652c2008-02-29 01:47:35 +0000123 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen6e547b42008-10-09 23:00:39 +0000124 bitcastToAPInt().isAllOnesValue())
Dan Gohman161652c2008-02-29 01:47:35 +0000125 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 } else
127 return false;
128
129 // Okay, we have at least one ~0 value, check to see if the rest match or are
130 // undefs.
131 for (++i; i != e; ++i)
132 if (N->getOperand(i) != NotZero &&
133 N->getOperand(i).getOpcode() != ISD::UNDEF)
134 return false;
135 return true;
136}
137
138
139/// isBuildVectorAllZeros - Return true if the specified node is a
140/// BUILD_VECTOR where all of the elements are 0 or undef.
141bool ISD::isBuildVectorAllZeros(const SDNode *N) {
142 // Look through a bit convert.
143 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greif1c80d112008-08-28 21:40:38 +0000144 N = N->getOperand(0).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145
146 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
147
148 unsigned i = 0, e = N->getNumOperands();
149
150 // Skip over all of the undef values.
151 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
152 ++i;
153
154 // Do not accept an all-undef vector.
155 if (i == e) return false;
156
157 // Do not accept build_vectors that aren't all constants or which have non-~0
158 // elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 SDValue Zero = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 if (isa<ConstantSDNode>(Zero)) {
161 if (!cast<ConstantSDNode>(Zero)->isNullValue())
162 return false;
163 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000164 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 return false;
166 } else
167 return false;
168
169 // Okay, we have at least one ~0 value, check to see if the rest match or are
170 // undefs.
171 for (++i; i != e; ++i)
172 if (N->getOperand(i) != Zero &&
173 N->getOperand(i).getOpcode() != ISD::UNDEF)
174 return false;
175 return true;
176}
177
Evan Chengd1045a62008-02-18 23:04:32 +0000178/// isScalarToVector - Return true if the specified node is a
179/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
180/// element is not an undef.
181bool ISD::isScalarToVector(const SDNode *N) {
182 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
183 return true;
184
185 if (N->getOpcode() != ISD::BUILD_VECTOR)
186 return false;
187 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
188 return false;
189 unsigned NumElems = N->getNumOperands();
190 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000191 SDValue V = N->getOperand(i);
Evan Chengd1045a62008-02-18 23:04:32 +0000192 if (V.getOpcode() != ISD::UNDEF)
193 return false;
194 }
195 return true;
196}
197
198
Evan Cheng13d1c292008-01-31 09:59:15 +0000199/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohmanfa607c92008-07-01 00:05:16 +0000200/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Cheng13d1c292008-01-31 09:59:15 +0000201bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000202 SDValue Zero;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000203 if (N->getOpcode() == ISD::DBG_LABEL)
204 return true;
Dan Gohmanbd68c792008-07-17 19:10:17 +0000205 if (N->isMachineOpcode() &&
206 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohmanfa607c92008-07-01 00:05:16 +0000207 return true;
208 return false;
Evan Cheng13d1c292008-01-31 09:59:15 +0000209}
210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
212/// when given the operation for (X op Y).
213ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
214 // To perform this operation, we just need to swap the L and G bits of the
215 // operation.
216 unsigned OldL = (Operation >> 2) & 1;
217 unsigned OldG = (Operation >> 1) & 1;
218 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
219 (OldL << 1) | // New G bit
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000220 (OldG << 2)); // New L bit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221}
222
223/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
224/// 'op' is a valid SetCC operation.
225ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
226 unsigned Operation = Op;
227 if (isInteger)
228 Operation ^= 7; // Flip L, G, E bits, but not U.
229 else
230 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 if (Operation > ISD::SETTRUE2)
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000233 Operation &= ~8; // Don't let N and U bits get set.
234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 return ISD::CondCode(Operation);
236}
237
238
239/// isSignedOp - For an integer comparison, return 1 if the comparison is a
240/// signed operation and 2 if the result is an unsigned comparison. Return zero
241/// if the operation does not depend on the sign of the input (setne and seteq).
242static int isSignedOp(ISD::CondCode Opcode) {
243 switch (Opcode) {
244 default: assert(0 && "Illegal integer setcc operation!");
245 case ISD::SETEQ:
246 case ISD::SETNE: return 0;
247 case ISD::SETLT:
248 case ISD::SETLE:
249 case ISD::SETGT:
250 case ISD::SETGE: return 1;
251 case ISD::SETULT:
252 case ISD::SETULE:
253 case ISD::SETUGT:
254 case ISD::SETUGE: return 2;
255 }
256}
257
258/// getSetCCOrOperation - Return the result of a logical OR between different
259/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
260/// returns SETCC_INVALID if it is not possible to represent the resultant
261/// comparison.
262ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
263 bool isInteger) {
264 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
265 // Cannot fold a signed integer setcc with an unsigned integer setcc.
266 return ISD::SETCC_INVALID;
267
268 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
269
270 // If the N and U bits get set then the resultant comparison DOES suddenly
271 // care about orderedness, and is true when ordered.
272 if (Op > ISD::SETTRUE2)
273 Op &= ~16; // Clear the U bit if the N bit is set.
274
275 // Canonicalize illegal integer setcc's.
276 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
277 Op = ISD::SETNE;
278
279 return ISD::CondCode(Op);
280}
281
282/// getSetCCAndOperation - Return the result of a logical AND between different
283/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
284/// function returns zero if it is not possible to represent the resultant
285/// comparison.
286ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
287 bool isInteger) {
288 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
289 // Cannot fold a signed setcc with an unsigned setcc.
290 return ISD::SETCC_INVALID;
291
292 // Combine all of the condition bits.
293 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
294
295 // Canonicalize illegal integer setcc's.
296 if (isInteger) {
297 switch (Result) {
298 default: break;
299 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmanf1e8e552008-05-14 18:17:09 +0000300 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
302 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
303 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
304 }
305 }
306
307 return Result;
308}
309
310const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman404e8542008-09-04 15:39:15 +0000311 return MF->getTarget();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312}
313
314//===----------------------------------------------------------------------===//
315// SDNode Profile Support
316//===----------------------------------------------------------------------===//
317
318/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
319///
320static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
321 ID.AddInteger(OpC);
322}
323
324/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
325/// solely with their pointer.
Dan Gohman089efff2008-05-13 00:00:25 +0000326static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 ID.AddPointer(VTList.VTs);
328}
329
330/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
331///
332static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman8181bd12008-07-27 21:46:04 +0000333 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 for (; NumOps; --NumOps, ++Ops) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000335 ID.AddPointer(Ops->getNode());
Gabor Greif46bf5472008-08-26 22:36:50 +0000336 ID.AddInteger(Ops->getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 }
338}
339
Dan Gohman703ce5d2008-07-07 18:26:29 +0000340/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
341///
342static void AddNodeIDOperands(FoldingSetNodeID &ID,
343 const SDUse *Ops, unsigned NumOps) {
344 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000345 ID.AddPointer(Ops->getVal());
Gabor Greif46bf5472008-08-26 22:36:50 +0000346 ID.AddInteger(Ops->getSDValue().getResNo());
Dan Gohman703ce5d2008-07-07 18:26:29 +0000347 }
348}
349
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350static void AddNodeIDNode(FoldingSetNodeID &ID,
351 unsigned short OpC, SDVTList VTList,
Dan Gohman8181bd12008-07-27 21:46:04 +0000352 const SDValue *OpList, unsigned N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 AddNodeIDOpcode(ID, OpC);
354 AddNodeIDValueTypes(ID, VTList);
355 AddNodeIDOperands(ID, OpList, N);
356}
357
Duncan Sands1e450d42008-10-27 15:30:53 +0000358/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
359/// the NodeID data.
360static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 switch (N->getOpcode()) {
362 default: break; // Normal nodes don't need extra info.
Duncan Sandsc93fae32008-03-21 09:14:45 +0000363 case ISD::ARG_FLAGS:
364 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
365 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 case ISD::TargetConstant:
367 case ISD::Constant:
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000368 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 break;
370 case ISD::TargetConstantFP:
Dale Johannesendf8a8312007-08-31 04:03:46 +0000371 case ISD::ConstantFP: {
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000372 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 break;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000374 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 case ISD::TargetGlobalAddress:
376 case ISD::GlobalAddress:
377 case ISD::TargetGlobalTLSAddress:
378 case ISD::GlobalTLSAddress: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000379 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 ID.AddPointer(GA->getGlobal());
381 ID.AddInteger(GA->getOffset());
382 break;
383 }
384 case ISD::BasicBlock:
385 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
386 break;
387 case ISD::Register:
388 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
389 break;
Dan Gohman472d12c2008-06-30 20:59:49 +0000390 case ISD::DBG_STOPPOINT: {
391 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
392 ID.AddInteger(DSP->getLine());
393 ID.AddInteger(DSP->getColumn());
394 ID.AddPointer(DSP->getCompileUnit());
395 break;
396 }
Dan Gohman12a9c082008-02-06 22:27:42 +0000397 case ISD::SRCVALUE:
398 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
399 break;
400 case ISD::MEMOPERAND: {
Dan Gohman1fad9e62008-04-07 19:35:22 +0000401 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman98beebe2008-08-20 15:58:01 +0000402 MO.Profile(ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 break;
404 }
405 case ISD::FrameIndex:
406 case ISD::TargetFrameIndex:
407 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
408 break;
409 case ISD::JumpTable:
410 case ISD::TargetJumpTable:
411 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
412 break;
413 case ISD::ConstantPool:
414 case ISD::TargetConstantPool: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000415 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 ID.AddInteger(CP->getAlignment());
417 ID.AddInteger(CP->getOffset());
418 if (CP->isMachineConstantPoolEntry())
419 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
420 else
421 ID.AddPointer(CP->getConstVal());
422 break;
423 }
Dan Gohman791e12a2008-09-15 19:46:03 +0000424 case ISD::CALL: {
425 const CallSDNode *Call = cast<CallSDNode>(N);
426 ID.AddInteger(Call->getCallingConv());
427 ID.AddInteger(Call->isVarArg());
428 break;
429 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 case ISD::LOAD: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000431 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 ID.AddInteger(LD->getAddressingMode());
433 ID.AddInteger(LD->getExtensionType());
Duncan Sands3f5d2432008-06-06 12:49:32 +0000434 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman98beebe2008-08-20 15:58:01 +0000435 ID.AddInteger(LD->getRawFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 break;
437 }
438 case ISD::STORE: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000439 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 ID.AddInteger(ST->getAddressingMode());
441 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3f5d2432008-06-06 12:49:32 +0000442 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman98beebe2008-08-20 15:58:01 +0000443 ID.AddInteger(ST->getRawFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 break;
445 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000446 case ISD::ATOMIC_CMP_SWAP:
447 case ISD::ATOMIC_SWAP:
448 case ISD::ATOMIC_LOAD_ADD:
449 case ISD::ATOMIC_LOAD_SUB:
450 case ISD::ATOMIC_LOAD_AND:
451 case ISD::ATOMIC_LOAD_OR:
452 case ISD::ATOMIC_LOAD_XOR:
453 case ISD::ATOMIC_LOAD_NAND:
454 case ISD::ATOMIC_LOAD_MIN:
455 case ISD::ATOMIC_LOAD_MAX:
456 case ISD::ATOMIC_LOAD_UMIN:
457 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000458 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
459 ID.AddInteger(AT->getRawFlags());
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000460 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000462 } // end switch (N->getOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463}
464
Duncan Sands1e450d42008-10-27 15:30:53 +0000465/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
466/// data.
467static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
468 AddNodeIDOpcode(ID, N->getOpcode());
469 // Add the return value info.
470 AddNodeIDValueTypes(ID, N->getVTList());
471 // Add the operand info.
472 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
473
474 // Handle SDNode leafs with special info.
475 AddNodeIDCustom(ID, N);
476}
477
Dan Gohman98beebe2008-08-20 15:58:01 +0000478/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
479/// the CSE map that carries both alignment and volatility information.
480///
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000481static inline unsigned
482encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
Dan Gohman98beebe2008-08-20 15:58:01 +0000483 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
484}
485
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486//===----------------------------------------------------------------------===//
487// SelectionDAG Class
488//===----------------------------------------------------------------------===//
489
Duncan Sands1e450d42008-10-27 15:30:53 +0000490/// doNotCSE - Return true if CSE should not be performed for this node.
491static bool doNotCSE(SDNode *N) {
492 if (N->getValueType(0) == MVT::Flag)
493 return true; // Never CSE anything that produces a flag.
494
495 switch (N->getOpcode()) {
496 default: break;
497 case ISD::HANDLENODE:
498 case ISD::DBG_LABEL:
499 case ISD::DBG_STOPPOINT:
500 case ISD::EH_LABEL:
501 case ISD::DECLARE:
502 return true; // Never CSE these nodes.
503 }
504
505 // Check that remaining values produced are not flags.
506 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
507 if (N->getValueType(i) == MVT::Flag)
508 return true; // Never CSE anything that produces a flag.
509
510 return false;
511}
512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513/// RemoveDeadNodes - This method deletes all unreachable nodes in the
514/// SelectionDAG.
515void SelectionDAG::RemoveDeadNodes() {
516 // Create a dummy node (which is not added to allnodes), that adds a reference
517 // to the root node, preventing it from being deleted.
518 HandleSDNode Dummy(getRoot());
519
520 SmallVector<SDNode*, 128> DeadNodes;
521
522 // Add all obviously-dead nodes to the DeadNodes worklist.
523 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
524 if (I->use_empty())
525 DeadNodes.push_back(I);
526
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000527 RemoveDeadNodes(DeadNodes);
528
529 // If the root changed (e.g. it was a dead load, update the root).
530 setRoot(Dummy.getValue());
531}
532
533/// RemoveDeadNodes - This method deletes the unreachable nodes in the
534/// given list, and any nodes that become unreachable as a result.
535void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
536 DAGUpdateListener *UpdateListener) {
537
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 // Process the worklist, deleting the nodes and adding their uses to the
539 // worklist.
540 while (!DeadNodes.empty()) {
541 SDNode *N = DeadNodes.back();
542 DeadNodes.pop_back();
543
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000544 if (UpdateListener)
545 UpdateListener->NodeDeleted(N, 0);
546
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 // Take the node out of the appropriate CSE map.
548 RemoveNodeFromCSEMaps(N);
549
550 // Next, brutally remove the operand list. This is safe to do, as there are
551 // no cycles in the graph.
552 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000553 SDNode *Operand = I->getVal();
Roman Levenstein05650fd2008-04-07 10:06:32 +0000554 Operand->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555
556 // Now that we removed this operand, see if there are no uses of it left.
557 if (Operand->use_empty())
558 DeadNodes.push_back(Operand);
559 }
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000560
Dan Gohman98a355c2009-01-19 22:39:36 +0000561 DeallocateNode(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563}
564
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000565void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmanbd68c792008-07-17 19:10:17 +0000566 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000567 RemoveDeadNodes(DeadNodes, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568}
569
570void SelectionDAG::DeleteNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 // First take this out of the appropriate CSE map.
572 RemoveNodeFromCSEMaps(N);
573
574 // Finally, remove uses due to operands of this node, remove from the
575 // AllNodes list, and delete the node.
576 DeleteNodeNotInCSEMaps(N);
577}
578
579void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
djgf03abfe2009-01-25 16:20:37 +0000580 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
581 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman98a355c2009-01-19 22:39:36 +0000582
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000583 // Drop all of the operands and decrement used node's use counts.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000585 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000586
Dan Gohman98a355c2009-01-19 22:39:36 +0000587 DeallocateNode(N);
588}
589
590void SelectionDAG::DeallocateNode(SDNode *N) {
591 if (N->OperandsNeedDelete)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 delete[] N->OperandList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593
Dan Gohman98a355c2009-01-19 22:39:36 +0000594 // Set the opcode to DELETED_NODE to help catch bugs when node
595 // memory is reallocated.
596 N->NodeType = ISD::DELETED_NODE;
597
Dan Gohman0b08b152008-08-26 01:44:34 +0000598 NodeAllocator.Deallocate(AllNodes.remove(N));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599}
600
601/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
602/// correspond to it. This is useful when we're about to delete or repurpose
603/// the node. We don't want future request for structurally identical nodes
604/// to return N anymore.
Dan Gohman705e3f72008-09-13 01:54:27 +0000605bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 bool Erased = false;
607 switch (N->getOpcode()) {
Dan Gohman14a66442008-08-23 02:25:05 +0000608 case ISD::EntryToken:
609 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman705e3f72008-09-13 01:54:27 +0000610 return false;
611 case ISD::HANDLENODE: return false; // noop.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 case ISD::CONDCODE:
613 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
614 "Cond code doesn't exist!");
615 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
616 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
617 break;
Bill Wendlingfef06052008-09-16 21:48:12 +0000618 case ISD::ExternalSymbol:
619 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 break;
Bill Wendlingfef06052008-09-16 21:48:12 +0000621 case ISD::TargetExternalSymbol:
622 Erased =
623 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000625 case ISD::VALUETYPE: {
Duncan Sands92c43912008-06-06 12:08:01 +0000626 MVT VT = cast<VTSDNode>(N)->getVT();
627 if (VT.isExtended()) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000628 Erased = ExtendedValueTypeNodes.erase(VT);
629 } else {
Duncan Sands92c43912008-06-06 12:08:01 +0000630 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
631 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000632 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000634 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 default:
636 // Remove it from the CSE Map.
637 Erased = CSEMap.RemoveNode(N);
638 break;
639 }
640#ifndef NDEBUG
641 // Verify that the node was actually in one of the CSE maps, unless it has a
642 // flag result (which cannot be CSE'd) or is one of the special cases that are
643 // not subject to CSE.
644 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands1e450d42008-10-27 15:30:53 +0000645 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 N->dump(this);
647 cerr << "\n";
648 assert(0 && "Node is not in map!");
649 }
650#endif
Dan Gohman705e3f72008-09-13 01:54:27 +0000651 return Erased;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652}
653
djg943376a2009-01-25 16:29:12 +0000654/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
655/// maps and modified in place. Add it back to the CSE maps, unless an identical
656/// node already exists, in which case transfer all its users to the existing
657/// node. This transfer can potentially trigger recursive merging.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658///
djg943376a2009-01-25 16:29:12 +0000659void
660SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
661 DAGUpdateListener *UpdateListener) {
662 // For node types that aren't CSE'd, just act as if no identical node
663 // already exists.
664 if (!doNotCSE(N)) {
665 SDNode *Existing = CSEMap.GetOrInsertNode(N);
666 if (Existing != N) {
667 // If there was already an existing matching node, use ReplaceAllUsesWith
668 // to replace the dead one with the existing one. This can cause
669 // recursive merging of other unrelated nodes down the line.
670 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Chengd6f57682008-07-08 20:06:39 +0000671
djg943376a2009-01-25 16:29:12 +0000672 // N is now dead. Inform the listener if it exists and delete it.
673 if (UpdateListener)
674 UpdateListener->NodeDeleted(N, Existing);
675 DeleteNodeNotInCSEMaps(N);
676 return;
677 }
678 }
Evan Chengd6f57682008-07-08 20:06:39 +0000679
djg943376a2009-01-25 16:29:12 +0000680 // If the node doesn't already exist, we updated it. Inform a listener if
681 // it exists.
682 if (UpdateListener)
683 UpdateListener->NodeUpdated(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684}
685
686/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
687/// were replaced with those specified. If this node is never memoized,
688/// return null, otherwise return a pointer to the slot it would take. If a
689/// node already exists with these operands, the slot will be non-null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000690SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000692 if (doNotCSE(N))
693 return 0;
Evan Chengd6f57682008-07-08 20:06:39 +0000694
Dan Gohman8181bd12008-07-27 21:46:04 +0000695 SDValue Ops[] = { Op };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 FoldingSetNodeID ID;
697 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands1e450d42008-10-27 15:30:53 +0000698 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
700}
701
702/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
703/// were replaced with those specified. If this node is never memoized,
704/// return null, otherwise return a pointer to the slot it would take. If a
705/// node already exists with these operands, the slot will be non-null.
706SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000707 SDValue Op1, SDValue Op2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000709 if (doNotCSE(N))
710 return 0;
711
Dan Gohman8181bd12008-07-27 21:46:04 +0000712 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 FoldingSetNodeID ID;
714 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands1e450d42008-10-27 15:30:53 +0000715 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
717}
718
719
720/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
721/// were replaced with those specified. If this node is never memoized,
722/// return null, otherwise return a pointer to the slot it would take. If a
723/// node already exists with these operands, the slot will be non-null.
724SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000725 const SDValue *Ops,unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000727 if (doNotCSE(N))
728 return 0;
Evan Chengd6f57682008-07-08 20:06:39 +0000729
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 FoldingSetNodeID ID;
731 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands1e450d42008-10-27 15:30:53 +0000732 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
734}
735
Duncan Sandsd3ace282008-07-21 10:20:31 +0000736/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
737void SelectionDAG::VerifyNode(SDNode *N) {
738 switch (N->getOpcode()) {
739 default:
740 break;
Duncan Sands70269a72008-10-29 14:22:20 +0000741 case ISD::BUILD_PAIR: {
742 MVT VT = N->getValueType(0);
743 assert(N->getNumValues() == 1 && "Too many results!");
744 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
745 "Wrong return type!");
746 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
747 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
748 "Mismatched operand types!");
749 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
750 "Wrong operand type!");
751 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
752 "Wrong return type size");
753 break;
754 }
Duncan Sandsd3ace282008-07-21 10:20:31 +0000755 case ISD::BUILD_VECTOR: {
Duncan Sands70269a72008-10-29 14:22:20 +0000756 assert(N->getNumValues() == 1 && "Too many results!");
757 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd3ace282008-07-21 10:20:31 +0000758 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands70269a72008-10-29 14:22:20 +0000759 "Wrong number of operands!");
Duncan Sands944d0d52008-10-22 09:00:33 +0000760 // FIXME: Change vector_shuffle to a variadic node with mask elements being
761 // operands of the node. Currently the mask is a BUILD_VECTOR passed as an
762 // operand, and it is not always possible to legalize it. Turning off the
763 // following checks at least makes it possible to legalize most of the time.
764// MVT EltVT = N->getValueType(0).getVectorElementType();
765// for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
766// assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sands70269a72008-10-29 14:22:20 +0000767// "Wrong operand type!");
Duncan Sandsd3ace282008-07-21 10:20:31 +0000768 break;
769 }
770 }
771}
772
Dan Gohman9e3a3422008-07-08 23:46:32 +0000773/// getMVTAlignment - Compute the default alignment value for the
774/// given type.
775///
776unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
777 const Type *Ty = VT == MVT::iPTR ?
778 PointerType::get(Type::Int8Ty, 0) :
779 VT.getTypeForMVT();
780
781 return TLI.getTargetData()->getABITypeAlignment(Ty);
782}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000784SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
785 : TLI(tli), FLI(fli),
786 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohman14a66442008-08-23 02:25:05 +0000787 Root(getEntryNode()) {
788 AllNodes.push_back(&EntryNode);
Dan Gohman03405562008-08-23 00:50:30 +0000789}
790
Devang Patel10e63332009-01-09 19:11:50 +0000791void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
792 DwarfWriter *dw) {
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000793 MF = &mf;
794 MMI = mmi;
Devang Patel10e63332009-01-09 19:11:50 +0000795 DW = dw;
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000796}
797
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798SelectionDAG::~SelectionDAG() {
Dan Gohman14a66442008-08-23 02:25:05 +0000799 allnodes_clear();
800}
801
802void SelectionDAG::allnodes_clear() {
Dan Gohman0b08b152008-08-26 01:44:34 +0000803 assert(&*AllNodes.begin() == &EntryNode);
804 AllNodes.remove(AllNodes.begin());
Dan Gohman98a355c2009-01-19 22:39:36 +0000805 while (!AllNodes.empty())
806 DeallocateNode(AllNodes.begin());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807}
808
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000809void SelectionDAG::clear() {
Dan Gohman14a66442008-08-23 02:25:05 +0000810 allnodes_clear();
811 OperandAllocator.Reset();
812 CSEMap.clear();
813
814 ExtendedValueTypeNodes.clear();
Bill Wendlingfef06052008-09-16 21:48:12 +0000815 ExternalSymbols.clear();
816 TargetExternalSymbols.clear();
Dan Gohman14a66442008-08-23 02:25:05 +0000817 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
818 static_cast<CondCodeSDNode*>(0));
819 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
820 static_cast<SDNode*>(0));
821
822 EntryNode.Uses = 0;
823 AllNodes.push_back(&EntryNode);
824 Root = getEntryNode();
825}
826
Dan Gohman8181bd12008-07-27 21:46:04 +0000827SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 if (Op.getValueType() == VT) return Op;
Dan Gohman161652c2008-02-29 01:47:35 +0000829 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +0000830 VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 return getNode(ISD::AND, Op.getValueType(), Op,
832 getConstant(Imm, Op.getValueType()));
833}
834
Bob Wilson81a42cf2009-01-22 17:39:32 +0000835/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
836///
837SDValue SelectionDAG::getNOT(SDValue Val, MVT VT) {
838 SDValue NegOne;
839 if (VT.isVector()) {
840 MVT EltVT = VT.getVectorElementType();
841 SDValue NegOneElt = getConstant(EltVT.getIntegerVTBitMask(), EltVT);
842 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOneElt);
843 NegOne = getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0], NegOnes.size());
844 } else
845 NegOne = getConstant(VT.getIntegerVTBitMask(), VT);
846
847 return getNode(ISD::XOR, VT, Val, NegOne);
848}
849
Dan Gohman8181bd12008-07-27 21:46:04 +0000850SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng833501d2008-06-30 07:31:25 +0000851 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands92c43912008-06-06 12:08:01 +0000852 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohmandc458cf2008-02-08 22:59:30 +0000853}
854
Dan Gohman8181bd12008-07-27 21:46:04 +0000855SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000856 return getConstant(*ConstantInt::get(Val), VT, isT);
857}
858
859SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000860 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000861
Evan Cheng833501d2008-06-30 07:31:25 +0000862 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands92c43912008-06-06 12:08:01 +0000863 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohmandc458cf2008-02-08 22:59:30 +0000864 "APInt size does not match type size!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865
866 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
867 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000868 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000869 ID.AddPointer(&Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000871 SDNode *N = NULL;
872 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000873 if (!VT.isVector())
Dan Gohman8181bd12008-07-27 21:46:04 +0000874 return SDValue(N, 0);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000875 if (!N) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000876 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000877 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000878 CSEMap.InsertNode(N, IP);
879 AllNodes.push_back(N);
880 }
881
Dan Gohman8181bd12008-07-27 21:46:04 +0000882 SDValue Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000883 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000884 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000885 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000886 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
887 }
888 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889}
890
Dan Gohman8181bd12008-07-27 21:46:04 +0000891SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner5872a362008-01-17 07:00:52 +0000892 return getConstant(Val, TLI.getPointerTy(), isTarget);
893}
894
895
Dan Gohman8181bd12008-07-27 21:46:04 +0000896SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000897 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
898}
899
900SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands92c43912008-06-06 12:08:01 +0000901 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000902
Duncan Sands92c43912008-06-06 12:08:01 +0000903 MVT EltVT =
904 VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905
906 // Do the map lookup using the actual bit pattern for the floating point
907 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
908 // we don't have issues with SNANs.
909 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
910 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000911 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000912 ID.AddPointer(&V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 void *IP = 0;
914 SDNode *N = NULL;
915 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000916 if (!VT.isVector())
Dan Gohman8181bd12008-07-27 21:46:04 +0000917 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 if (!N) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000919 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000920 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 }
924
Dan Gohman8181bd12008-07-27 21:46:04 +0000925 SDValue Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000926 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000927 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000928 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
930 }
931 return Result;
932}
933
Dan Gohman8181bd12008-07-27 21:46:04 +0000934SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands92c43912008-06-06 12:08:01 +0000935 MVT EltVT =
936 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000937 if (EltVT==MVT::f32)
938 return getConstantFP(APFloat((float)Val), VT, isTarget);
939 else
940 return getConstantFP(APFloat(Val), VT, isTarget);
941}
942
Dan Gohman8181bd12008-07-27 21:46:04 +0000943SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +0000944 MVT VT, int64_t Offset,
Dan Gohman8181bd12008-07-27 21:46:04 +0000945 bool isTargetGA) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 unsigned Opc;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000947
Dan Gohman36322c72008-10-18 02:06:02 +0000948 // Truncate (with sign-extension) the offset value to the pointer size.
Dan Gohman3d5257c2008-10-21 03:38:42 +0000949 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman36322c72008-10-18 02:06:02 +0000950 if (BitWidth < 64)
951 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
952
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000953 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
954 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000955 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000956 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000957 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000958 }
959
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000960 if (GVar && GVar->isThreadLocal())
961 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
962 else
963 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000964
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000965 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000966 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 ID.AddPointer(GV);
968 ID.AddInteger(Offset);
969 void *IP = 0;
970 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
djg08f48f52009-01-25 16:21:38 +0000971 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000972 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +0000973 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 CSEMap.InsertNode(N, IP);
975 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000976 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977}
978
Dan Gohman8181bd12008-07-27 21:46:04 +0000979SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
981 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000982 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 ID.AddInteger(FI);
984 void *IP = 0;
985 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +0000986 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000987 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +0000988 new (N) FrameIndexSDNode(FI, VT, isTarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 CSEMap.InsertNode(N, IP);
990 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000991 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992}
993
Dan Gohman8181bd12008-07-27 21:46:04 +0000994SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
996 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000997 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 ID.AddInteger(JTI);
999 void *IP = 0;
1000 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001001 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001002 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001003 new (N) JumpTableSDNode(JTI, VT, isTarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 CSEMap.InsertNode(N, IP);
1005 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001006 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007}
1008
Dan Gohman8181bd12008-07-27 21:46:04 +00001009SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1010 unsigned Alignment, int Offset,
1011 bool isTarget) {
Dan Gohman04637d12008-09-16 22:05:41 +00001012 if (Alignment == 0)
1013 Alignment =
1014 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1016 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001017 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018 ID.AddInteger(Alignment);
1019 ID.AddInteger(Offset);
1020 ID.AddPointer(C);
1021 void *IP = 0;
1022 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001023 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001024 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001025 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 CSEMap.InsertNode(N, IP);
1027 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001028 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029}
1030
1031
Dan Gohman8181bd12008-07-27 21:46:04 +00001032SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1033 unsigned Alignment, int Offset,
1034 bool isTarget) {
Dan Gohman04637d12008-09-16 22:05:41 +00001035 if (Alignment == 0)
1036 Alignment =
1037 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1039 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001040 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 ID.AddInteger(Alignment);
1042 ID.AddInteger(Offset);
1043 C->AddSelectionDAGCSEId(ID);
1044 void *IP = 0;
1045 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001046 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001047 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001048 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 CSEMap.InsertNode(N, IP);
1050 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001051 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052}
1053
1054
Dan Gohman8181bd12008-07-27 21:46:04 +00001055SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001057 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 ID.AddPointer(MBB);
1059 void *IP = 0;
1060 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001061 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001062 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001063 new (N) BasicBlockSDNode(MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 CSEMap.InsertNode(N, IP);
1065 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001066 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067}
1068
Dan Gohman8181bd12008-07-27 21:46:04 +00001069SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001070 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001071 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001072 ID.AddInteger(Flags.getRawBits());
1073 void *IP = 0;
1074 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001075 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001076 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001077 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001078 CSEMap.InsertNode(N, IP);
1079 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001080 return SDValue(N, 0);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001081}
1082
Dan Gohman8181bd12008-07-27 21:46:04 +00001083SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands92c43912008-06-06 12:08:01 +00001084 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1085 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086
Duncan Sands92c43912008-06-06 12:08:01 +00001087 SDNode *&N = VT.isExtended() ?
1088 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsd7307a92007-10-17 13:49:58 +00001089
Dan Gohman8181bd12008-07-27 21:46:04 +00001090 if (N) return SDValue(N, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001091 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001092 new (N) VTSDNode(VT);
Duncan Sandsd7307a92007-10-17 13:49:58 +00001093 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001094 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095}
1096
Bill Wendlingfef06052008-09-16 21:48:12 +00001097SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1098 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman8181bd12008-07-27 21:46:04 +00001099 if (N) return SDValue(N, 0);
Bill Wendlingfef06052008-09-16 21:48:12 +00001100 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1101 new (N) ExternalSymbolSDNode(false, Sym, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001103 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104}
1105
Bill Wendlingfef06052008-09-16 21:48:12 +00001106SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
1107 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman8181bd12008-07-27 21:46:04 +00001108 if (N) return SDValue(N, 0);
Bill Wendlingfef06052008-09-16 21:48:12 +00001109 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1110 new (N) ExternalSymbolSDNode(true, Sym, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001112 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113}
1114
Dan Gohman8181bd12008-07-27 21:46:04 +00001115SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116 if ((unsigned)Cond >= CondCodeNodes.size())
1117 CondCodeNodes.resize(Cond+1);
Duncan Sands92c43912008-06-06 12:08:01 +00001118
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 if (CondCodeNodes[Cond] == 0) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001120 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001121 new (N) CondCodeSDNode(Cond);
1122 CondCodeNodes[Cond] = N;
1123 AllNodes.push_back(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001125 return SDValue(CondCodeNodes[Cond], 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126}
1127
Mon P Wang73d31542008-11-10 20:54:11 +00001128SDValue SelectionDAG::getConvertRndSat(MVT VT, SDValue Val, SDValue DTy,
1129 SDValue STy, SDValue Rnd, SDValue Sat,
1130 ISD::CvtCode Code) {
Mon P Wangfa252e52008-12-11 03:30:13 +00001131 // If the src and dest types are the same, no conversion is necessary.
1132 if (DTy == STy)
1133 return Val;
1134
Mon P Wang73d31542008-11-10 20:54:11 +00001135 FoldingSetNodeID ID;
1136 void* IP = 0;
1137 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1138 return SDValue(E, 0);
1139 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1140 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1141 new (N) CvtRndSatSDNode(VT, Ops, 5, Code);
1142 CSEMap.InsertNode(N, IP);
1143 AllNodes.push_back(N);
1144 return SDValue(N, 0);
1145}
1146
Dan Gohman8181bd12008-07-27 21:46:04 +00001147SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001149 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 ID.AddInteger(RegNo);
1151 void *IP = 0;
1152 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001153 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001154 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001155 new (N) RegisterSDNode(RegNo, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 CSEMap.InsertNode(N, IP);
1157 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001158 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159}
1160
Dan Gohman8181bd12008-07-27 21:46:04 +00001161SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Devang Patelfcf1c752009-01-13 00:35:13 +00001162 unsigned Line, unsigned Col,
1163 Value *CU) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001164 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001165 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman472d12c2008-06-30 20:59:49 +00001166 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001167 return SDValue(N, 0);
Dan Gohman472d12c2008-06-30 20:59:49 +00001168}
1169
Dan Gohman8181bd12008-07-27 21:46:04 +00001170SDValue SelectionDAG::getLabel(unsigned Opcode,
1171 SDValue Root,
1172 unsigned LabelID) {
Dan Gohmanfa607c92008-07-01 00:05:16 +00001173 FoldingSetNodeID ID;
Dan Gohman8181bd12008-07-27 21:46:04 +00001174 SDValue Ops[] = { Root };
Dan Gohmanfa607c92008-07-01 00:05:16 +00001175 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1176 ID.AddInteger(LabelID);
1177 void *IP = 0;
1178 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001179 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001180 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001181 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001182 CSEMap.InsertNode(N, IP);
1183 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001184 return SDValue(N, 0);
Dan Gohmanfa607c92008-07-01 00:05:16 +00001185}
1186
Dan Gohman8181bd12008-07-27 21:46:04 +00001187SDValue SelectionDAG::getSrcValue(const Value *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188 assert((!V || isa<PointerType>(V->getType())) &&
1189 "SrcValue is not a pointer?");
1190
1191 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001192 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 ID.AddPointer(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001194
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195 void *IP = 0;
1196 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001197 return SDValue(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001198
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001199 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001200 new (N) SrcValueSDNode(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001201 CSEMap.InsertNode(N, IP);
1202 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001203 return SDValue(N, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001204}
1205
Dan Gohman8181bd12008-07-27 21:46:04 +00001206SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Chengcf576fd2008-11-24 07:09:49 +00001207#ifndef NDEBUG
Dan Gohman12a9c082008-02-06 22:27:42 +00001208 const Value *v = MO.getValue();
1209 assert((!v || isa<PointerType>(v->getType())) &&
1210 "SrcValue is not a pointer?");
Evan Chengcf576fd2008-11-24 07:09:49 +00001211#endif
Dan Gohman12a9c082008-02-06 22:27:42 +00001212
1213 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001214 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman98beebe2008-08-20 15:58:01 +00001215 MO.Profile(ID);
Dan Gohman12a9c082008-02-06 22:27:42 +00001216
1217 void *IP = 0;
1218 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001219 return SDValue(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001220
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001221 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001222 new (N) MemOperandSDNode(MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001223 CSEMap.InsertNode(N, IP);
1224 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001225 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001226}
1227
Chris Lattner53f5aee2007-10-15 17:47:20 +00001228/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1229/// specified value type.
Dan Gohman8181bd12008-07-27 21:46:04 +00001230SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner53f5aee2007-10-15 17:47:20 +00001231 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands76099772008-12-04 18:08:40 +00001232 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Duncan Sands92c43912008-06-06 12:08:01 +00001233 const Type *Ty = VT.getTypeForMVT();
Mon P Wang55854cc2008-07-05 20:40:31 +00001234 unsigned StackAlign =
1235 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1236
Chris Lattner53f5aee2007-10-15 17:47:20 +00001237 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1238 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1239}
1240
Duncan Sands871e55f2008-12-09 21:33:20 +00001241/// CreateStackTemporary - Create a stack temporary suitable for holding
1242/// either of the specified value types.
1243SDValue SelectionDAG::CreateStackTemporary(MVT VT1, MVT VT2) {
1244 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1245 VT2.getStoreSizeInBits())/8;
1246 const Type *Ty1 = VT1.getTypeForMVT();
1247 const Type *Ty2 = VT2.getTypeForMVT();
1248 const TargetData *TD = TLI.getTargetData();
1249 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1250 TD->getPrefTypeAlignment(Ty2));
1251
1252 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1253 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1254 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1255}
1256
Dan Gohman8181bd12008-07-27 21:46:04 +00001257SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1258 SDValue N2, ISD::CondCode Cond) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 // These setcc operations always fold.
1260 switch (Cond) {
1261 default: break;
1262 case ISD::SETFALSE:
1263 case ISD::SETFALSE2: return getConstant(0, VT);
1264 case ISD::SETTRUE:
1265 case ISD::SETTRUE2: return getConstant(1, VT);
1266
1267 case ISD::SETOEQ:
1268 case ISD::SETOGT:
1269 case ISD::SETOGE:
1270 case ISD::SETOLT:
1271 case ISD::SETOLE:
1272 case ISD::SETONE:
1273 case ISD::SETO:
1274 case ISD::SETUO:
1275 case ISD::SETUEQ:
1276 case ISD::SETUNE:
Duncan Sands92c43912008-06-06 12:08:01 +00001277 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 break;
1279 }
1280
Gabor Greif1c80d112008-08-28 21:40:38 +00001281 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00001282 const APInt &C2 = N2C->getAPIntValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00001283 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00001284 const APInt &C1 = N1C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001285
1286 switch (Cond) {
1287 default: assert(0 && "Unknown integer setcc!");
1288 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1289 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00001290 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1291 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1292 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1293 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1294 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1295 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1296 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1297 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 }
1299 }
1300 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001301 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1302 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001303 // No compile time operations on this type yet.
1304 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman8181bd12008-07-27 21:46:04 +00001305 return SDValue();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001306
1307 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001309 default: break;
Dale Johannesen76844472007-08-31 17:03:33 +00001310 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1311 return getNode(ISD::UNDEF, VT);
1312 // fall through
1313 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1314 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1315 return getNode(ISD::UNDEF, VT);
1316 // fall through
1317 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001318 R==APFloat::cmpLessThan, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001319 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1320 return getNode(ISD::UNDEF, VT);
1321 // fall through
1322 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1323 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1324 return getNode(ISD::UNDEF, VT);
1325 // fall through
1326 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1327 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1328 return getNode(ISD::UNDEF, VT);
1329 // fall through
1330 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001331 R==APFloat::cmpEqual, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001332 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1333 return getNode(ISD::UNDEF, VT);
1334 // fall through
1335 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001336 R==APFloat::cmpEqual, VT);
1337 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1338 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1339 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1340 R==APFloat::cmpEqual, VT);
1341 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1342 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1343 R==APFloat::cmpLessThan, VT);
1344 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1345 R==APFloat::cmpUnordered, VT);
1346 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1347 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 }
1349 } else {
1350 // Ensure that the constant occurs on the RHS.
1351 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1352 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001353 }
1354
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355 // Could not fold it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001356 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357}
1358
Dan Gohman07961cd2008-02-25 21:11:39 +00001359/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1360/// use this predicate to simplify operations downstream.
Dan Gohman8181bd12008-07-27 21:46:04 +00001361bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001362 unsigned BitWidth = Op.getValueSizeInBits();
1363 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1364}
1365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1367/// this predicate to simplify operations downstream. Mask is known to be zero
1368/// for bits that V cannot have.
Dan Gohman8181bd12008-07-27 21:46:04 +00001369bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001371 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1373 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1374 return (KnownZero & Mask) == Mask;
1375}
1376
1377/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1378/// known to be either zero or one and return them in the KnownZero/KnownOne
1379/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1380/// processing.
Dan Gohman8181bd12008-07-27 21:46:04 +00001381void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00001382 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 unsigned Depth) const {
Dan Gohman229fa052008-02-13 00:35:47 +00001384 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands92c43912008-06-06 12:08:01 +00001385 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohman56eaab32008-02-13 23:13:32 +00001386 "Mask size mismatches value type size!");
1387
Dan Gohman229fa052008-02-13 00:35:47 +00001388 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389 if (Depth == 6 || Mask == 0)
1390 return; // Limit search depth.
1391
Dan Gohman229fa052008-02-13 00:35:47 +00001392 APInt KnownZero2, KnownOne2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393
1394 switch (Op.getOpcode()) {
1395 case ISD::Constant:
1396 // We know all of the bits for a constant!
Dan Gohman229fa052008-02-13 00:35:47 +00001397 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 KnownZero = ~KnownOne & Mask;
1399 return;
1400 case ISD::AND:
1401 // If either the LHS or the RHS are Zero, the result is zero.
1402 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001403 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1404 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1406 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1407
1408 // Output known-1 bits are only known if set in both the LHS & RHS.
1409 KnownOne &= KnownOne2;
1410 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1411 KnownZero |= KnownZero2;
1412 return;
1413 case ISD::OR:
1414 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001415 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1416 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1418 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1419
1420 // Output known-0 bits are only known if clear in both the LHS & RHS.
1421 KnownZero &= KnownZero2;
1422 // Output known-1 are known to be set if set in either the LHS | RHS.
1423 KnownOne |= KnownOne2;
1424 return;
1425 case ISD::XOR: {
1426 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1427 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1428 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1429 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1430
1431 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohman229fa052008-02-13 00:35:47 +00001432 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1434 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1435 KnownZero = KnownZeroOut;
1436 return;
1437 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001438 case ISD::MUL: {
1439 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1440 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1441 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1442 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1443 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1444
1445 // If low bits are zero in either operand, output low known-0 bits.
1446 // Also compute a conserative estimate for high known-0 bits.
1447 // More trickiness is possible, but this is sufficient for the
1448 // interesting case of alignment computation.
1449 KnownOne.clear();
1450 unsigned TrailZ = KnownZero.countTrailingOnes() +
1451 KnownZero2.countTrailingOnes();
1452 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman4c451852008-05-07 00:35:55 +00001453 KnownZero2.countLeadingOnes(),
1454 BitWidth) - BitWidth;
Dan Gohmanbec16052008-04-28 17:02:21 +00001455
1456 TrailZ = std::min(TrailZ, BitWidth);
1457 LeadZ = std::min(LeadZ, BitWidth);
1458 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1459 APInt::getHighBitsSet(BitWidth, LeadZ);
1460 KnownZero &= Mask;
1461 return;
1462 }
1463 case ISD::UDIV: {
1464 // For the purposes of computing leading zeros we can conservatively
1465 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001466 // be less than the denominator.
Dan Gohmanbec16052008-04-28 17:02:21 +00001467 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1468 ComputeMaskedBits(Op.getOperand(0),
1469 AllOnes, KnownZero2, KnownOne2, Depth+1);
1470 unsigned LeadZ = KnownZero2.countLeadingOnes();
1471
1472 KnownOne2.clear();
1473 KnownZero2.clear();
1474 ComputeMaskedBits(Op.getOperand(1),
1475 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001476 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1477 if (RHSUnknownLeadingOnes != BitWidth)
1478 LeadZ = std::min(BitWidth,
1479 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001480
1481 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1482 return;
1483 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001484 case ISD::SELECT:
1485 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1486 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1487 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1488 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1489
1490 // Only known if known in both the LHS and RHS.
1491 KnownOne &= KnownOne2;
1492 KnownZero &= KnownZero2;
1493 return;
1494 case ISD::SELECT_CC:
1495 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1496 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1497 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1498 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1499
1500 // Only known if known in both the LHS and RHS.
1501 KnownOne &= KnownOne2;
1502 KnownZero &= KnownZero2;
1503 return;
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001504 case ISD::SADDO:
1505 case ISD::UADDO:
Bill Wendling7e04be62008-12-09 22:08:41 +00001506 case ISD::SSUBO:
1507 case ISD::USUBO:
1508 case ISD::SMULO:
1509 case ISD::UMULO:
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001510 if (Op.getResNo() != 1)
1511 return;
Duncan Sands8cf4a822008-11-23 15:47:28 +00001512 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513 case ISD::SETCC:
1514 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands8cf4a822008-11-23 15:47:28 +00001515 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohman229fa052008-02-13 00:35:47 +00001516 BitWidth > 1)
1517 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 return;
1519 case ISD::SHL:
1520 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1521 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001522 unsigned ShAmt = SA->getZExtValue();
Dan Gohman4b8d0002008-02-26 18:50:50 +00001523
1524 // If the shift count is an invalid immediate, don't do anything.
1525 if (ShAmt >= BitWidth)
1526 return;
1527
1528 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 KnownZero, KnownOne, Depth+1);
1530 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman4b8d0002008-02-26 18:50:50 +00001531 KnownZero <<= ShAmt;
1532 KnownOne <<= ShAmt;
Dan Gohman229fa052008-02-13 00:35:47 +00001533 // low bits known zero.
Dan Gohman4b8d0002008-02-26 18:50:50 +00001534 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535 }
1536 return;
1537 case ISD::SRL:
1538 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1539 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001540 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541
Dan Gohman4b8d0002008-02-26 18:50:50 +00001542 // If the shift count is an invalid immediate, don't do anything.
1543 if (ShAmt >= BitWidth)
1544 return;
1545
Dan Gohman229fa052008-02-13 00:35:47 +00001546 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 KnownZero, KnownOne, Depth+1);
1548 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001549 KnownZero = KnownZero.lshr(ShAmt);
1550 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551
Dan Gohman4d81a742008-02-13 22:43:25 +00001552 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553 KnownZero |= HighBits; // High bits known zero.
1554 }
1555 return;
1556 case ISD::SRA:
1557 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001558 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559
Dan Gohman4b8d0002008-02-26 18:50:50 +00001560 // If the shift count is an invalid immediate, don't do anything.
1561 if (ShAmt >= BitWidth)
1562 return;
1563
Dan Gohman229fa052008-02-13 00:35:47 +00001564 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001565 // If any of the demanded bits are produced by the sign extension, we also
1566 // demand the input sign bit.
Dan Gohman4d81a742008-02-13 22:43:25 +00001567 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1568 if (HighBits.getBoolValue())
Dan Gohman229fa052008-02-13 00:35:47 +00001569 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570
1571 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1572 Depth+1);
1573 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001574 KnownZero = KnownZero.lshr(ShAmt);
1575 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576
1577 // Handle the sign bits.
Dan Gohman229fa052008-02-13 00:35:47 +00001578 APInt SignBit = APInt::getSignBit(BitWidth);
1579 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580
Dan Gohman91507292008-02-20 16:30:17 +00001581 if (KnownZero.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001582 KnownZero |= HighBits; // New bits are known zero.
Dan Gohman91507292008-02-20 16:30:17 +00001583 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 KnownOne |= HighBits; // New bits are known one.
1585 }
1586 }
1587 return;
1588 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001589 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1590 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591
1592 // Sign extension. Compute the demanded bits in the result that are not
1593 // present in the input.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001594 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595
Dan Gohmand0dfc772008-02-13 22:28:48 +00001596 APInt InSignBit = APInt::getSignBit(EBits);
1597 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001598
1599 // If the sign extended bits are demanded, we know that the sign
1600 // bit is demanded.
Dan Gohman229fa052008-02-13 00:35:47 +00001601 InSignBit.zext(BitWidth);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001602 if (NewBits.getBoolValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 InputDemandedBits |= InSignBit;
1604
1605 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1606 KnownZero, KnownOne, Depth+1);
1607 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1608
1609 // If the sign bit of the input is known set or clear, then we know the
1610 // top bits of the result.
Dan Gohman91507292008-02-20 16:30:17 +00001611 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 KnownZero |= NewBits;
1613 KnownOne &= ~NewBits;
Dan Gohman91507292008-02-20 16:30:17 +00001614 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615 KnownOne |= NewBits;
1616 KnownZero &= ~NewBits;
1617 } else { // Input sign bit unknown
1618 KnownZero &= ~NewBits;
1619 KnownOne &= ~NewBits;
1620 }
1621 return;
1622 }
1623 case ISD::CTTZ:
1624 case ISD::CTLZ:
1625 case ISD::CTPOP: {
Dan Gohman229fa052008-02-13 00:35:47 +00001626 unsigned LowBits = Log2_32(BitWidth)+1;
1627 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman75e24fb2008-06-21 22:02:15 +00001628 KnownOne.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001629 return;
1630 }
1631 case ISD::LOAD: {
Gabor Greif1c80d112008-08-28 21:40:38 +00001632 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands92c43912008-06-06 12:08:01 +00001634 MVT VT = LD->getMemoryVT();
1635 unsigned MemBits = VT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001636 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001637 }
1638 return;
1639 }
1640 case ISD::ZERO_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001641 MVT InVT = Op.getOperand(0).getValueType();
1642 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001643 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1644 APInt InMask = Mask;
1645 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001646 KnownZero.trunc(InBits);
1647 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001648 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001649 KnownZero.zext(BitWidth);
1650 KnownOne.zext(BitWidth);
1651 KnownZero |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001652 return;
1653 }
1654 case ISD::SIGN_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001655 MVT InVT = Op.getOperand(0).getValueType();
1656 unsigned InBits = InVT.getSizeInBits();
Dan Gohman229fa052008-02-13 00:35:47 +00001657 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001658 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1659 APInt InMask = Mask;
1660 InMask.trunc(InBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661
1662 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohmand0dfc772008-02-13 22:28:48 +00001663 // bit is demanded. Temporarily set this bit in the mask for our callee.
1664 if (NewBits.getBoolValue())
1665 InMask |= InSignBit;
Dan Gohman229fa052008-02-13 00:35:47 +00001666
Dan Gohman229fa052008-02-13 00:35:47 +00001667 KnownZero.trunc(InBits);
1668 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001669 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1670
1671 // Note if the sign bit is known to be zero or one.
1672 bool SignBitKnownZero = KnownZero.isNegative();
1673 bool SignBitKnownOne = KnownOne.isNegative();
1674 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1675 "Sign bit can't be known to be both zero and one!");
1676
1677 // If the sign bit wasn't actually demanded by our caller, we don't
1678 // want it set in the KnownZero and KnownOne result values. Reset the
1679 // mask and reapply it to the result values.
1680 InMask = Mask;
1681 InMask.trunc(InBits);
1682 KnownZero &= InMask;
1683 KnownOne &= InMask;
1684
Dan Gohman229fa052008-02-13 00:35:47 +00001685 KnownZero.zext(BitWidth);
1686 KnownOne.zext(BitWidth);
1687
Dan Gohmand0dfc772008-02-13 22:28:48 +00001688 // If the sign bit is known zero or one, the top bits match.
1689 if (SignBitKnownZero)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 KnownZero |= NewBits;
Dan Gohmand0dfc772008-02-13 22:28:48 +00001691 else if (SignBitKnownOne)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001692 KnownOne |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 return;
1694 }
1695 case ISD::ANY_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001696 MVT InVT = Op.getOperand(0).getValueType();
1697 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001698 APInt InMask = Mask;
1699 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001700 KnownZero.trunc(InBits);
1701 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001702 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001703 KnownZero.zext(BitWidth);
1704 KnownOne.zext(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705 return;
1706 }
1707 case ISD::TRUNCATE: {
Duncan Sands92c43912008-06-06 12:08:01 +00001708 MVT InVT = Op.getOperand(0).getValueType();
1709 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001710 APInt InMask = Mask;
1711 InMask.zext(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001712 KnownZero.zext(InBits);
1713 KnownOne.zext(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001714 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001716 KnownZero.trunc(BitWidth);
1717 KnownOne.trunc(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001718 break;
1719 }
1720 case ISD::AssertZext: {
Duncan Sands92c43912008-06-06 12:08:01 +00001721 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1722 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001723 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1724 KnownOne, Depth+1);
1725 KnownZero |= (~InMask) & Mask;
1726 return;
1727 }
Chris Lattner13f06832007-12-22 21:26:52 +00001728 case ISD::FGETSIGN:
1729 // All bits are zero except the low bit.
Dan Gohman229fa052008-02-13 00:35:47 +00001730 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattner13f06832007-12-22 21:26:52 +00001731 return;
1732
Dan Gohmanbec16052008-04-28 17:02:21 +00001733 case ISD::SUB: {
1734 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1735 // We know that the top bits of C-X are clear if X contains less bits
1736 // than C (i.e. no wrap-around can happen). For example, 20-X is
1737 // positive if we can prove that X is >= 0 and < 16.
1738 if (CLHS->getAPIntValue().isNonNegative()) {
1739 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1740 // NLZ can't be BitWidth with no sign bit
1741 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1742 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1743 Depth+1);
1744
1745 // If all of the MaskV bits are known to be zero, then we know the
1746 // output top bits are zero, because we now know that the output is
1747 // from [0-C].
1748 if ((KnownZero2 & MaskV) == MaskV) {
1749 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1750 // Top bits known zero.
1751 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1752 }
1753 }
1754 }
1755 }
1756 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757 case ISD::ADD: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758 // Output known-0 bits are known if clear or set in both the low clear bits
1759 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1760 // low 3 bits clear.
Dan Gohmanbec16052008-04-28 17:02:21 +00001761 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1762 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1763 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1764 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1765
1766 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1767 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1768 KnownZeroOut = std::min(KnownZeroOut,
1769 KnownZero2.countTrailingOnes());
1770
1771 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772 return;
1773 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001774 case ISD::SREM:
1775 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman1b1a3c62008-07-03 00:52:03 +00001776 const APInt &RA = Rem->getAPIntValue();
Dan Gohmanbec16052008-04-28 17:02:21 +00001777 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman5a154a12008-05-06 00:51:48 +00001778 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohmanbec16052008-04-28 17:02:21 +00001779 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1780 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001781
Dan Gohman2275be02008-08-13 23:12:35 +00001782 // If the sign bit of the first operand is zero, the sign bit of
1783 // the result is zero. If the first operand has no one bits below
1784 // the second operand's single 1 bit, its sign will be zero.
Dan Gohmanbec16052008-04-28 17:02:21 +00001785 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1786 KnownZero2 |= ~LowBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001787
Dan Gohmanbec16052008-04-28 17:02:21 +00001788 KnownZero |= KnownZero2 & Mask;
Dan Gohmanbec16052008-04-28 17:02:21 +00001789
1790 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791 }
1792 }
1793 return;
Dan Gohmanbec16052008-04-28 17:02:21 +00001794 case ISD::UREM: {
1795 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman1b1a3c62008-07-03 00:52:03 +00001796 const APInt &RA = Rem->getAPIntValue();
Dan Gohman5a154a12008-05-06 00:51:48 +00001797 if (RA.isPowerOf2()) {
1798 APInt LowBits = (RA - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001799 APInt Mask2 = LowBits & Mask;
1800 KnownZero |= ~LowBits & Mask;
1801 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1802 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1803 break;
1804 }
1805 }
1806
1807 // Since the result is less than or equal to either operand, any leading
1808 // zero bits in either operand must also exist in the result.
1809 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1810 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1811 Depth+1);
1812 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1813 Depth+1);
1814
1815 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1816 KnownZero2.countLeadingOnes());
1817 KnownOne.clear();
1818 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1819 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001820 }
1821 default:
1822 // Allow the target to implement this method for its nodes.
1823 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1824 case ISD::INTRINSIC_WO_CHAIN:
1825 case ISD::INTRINSIC_W_CHAIN:
1826 case ISD::INTRINSIC_VOID:
1827 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1828 }
1829 return;
1830 }
1831}
1832
1833/// ComputeNumSignBits - Return the number of times the sign bit of the
1834/// register is replicated into the other bits. We know that at least 1 bit
1835/// is always equal to the sign bit (itself), but other cases can give us
1836/// information. For example, immediately after an "SRA X, 2", we know that
1837/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman8181bd12008-07-27 21:46:04 +00001838unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands92c43912008-06-06 12:08:01 +00001839 MVT VT = Op.getValueType();
1840 assert(VT.isInteger() && "Invalid VT!");
1841 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001842 unsigned Tmp, Tmp2;
Dan Gohman4afc4252008-05-23 02:28:01 +00001843 unsigned FirstAnswer = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001844
1845 if (Depth == 6)
1846 return 1; // Limit search depth.
1847
1848 switch (Op.getOpcode()) {
1849 default: break;
1850 case ISD::AssertSext:
Duncan Sands92c43912008-06-06 12:08:01 +00001851 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001852 return VTBits-Tmp+1;
1853 case ISD::AssertZext:
Duncan Sands92c43912008-06-06 12:08:01 +00001854 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001855 return VTBits-Tmp;
1856
1857 case ISD::Constant: {
Dan Gohman463db8c2008-03-03 23:35:36 +00001858 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1859 // If negative, return # leading ones.
1860 if (Val.isNegative())
1861 return Val.countLeadingOnes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001862
Dan Gohman463db8c2008-03-03 23:35:36 +00001863 // Return # leading zeros.
1864 return Val.countLeadingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001865 }
1866
1867 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00001868 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1870
1871 case ISD::SIGN_EXTEND_INREG:
1872 // Max of the input and what this extends.
Duncan Sands92c43912008-06-06 12:08:01 +00001873 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001874 Tmp = VTBits-Tmp+1;
1875
1876 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1877 return std::max(Tmp, Tmp2);
1878
1879 case ISD::SRA:
1880 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1881 // SRA X, C -> adds C sign bits.
1882 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001883 Tmp += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001884 if (Tmp > VTBits) Tmp = VTBits;
1885 }
1886 return Tmp;
1887 case ISD::SHL:
1888 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1889 // shl destroys sign bits.
1890 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001891 if (C->getZExtValue() >= VTBits || // Bad shift.
1892 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1893 return Tmp - C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894 }
1895 break;
1896 case ISD::AND:
1897 case ISD::OR:
1898 case ISD::XOR: // NOT is handled here.
Dan Gohman4afc4252008-05-23 02:28:01 +00001899 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman4afc4252008-05-23 02:28:01 +00001901 if (Tmp != 1) {
1902 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1903 FirstAnswer = std::min(Tmp, Tmp2);
1904 // We computed what we know about the sign bits as our first
1905 // answer. Now proceed to the generic code that uses
1906 // ComputeMaskedBits, and pick whichever answer is better.
1907 }
1908 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001909
1910 case ISD::SELECT:
Dan Gohman6ffcf262008-05-20 20:59:51 +00001911 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 if (Tmp == 1) return 1; // Early out.
Dan Gohman6ffcf262008-05-20 20:59:51 +00001913 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001914 return std::min(Tmp, Tmp2);
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001915
1916 case ISD::SADDO:
1917 case ISD::UADDO:
Bill Wendling7e04be62008-12-09 22:08:41 +00001918 case ISD::SSUBO:
1919 case ISD::USUBO:
1920 case ISD::SMULO:
1921 case ISD::UMULO:
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001922 if (Op.getResNo() != 1)
1923 break;
Duncan Sands8cf4a822008-11-23 15:47:28 +00001924 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 case ISD::SETCC:
1926 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands8cf4a822008-11-23 15:47:28 +00001927 if (TLI.getBooleanContents() ==
1928 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929 return VTBits;
1930 break;
1931 case ISD::ROTL:
1932 case ISD::ROTR:
1933 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001934 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001935
1936 // Handle rotate right by N like a rotate left by 32-N.
1937 if (Op.getOpcode() == ISD::ROTR)
1938 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1939
1940 // If we aren't rotating out all of the known-in sign bits, return the
1941 // number that are left. This handles rotl(sext(x), 1) for example.
1942 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1943 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1944 }
1945 break;
1946 case ISD::ADD:
1947 // Add can have at most one carry bit. Thus we know that the output
1948 // is, at worst, one more bit than the inputs.
1949 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1950 if (Tmp == 1) return 1; // Early out.
1951
1952 // Special case decrementing a value (ADD X, -1):
1953 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1954 if (CRHS->isAllOnesValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00001955 APInt KnownZero, KnownOne;
1956 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001957 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1958
1959 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1960 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00001961 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001962 return VTBits;
1963
1964 // If we are subtracting one from a positive number, there is no carry
1965 // out of the result.
Dan Gohman63f4e462008-02-27 01:23:58 +00001966 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001967 return Tmp;
1968 }
1969
1970 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1971 if (Tmp2 == 1) return 1;
1972 return std::min(Tmp, Tmp2)-1;
1973 break;
1974
1975 case ISD::SUB:
1976 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1977 if (Tmp2 == 1) return 1;
1978
1979 // Handle NEG.
1980 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00001981 if (CLHS->isNullValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00001982 APInt KnownZero, KnownOne;
1983 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001984 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1985 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1986 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00001987 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988 return VTBits;
1989
1990 // If the input is known to be positive (the sign bit is known clear),
1991 // the output of the NEG has the same number of sign bits as the input.
Dan Gohman63f4e462008-02-27 01:23:58 +00001992 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001993 return Tmp2;
1994
1995 // Otherwise, we treat this like a SUB.
1996 }
1997
1998 // Sub can have at most one carry bit. Thus we know that the output
1999 // is, at worst, one more bit than the inputs.
2000 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2001 if (Tmp == 1) return 1; // Early out.
2002 return std::min(Tmp, Tmp2)-1;
2003 break;
2004 case ISD::TRUNCATE:
2005 // FIXME: it's tricky to do anything useful for this, but it is an important
2006 // case for targets like X86.
2007 break;
2008 }
2009
2010 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2011 if (Op.getOpcode() == ISD::LOAD) {
2012 LoadSDNode *LD = cast<LoadSDNode>(Op);
2013 unsigned ExtType = LD->getExtensionType();
2014 switch (ExtType) {
2015 default: break;
2016 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands92c43912008-06-06 12:08:01 +00002017 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002018 return VTBits-Tmp+1;
2019 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands92c43912008-06-06 12:08:01 +00002020 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 return VTBits-Tmp;
2022 }
2023 }
2024
2025 // Allow the target to implement this method for its nodes.
2026 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2027 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2028 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2029 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2030 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman4afc4252008-05-23 02:28:01 +00002031 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002032 }
2033
2034 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2035 // use this information.
Dan Gohman63f4e462008-02-27 01:23:58 +00002036 APInt KnownZero, KnownOne;
2037 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002038 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
2039
Dan Gohman63f4e462008-02-27 01:23:58 +00002040 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002041 Mask = KnownZero;
Dan Gohman63f4e462008-02-27 01:23:58 +00002042 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043 Mask = KnownOne;
2044 } else {
2045 // Nothing known.
Dan Gohman4afc4252008-05-23 02:28:01 +00002046 return FirstAnswer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002047 }
2048
2049 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2050 // the number of identical bits in the top of the input value.
Dan Gohman63f4e462008-02-27 01:23:58 +00002051 Mask = ~Mask;
2052 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002053 // Return # leading zeros. We use 'min' here in case Val was zero before
2054 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman4afc4252008-05-23 02:28:01 +00002055 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002056}
2057
2058
Dan Gohman8181bd12008-07-27 21:46:04 +00002059bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Cheng2e28d622008-02-02 04:07:54 +00002060 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2061 if (!GA) return false;
Dan Gohman36322c72008-10-18 02:06:02 +00002062 if (GA->getOffset() != 0) return false;
Evan Cheng2e28d622008-02-02 04:07:54 +00002063 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2064 if (!GV) return false;
Devang Patelfb3a4882009-01-13 22:54:57 +00002065 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patel42f6bed2009-01-13 23:54:55 +00002066 return MMI && MMI->hasDebugInfo();
Evan Cheng2e28d622008-02-02 04:07:54 +00002067}
2068
2069
Evan Cheng411fc172008-05-13 08:35:03 +00002070/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2071/// element of the result of the vector shuffle.
Dan Gohman8181bd12008-07-27 21:46:04 +00002072SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands92c43912008-06-06 12:08:01 +00002073 MVT VT = N->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002074 SDValue PermMask = N->getOperand(2);
2075 SDValue Idx = PermMask.getOperand(i);
Evan Cheng57db53b2008-06-25 20:52:59 +00002076 if (Idx.getOpcode() == ISD::UNDEF)
2077 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002078 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002079 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002080 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Cheng57db53b2008-06-25 20:52:59 +00002081 Index %= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00002082
2083 if (V.getOpcode() == ISD::BIT_CONVERT) {
2084 V = V.getOperand(0);
Mon P Wang9901e732008-12-09 05:46:39 +00002085 MVT VVT = V.getValueType();
2086 if (!VVT.isVector() || VVT.getVectorNumElements() != NumElems)
Dan Gohman8181bd12008-07-27 21:46:04 +00002087 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002088 }
Evan Chengdea99362008-05-29 08:22:04 +00002089 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00002090 return (Index == 0) ? V.getOperand(0)
Duncan Sands92c43912008-06-06 12:08:01 +00002091 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengdea99362008-05-29 08:22:04 +00002092 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00002093 return V.getOperand(Index);
2094 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greif1c80d112008-08-28 21:40:38 +00002095 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman8181bd12008-07-27 21:46:04 +00002096 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002097}
2098
2099
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002100/// getNode - Gets or creates the specified node.
2101///
Dan Gohman8181bd12008-07-27 21:46:04 +00002102SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00002104 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 void *IP = 0;
2106 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002107 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002108 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00002109 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002110 CSEMap.InsertNode(N, IP);
2111
2112 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002113#ifndef NDEBUG
2114 VerifyNode(N);
2115#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002116 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117}
2118
Dan Gohman8181bd12008-07-27 21:46:04 +00002119SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002120 // Constant fold unary operations with an integer constant operand.
Gabor Greif1c80d112008-08-28 21:40:38 +00002121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00002122 const APInt &Val = C->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002123 unsigned BitWidth = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002124 switch (Opcode) {
2125 default: break;
Evan Chengeadaf442008-03-06 17:42:34 +00002126 case ISD::SIGN_EXTEND:
2127 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002128 case ISD::ANY_EXTEND:
Dan Gohman161652c2008-02-29 01:47:35 +00002129 case ISD::ZERO_EXTEND:
Evan Chengeadaf442008-03-06 17:42:34 +00002130 case ISD::TRUNCATE:
2131 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00002132 case ISD::UINT_TO_FP:
2133 case ISD::SINT_TO_FP: {
2134 const uint64_t zero[] = {0, 0};
Dale Johannesenb89072e2007-10-16 23:38:29 +00002135 // No compile time operations on this type.
2136 if (VT==MVT::ppcf128)
2137 break;
Dan Gohman161652c2008-02-29 01:47:35 +00002138 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2139 (void)apf.convertFromAPInt(Val,
2140 Opcode==ISD::SINT_TO_FP,
2141 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00002142 return getConstantFP(apf, VT);
2143 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002144 case ISD::BIT_CONVERT:
2145 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman161652c2008-02-29 01:47:35 +00002146 return getConstantFP(Val.bitsToFloat(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002147 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman161652c2008-02-29 01:47:35 +00002148 return getConstantFP(Val.bitsToDouble(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002149 break;
2150 case ISD::BSWAP:
Dan Gohman161652c2008-02-29 01:47:35 +00002151 return getConstant(Val.byteSwap(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002152 case ISD::CTPOP:
Dan Gohman161652c2008-02-29 01:47:35 +00002153 return getConstant(Val.countPopulation(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 case ISD::CTLZ:
Dan Gohman161652c2008-02-29 01:47:35 +00002155 return getConstant(Val.countLeadingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002156 case ISD::CTTZ:
Dan Gohman161652c2008-02-29 01:47:35 +00002157 return getConstant(Val.countTrailingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002158 }
2159 }
2160
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002161 // Constant fold unary operations with a floating point constant operand.
Gabor Greif1c80d112008-08-28 21:40:38 +00002162 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002163 APFloat V = C->getValueAPF(); // make copy
Chris Lattner5872a362008-01-17 07:00:52 +00002164 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00002165 switch (Opcode) {
2166 case ISD::FNEG:
2167 V.changeSign();
2168 return getConstantFP(V, VT);
2169 case ISD::FABS:
2170 V.clearSign();
2171 return getConstantFP(V, VT);
2172 case ISD::FP_ROUND:
Dale Johannesen6e547b42008-10-09 23:00:39 +00002173 case ISD::FP_EXTEND: {
2174 bool ignored;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002175 // This can return overflow, underflow, or inexact; we don't care.
2176 // FIXME need to be more flexible about rounding mode.
Chris Lattnerd037d482008-03-05 06:48:13 +00002177 (void)V.convert(*MVTToAPFloatSemantics(VT),
Dale Johannesen6e547b42008-10-09 23:00:39 +00002178 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002179 return getConstantFP(V, VT);
Dale Johannesen6e547b42008-10-09 23:00:39 +00002180 }
Dale Johannesenb89072e2007-10-16 23:38:29 +00002181 case ISD::FP_TO_SINT:
2182 case ISD::FP_TO_UINT: {
2183 integerPart x;
Dale Johannesen6e547b42008-10-09 23:00:39 +00002184 bool ignored;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002185 assert(integerPartWidth >= 64);
2186 // FIXME need to be more flexible about rounding mode.
2187 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2188 Opcode==ISD::FP_TO_SINT,
Dale Johannesen6e547b42008-10-09 23:00:39 +00002189 APFloat::rmTowardZero, &ignored);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002190 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2191 break;
2192 return getConstant(x, VT);
2193 }
2194 case ISD::BIT_CONVERT:
2195 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen6e547b42008-10-09 23:00:39 +00002196 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002197 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen6e547b42008-10-09 23:00:39 +00002198 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002199 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002200 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002201 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002202 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002203
Gabor Greif1c80d112008-08-28 21:40:38 +00002204 unsigned OpOpcode = Operand.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002205 switch (Opcode) {
2206 case ISD::TokenFactor:
Duncan Sands42d7bb82008-12-01 11:41:29 +00002207 case ISD::MERGE_VALUES:
Dan Gohman29c3cef2008-08-14 20:04:46 +00002208 case ISD::CONCAT_VECTORS:
Duncan Sands42d7bb82008-12-01 11:41:29 +00002209 return Operand; // Factor, merge or concat of one node? No need.
Chris Lattner5872a362008-01-17 07:00:52 +00002210 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002211 case ISD::FP_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002212 assert(VT.isFloatingPoint() &&
2213 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00002214 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnere6465dd2008-03-11 06:21:08 +00002215 if (Operand.getOpcode() == ISD::UNDEF)
2216 return getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002217 break;
Chris Lattnere6465dd2008-03-11 06:21:08 +00002218 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002219 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002220 "Invalid SIGN_EXTEND!");
2221 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002222 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002223 && "Invalid sext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002224 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greif1c80d112008-08-28 21:40:38 +00002225 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002226 break;
2227 case ISD::ZERO_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002228 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 "Invalid ZERO_EXTEND!");
2230 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002231 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002232 && "Invalid zext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002233 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greif1c80d112008-08-28 21:40:38 +00002234 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002235 break;
2236 case ISD::ANY_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002237 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002238 "Invalid ANY_EXTEND!");
2239 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002240 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002241 && "Invalid anyext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002242 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2243 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greif1c80d112008-08-28 21:40:38 +00002244 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002245 break;
2246 case ISD::TRUNCATE:
Duncan Sands92c43912008-06-06 12:08:01 +00002247 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002248 "Invalid TRUNCATE!");
2249 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsec142ee2008-06-08 20:54:56 +00002250 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002251 && "Invalid truncate node, src < dst!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002252 if (OpOpcode == ISD::TRUNCATE)
Gabor Greif1c80d112008-08-28 21:40:38 +00002253 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2255 OpOpcode == ISD::ANY_EXTEND) {
2256 // If the source is smaller than the dest, we still need an extend.
Gabor Greif1c80d112008-08-28 21:40:38 +00002257 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2258 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2259 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2260 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002261 else
Gabor Greif1c80d112008-08-28 21:40:38 +00002262 return Operand.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002263 }
2264 break;
2265 case ISD::BIT_CONVERT:
2266 // Basic sanity checking.
Duncan Sands92c43912008-06-06 12:08:01 +00002267 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002268 && "Cannot BIT_CONVERT between types of different sizes!");
2269 if (VT == Operand.getValueType()) return Operand; // noop conversion.
2270 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2271 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
2272 if (OpOpcode == ISD::UNDEF)
2273 return getNode(ISD::UNDEF, VT);
2274 break;
2275 case ISD::SCALAR_TO_VECTOR:
Duncan Sands92c43912008-06-06 12:08:01 +00002276 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2277 VT.getVectorElementType() == Operand.getValueType() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattner9f0705c2008-03-08 23:43:36 +00002279 if (OpOpcode == ISD::UNDEF)
2280 return getNode(ISD::UNDEF, VT);
2281 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2282 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2283 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2284 Operand.getConstantOperandVal(1) == 0 &&
2285 Operand.getOperand(0).getValueType() == VT)
2286 return Operand.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002287 break;
2288 case ISD::FNEG:
2289 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
Gabor Greif1c80d112008-08-28 21:40:38 +00002290 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2291 Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greif1c80d112008-08-28 21:40:38 +00002293 return Operand.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002294 break;
2295 case ISD::FABS:
2296 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greif1c80d112008-08-28 21:40:38 +00002297 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002298 break;
2299 }
2300
2301 SDNode *N;
2302 SDVTList VTs = getVTList(VT);
2303 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
2304 FoldingSetNodeID ID;
Dan Gohman8181bd12008-07-27 21:46:04 +00002305 SDValue Ops[1] = { Operand };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
2307 void *IP = 0;
2308 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002309 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002310 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00002311 new (N) UnarySDNode(Opcode, VTs, Operand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002312 CSEMap.InsertNode(N, IP);
2313 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002314 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00002315 new (N) UnarySDNode(Opcode, VTs, Operand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002316 }
Duncan Sandsd3ace282008-07-21 10:20:31 +00002317
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002318 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002319#ifndef NDEBUG
2320 VerifyNode(N);
2321#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002322 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002323}
2324
Bill Wendling16bc18c2008-09-24 10:16:24 +00002325SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
2326 MVT VT,
2327 ConstantSDNode *Cst1,
2328 ConstantSDNode *Cst2) {
2329 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2330
2331 switch (Opcode) {
2332 case ISD::ADD: return getConstant(C1 + C2, VT);
2333 case ISD::SUB: return getConstant(C1 - C2, VT);
2334 case ISD::MUL: return getConstant(C1 * C2, VT);
2335 case ISD::UDIV:
2336 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2337 break;
2338 case ISD::UREM:
2339 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2340 break;
2341 case ISD::SDIV:
2342 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2343 break;
2344 case ISD::SREM:
2345 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2346 break;
2347 case ISD::AND: return getConstant(C1 & C2, VT);
2348 case ISD::OR: return getConstant(C1 | C2, VT);
2349 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2350 case ISD::SHL: return getConstant(C1 << C2, VT);
2351 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2352 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2353 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2354 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2355 default: break;
2356 }
2357
2358 return SDValue();
2359}
2360
Dan Gohman8181bd12008-07-27 21:46:04 +00002361SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2362 SDValue N1, SDValue N2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002363 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2364 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002365 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002366 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367 case ISD::TokenFactor:
2368 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2369 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002370 // Fold trivial token factors.
2371 if (N1.getOpcode() == ISD::EntryToken) return N2;
2372 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanc0e18d62008-10-01 15:11:19 +00002373 if (N1 == N2) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374 break;
Dan Gohman29c3cef2008-08-14 20:04:46 +00002375 case ISD::CONCAT_VECTORS:
2376 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2377 // one big BUILD_VECTOR.
2378 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2379 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002380 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2381 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman29c3cef2008-08-14 20:04:46 +00002382 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2383 }
2384 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 case ISD::AND:
Duncan Sands92c43912008-06-06 12:08:01 +00002386 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002387 N1.getValueType() == VT && "Binary operator types must match!");
2388 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2389 // worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002390 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002391 return N2;
Chris Lattner8aa8a5e2008-01-26 01:05:42 +00002392 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2393 return N1;
Chris Lattnercc126e32008-01-22 19:09:33 +00002394 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002395 case ISD::OR:
2396 case ISD::XOR:
Dan Gohman3f76aed2008-06-02 22:27:05 +00002397 case ISD::ADD:
2398 case ISD::SUB:
Duncan Sands92c43912008-06-06 12:08:01 +00002399 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002400 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman3f76aed2008-06-02 22:27:05 +00002401 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2402 // it's worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002403 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002404 return N1;
2405 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002406 case ISD::UDIV:
2407 case ISD::UREM:
2408 case ISD::MULHU:
2409 case ISD::MULHS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 case ISD::MUL:
2411 case ISD::SDIV:
2412 case ISD::SREM:
Dan Gohman3d015562009-01-22 21:58:43 +00002413 assert(VT.isInteger() && "This operator does not apply to FP types!");
2414 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 case ISD::FADD:
2416 case ISD::FSUB:
2417 case ISD::FMUL:
2418 case ISD::FDIV:
2419 case ISD::FREM:
Dan Gohman46ef3eb2009-01-23 19:10:37 +00002420 if (UnsafeFPMath) {
2421 if (Opcode == ISD::FADD) {
2422 // 0+x --> x
2423 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2424 if (CFP->getValueAPF().isZero())
2425 return N2;
2426 // x+0 --> x
2427 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2428 if (CFP->getValueAPF().isZero())
2429 return N1;
2430 } else if (Opcode == ISD::FSUB) {
2431 // x-0 --> x
2432 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2433 if (CFP->getValueAPF().isZero())
2434 return N1;
2435 }
Dan Gohman3d015562009-01-22 21:58:43 +00002436 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 assert(N1.getValueType() == N2.getValueType() &&
2438 N1.getValueType() == VT && "Binary operator types must match!");
2439 break;
2440 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2441 assert(N1.getValueType() == VT &&
Duncan Sands92c43912008-06-06 12:08:01 +00002442 N1.getValueType().isFloatingPoint() &&
2443 N2.getValueType().isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 "Invalid FCOPYSIGN!");
2445 break;
2446 case ISD::SHL:
2447 case ISD::SRA:
2448 case ISD::SRL:
2449 case ISD::ROTL:
2450 case ISD::ROTR:
2451 assert(VT == N1.getValueType() &&
2452 "Shift operators return type must be the same as their first arg");
Duncan Sands92c43912008-06-06 12:08:01 +00002453 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattnera113d3e2008-07-02 17:01:57 +00002454 "Shifts only work on integers");
Mon P Wang9901e732008-12-09 05:46:39 +00002455 assert((N2.getValueType() == TLI.getShiftAmountTy() ||
2456 (N2.getValueType().isVector() && N2.getValueType().isInteger())) &&
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00002457 "Wrong type for shift amount");
Chris Lattnera113d3e2008-07-02 17:01:57 +00002458
2459 // Always fold shifts of i1 values so the code generator doesn't need to
2460 // handle them. Since we know the size of the shift has to be less than the
2461 // size of the value, the shift/rotate count is guaranteed to be zero.
2462 if (VT == MVT::i1)
2463 return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002464 break;
2465 case ISD::FP_ROUND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00002466 MVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002467 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands92c43912008-06-06 12:08:01 +00002468 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002470 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002471 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 break;
2473 }
Chris Lattner5872a362008-01-17 07:00:52 +00002474 case ISD::FP_ROUND:
Duncan Sands92c43912008-06-06 12:08:01 +00002475 assert(VT.isFloatingPoint() &&
2476 N1.getValueType().isFloatingPoint() &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00002477 VT.bitsLE(N1.getValueType()) &&
Chris Lattner5872a362008-01-17 07:00:52 +00002478 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002479 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00002480 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00002482 case ISD::AssertZext: {
Duncan Sands92c43912008-06-06 12:08:01 +00002483 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattnercc126e32008-01-22 19:09:33 +00002484 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002485 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002486 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002487 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands539510b2008-02-10 10:08:52 +00002488 if (VT == EVT) return N1; // noop assertion.
Chris Lattnercc126e32008-01-22 19:09:33 +00002489 break;
2490 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002491 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00002492 MVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002493 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002494 assert(VT.isInteger() && EVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002496 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002497 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002498
Chris Lattnercc126e32008-01-22 19:09:33 +00002499 if (N1C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002500 APInt Val = N1C->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002501 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman161652c2008-02-29 01:47:35 +00002502 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng7faa1d72008-03-06 08:20:51 +00002503 Val = Val.ashr(Val.getBitWidth()-FromBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 return getConstant(Val, VT);
2505 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002506 break;
2507 }
2508 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattner9f0705c2008-03-08 23:43:36 +00002509 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2510 if (N1.getOpcode() == ISD::UNDEF)
2511 return getNode(ISD::UNDEF, VT);
2512
Chris Lattnercc126e32008-01-22 19:09:33 +00002513 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2514 // expanding copies of large vectors from registers.
Dan Gohman101124c2008-08-13 21:51:37 +00002515 if (N2C &&
2516 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002517 N1.getNumOperands() > 0) {
2518 unsigned Factor =
Duncan Sands92c43912008-06-06 12:08:01 +00002519 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattnercc126e32008-01-22 19:09:33 +00002520 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002521 N1.getOperand(N2C->getZExtValue() / Factor),
2522 getConstant(N2C->getZExtValue() % Factor,
2523 N2.getValueType()));
Chris Lattnercc126e32008-01-22 19:09:33 +00002524 }
2525
2526 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2527 // expanding large vector constants.
Dan Gohman101124c2008-08-13 21:51:37 +00002528 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002529 return N1.getOperand(N2C->getZExtValue());
Chris Lattner9f0705c2008-03-08 23:43:36 +00002530
Chris Lattnercc126e32008-01-22 19:09:33 +00002531 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2532 // operations are lowered to scalars.
Dan Gohman101124c2008-08-13 21:51:37 +00002533 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2534 if (N1.getOperand(2) == N2)
2535 return N1.getOperand(1);
2536 else
2537 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2538 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002539 break;
2540 case ISD::EXTRACT_ELEMENT:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002541 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands1568af82008-06-25 20:24:48 +00002542 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2543 (N1.getValueType().isInteger() == VT.isInteger()) &&
2544 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sandsc4d85172008-03-12 20:30:08 +00002545
Chris Lattnercc126e32008-01-22 19:09:33 +00002546 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2547 // 64-bit integers into 32-bit parts. Instead of building the extract of
2548 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2549 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002550 return N1.getOperand(N2C->getZExtValue());
Duncan Sandsc4d85172008-03-12 20:30:08 +00002551
Chris Lattnercc126e32008-01-22 19:09:33 +00002552 // EXTRACT_ELEMENT of a constant int is also very common.
2553 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002554 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002555 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman452b4ce2008-03-24 16:38:05 +00002556 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2557 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattnercc126e32008-01-22 19:09:33 +00002558 }
2559 break;
Duncan Sandsbd13a812008-02-20 17:38:09 +00002560 case ISD::EXTRACT_SUBVECTOR:
2561 if (N1.getValueType() == VT) // Trivial extraction.
2562 return N1;
2563 break;
Chris Lattnercc126e32008-01-22 19:09:33 +00002564 }
2565
2566 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002567 if (N2C) {
Bill Wendling16bc18c2008-09-24 10:16:24 +00002568 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2569 if (SV.getNode()) return SV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 } else { // Cannonicalize constant to RHS if commutative
2571 if (isCommutativeBinOp(Opcode)) {
2572 std::swap(N1C, N2C);
2573 std::swap(N1, N2);
2574 }
2575 }
2576 }
2577
Chris Lattnercc126e32008-01-22 19:09:33 +00002578 // Constant fold FP operations.
Gabor Greif1c80d112008-08-28 21:40:38 +00002579 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2580 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002581 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002582 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2583 // Cannonicalize constant to RHS if commutative
2584 std::swap(N1CFP, N2CFP);
2585 std::swap(N1, N2);
2586 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002587 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2588 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002589 switch (Opcode) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002590 case ISD::FADD:
2591 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002592 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002593 return getConstantFP(V1, VT);
2594 break;
2595 case ISD::FSUB:
2596 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2597 if (s!=APFloat::opInvalidOp)
2598 return getConstantFP(V1, VT);
2599 break;
2600 case ISD::FMUL:
2601 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2602 if (s!=APFloat::opInvalidOp)
2603 return getConstantFP(V1, VT);
2604 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002606 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2607 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2608 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 break;
2610 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002611 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2612 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2613 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002614 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002615 case ISD::FCOPYSIGN:
2616 V1.copySign(V2);
2617 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618 default: break;
2619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 }
2621 }
2622
2623 // Canonicalize an UNDEF to the RHS, even over a constant.
2624 if (N1.getOpcode() == ISD::UNDEF) {
2625 if (isCommutativeBinOp(Opcode)) {
2626 std::swap(N1, N2);
2627 } else {
2628 switch (Opcode) {
2629 case ISD::FP_ROUND_INREG:
2630 case ISD::SIGN_EXTEND_INREG:
2631 case ISD::SUB:
2632 case ISD::FSUB:
2633 case ISD::FDIV:
2634 case ISD::FREM:
2635 case ISD::SRA:
2636 return N1; // fold op(undef, arg2) -> undef
2637 case ISD::UDIV:
2638 case ISD::SDIV:
2639 case ISD::UREM:
2640 case ISD::SREM:
2641 case ISD::SRL:
2642 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002643 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2645 // For vectors, we can't easily build an all zero vector, just return
2646 // the LHS.
2647 return N2;
2648 }
2649 }
2650 }
2651
2652 // Fold a bunch of operators when the RHS is undef.
2653 if (N2.getOpcode() == ISD::UNDEF) {
2654 switch (Opcode) {
Evan Cheng5d00cb42008-03-25 20:08:07 +00002655 case ISD::XOR:
2656 if (N1.getOpcode() == ISD::UNDEF)
2657 // Handle undef ^ undef -> 0 special case. This is a common
2658 // idiom (misuse).
2659 return getConstant(0, VT);
2660 // fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002661 case ISD::ADD:
2662 case ISD::ADDC:
2663 case ISD::ADDE:
2664 case ISD::SUB:
2665 case ISD::FADD:
2666 case ISD::FSUB:
2667 case ISD::FMUL:
2668 case ISD::FDIV:
2669 case ISD::FREM:
2670 case ISD::UDIV:
2671 case ISD::SDIV:
2672 case ISD::UREM:
2673 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674 return N2; // fold op(arg1, undef) -> undef
2675 case ISD::MUL:
2676 case ISD::AND:
2677 case ISD::SRL:
2678 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002679 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2681 // For vectors, we can't easily build an all zero vector, just return
2682 // the LHS.
2683 return N1;
2684 case ISD::OR:
Duncan Sands92c43912008-06-06 12:08:01 +00002685 if (!VT.isVector())
2686 return getConstant(VT.getIntegerVTBitMask(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 // For vectors, we can't easily build an all one vector, just return
2688 // the LHS.
2689 return N1;
2690 case ISD::SRA:
2691 return N1;
2692 }
2693 }
2694
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002695 // Memoize this node if possible.
2696 SDNode *N;
2697 SDVTList VTs = getVTList(VT);
2698 if (VT != MVT::Flag) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002699 SDValue Ops[] = { N1, N2 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700 FoldingSetNodeID ID;
2701 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2702 void *IP = 0;
2703 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002704 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002705 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00002706 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002707 CSEMap.InsertNode(N, IP);
2708 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002709 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00002710 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002711 }
2712
2713 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002714#ifndef NDEBUG
2715 VerifyNode(N);
2716#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002717 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718}
2719
Dan Gohman8181bd12008-07-27 21:46:04 +00002720SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2721 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 // Perform various simplifications.
Gabor Greif1c80d112008-08-28 21:40:38 +00002723 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2724 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725 switch (Opcode) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00002726 case ISD::CONCAT_VECTORS:
2727 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2728 // one big BUILD_VECTOR.
2729 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2730 N2.getOpcode() == ISD::BUILD_VECTOR &&
2731 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002732 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2733 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2734 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman29c3cef2008-08-14 20:04:46 +00002735 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2736 }
2737 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002738 case ISD::SETCC: {
2739 // Use FoldSetCC to simplify SETCC's.
Dan Gohman8181bd12008-07-27 21:46:04 +00002740 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greif1c80d112008-08-28 21:40:38 +00002741 if (Simp.getNode()) return Simp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002742 break;
2743 }
2744 case ISD::SELECT:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002745 if (N1C) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002746 if (N1C->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747 return N2; // select true, X, Y -> X
2748 else
2749 return N3; // select false, X, Y -> Y
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002750 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002751
2752 if (N2 == N3) return N2; // select C, X, X -> X
2753 break;
2754 case ISD::BRCOND:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002755 if (N2C) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002756 if (N2C->getZExtValue()) // Unconditional branch
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002757 return getNode(ISD::BR, MVT::Other, N1, N3);
2758 else
2759 return N1; // Never-taken branch
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002760 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 break;
2762 case ISD::VECTOR_SHUFFLE:
Mon P Wangbff5d9c2008-11-10 04:46:22 +00002763 assert(N1.getValueType() == N2.getValueType() &&
2764 N1.getValueType().isVector() &&
Duncan Sands92c43912008-06-06 12:08:01 +00002765 VT.isVector() && N3.getValueType().isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002766 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands92c43912008-06-06 12:08:01 +00002767 VT.getVectorNumElements() == N3.getNumOperands() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 "Illegal VECTOR_SHUFFLE node!");
2769 break;
2770 case ISD::BIT_CONVERT:
2771 // Fold bit_convert nodes from a type to themselves.
2772 if (N1.getValueType() == VT)
2773 return N1;
2774 break;
2775 }
2776
2777 // Memoize node if it doesn't produce a flag.
2778 SDNode *N;
2779 SDVTList VTs = getVTList(VT);
2780 if (VT != MVT::Flag) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002781 SDValue Ops[] = { N1, N2, N3 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782 FoldingSetNodeID ID;
2783 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2784 void *IP = 0;
2785 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002786 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002787 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00002788 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002789 CSEMap.InsertNode(N, IP);
2790 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002791 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00002792 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002793 }
2794 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002795#ifndef NDEBUG
2796 VerifyNode(N);
2797#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002798 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002799}
2800
Dan Gohman8181bd12008-07-27 21:46:04 +00002801SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2802 SDValue N1, SDValue N2, SDValue N3,
2803 SDValue N4) {
2804 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002805 return getNode(Opcode, VT, Ops, 4);
2806}
2807
Dan Gohman8181bd12008-07-27 21:46:04 +00002808SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2809 SDValue N1, SDValue N2, SDValue N3,
2810 SDValue N4, SDValue N5) {
2811 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812 return getNode(Opcode, VT, Ops, 5);
2813}
2814
Dan Gohmane8b391e2008-04-12 04:36:06 +00002815/// getMemsetValue - Vectorized representation of the memset value
2816/// operand.
Dan Gohman8181bd12008-07-27 21:46:04 +00002817static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002818 unsigned NumBits = VT.isVector() ?
2819 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohmane8b391e2008-04-12 04:36:06 +00002820 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002821 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002822 unsigned Shift = 8;
Evan Cheng8c590372008-05-15 08:39:06 +00002823 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002824 Val = (Val << Shift) | Val;
2825 Shift <<= 1;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002826 }
Duncan Sands92c43912008-06-06 12:08:01 +00002827 if (VT.isInteger())
Evan Cheng8c590372008-05-15 08:39:06 +00002828 return DAG.getConstant(Val, VT);
2829 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002830 }
Evan Cheng8c590372008-05-15 08:39:06 +00002831
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00002832 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Evan Cheng8c590372008-05-15 08:39:06 +00002833 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2834 unsigned Shift = 8;
2835 for (unsigned i = NumBits; i > 8; i >>= 1) {
2836 Value = DAG.getNode(ISD::OR, VT,
2837 DAG.getNode(ISD::SHL, VT, Value,
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00002838 DAG.getConstant(Shift,
2839 TLI.getShiftAmountTy())),
2840 Value);
Evan Cheng8c590372008-05-15 08:39:06 +00002841 Shift <<= 1;
2842 }
2843
2844 return Value;
Rafael Espindola80825902007-10-19 10:41:11 +00002845}
2846
Dan Gohmane8b391e2008-04-12 04:36:06 +00002847/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2848/// used when a memcpy is turned into a memset when the source is a constant
2849/// string ptr.
Dan Gohman8181bd12008-07-27 21:46:04 +00002850static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohmane8b391e2008-04-12 04:36:06 +00002851 const TargetLowering &TLI,
2852 std::string &Str, unsigned Offset) {
Evan Cheng833501d2008-06-30 07:31:25 +00002853 // Handle vector with all elements zero.
2854 if (Str.empty()) {
2855 if (VT.isInteger())
2856 return DAG.getConstant(0, VT);
2857 unsigned NumElts = VT.getVectorNumElements();
2858 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2859 return DAG.getNode(ISD::BIT_CONVERT, VT,
2860 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2861 }
2862
Duncan Sands92c43912008-06-06 12:08:01 +00002863 assert(!VT.isVector() && "Can't handle vector type here!");
2864 unsigned NumBits = VT.getSizeInBits();
Evan Cheng8c590372008-05-15 08:39:06 +00002865 unsigned MSB = NumBits / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002866 uint64_t Val = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002867 if (TLI.isLittleEndian())
2868 Offset = Offset + MSB - 1;
2869 for (unsigned i = 0; i != MSB; ++i) {
2870 Val = (Val << 8) | (unsigned char)Str[Offset];
2871 Offset += TLI.isLittleEndian() ? -1 : 1;
2872 }
2873 return DAG.getConstant(Val, VT);
Rafael Espindola80825902007-10-19 10:41:11 +00002874}
2875
Dan Gohmane8b391e2008-04-12 04:36:06 +00002876/// getMemBasePlusOffset - Returns base and offset node for the
Evan Cheng8c590372008-05-15 08:39:06 +00002877///
Dan Gohman8181bd12008-07-27 21:46:04 +00002878static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohmane8b391e2008-04-12 04:36:06 +00002879 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002880 MVT VT = Base.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00002881 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2882}
2883
Evan Cheng8c590372008-05-15 08:39:06 +00002884/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2885///
Dan Gohman8181bd12008-07-27 21:46:04 +00002886static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Cheng8c590372008-05-15 08:39:06 +00002887 unsigned SrcDelta = 0;
2888 GlobalAddressSDNode *G = NULL;
2889 if (Src.getOpcode() == ISD::GlobalAddress)
2890 G = cast<GlobalAddressSDNode>(Src);
2891 else if (Src.getOpcode() == ISD::ADD &&
2892 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2893 Src.getOperand(1).getOpcode() == ISD::Constant) {
2894 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002895 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Cheng8c590372008-05-15 08:39:06 +00002896 }
2897 if (!G)
2898 return false;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002899
Evan Cheng8c590372008-05-15 08:39:06 +00002900 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng833501d2008-06-30 07:31:25 +00002901 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2902 return true;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002903
Evan Cheng8c590372008-05-15 08:39:06 +00002904 return false;
2905}
Dan Gohmane8b391e2008-04-12 04:36:06 +00002906
Evan Cheng8c590372008-05-15 08:39:06 +00002907/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2908/// to replace the memset / memcpy is below the threshold. It also returns the
2909/// types of the sequence of memory ops to perform memset / memcpy.
2910static
Duncan Sands92c43912008-06-06 12:08:01 +00002911bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman8181bd12008-07-27 21:46:04 +00002912 SDValue Dst, SDValue Src,
Evan Cheng8c590372008-05-15 08:39:06 +00002913 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng833501d2008-06-30 07:31:25 +00002914 std::string &Str, bool &isSrcStr,
Evan Cheng8c590372008-05-15 08:39:06 +00002915 SelectionDAG &DAG,
2916 const TargetLowering &TLI) {
Evan Cheng833501d2008-06-30 07:31:25 +00002917 isSrcStr = isMemSrcFromString(Src, Str);
Evan Cheng8c590372008-05-15 08:39:06 +00002918 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng833501d2008-06-30 07:31:25 +00002919 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Chris Lattner23d3bdf2008-10-28 07:10:51 +00002920 MVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Cheng8c590372008-05-15 08:39:06 +00002921 if (VT != MVT::iAny) {
2922 unsigned NewAlign = (unsigned)
Duncan Sands92c43912008-06-06 12:08:01 +00002923 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng8c590372008-05-15 08:39:06 +00002924 // If source is a string constant, this will require an unaligned load.
2925 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2926 if (Dst.getOpcode() != ISD::FrameIndex) {
2927 // Can't change destination alignment. It requires a unaligned store.
2928 if (AllowUnalign)
2929 VT = MVT::iAny;
2930 } else {
2931 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2932 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2933 if (MFI->isFixedObjectIndex(FI)) {
2934 // Can't change destination alignment. It requires a unaligned store.
2935 if (AllowUnalign)
2936 VT = MVT::iAny;
2937 } else {
Evan Cheng36cd8862008-06-04 23:37:54 +00002938 // Give the stack frame object a larger alignment if needed.
2939 if (MFI->getObjectAlignment(FI) < NewAlign)
2940 MFI->setObjectAlignment(FI, NewAlign);
Evan Cheng8c590372008-05-15 08:39:06 +00002941 Align = NewAlign;
2942 }
2943 }
2944 }
2945 }
2946
2947 if (VT == MVT::iAny) {
2948 if (AllowUnalign) {
2949 VT = MVT::i64;
2950 } else {
2951 switch (Align & 7) {
2952 case 0: VT = MVT::i64; break;
2953 case 4: VT = MVT::i32; break;
2954 case 2: VT = MVT::i16; break;
2955 default: VT = MVT::i8; break;
2956 }
2957 }
2958
Duncan Sands92c43912008-06-06 12:08:01 +00002959 MVT LVT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00002960 while (!TLI.isTypeLegal(LVT))
Duncan Sands92c43912008-06-06 12:08:01 +00002961 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2962 assert(LVT.isInteger());
Evan Cheng8c590372008-05-15 08:39:06 +00002963
Duncan Sandsec142ee2008-06-08 20:54:56 +00002964 if (VT.bitsGT(LVT))
Evan Cheng8c590372008-05-15 08:39:06 +00002965 VT = LVT;
2966 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002967
2968 unsigned NumMemOps = 0;
2969 while (Size != 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00002970 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002971 while (VTSize > Size) {
Evan Cheng8c590372008-05-15 08:39:06 +00002972 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands92c43912008-06-06 12:08:01 +00002973 if (VT.isVector()) {
Evan Cheng8c590372008-05-15 08:39:06 +00002974 VT = MVT::i64;
2975 while (!TLI.isTypeLegal(VT))
Duncan Sands92c43912008-06-06 12:08:01 +00002976 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2977 VTSize = VT.getSizeInBits() / 8;
Evan Cheng8c590372008-05-15 08:39:06 +00002978 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002979 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Cheng8c590372008-05-15 08:39:06 +00002980 VTSize >>= 1;
2981 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002982 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002983
2984 if (++NumMemOps > Limit)
2985 return false;
2986 MemOps.push_back(VT);
2987 Size -= VTSize;
2988 }
2989
2990 return true;
2991}
2992
Dan Gohman8181bd12008-07-27 21:46:04 +00002993static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2994 SDValue Chain, SDValue Dst,
2995 SDValue Src, uint64_t Size,
Evan Cheng8c590372008-05-15 08:39:06 +00002996 unsigned Align, bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00002997 const Value *DstSV, uint64_t DstSVOff,
2998 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00002999 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3000
Dan Gohman42d311c2008-05-29 19:42:22 +00003001 // Expand memcpy to a series of load and store ops if the size operand falls
3002 // below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00003003 std::vector<MVT> MemOps;
Dan Gohman88566ae2008-10-03 17:56:45 +00003004 uint64_t Limit = -1ULL;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003005 if (!AlwaysInline)
3006 Limit = TLI.getMaxStoresPerMemcpy();
Evan Cheng8c590372008-05-15 08:39:06 +00003007 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng833501d2008-06-30 07:31:25 +00003008 std::string Str;
3009 bool CopyFromStr;
Evan Cheng8c590372008-05-15 08:39:06 +00003010 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng833501d2008-06-30 07:31:25 +00003011 Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003012 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003013
Dan Gohmane8b391e2008-04-12 04:36:06 +00003014
Evan Cheng833501d2008-06-30 07:31:25 +00003015 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman8181bd12008-07-27 21:46:04 +00003016 SmallVector<SDValue, 8> OutChains;
Evan Cheng8c590372008-05-15 08:39:06 +00003017 unsigned NumMemOps = MemOps.size();
Evan Cheng833501d2008-06-30 07:31:25 +00003018 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003019 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00003020 MVT VT = MemOps[i];
3021 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003022 SDValue Value, Store;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003023
Evan Cheng833501d2008-06-30 07:31:25 +00003024 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Cheng8c590372008-05-15 08:39:06 +00003025 // It's unlikely a store of a vector immediate can be done in a single
3026 // instruction. It would require a load from a constantpool first.
Evan Cheng833501d2008-06-30 07:31:25 +00003027 // We also handle store a vector with all zero's.
3028 // FIXME: Handle other cases where store of vector immediate is done in
3029 // a single instruction.
Dan Gohmane8b391e2008-04-12 04:36:06 +00003030 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Cheng8c590372008-05-15 08:39:06 +00003031 Store = DAG.getStore(Chain, Value,
3032 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng757ec312008-07-09 06:38:06 +00003033 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003034 } else {
3035 Value = DAG.getLoad(VT, Chain,
3036 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman65118f42008-04-28 17:15:20 +00003037 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Cheng8c590372008-05-15 08:39:06 +00003038 Store = DAG.getStore(Chain, Value,
3039 getMemBasePlusOffset(Dst, DstOff, DAG),
3040 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003041 }
3042 OutChains.push_back(Store);
3043 SrcOff += VTSize;
3044 DstOff += VTSize;
3045 }
3046
3047 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3048 &OutChains[0], OutChains.size());
3049}
3050
Dan Gohman8181bd12008-07-27 21:46:04 +00003051static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
3052 SDValue Chain, SDValue Dst,
3053 SDValue Src, uint64_t Size,
Dan Gohman42d311c2008-05-29 19:42:22 +00003054 unsigned Align, bool AlwaysInline,
3055 const Value *DstSV, uint64_t DstSVOff,
3056 const Value *SrcSV, uint64_t SrcSVOff){
3057 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3058
3059 // Expand memmove to a series of load and store ops if the size operand falls
3060 // below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00003061 std::vector<MVT> MemOps;
Dan Gohman88566ae2008-10-03 17:56:45 +00003062 uint64_t Limit = -1ULL;
Dan Gohman42d311c2008-05-29 19:42:22 +00003063 if (!AlwaysInline)
3064 Limit = TLI.getMaxStoresPerMemmove();
3065 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng833501d2008-06-30 07:31:25 +00003066 std::string Str;
3067 bool CopyFromStr;
Dan Gohman42d311c2008-05-29 19:42:22 +00003068 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng833501d2008-06-30 07:31:25 +00003069 Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003070 return SDValue();
Dan Gohman42d311c2008-05-29 19:42:22 +00003071
Dan Gohman42d311c2008-05-29 19:42:22 +00003072 uint64_t SrcOff = 0, DstOff = 0;
3073
Dan Gohman8181bd12008-07-27 21:46:04 +00003074 SmallVector<SDValue, 8> LoadValues;
3075 SmallVector<SDValue, 8> LoadChains;
3076 SmallVector<SDValue, 8> OutChains;
Dan Gohman42d311c2008-05-29 19:42:22 +00003077 unsigned NumMemOps = MemOps.size();
3078 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00003079 MVT VT = MemOps[i];
3080 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003081 SDValue Value, Store;
Dan Gohman42d311c2008-05-29 19:42:22 +00003082
3083 Value = DAG.getLoad(VT, Chain,
3084 getMemBasePlusOffset(Src, SrcOff, DAG),
3085 SrcSV, SrcSVOff + SrcOff, false, Align);
3086 LoadValues.push_back(Value);
3087 LoadChains.push_back(Value.getValue(1));
3088 SrcOff += VTSize;
3089 }
3090 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3091 &LoadChains[0], LoadChains.size());
3092 OutChains.clear();
3093 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00003094 MVT VT = MemOps[i];
3095 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003096 SDValue Value, Store;
Dan Gohman42d311c2008-05-29 19:42:22 +00003097
3098 Store = DAG.getStore(Chain, LoadValues[i],
3099 getMemBasePlusOffset(Dst, DstOff, DAG),
3100 DstSV, DstSVOff + DstOff, false, DstAlign);
3101 OutChains.push_back(Store);
3102 DstOff += VTSize;
3103 }
3104
3105 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3106 &OutChains[0], OutChains.size());
3107}
3108
Dan Gohman8181bd12008-07-27 21:46:04 +00003109static SDValue getMemsetStores(SelectionDAG &DAG,
3110 SDValue Chain, SDValue Dst,
3111 SDValue Src, uint64_t Size,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003112 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00003113 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003114 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3115
3116 // Expand memset to a series of load/store ops if the size operand
3117 // falls below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00003118 std::vector<MVT> MemOps;
Evan Cheng833501d2008-06-30 07:31:25 +00003119 std::string Str;
3120 bool CopyFromStr;
Evan Cheng8c590372008-05-15 08:39:06 +00003121 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng833501d2008-06-30 07:31:25 +00003122 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003123 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003124
Dan Gohman8181bd12008-07-27 21:46:04 +00003125 SmallVector<SDValue, 8> OutChains;
Dan Gohman65118f42008-04-28 17:15:20 +00003126 uint64_t DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003127
3128 unsigned NumMemOps = MemOps.size();
3129 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00003130 MVT VT = MemOps[i];
3131 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003132 SDValue Value = getMemsetValue(Src, VT, DAG);
3133 SDValue Store = DAG.getStore(Chain, Value,
Chris Lattner23d3bdf2008-10-28 07:10:51 +00003134 getMemBasePlusOffset(Dst, DstOff, DAG),
3135 DstSV, DstSVOff + DstOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003136 OutChains.push_back(Store);
3137 DstOff += VTSize;
3138 }
3139
3140 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3141 &OutChains[0], OutChains.size());
3142}
3143
Dan Gohman8181bd12008-07-27 21:46:04 +00003144SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3145 SDValue Src, SDValue Size,
3146 unsigned Align, bool AlwaysInline,
3147 const Value *DstSV, uint64_t DstSVOff,
3148 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003149
3150 // Check to see if we should lower the memcpy to loads and stores first.
3151 // For cases within the target-specified limits, this is the best choice.
3152 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3153 if (ConstantSize) {
3154 // Memcpy with size zero? Just return the original chain.
3155 if (ConstantSize->isNullValue())
3156 return Chain;
3157
Dan Gohman8181bd12008-07-27 21:46:04 +00003158 SDValue Result =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003159 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3160 ConstantSize->getZExtValue(),
Dan Gohman65118f42008-04-28 17:15:20 +00003161 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003162 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003163 return Result;
3164 }
3165
3166 // Then check to see if we should lower the memcpy with target-specific
3167 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003168 SDValue Result =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003169 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3170 AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00003171 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003172 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003173 return Result;
3174
3175 // If we really need inline code and the target declined to provide it,
3176 // use a (potentially long) sequence of loads and stores.
3177 if (AlwaysInline) {
3178 assert(ConstantSize && "AlwaysInline requires a constant size!");
3179 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003180 ConstantSize->getZExtValue(), Align, true,
Dan Gohman65118f42008-04-28 17:15:20 +00003181 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003182 }
3183
3184 // Emit a library call.
3185 TargetLowering::ArgListTy Args;
3186 TargetLowering::ArgListEntry Entry;
3187 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3188 Entry.Node = Dst; Args.push_back(Entry);
3189 Entry.Node = Src; Args.push_back(Entry);
3190 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00003191 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003192 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00003193 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00003194 getExternalSymbol("memcpy", TLI.getPointerTy()),
Dan Gohmane8b391e2008-04-12 04:36:06 +00003195 Args, *this);
3196 return CallResult.second;
3197}
3198
Dan Gohman8181bd12008-07-27 21:46:04 +00003199SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3200 SDValue Src, SDValue Size,
3201 unsigned Align,
3202 const Value *DstSV, uint64_t DstSVOff,
3203 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003204
Dan Gohman42d311c2008-05-29 19:42:22 +00003205 // Check to see if we should lower the memmove to loads and stores first.
3206 // For cases within the target-specified limits, this is the best choice.
3207 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3208 if (ConstantSize) {
3209 // Memmove with size zero? Just return the original chain.
3210 if (ConstantSize->isNullValue())
3211 return Chain;
3212
Dan Gohman8181bd12008-07-27 21:46:04 +00003213 SDValue Result =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003214 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3215 ConstantSize->getZExtValue(),
Dan Gohman42d311c2008-05-29 19:42:22 +00003216 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003217 if (Result.getNode())
Dan Gohman42d311c2008-05-29 19:42:22 +00003218 return Result;
3219 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003220
3221 // Then check to see if we should lower the memmove with target-specific
3222 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003223 SDValue Result =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003224 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman65118f42008-04-28 17:15:20 +00003225 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003226 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003227 return Result;
3228
3229 // Emit a library call.
3230 TargetLowering::ArgListTy Args;
3231 TargetLowering::ArgListEntry Entry;
3232 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3233 Entry.Node = Dst; Args.push_back(Entry);
3234 Entry.Node = Src; Args.push_back(Entry);
3235 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00003236 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003237 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00003238 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00003239 getExternalSymbol("memmove", TLI.getPointerTy()),
Dan Gohmane8b391e2008-04-12 04:36:06 +00003240 Args, *this);
3241 return CallResult.second;
3242}
3243
Dan Gohman8181bd12008-07-27 21:46:04 +00003244SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3245 SDValue Src, SDValue Size,
3246 unsigned Align,
3247 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003248
3249 // Check to see if we should lower the memset to stores first.
3250 // For cases within the target-specified limits, this is the best choice.
3251 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3252 if (ConstantSize) {
3253 // Memset with size zero? Just return the original chain.
3254 if (ConstantSize->isNullValue())
3255 return Chain;
3256
Dan Gohman8181bd12008-07-27 21:46:04 +00003257 SDValue Result =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003258 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3259 Align, DstSV, DstSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003260 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003261 return Result;
3262 }
3263
3264 // Then check to see if we should lower the memset with target-specific
3265 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003266 SDValue Result =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003267 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Bill Wendling4b2e3782008-10-01 00:59:58 +00003268 DstSV, DstSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003269 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003270 return Result;
3271
3272 // Emit a library call.
3273 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3274 TargetLowering::ArgListTy Args;
3275 TargetLowering::ArgListEntry Entry;
3276 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3277 Args.push_back(Entry);
3278 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sandsec142ee2008-06-08 20:54:56 +00003279 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohmane8b391e2008-04-12 04:36:06 +00003280 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3281 else
3282 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3283 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3284 Args.push_back(Entry);
3285 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3286 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00003287 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003288 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00003289 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00003290 getExternalSymbol("memset", TLI.getPointerTy()),
Dan Gohmane8b391e2008-04-12 04:36:06 +00003291 Args, *this);
3292 return CallResult.second;
Rafael Espindola80825902007-10-19 10:41:11 +00003293}
3294
Dan Gohmanbebba8d2008-12-23 21:37:04 +00003295SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3296 SDValue Chain,
Dan Gohman8181bd12008-07-27 21:46:04 +00003297 SDValue Ptr, SDValue Cmp,
3298 SDValue Swp, const Value* PtrVal,
3299 unsigned Alignment) {
Dan Gohmanbebba8d2008-12-23 21:37:04 +00003300 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00003301 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9e3a3422008-07-08 23:46:32 +00003302
3303 MVT VT = Cmp.getValueType();
3304
3305 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanbebba8d2008-12-23 21:37:04 +00003306 Alignment = getMVTAlignment(MemVT);
Dan Gohman9e3a3422008-07-08 23:46:32 +00003307
3308 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003309 FoldingSetNodeID ID;
Dan Gohman8181bd12008-07-27 21:46:04 +00003310 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003311 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003312 void* IP = 0;
3313 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003314 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003315 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohmanbebba8d2008-12-23 21:37:04 +00003316 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3317 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003318 CSEMap.InsertNode(N, IP);
3319 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003320 return SDValue(N, 0);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003321}
3322
Dan Gohmanbebba8d2008-12-23 21:37:04 +00003323SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3324 SDValue Chain,
Dan Gohman8181bd12008-07-27 21:46:04 +00003325 SDValue Ptr, SDValue Val,
3326 const Value* PtrVal,
3327 unsigned Alignment) {
Dan Gohmanbebba8d2008-12-23 21:37:04 +00003328 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3329 Opcode == ISD::ATOMIC_LOAD_SUB ||
3330 Opcode == ISD::ATOMIC_LOAD_AND ||
3331 Opcode == ISD::ATOMIC_LOAD_OR ||
3332 Opcode == ISD::ATOMIC_LOAD_XOR ||
3333 Opcode == ISD::ATOMIC_LOAD_NAND ||
3334 Opcode == ISD::ATOMIC_LOAD_MIN ||
3335 Opcode == ISD::ATOMIC_LOAD_MAX ||
3336 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3337 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3338 Opcode == ISD::ATOMIC_SWAP) &&
3339 "Invalid Atomic Op");
Dan Gohman9e3a3422008-07-08 23:46:32 +00003340
3341 MVT VT = Val.getValueType();
3342
3343 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanbebba8d2008-12-23 21:37:04 +00003344 Alignment = getMVTAlignment(MemVT);
Dan Gohman9e3a3422008-07-08 23:46:32 +00003345
3346 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003347 FoldingSetNodeID ID;
Dan Gohman8181bd12008-07-27 21:46:04 +00003348 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003349 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003350 void* IP = 0;
3351 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003352 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003353 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohmanbebba8d2008-12-23 21:37:04 +00003354 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3355 Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003356 CSEMap.InsertNode(N, IP);
3357 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003358 return SDValue(N, 0);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003359}
3360
Duncan Sands698842f2008-07-02 17:40:58 +00003361/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3362/// Allowed to return something different (and simpler) if Simplify is true.
Duncan Sands42d7bb82008-12-01 11:41:29 +00003363SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps) {
3364 if (NumOps == 1)
Duncan Sands698842f2008-07-02 17:40:58 +00003365 return Ops[0];
3366
3367 SmallVector<MVT, 4> VTs;
3368 VTs.reserve(NumOps);
3369 for (unsigned i = 0; i < NumOps; ++i)
3370 VTs.push_back(Ops[i].getValueType());
3371 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3372}
3373
Dan Gohman8181bd12008-07-27 21:46:04 +00003374SDValue
Mon P Wang89423382008-10-17 18:22:58 +00003375SelectionDAG::getMemIntrinsicNode(unsigned Opcode,
3376 const MVT *VTs, unsigned NumVTs,
3377 const SDValue *Ops, unsigned NumOps,
3378 MVT MemVT, const Value *srcValue, int SVOff,
3379 unsigned Align, bool Vol,
3380 bool ReadMem, bool WriteMem) {
3381 return getMemIntrinsicNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps,
3382 MemVT, srcValue, SVOff, Align, Vol,
3383 ReadMem, WriteMem);
3384}
3385
3386SDValue
3387SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDVTList VTList,
3388 const SDValue *Ops, unsigned NumOps,
3389 MVT MemVT, const Value *srcValue, int SVOff,
3390 unsigned Align, bool Vol,
3391 bool ReadMem, bool WriteMem) {
3392 // Memoize the node unless it returns a flag.
3393 MemIntrinsicSDNode *N;
3394 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3395 FoldingSetNodeID ID;
3396 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3397 void *IP = 0;
3398 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3399 return SDValue(E, 0);
3400
3401 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3402 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3403 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3404 CSEMap.InsertNode(N, IP);
3405 } else {
3406 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3407 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3408 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3409 }
3410 AllNodes.push_back(N);
3411 return SDValue(N, 0);
3412}
3413
3414SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00003415SelectionDAG::getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00003416 bool IsInreg, SDVTList VTs,
Dan Gohman705e3f72008-09-13 01:54:27 +00003417 const SDValue *Operands, unsigned NumOperands) {
Dan Gohman791e12a2008-09-15 19:46:03 +00003418 // Do not include isTailCall in the folding set profile.
3419 FoldingSetNodeID ID;
3420 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3421 ID.AddInteger(CallingConv);
3422 ID.AddInteger(IsVarArgs);
3423 void *IP = 0;
3424 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3425 // Instead of including isTailCall in the folding set, we just
3426 // set the flag of the existing node.
3427 if (!IsTailCall)
3428 cast<CallSDNode>(E)->setNotTailCall();
3429 return SDValue(E, 0);
3430 }
Dan Gohman705e3f72008-09-13 01:54:27 +00003431 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
Dale Johannesen67cc9b62008-09-26 19:31:26 +00003432 new (N) CallSDNode(CallingConv, IsVarArgs, IsTailCall, IsInreg,
Dan Gohman705e3f72008-09-13 01:54:27 +00003433 VTs, Operands, NumOperands);
Dan Gohman791e12a2008-09-15 19:46:03 +00003434 CSEMap.InsertNode(N, IP);
Dan Gohman705e3f72008-09-13 01:54:27 +00003435 AllNodes.push_back(N);
3436 return SDValue(N, 0);
3437}
3438
3439SDValue
Duncan Sandsb2589712008-03-28 09:45:24 +00003440SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman8181bd12008-07-27 21:46:04 +00003441 MVT VT, SDValue Chain,
3442 SDValue Ptr, SDValue Offset,
Duncan Sands92c43912008-06-06 12:08:01 +00003443 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandsb2589712008-03-28 09:45:24 +00003444 bool isVolatile, unsigned Alignment) {
Dan Gohman9e3a3422008-07-08 23:46:32 +00003445 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3446 Alignment = getMVTAlignment(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003447
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003448 if (VT == EVT) {
3449 ExtType = ISD::NON_EXTLOAD;
3450 } else if (ExtType == ISD::NON_EXTLOAD) {
3451 assert(VT == EVT && "Non-extending load from different memory type!");
3452 } else {
3453 // Extending load.
Duncan Sands92c43912008-06-06 12:08:01 +00003454 if (VT.isVector())
Dan Gohman29c3cef2008-08-14 20:04:46 +00003455 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3456 "Invalid vector extload!");
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003457 else
Duncan Sandsec142ee2008-06-08 20:54:56 +00003458 assert(EVT.bitsLT(VT) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003459 "Should only be an extending load, not truncating!");
Duncan Sands92c43912008-06-06 12:08:01 +00003460 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003461 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands92c43912008-06-06 12:08:01 +00003462 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003463 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003464 }
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003465
3466 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sandsf6890712008-05-27 11:50:51 +00003467 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003468 "Unindexed load with an offset!");
3469
3470 SDVTList VTs = Indexed ?
3471 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00003472 SDValue Ops[] = { Chain, Ptr, Offset };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003473 FoldingSetNodeID ID;
3474 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003475 ID.AddInteger(AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003476 ID.AddInteger(ExtType);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003477 ID.AddInteger(EVT.getRawBits());
Dan Gohman98beebe2008-08-20 15:58:01 +00003478 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003479 void *IP = 0;
3480 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003481 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003482 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003483 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3484 Alignment, isVolatile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485 CSEMap.InsertNode(N, IP);
3486 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003487 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003488}
3489
Dan Gohman8181bd12008-07-27 21:46:04 +00003490SDValue SelectionDAG::getLoad(MVT VT,
3491 SDValue Chain, SDValue Ptr,
3492 const Value *SV, int SVOffset,
3493 bool isVolatile, unsigned Alignment) {
3494 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandsb2589712008-03-28 09:45:24 +00003495 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3496 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003497}
3498
Dan Gohman8181bd12008-07-27 21:46:04 +00003499SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3500 SDValue Chain, SDValue Ptr,
3501 const Value *SV,
3502 int SVOffset, MVT EVT,
3503 bool isVolatile, unsigned Alignment) {
3504 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandsb2589712008-03-28 09:45:24 +00003505 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3506 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003507}
3508
Dan Gohman8181bd12008-07-27 21:46:04 +00003509SDValue
3510SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3511 SDValue Offset, ISD::MemIndexedMode AM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003512 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3513 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3514 "Load is already a indexed load!");
Duncan Sandsb2589712008-03-28 09:45:24 +00003515 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3516 LD->getChain(), Base, Offset, LD->getSrcValue(),
3517 LD->getSrcValueOffset(), LD->getMemoryVT(),
3518 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003519}
3520
Dan Gohman8181bd12008-07-27 21:46:04 +00003521SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3522 SDValue Ptr, const Value *SV, int SVOffset,
3523 bool isVolatile, unsigned Alignment) {
Duncan Sands92c43912008-06-06 12:08:01 +00003524 MVT VT = Val.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525
Dan Gohman9e3a3422008-07-08 23:46:32 +00003526 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3527 Alignment = getMVTAlignment(VT);
3528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00003530 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3531 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003532 FoldingSetNodeID ID;
3533 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3534 ID.AddInteger(ISD::UNINDEXED);
3535 ID.AddInteger(false);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003536 ID.AddInteger(VT.getRawBits());
Dan Gohman98beebe2008-08-20 15:58:01 +00003537 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003538 void *IP = 0;
3539 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003540 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003541 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003542 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3543 VT, SV, SVOffset, Alignment, isVolatile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003544 CSEMap.InsertNode(N, IP);
3545 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003546 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003547}
3548
Dan Gohman8181bd12008-07-27 21:46:04 +00003549SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3550 SDValue Ptr, const Value *SV,
3551 int SVOffset, MVT SVT,
3552 bool isVolatile, unsigned Alignment) {
Duncan Sands92c43912008-06-06 12:08:01 +00003553 MVT VT = Val.getValueType();
Duncan Sands06fcf652007-10-30 12:40:58 +00003554
3555 if (VT == SVT)
3556 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003557
Duncan Sandsec142ee2008-06-08 20:54:56 +00003558 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands92c43912008-06-06 12:08:01 +00003559 assert(VT.isInteger() == SVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003560 "Can't do FP-INT conversion!");
3561
Dan Gohman9e3a3422008-07-08 23:46:32 +00003562 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3563 Alignment = getMVTAlignment(VT);
3564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003565 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00003566 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3567 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003568 FoldingSetNodeID ID;
3569 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3570 ID.AddInteger(ISD::UNINDEXED);
Duncan Sands06fcf652007-10-30 12:40:58 +00003571 ID.AddInteger(1);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003572 ID.AddInteger(SVT.getRawBits());
Dan Gohman98beebe2008-08-20 15:58:01 +00003573 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003574 void *IP = 0;
3575 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003576 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003577 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003578 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3579 SVT, SV, SVOffset, Alignment, isVolatile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003580 CSEMap.InsertNode(N, IP);
3581 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003582 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003583}
3584
Dan Gohman8181bd12008-07-27 21:46:04 +00003585SDValue
3586SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3587 SDValue Offset, ISD::MemIndexedMode AM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003588 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3589 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3590 "Store is already a indexed store!");
3591 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00003592 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003593 FoldingSetNodeID ID;
3594 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3595 ID.AddInteger(AM);
3596 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3f5d2432008-06-06 12:49:32 +00003597 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman98beebe2008-08-20 15:58:01 +00003598 ID.AddInteger(ST->getRawFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003599 void *IP = 0;
3600 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003601 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003602 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003603 new (N) StoreSDNode(Ops, VTs, AM,
3604 ST->isTruncatingStore(), ST->getMemoryVT(),
3605 ST->getSrcValue(), ST->getSrcValueOffset(),
3606 ST->getAlignment(), ST->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607 CSEMap.InsertNode(N, IP);
3608 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003609 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003610}
3611
Dan Gohman8181bd12008-07-27 21:46:04 +00003612SDValue SelectionDAG::getVAArg(MVT VT,
3613 SDValue Chain, SDValue Ptr,
3614 SDValue SV) {
3615 SDValue Ops[] = { Chain, Ptr, SV };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003616 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
3617}
3618
Dan Gohman8181bd12008-07-27 21:46:04 +00003619SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3620 const SDUse *Ops, unsigned NumOps) {
Dan Gohman703ce5d2008-07-07 18:26:29 +00003621 switch (NumOps) {
3622 case 0: return getNode(Opcode, VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00003623 case 1: return getNode(Opcode, VT, Ops[0]);
3624 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3625 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman703ce5d2008-07-07 18:26:29 +00003626 default: break;
3627 }
3628
Dan Gohman8181bd12008-07-27 21:46:04 +00003629 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman703ce5d2008-07-07 18:26:29 +00003630 // the regular getNode logic.
Dan Gohman8181bd12008-07-27 21:46:04 +00003631 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3632 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman703ce5d2008-07-07 18:26:29 +00003633}
3634
Dan Gohman8181bd12008-07-27 21:46:04 +00003635SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3636 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003637 switch (NumOps) {
3638 case 0: return getNode(Opcode, VT);
3639 case 1: return getNode(Opcode, VT, Ops[0]);
3640 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3641 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
3642 default: break;
3643 }
3644
3645 switch (Opcode) {
3646 default: break;
3647 case ISD::SELECT_CC: {
3648 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
3649 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3650 "LHS and RHS of condition must have same type!");
3651 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3652 "True and False arms of SelectCC must have same type!");
3653 assert(Ops[2].getValueType() == VT &&
3654 "select_cc node must be of same type as true and false value!");
3655 break;
3656 }
3657 case ISD::BR_CC: {
3658 assert(NumOps == 5 && "BR_CC takes 5 operands!");
3659 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3660 "LHS/RHS of comparison should match types!");
3661 break;
3662 }
3663 }
3664
3665 // Memoize nodes.
3666 SDNode *N;
3667 SDVTList VTs = getVTList(VT);
3668 if (VT != MVT::Flag) {
3669 FoldingSetNodeID ID;
3670 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
3671 void *IP = 0;
3672 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003673 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003674 N = NodeAllocator.Allocate<SDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003675 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003676 CSEMap.InsertNode(N, IP);
3677 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003678 N = NodeAllocator.Allocate<SDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003679 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003680 }
3681 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003682#ifndef NDEBUG
3683 VerifyNode(N);
3684#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00003685 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003686}
3687
Dan Gohman8181bd12008-07-27 21:46:04 +00003688SDValue SelectionDAG::getNode(unsigned Opcode,
3689 const std::vector<MVT> &ResultTys,
3690 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003691 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3692 Ops, NumOps);
3693}
3694
Dan Gohman8181bd12008-07-27 21:46:04 +00003695SDValue SelectionDAG::getNode(unsigned Opcode,
3696 const MVT *VTs, unsigned NumVTs,
3697 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003698 if (NumVTs == 1)
3699 return getNode(Opcode, VTs[0], Ops, NumOps);
3700 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3701}
3702
Dan Gohman8181bd12008-07-27 21:46:04 +00003703SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3704 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003705 if (VTList.NumVTs == 1)
3706 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
3707
3708 switch (Opcode) {
3709 // FIXME: figure out how to safely handle things like
3710 // int foo(int x) { return 1 << (x & 255); }
3711 // int bar() { return foo(256); }
3712#if 0
3713 case ISD::SRA_PARTS:
3714 case ISD::SRL_PARTS:
3715 case ISD::SHL_PARTS:
3716 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3717 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
3718 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3719 else if (N3.getOpcode() == ISD::AND)
3720 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3721 // If the and is only masking out bits that cannot effect the shift,
3722 // eliminate the and.
Duncan Sands92c43912008-06-06 12:08:01 +00003723 unsigned NumBits = VT.getSizeInBits()*2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003724 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3725 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3726 }
3727 break;
3728#endif
3729 }
3730
3731 // Memoize the node unless it returns a flag.
3732 SDNode *N;
3733 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3734 FoldingSetNodeID ID;
3735 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3736 void *IP = 0;
3737 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003738 return SDValue(E, 0);
Dan Gohmaned825d12008-07-07 23:02:41 +00003739 if (NumOps == 1) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003740 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003741 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3742 } else if (NumOps == 2) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003743 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003744 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3745 } else if (NumOps == 3) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003746 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003747 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3748 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003749 N = NodeAllocator.Allocate<SDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003750 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3751 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003752 CSEMap.InsertNode(N, IP);
3753 } else {
Dan Gohmaned825d12008-07-07 23:02:41 +00003754 if (NumOps == 1) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003755 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003756 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3757 } else if (NumOps == 2) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003758 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003759 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3760 } else if (NumOps == 3) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003761 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003762 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3763 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003764 N = NodeAllocator.Allocate<SDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003765 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3766 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003767 }
3768 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003769#ifndef NDEBUG
3770 VerifyNode(N);
3771#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00003772 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003773}
3774
Dan Gohman8181bd12008-07-27 21:46:04 +00003775SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman703ce5d2008-07-07 18:26:29 +00003776 return getNode(Opcode, VTList, 0, 0);
Dan Gohman798d1272007-10-08 15:49:58 +00003777}
3778
Dan Gohman8181bd12008-07-27 21:46:04 +00003779SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3780 SDValue N1) {
3781 SDValue Ops[] = { N1 };
Dan Gohman798d1272007-10-08 15:49:58 +00003782 return getNode(Opcode, VTList, Ops, 1);
3783}
3784
Dan Gohman8181bd12008-07-27 21:46:04 +00003785SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3786 SDValue N1, SDValue N2) {
3787 SDValue Ops[] = { N1, N2 };
Dan Gohman798d1272007-10-08 15:49:58 +00003788 return getNode(Opcode, VTList, Ops, 2);
3789}
3790
Dan Gohman8181bd12008-07-27 21:46:04 +00003791SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3792 SDValue N1, SDValue N2, SDValue N3) {
3793 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman798d1272007-10-08 15:49:58 +00003794 return getNode(Opcode, VTList, Ops, 3);
3795}
3796
Dan Gohman8181bd12008-07-27 21:46:04 +00003797SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3798 SDValue N1, SDValue N2, SDValue N3,
3799 SDValue N4) {
3800 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman798d1272007-10-08 15:49:58 +00003801 return getNode(Opcode, VTList, Ops, 4);
3802}
3803
Dan Gohman8181bd12008-07-27 21:46:04 +00003804SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3805 SDValue N1, SDValue N2, SDValue N3,
3806 SDValue N4, SDValue N5) {
3807 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman798d1272007-10-08 15:49:58 +00003808 return getNode(Opcode, VTList, Ops, 5);
3809}
3810
Duncan Sands92c43912008-06-06 12:08:01 +00003811SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00003812 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003813}
3814
Duncan Sands92c43912008-06-06 12:08:01 +00003815SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00003816 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3817 E = VTList.rend(); I != E; ++I)
3818 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3819 return *I;
3820
3821 MVT *Array = Allocator.Allocate<MVT>(2);
3822 Array[0] = VT1;
3823 Array[1] = VT2;
3824 SDVTList Result = makeVTList(Array, 2);
3825 VTList.push_back(Result);
3826 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827}
Dan Gohmanbd68c792008-07-17 19:10:17 +00003828
3829SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3830 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3831 E = VTList.rend(); I != E; ++I)
3832 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3833 I->VTs[2] == VT3)
3834 return *I;
3835
3836 MVT *Array = Allocator.Allocate<MVT>(3);
3837 Array[0] = VT1;
3838 Array[1] = VT2;
3839 Array[2] = VT3;
3840 SDVTList Result = makeVTList(Array, 3);
3841 VTList.push_back(Result);
3842 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843}
3844
Bill Wendling7a4076f2008-12-01 23:28:22 +00003845SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
3846 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3847 E = VTList.rend(); I != E; ++I)
3848 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3849 I->VTs[2] == VT3 && I->VTs[3] == VT4)
3850 return *I;
3851
3852 MVT *Array = Allocator.Allocate<MVT>(3);
3853 Array[0] = VT1;
3854 Array[1] = VT2;
3855 Array[2] = VT3;
3856 Array[3] = VT4;
3857 SDVTList Result = makeVTList(Array, 4);
3858 VTList.push_back(Result);
3859 return Result;
3860}
3861
Duncan Sands92c43912008-06-06 12:08:01 +00003862SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003863 switch (NumVTs) {
3864 case 0: assert(0 && "Cannot have nodes without results!");
3865 case 1: return getVTList(VTs[0]);
3866 case 2: return getVTList(VTs[0], VTs[1]);
3867 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3868 default: break;
3869 }
3870
Dan Gohmanbd68c792008-07-17 19:10:17 +00003871 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3872 E = VTList.rend(); I != E; ++I) {
3873 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3874 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875
3876 bool NoMatch = false;
3877 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmanbd68c792008-07-17 19:10:17 +00003878 if (VTs[i] != I->VTs[i]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003879 NoMatch = true;
3880 break;
3881 }
3882 if (!NoMatch)
Dan Gohmanbd68c792008-07-17 19:10:17 +00003883 return *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003884 }
3885
Dan Gohmanbd68c792008-07-17 19:10:17 +00003886 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3887 std::copy(VTs, VTs+NumVTs, Array);
3888 SDVTList Result = makeVTList(Array, NumVTs);
3889 VTList.push_back(Result);
3890 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891}
3892
3893
3894/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3895/// specified operands. If the resultant node already exists in the DAG,
3896/// this does not modify the specified node, instead it returns the node that
3897/// already exists. If the resultant node does not exist in the DAG, the
3898/// input node is returned. As a degenerate case, if you specify the same
3899/// input operands as the node already has, the input node is returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00003900SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003901 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003902 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3903
3904 // Check to see if there is no change.
3905 if (Op == N->getOperand(0)) return InN;
3906
3907 // See if the modified node already exists.
3908 void *InsertPos = 0;
3909 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00003910 return SDValue(Existing, InN.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911
Dan Gohman7d919ea2008-07-21 22:38:59 +00003912 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003913 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00003914 if (!RemoveNodeFromCSEMaps(N))
3915 InsertPos = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916
3917 // Now we update the operands.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003918 N->OperandList[0].getVal()->removeUser(0, N);
djg943376a2009-01-25 16:29:12 +00003919 N->OperandList[0].getSDValue() = Op;
Gabor Greif1c80d112008-08-28 21:40:38 +00003920 Op.getNode()->addUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921
3922 // If this gets put into a CSE map, add it.
3923 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3924 return InN;
3925}
3926
Dan Gohman8181bd12008-07-27 21:46:04 +00003927SDValue SelectionDAG::
3928UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003929 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003930 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3931
3932 // Check to see if there is no change.
3933 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3934 return InN; // No operands changed, just return the input node.
3935
3936 // See if the modified node already exists.
3937 void *InsertPos = 0;
3938 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00003939 return SDValue(Existing, InN.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003940
Dan Gohman7d919ea2008-07-21 22:38:59 +00003941 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003942 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00003943 if (!RemoveNodeFromCSEMaps(N))
3944 InsertPos = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003945
3946 // Now we update the operands.
3947 if (N->OperandList[0] != Op1) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003948 N->OperandList[0].getVal()->removeUser(0, N);
djg943376a2009-01-25 16:29:12 +00003949 N->OperandList[0].getSDValue() = Op1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003950 Op1.getNode()->addUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 }
3952 if (N->OperandList[1] != Op2) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003953 N->OperandList[1].getVal()->removeUser(1, N);
djg943376a2009-01-25 16:29:12 +00003954 N->OperandList[1].getSDValue() = Op2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003955 Op2.getNode()->addUser(1, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003956 }
3957
3958 // If this gets put into a CSE map, add it.
3959 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3960 return InN;
3961}
3962
Dan Gohman8181bd12008-07-27 21:46:04 +00003963SDValue SelectionDAG::
3964UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3965 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003966 return UpdateNodeOperands(N, Ops, 3);
3967}
3968
Dan Gohman8181bd12008-07-27 21:46:04 +00003969SDValue SelectionDAG::
3970UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3971 SDValue Op3, SDValue Op4) {
3972 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 return UpdateNodeOperands(N, Ops, 4);
3974}
3975
Dan Gohman8181bd12008-07-27 21:46:04 +00003976SDValue SelectionDAG::
3977UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3978 SDValue Op3, SDValue Op4, SDValue Op5) {
3979 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 return UpdateNodeOperands(N, Ops, 5);
3981}
3982
Dan Gohman8181bd12008-07-27 21:46:04 +00003983SDValue SelectionDAG::
3984UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003985 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 assert(N->getNumOperands() == NumOps &&
3987 "Update with wrong number of operands");
3988
3989 // Check to see if there is no change.
3990 bool AnyChange = false;
3991 for (unsigned i = 0; i != NumOps; ++i) {
3992 if (Ops[i] != N->getOperand(i)) {
3993 AnyChange = true;
3994 break;
3995 }
3996 }
3997
3998 // No operands changed, just return the input node.
3999 if (!AnyChange) return InN;
4000
4001 // See if the modified node already exists.
4002 void *InsertPos = 0;
4003 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004004 return SDValue(Existing, InN.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005
Dan Gohman14a027d2008-05-02 00:05:03 +00004006 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004007 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004008 if (!RemoveNodeFromCSEMaps(N))
4009 InsertPos = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010
4011 // Now we update the operands.
4012 for (unsigned i = 0; i != NumOps; ++i) {
4013 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004014 N->OperandList[i].getVal()->removeUser(i, N);
djg943376a2009-01-25 16:29:12 +00004015 N->OperandList[i].getSDValue() = Ops[i];
Gabor Greif1c80d112008-08-28 21:40:38 +00004016 Ops[i].getNode()->addUser(i, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017 }
4018 }
4019
4020 // If this gets put into a CSE map, add it.
4021 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4022 return InN;
4023}
4024
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004025/// DropOperands - Release the operands and set this node to have
Dan Gohmanbd68c792008-07-17 19:10:17 +00004026/// zero operands.
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004027void SDNode::DropOperands() {
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004028 // Unlike the code in MorphNodeTo that does this, we don't need to
4029 // watch for dead nodes here.
4030 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
4031 I->getVal()->removeUser(std::distance(op_begin(), I), this);
4032
4033 NumOperands = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034}
4035
Dan Gohmanbd68c792008-07-17 19:10:17 +00004036/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4037/// machine opcode.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038///
Dan Gohmanbd68c792008-07-17 19:10:17 +00004039SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00004040 MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004041 SDVTList VTs = getVTList(VT);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004042 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043}
4044
Dan Gohmanbd68c792008-07-17 19:10:17 +00004045SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004046 MVT VT, SDValue Op1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004047 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004048 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004049 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050}
4051
Dan Gohmanbd68c792008-07-17 19:10:17 +00004052SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004053 MVT VT, SDValue Op1,
4054 SDValue Op2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004056 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004057 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004058}
4059
Dan Gohmanbd68c792008-07-17 19:10:17 +00004060SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004061 MVT VT, SDValue Op1,
4062 SDValue Op2, SDValue Op3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004063 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004064 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004065 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066}
4067
Dan Gohmanbd68c792008-07-17 19:10:17 +00004068SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004069 MVT VT, const SDValue *Ops,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 SDVTList VTs = getVTList(VT);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004072 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004073}
4074
Dan Gohmanbd68c792008-07-17 19:10:17 +00004075SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004076 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohman7eced112008-07-02 23:23:19 +00004077 unsigned NumOps) {
4078 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004079 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004080}
4081
Dan Gohmanbd68c792008-07-17 19:10:17 +00004082SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman7eced112008-07-02 23:23:19 +00004083 MVT VT1, MVT VT2) {
4084 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004085 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohman7eced112008-07-02 23:23:19 +00004086}
4087
Dan Gohmanbd68c792008-07-17 19:10:17 +00004088SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman703ce5d2008-07-07 18:26:29 +00004089 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004090 const SDValue *Ops, unsigned NumOps) {
Dan Gohman7eced112008-07-02 23:23:19 +00004091 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004092 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004093}
4094
Bill Wendling7a4076f2008-12-01 23:28:22 +00004095SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
4096 MVT VT1, MVT VT2, MVT VT3, MVT VT4,
4097 const SDValue *Ops, unsigned NumOps) {
4098 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4099 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4100}
4101
Dan Gohmanbd68c792008-07-17 19:10:17 +00004102SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman7eced112008-07-02 23:23:19 +00004103 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004104 SDValue Op1) {
Dan Gohman7eced112008-07-02 23:23:19 +00004105 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004106 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004107 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108}
4109
Dan Gohmanbd68c792008-07-17 19:10:17 +00004110SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00004111 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004112 SDValue Op1, SDValue Op2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004114 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004115 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004116}
4117
Dan Gohmanbd68c792008-07-17 19:10:17 +00004118SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00004119 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004120 SDValue Op1, SDValue Op2,
4121 SDValue Op3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004122 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004123 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004124 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman7eced112008-07-02 23:23:19 +00004125}
4126
Dan Gohmanbd68c792008-07-17 19:10:17 +00004127SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Bill Wendling7a4076f2008-12-01 23:28:22 +00004128 MVT VT1, MVT VT2, MVT VT3,
4129 SDValue Op1, SDValue Op2,
4130 SDValue Op3) {
4131 SDVTList VTs = getVTList(VT1, VT2, VT3);
4132 SDValue Ops[] = { Op1, Op2, Op3 };
4133 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4134}
4135
4136SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004137 SDVTList VTs, const SDValue *Ops,
Dan Gohman7eced112008-07-02 23:23:19 +00004138 unsigned NumOps) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004139 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4140}
4141
4142SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4143 MVT VT) {
4144 SDVTList VTs = getVTList(VT);
4145 return MorphNodeTo(N, Opc, VTs, 0, 0);
4146}
4147
4148SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004149 MVT VT, SDValue Op1) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004150 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004151 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004152 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4153}
4154
4155SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004156 MVT VT, SDValue Op1,
4157 SDValue Op2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004158 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004159 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004160 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4161}
4162
4163SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004164 MVT VT, SDValue Op1,
4165 SDValue Op2, SDValue Op3) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004166 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004167 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004168 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4169}
4170
4171SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004172 MVT VT, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004173 unsigned NumOps) {
4174 SDVTList VTs = getVTList(VT);
4175 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4176}
4177
4178SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004179 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004180 unsigned NumOps) {
4181 SDVTList VTs = getVTList(VT1, VT2);
4182 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4183}
4184
4185SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4186 MVT VT1, MVT VT2) {
4187 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004188 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004189}
4190
4191SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4192 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004193 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004194 SDVTList VTs = getVTList(VT1, VT2, VT3);
4195 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4196}
4197
4198SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4199 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004200 SDValue Op1) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004201 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004202 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004203 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4204}
4205
4206SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4207 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004208 SDValue Op1, SDValue Op2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004209 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004210 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004211 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4212}
4213
4214SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4215 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004216 SDValue Op1, SDValue Op2,
4217 SDValue Op3) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004218 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004219 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004220 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4221}
4222
4223/// MorphNodeTo - These *mutate* the specified node to have the specified
4224/// return type, opcode, and operands.
4225///
4226/// Note that MorphNodeTo returns the resultant node. If there is already a
4227/// node of the specified opcode and operands, it returns that node instead of
4228/// the current one.
4229///
4230/// Using MorphNodeTo is faster than creating a new node and swapping it in
4231/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif8b2235c2008-08-30 22:16:05 +00004232/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmanbd68c792008-07-17 19:10:17 +00004233/// the node's users.
4234///
4235SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004236 SDVTList VTs, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004237 unsigned NumOps) {
Dan Gohman7eced112008-07-02 23:23:19 +00004238 // If an identical node already exists, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004239 void *IP = 0;
Dan Gohmanbd68c792008-07-17 19:10:17 +00004240 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4241 FoldingSetNodeID ID;
4242 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4243 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4244 return ON;
4245 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246
Dan Gohman705e3f72008-09-13 01:54:27 +00004247 if (!RemoveNodeFromCSEMaps(N))
4248 IP = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004249
Dan Gohmanbd68c792008-07-17 19:10:17 +00004250 // Start the morphing.
4251 N->NodeType = Opc;
4252 N->ValueList = VTs.VTs;
4253 N->NumValues = VTs.NumVTs;
4254
4255 // Clear the operands list, updating used nodes to remove this from their
4256 // use list. Keep track of any operands that become dead as a result.
4257 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4258 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4259 I != E; ++I) {
4260 SDNode *Used = I->getVal();
4261 Used->removeUser(std::distance(B, I), N);
4262 if (Used->use_empty())
4263 DeadNodeSet.insert(Used);
4264 }
4265
4266 // If NumOps is larger than the # of operands we currently have, reallocate
4267 // the operand list.
4268 if (NumOps > N->NumOperands) {
4269 if (N->OperandsNeedDelete)
4270 delete[] N->OperandList;
Bill Wendlingb75ddd22008-10-19 20:51:12 +00004271
Dan Gohmanbd68c792008-07-17 19:10:17 +00004272 if (N->isMachineOpcode()) {
4273 // We're creating a final node that will live unmorphed for the
Dan Gohman14a66442008-08-23 02:25:05 +00004274 // remainder of the current SelectionDAG iteration, so we can allocate
4275 // the operands directly out of a pool with no recycling metadata.
4276 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004277 N->OperandsNeedDelete = false;
4278 } else {
4279 N->OperandList = new SDUse[NumOps];
4280 N->OperandsNeedDelete = true;
4281 }
4282 }
4283
4284 // Assign the new operands.
4285 N->NumOperands = NumOps;
4286 for (unsigned i = 0, e = NumOps; i != e; ++i) {
djg943376a2009-01-25 16:29:12 +00004287 N->OperandList[i].getSDValue() = Ops[i];
Dan Gohmanbd68c792008-07-17 19:10:17 +00004288 N->OperandList[i].setUser(N);
4289 SDNode *ToUse = N->OperandList[i].getVal();
4290 ToUse->addUser(i, N);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004291 }
4292
4293 // Delete any nodes that are still dead after adding the uses for the
4294 // new operands.
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004295 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmanbd68c792008-07-17 19:10:17 +00004296 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4297 E = DeadNodeSet.end(); I != E; ++I)
4298 if ((*I)->use_empty())
4299 DeadNodes.push_back(*I);
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004300 RemoveDeadNodes(DeadNodes);
4301
Dan Gohmanbd68c792008-07-17 19:10:17 +00004302 if (IP)
4303 CSEMap.InsertNode(N, IP); // Memoize the new node.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304 return N;
4305}
4306
4307
4308/// getTargetNode - These are used for target selectors to create a new node
4309/// with specified return type(s), target opcode, and operands.
4310///
4311/// Note that getTargetNode returns the resultant node. If there is already a
4312/// node of the specified opcode and operands, it returns that node instead of
4313/// the current one.
Duncan Sands92c43912008-06-06 12:08:01 +00004314SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004315 return getNode(~Opcode, VT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316}
Dan Gohman8181bd12008-07-27 21:46:04 +00004317SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004318 return getNode(~Opcode, VT, Op1).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004319}
Duncan Sands92c43912008-06-06 12:08:01 +00004320SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00004321 SDValue Op1, SDValue Op2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004322 return getNode(~Opcode, VT, Op1, Op2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323}
Duncan Sands92c43912008-06-06 12:08:01 +00004324SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00004325 SDValue Op1, SDValue Op2,
4326 SDValue Op3) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004327 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004328}
Duncan Sands92c43912008-06-06 12:08:01 +00004329SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00004330 const SDValue *Ops, unsigned NumOps) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004331 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332}
Duncan Sands92c43912008-06-06 12:08:01 +00004333SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4334 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004335 SDValue Op;
Gabor Greif1c80d112008-08-28 21:40:38 +00004336 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004337}
Duncan Sands92c43912008-06-06 12:08:01 +00004338SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004339 MVT VT2, SDValue Op1) {
Duncan Sands92c43912008-06-06 12:08:01 +00004340 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004341 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342}
Duncan Sands92c43912008-06-06 12:08:01 +00004343SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004344 MVT VT2, SDValue Op1,
4345 SDValue Op2) {
Duncan Sands92c43912008-06-06 12:08:01 +00004346 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004347 SDValue Ops[] = { Op1, Op2 };
Gabor Greif1c80d112008-08-28 21:40:38 +00004348 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004349}
Duncan Sands92c43912008-06-06 12:08:01 +00004350SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004351 MVT VT2, SDValue Op1,
4352 SDValue Op2, SDValue Op3) {
Duncan Sands92c43912008-06-06 12:08:01 +00004353 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004354 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greif1c80d112008-08-28 21:40:38 +00004355 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356}
Duncan Sands92c43912008-06-06 12:08:01 +00004357SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004358 const SDValue *Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00004359 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004360 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361}
Duncan Sands92c43912008-06-06 12:08:01 +00004362SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004363 SDValue Op1, SDValue Op2) {
Duncan Sands92c43912008-06-06 12:08:01 +00004364 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman8181bd12008-07-27 21:46:04 +00004365 SDValue Ops[] = { Op1, Op2 };
Gabor Greif1c80d112008-08-28 21:40:38 +00004366 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004367}
Duncan Sands92c43912008-06-06 12:08:01 +00004368SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004369 SDValue Op1, SDValue Op2,
4370 SDValue Op3) {
Duncan Sands92c43912008-06-06 12:08:01 +00004371 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman8181bd12008-07-27 21:46:04 +00004372 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greif1c80d112008-08-28 21:40:38 +00004373 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004374}
Duncan Sands92c43912008-06-06 12:08:01 +00004375SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004376 const SDValue *Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00004377 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Gabor Greif1c80d112008-08-28 21:40:38 +00004378 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004379}
Duncan Sands92c43912008-06-06 12:08:01 +00004380SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4381 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman8181bd12008-07-27 21:46:04 +00004382 const SDValue *Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00004383 std::vector<MVT> VTList;
Evan Chenge1d067e2007-09-12 23:39:49 +00004384 VTList.push_back(VT1);
4385 VTList.push_back(VT2);
4386 VTList.push_back(VT3);
4387 VTList.push_back(VT4);
Duncan Sands92c43912008-06-06 12:08:01 +00004388 const MVT *VTs = getNodeValueTypes(VTList);
Gabor Greif1c80d112008-08-28 21:40:38 +00004389 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Chenge1d067e2007-09-12 23:39:49 +00004390}
Evan Chenge3940912007-10-05 01:10:49 +00004391SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmana36422a2008-07-09 00:00:42 +00004392 const std::vector<MVT> &ResultTys,
Dan Gohman8181bd12008-07-27 21:46:04 +00004393 const SDValue *Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00004394 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004395 return getNode(~Opcode, VTs, ResultTys.size(),
Gabor Greif1c80d112008-08-28 21:40:38 +00004396 Ops, NumOps).getNode();
Evan Chenge3940912007-10-05 01:10:49 +00004397}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398
Evan Chengd1113582008-03-22 01:55:50 +00004399/// getNodeIfExists - Get the specified node if it's already available, or
4400/// else return NULL.
4401SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman8181bd12008-07-27 21:46:04 +00004402 const SDValue *Ops, unsigned NumOps) {
Evan Chengd1113582008-03-22 01:55:50 +00004403 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4404 FoldingSetNodeID ID;
4405 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4406 void *IP = 0;
4407 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4408 return E;
4409 }
4410 return NULL;
4411}
4412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4414/// This can cause recursive merging of nodes in the DAG.
4415///
Chris Lattnerdca329f2008-02-03 03:35:22 +00004416/// This version assumes From has a single result value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417///
Dan Gohman8181bd12008-07-27 21:46:04 +00004418void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004419 DAGUpdateListener *UpdateListener) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004420 SDNode *From = FromN.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00004421 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004422 "Cannot replace with this method!");
Gabor Greif1c80d112008-08-28 21:40:38 +00004423 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein05650fd2008-04-07 10:06:32 +00004424
djg943376a2009-01-25 16:29:12 +00004425 // Iterate over all the existing uses of From. New uses will be added
4426 // to the beginning of the use list, which we avoid visiting.
4427 // This specifically avoids visiting uses of From that arise while the
4428 // replacement is happening, because any such uses would be the result
4429 // of CSE: If an existing node looks like From after one of its operands
4430 // is replaced by To, we don't want to replace of all its users with To
4431 // too. See PR3018 for more info.
Dan Gohman86bf1392009-01-19 21:44:21 +00004432 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4433 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004434 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004435
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004437 RemoveNodeFromCSEMaps(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004438
djg943376a2009-01-25 16:29:12 +00004439 // A user can appear in a use list multiple times, and when this
4440 // happens the uses are usually next to each other in the list.
4441 // To help reduce the number of CSE recomputations, process all
4442 // the uses of this user that we can find this way.
4443 do {
4444 unsigned operandNum = UI.getOperandNo();
4445 ++UI;
4446 From->removeUser(operandNum, User);
4447 User->OperandList[operandNum].getSDValue() = To;
4448 To.getNode()->addUser(operandNum, User);
4449 } while (UI != UE && *UI == User);
4450
4451 // Now that we have modified User, add it back to the CSE maps. If it
4452 // already exists there, recursively merge the results together.
4453 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004454 }
4455}
4456
4457/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4458/// This can cause recursive merging of nodes in the DAG.
4459///
4460/// This version assumes From/To have matching types and numbers of result
4461/// values.
4462///
4463void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004464 DAGUpdateListener *UpdateListener) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004465 assert(From->getVTList().VTs == To->getVTList().VTs &&
4466 From->getNumValues() == To->getNumValues() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004467 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmanbd68c792008-07-17 19:10:17 +00004468
4469 // Handle the trivial case.
4470 if (From == To)
4471 return;
4472
Dan Gohman86bf1392009-01-19 21:44:21 +00004473 // Iterate over just the existing users of From. See the comments in
4474 // the ReplaceAllUsesWith above.
4475 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4476 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004477 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004478
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004480 RemoveNodeFromCSEMaps(User);
Roman Levenstein05650fd2008-04-07 10:06:32 +00004481
djg943376a2009-01-25 16:29:12 +00004482 // A user can appear in a use list multiple times, and when this
4483 // happens the uses are usually next to each other in the list.
4484 // To help reduce the number of CSE recomputations, process all
4485 // the uses of this user that we can find this way.
4486 do {
4487 unsigned operandNum = UI.getOperandNo();
4488 ++UI;
4489 From->removeUser(operandNum, User);
4490 User->OperandList[operandNum].getSDValue().setNode(To);
4491 To->addUser(operandNum, User);
4492 } while (UI != UE && *UI == User);
4493
4494 // Now that we have modified User, add it back to the CSE maps. If it
4495 // already exists there, recursively merge the results together.
4496 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 }
4498}
4499
4500/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4501/// This can cause recursive merging of nodes in the DAG.
4502///
4503/// This version can replace From with any result values. To must match the
4504/// number and types of values returned by From.
4505void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman8181bd12008-07-27 21:46:04 +00004506 const SDValue *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004507 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00004508 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman8181bd12008-07-27 21:46:04 +00004509 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510
Dan Gohman86bf1392009-01-19 21:44:21 +00004511 // Iterate over just the existing users of From. See the comments in
4512 // the ReplaceAllUsesWith above.
4513 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4514 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004515 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004516
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004518 RemoveNodeFromCSEMaps(User);
Roman Levenstein05650fd2008-04-07 10:06:32 +00004519
djg943376a2009-01-25 16:29:12 +00004520 // A user can appear in a use list multiple times, and when this
4521 // happens the uses are usually next to each other in the list.
4522 // To help reduce the number of CSE recomputations, process all
4523 // the uses of this user that we can find this way.
4524 do {
4525 unsigned operandNum = UI.getOperandNo();
4526 const SDValue &ToOp =
4527 To[User->OperandList[operandNum].getSDValue().getResNo()];
4528 ++UI;
4529 From->removeUser(operandNum, User);
4530 User->OperandList[operandNum].getSDValue() = ToOp;
4531 ToOp.getNode()->addUser(operandNum, User);
4532 } while (UI != UE && *UI == User);
4533
4534 // Now that we have modified User, add it back to the CSE maps. If it
4535 // already exists there, recursively merge the results together.
4536 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537 }
4538}
4539
4540/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Gabor Greif1c80d112008-08-28 21:40:38 +00004541/// uses of other values produced by From.getVal() alone. The Deleted vector is
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004542/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman8181bd12008-07-27 21:46:04 +00004543void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004544 DAGUpdateListener *UpdateListener){
Dan Gohmanbd68c792008-07-17 19:10:17 +00004545 // Handle the really simple, really trivial case efficiently.
4546 if (From == To) return;
4547
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004548 // Handle the simple, trivial, case efficiently.
Gabor Greif1c80d112008-08-28 21:40:38 +00004549 if (From.getNode()->getNumValues() == 1) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004550 ReplaceAllUsesWith(From, To, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551 return;
4552 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004553
djg943376a2009-01-25 16:29:12 +00004554 // Iterate over just the existing users of From. See the comments in
4555 // the ReplaceAllUsesWith above.
4556 SDNode::use_iterator UI = From.getNode()->use_begin(),
4557 UE = From.getNode()->use_end();
4558 while (UI != UE) {
4559 SDNode *User = *UI;
4560 bool UserRemovedFromCSEMaps = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561
djg943376a2009-01-25 16:29:12 +00004562 // A user can appear in a use list multiple times, and when this
4563 // happens the uses are usually next to each other in the list.
4564 // To help reduce the number of CSE recomputations, process all
4565 // the uses of this user that we can find this way.
4566 do {
4567 unsigned operandNum = UI.getOperandNo();
4568
4569 // Skip uses of different values from the same node.
4570 if (User->OperandList[operandNum].getSDValue().getResNo() !=
4571 From.getResNo()) {
4572 ++UI;
4573 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574 }
djg943376a2009-01-25 16:29:12 +00004575
4576 // If this node hasn't been modified yet, it's still in the CSE maps,
4577 // so remove its old self from the CSE maps.
4578 if (!UserRemovedFromCSEMaps) {
4579 RemoveNodeFromCSEMaps(User);
4580 UserRemovedFromCSEMaps = true;
4581 }
4582
4583 ++UI;
4584 From.getNode()->removeUser(operandNum, User);
4585 User->OperandList[operandNum].getSDValue() = To;
4586 To.getNode()->addUser(operandNum, User);
4587 } while (UI != UE && *UI == User);
4588
4589 // We are iterating over all uses of the From node, so if a use
4590 // doesn't use the specific value, no changes are made.
4591 if (!UserRemovedFromCSEMaps)
4592 continue;
4593
Chris Lattner8a258202007-10-15 06:10:22 +00004594 // Now that we have modified User, add it back to the CSE maps. If it
4595 // already exists there, recursively merge the results together.
djg943376a2009-01-25 16:29:12 +00004596 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004597 }
4598}
4599
djg943376a2009-01-25 16:29:12 +00004600namespace {
4601 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
4602 /// to record information about a use.
4603 struct UseMemo {
4604 SDNode *User;
4605 unsigned Index;
4606 unsigned operandNum;
4607 };
4608}
4609
Dan Gohmanbd68c792008-07-17 19:10:17 +00004610/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Gabor Greif1c80d112008-08-28 21:40:38 +00004611/// uses of other values produced by From.getVal() alone. The same value may
Dan Gohmanbd68c792008-07-17 19:10:17 +00004612/// appear in both the From and To list. The Deleted vector is
4613/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman8181bd12008-07-27 21:46:04 +00004614void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4615 const SDValue *To,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004616 unsigned Num,
4617 DAGUpdateListener *UpdateListener){
4618 // Handle the simple, trivial case efficiently.
4619 if (Num == 1)
4620 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4621
djg943376a2009-01-25 16:29:12 +00004622 // Read up all the uses and make records of them. This helps
4623 // processing new uses that are introduced during the
4624 // replacement process.
4625 SmallVector<UseMemo, 4> Uses;
4626 for (unsigned i = 0; i != Num; ++i) {
4627 unsigned FromResNo = From[i].getResNo();
4628 SDNode *FromNode = From[i].getNode();
4629 for (SDNode::use_iterator UI = FromNode->use_begin(),
4630 E = FromNode->use_end(); UI != E; ++UI)
4631 if (UI.getUse().getSDValue().getResNo() == FromResNo) {
4632 UseMemo Memo = { *UI, i, UI.getOperandNo() };
4633 Uses.push_back(Memo);
4634 }
4635 }
Dan Gohmanbd68c792008-07-17 19:10:17 +00004636
djg943376a2009-01-25 16:29:12 +00004637 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
4638 UseIndex != UseIndexEnd; ) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004639 // We know that this user uses some value of From. If it is the right
4640 // value, update it.
djg943376a2009-01-25 16:29:12 +00004641 SDNode *User = Uses[UseIndex].User;
4642
4643 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanbd68c792008-07-17 19:10:17 +00004644 RemoveNodeFromCSEMaps(User);
djg943376a2009-01-25 16:29:12 +00004645
4646 // A user can appear in a use list multiple times, and when this
4647 // happens the uses are usually next to each other in the list.
4648 // To help reduce the number of CSE recomputations, process all
4649 // the uses of this user that we can find this way.
4650 do {
4651 unsigned i = Uses[UseIndex].Index;
4652 unsigned operandNum = Uses[UseIndex].operandNum;
4653 ++UseIndex;
4654
4655 From[i].getNode()->removeUser(operandNum, User);
4656 User->OperandList[operandNum].getSDValue() = To[i];
4657 To[i].getNode()->addUser(operandNum, User);
4658 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
4659
Dan Gohmanbd68c792008-07-17 19:10:17 +00004660 // Now that we have modified User, add it back to the CSE maps. If it
4661 // already exists there, recursively merge the results together.
djg943376a2009-01-25 16:29:12 +00004662 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004663 }
4664}
4665
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
4667/// based on their topological order. It returns the maximum id and a vector
4668/// of the SDNodes* in assigned order by reference.
Dan Gohman2d2a7a32008-09-30 18:30:35 +00004669unsigned SelectionDAG::AssignTopologicalOrder() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670
Dan Gohman2d2a7a32008-09-30 18:30:35 +00004671 unsigned DAGSize = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004672
Dan Gohman2d2a7a32008-09-30 18:30:35 +00004673 // SortedPos tracks the progress of the algorithm. Nodes before it are
4674 // sorted, nodes after it are unsorted. When the algorithm completes
4675 // it is at the end of the list.
4676 allnodes_iterator SortedPos = allnodes_begin();
4677
Dan Gohman6e780312008-11-21 19:10:41 +00004678 // Visit all the nodes. Move nodes with no operands to the front of
4679 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman2d2a7a32008-09-30 18:30:35 +00004680 // operand count. Before we do this, the Node Id fields of the nodes
4681 // may contain arbitrary values. After, the Node Id fields for nodes
4682 // before SortedPos will contain the topological sort index, and the
4683 // Node Id fields for nodes At SortedPos and after will contain the
4684 // count of outstanding operands.
4685 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
4686 SDNode *N = I++;
4687 unsigned Degree = N->getNumOperands();
4688 if (Degree == 0) {
4689 // A node with no uses, add it to the result array immediately.
4690 N->setNodeId(DAGSize++);
4691 allnodes_iterator Q = N;
4692 if (Q != SortedPos)
4693 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
4694 ++SortedPos;
4695 } else {
4696 // Temporarily use the Node Id as scratch space for the degree count.
4697 N->setNodeId(Degree);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698 }
4699 }
4700
Dan Gohman2d2a7a32008-09-30 18:30:35 +00004701 // Visit all the nodes. As we iterate, moves nodes into sorted order,
4702 // such that by the time the end is reached all nodes will be sorted.
4703 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
4704 SDNode *N = I;
4705 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4706 UI != UE; ++UI) {
4707 SDNode *P = *UI;
4708 unsigned Degree = P->getNodeId();
4709 --Degree;
4710 if (Degree == 0) {
4711 // All of P's operands are sorted, so P may sorted now.
4712 P->setNodeId(DAGSize++);
4713 if (P != SortedPos)
4714 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
4715 ++SortedPos;
4716 } else {
4717 // Update P's outstanding operand count.
4718 P->setNodeId(Degree);
4719 }
4720 }
4721 }
4722
4723 assert(SortedPos == AllNodes.end() &&
4724 "Topological sort incomplete!");
4725 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
4726 "First node in topological sort is not the entry token!");
4727 assert(AllNodes.front().getNodeId() == 0 &&
4728 "First node in topological sort has non-zero id!");
4729 assert(AllNodes.front().getNumOperands() == 0 &&
4730 "First node in topological sort has operands!");
4731 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
4732 "Last node in topologic sort has unexpected id!");
4733 assert(AllNodes.back().use_empty() &&
4734 "Last node in topologic sort has users!");
Dan Gohman6e780312008-11-21 19:10:41 +00004735 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman2d2a7a32008-09-30 18:30:35 +00004736 return DAGSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004737}
4738
4739
4740
4741//===----------------------------------------------------------------------===//
4742// SDNode Class
4743//===----------------------------------------------------------------------===//
4744
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004745HandleSDNode::~HandleSDNode() {
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004746 DropOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747}
4748
4749GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Dan Gohman36322c72008-10-18 02:06:02 +00004750 MVT VT, int64_t o)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004751 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman53491e92007-07-23 20:24:29 +00004752 cast<GlobalVariable>(GA)->isThreadLocal() ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004753 // Thread Local
4754 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4755 // Non Thread Local
4756 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4757 getSDVTList(VT)), Offset(o) {
4758 TheGlobal = const_cast<GlobalValue*>(GA);
4759}
4760
Dan Gohman7ad1cca2008-07-09 22:08:04 +00004761MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman846947a2008-07-09 21:23:02 +00004762 const Value *srcValue, int SVO,
4763 unsigned alignment, bool vol)
Dan Gohman7ad1cca2008-07-09 22:08:04 +00004764 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohman98beebe2008-08-20 15:58:01 +00004765 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman846947a2008-07-09 21:23:02 +00004766
4767 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4768 assert(getAlignment() == alignment && "Alignment representation error!");
4769 assert(isVolatile() == vol && "Volatile representation error!");
4770}
4771
Mon P Wang89423382008-10-17 18:22:58 +00004772MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops,
4773 unsigned NumOps, MVT memvt, const Value *srcValue,
4774 int SVO, unsigned alignment, bool vol)
4775 : SDNode(Opc, VTs, Ops, NumOps),
4776 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
4777 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
4778 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4779 assert(getAlignment() == alignment && "Alignment representation error!");
4780 assert(isVolatile() == vol && "Volatile representation error!");
4781}
4782
Dan Gohman1fad9e62008-04-07 19:35:22 +00004783/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman7ad1cca2008-07-09 22:08:04 +00004784/// reference performed by this memory reference.
4785MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesen92434a52008-10-24 01:06:58 +00004786 int Flags = 0;
Dan Gohman7ad1cca2008-07-09 22:08:04 +00004787 if (isa<LoadSDNode>(this))
4788 Flags = MachineMemOperand::MOLoad;
4789 else if (isa<StoreSDNode>(this))
4790 Flags = MachineMemOperand::MOStore;
Mon P Wang89423382008-10-17 18:22:58 +00004791 else if (isa<AtomicSDNode>(this)) {
Dan Gohman7ad1cca2008-07-09 22:08:04 +00004792 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4793 }
Mon P Wang89423382008-10-17 18:22:58 +00004794 else {
4795 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
4796 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
4797 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
4798 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
4799 }
Dan Gohman7ad1cca2008-07-09 22:08:04 +00004800
4801 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004802 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4803
Dan Gohman7ad1cca2008-07-09 22:08:04 +00004804 // Check if the memory reference references a frame index
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004805 const FrameIndexSDNode *FI =
Gabor Greif1c80d112008-08-28 21:40:38 +00004806 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004807 if (!getSrcValue() && FI)
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004808 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4809 Flags, 0, Size, getAlignment());
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004810 else
4811 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4812 Size, getAlignment());
4813}
4814
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004815/// Profile - Gather unique data for the node.
4816///
Dan Gohman98beebe2008-08-20 15:58:01 +00004817void SDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818 AddNodeIDNode(ID, this);
4819}
4820
4821/// getValueTypeList - Return a pointer to the specified value type.
4822///
Duncan Sands92c43912008-06-06 12:08:01 +00004823const MVT *SDNode::getValueTypeList(MVT VT) {
4824 if (VT.isExtended()) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004825 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman8cdf7892008-02-08 03:26:46 +00004826 return &(*EVTs.insert(VT).first);
Duncan Sandsa9810f32007-10-16 09:56:48 +00004827 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00004828 static MVT VTs[MVT::LAST_VALUETYPE];
4829 VTs[VT.getSimpleVT()] = VT;
4830 return &VTs[VT.getSimpleVT()];
Duncan Sandsa9810f32007-10-16 09:56:48 +00004831 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004832}
Duncan Sandsa9810f32007-10-16 09:56:48 +00004833
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4835/// indicated value. This method ignores uses of other values defined by this
4836/// operation.
4837bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
4838 assert(Value < getNumValues() && "Bad value!");
4839
Roman Levenstein05650fd2008-04-07 10:06:32 +00004840 // TODO: Only iterate over uses of a given value of the node
4841 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif46bf5472008-08-26 22:36:50 +00004842 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00004843 if (NUses == 0)
4844 return false;
4845 --NUses;
4846 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847 }
4848
4849 // Found exactly the right number of uses?
4850 return NUses == 0;
4851}
4852
4853
Evan Cheng0af04f72007-08-02 05:29:38 +00004854/// hasAnyUseOfValue - Return true if there are any use of the indicated
4855/// value. This method ignores uses of other values defined by this operation.
4856bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4857 assert(Value < getNumValues() && "Bad value!");
4858
Dan Gohmana9651732008-07-09 22:39:01 +00004859 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif46bf5472008-08-26 22:36:50 +00004860 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohmana9651732008-07-09 22:39:01 +00004861 return true;
Evan Cheng0af04f72007-08-02 05:29:38 +00004862
4863 return false;
4864}
4865
4866
Dan Gohman07fadb02008-07-27 18:06:42 +00004867/// isOnlyUserOf - Return true if this node is the only use of N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868///
Dan Gohman07fadb02008-07-27 18:06:42 +00004869bool SDNode::isOnlyUserOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870 bool Seen = false;
4871 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004872 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 if (User == this)
4874 Seen = true;
4875 else
4876 return false;
4877 }
4878
4879 return Seen;
4880}
4881
4882/// isOperand - Return true if this node is an operand of N.
4883///
Dan Gohman8181bd12008-07-27 21:46:04 +00004884bool SDValue::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004885 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4886 if (*this == N->getOperand(i))
4887 return true;
4888 return false;
4889}
4890
Evan Chengd9387682008-03-04 00:41:45 +00004891bool SDNode::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004892 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004893 if (this == N->OperandList[i].getVal())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894 return true;
4895 return false;
4896}
4897
Chris Lattner10d94f92008-01-16 05:49:24 +00004898/// reachesChainWithoutSideEffects - Return true if this operand (which must
4899/// be a chain) reaches the specified operand without crossing any
4900/// side-effecting instructions. In practice, this looks through token
4901/// factors and non-volatile loads. In order to remain efficient, this only
4902/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman8181bd12008-07-27 21:46:04 +00004903bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner10d94f92008-01-16 05:49:24 +00004904 unsigned Depth) const {
4905 if (*this == Dest) return true;
4906
4907 // Don't search too deeply, we just want to be able to see through
4908 // TokenFactor's etc.
4909 if (Depth == 0) return false;
4910
4911 // If this is a token factor, all inputs to the TF happen in parallel. If any
4912 // of the operands of the TF reach dest, then we can do the xform.
4913 if (getOpcode() == ISD::TokenFactor) {
4914 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4915 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4916 return true;
4917 return false;
4918 }
4919
4920 // Loads don't have side effects, look through them.
4921 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4922 if (!Ld->isVolatile())
4923 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4924 }
4925 return false;
4926}
4927
4928
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004929static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
4930 SmallPtrSet<SDNode *, 32> &Visited) {
4931 if (found || !Visited.insert(N))
4932 return;
4933
4934 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004935 SDNode *Op = N->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004936 if (Op == P) {
4937 found = true;
4938 return;
4939 }
4940 findPredecessor(Op, P, found, Visited);
4941 }
4942}
4943
Evan Chengd9387682008-03-04 00:41:45 +00004944/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004945/// is either an operand of N or it can be reached by recursively traversing
4946/// up the operands.
4947/// NOTE: this is an expensive method. Use it carefully.
Evan Chengd9387682008-03-04 00:41:45 +00004948bool SDNode::isPredecessorOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004949 SmallPtrSet<SDNode *, 32> Visited;
4950 bool found = false;
4951 findPredecessor(N, this, found, Visited);
4952 return found;
4953}
4954
4955uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4956 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004957 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958}
4959
4960std::string SDNode::getOperationName(const SelectionDAG *G) const {
4961 switch (getOpcode()) {
4962 default:
4963 if (getOpcode() < ISD::BUILTIN_OP_END)
4964 return "<<Unknown DAG Node>>";
Dan Gohmanbd68c792008-07-17 19:10:17 +00004965 if (isMachineOpcode()) {
4966 if (G)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004967 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmanbd68c792008-07-17 19:10:17 +00004968 if (getMachineOpcode() < TII->getNumOpcodes())
4969 return TII->get(getMachineOpcode()).getName();
4970 return "<<Unknown Machine Node>>";
4971 }
4972 if (G) {
Dan Gohmana0c429e2009-01-15 16:58:17 +00004973 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmanbd68c792008-07-17 19:10:17 +00004974 const char *Name = TLI.getTargetNodeName(getOpcode());
4975 if (Name) return Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976 return "<<Unknown Target Node>>";
4977 }
Dan Gohmanbd68c792008-07-17 19:10:17 +00004978 return "<<Unknown Node>>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004979
Dan Gohmanbd68c792008-07-17 19:10:17 +00004980#ifndef NDEBUG
4981 case ISD::DELETED_NODE:
4982 return "<<Deleted Node!>>";
4983#endif
Evan Chengd1d68072008-03-08 00:58:38 +00004984 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth785610d2008-02-16 01:24:58 +00004985 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohmanbebba8d2008-12-23 21:37:04 +00004986 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4987 case ISD::ATOMIC_SWAP: return "AtomicSwap";
4988 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4989 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
4990 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4991 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4992 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
4993 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
4994 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4995 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4996 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4997 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004998 case ISD::PCMARKER: return "PCMarker";
4999 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
5000 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman12a9c082008-02-06 22:27:42 +00005001 case ISD::MEMOPERAND: return "MemOperand";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 case ISD::EntryToken: return "EntryToken";
5003 case ISD::TokenFactor: return "TokenFactor";
5004 case ISD::AssertSext: return "AssertSext";
5005 case ISD::AssertZext: return "AssertZext";
5006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005007 case ISD::BasicBlock: return "BasicBlock";
Duncan Sandsc93fae32008-03-21 09:14:45 +00005008 case ISD::ARG_FLAGS: return "ArgFlags";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 case ISD::VALUETYPE: return "ValueType";
5010 case ISD::Register: return "Register";
5011
5012 case ISD::Constant: return "Constant";
5013 case ISD::ConstantFP: return "ConstantFP";
5014 case ISD::GlobalAddress: return "GlobalAddress";
5015 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
5016 case ISD::FrameIndex: return "FrameIndex";
5017 case ISD::JumpTable: return "JumpTable";
5018 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendlingfef06052008-09-16 21:48:12 +00005019 case ISD::RETURNADDR: return "RETURNADDR";
5020 case ISD::FRAMEADDR: return "FRAMEADDR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005021 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
5022 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendlingfef06052008-09-16 21:48:12 +00005023 case ISD::EHSELECTION: return "EHSELECTION";
5024 case ISD::EH_RETURN: return "EH_RETURN";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005025 case ISD::ConstantPool: return "ConstantPool";
Bill Wendlingfef06052008-09-16 21:48:12 +00005026 case ISD::ExternalSymbol: return "ExternalSymbol";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005028 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005029 return Intrinsic::getName((Intrinsic::ID)IID);
5030 }
5031 case ISD::INTRINSIC_VOID:
5032 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005033 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034 return Intrinsic::getName((Intrinsic::ID)IID);
5035 }
5036
5037 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
5038 case ISD::TargetConstant: return "TargetConstant";
5039 case ISD::TargetConstantFP:return "TargetConstantFP";
5040 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
5041 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
5042 case ISD::TargetFrameIndex: return "TargetFrameIndex";
5043 case ISD::TargetJumpTable: return "TargetJumpTable";
5044 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendlingfef06052008-09-16 21:48:12 +00005045 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005046
5047 case ISD::CopyToReg: return "CopyToReg";
5048 case ISD::CopyFromReg: return "CopyFromReg";
5049 case ISD::UNDEF: return "undef";
5050 case ISD::MERGE_VALUES: return "merge_values";
5051 case ISD::INLINEASM: return "inlineasm";
Dan Gohmanfa607c92008-07-01 00:05:16 +00005052 case ISD::DBG_LABEL: return "dbg_label";
5053 case ISD::EH_LABEL: return "eh_label";
Evan Cheng2e28d622008-02-02 04:07:54 +00005054 case ISD::DECLARE: return "declare";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055 case ISD::HANDLENODE: return "handlenode";
5056 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
5057 case ISD::CALL: return "call";
5058
5059 // Unary operators
5060 case ISD::FABS: return "fabs";
5061 case ISD::FNEG: return "fneg";
5062 case ISD::FSQRT: return "fsqrt";
5063 case ISD::FSIN: return "fsin";
5064 case ISD::FCOS: return "fcos";
5065 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00005066 case ISD::FPOW: return "fpow";
Dan Gohmanc8b20e22008-08-21 17:55:02 +00005067 case ISD::FTRUNC: return "ftrunc";
5068 case ISD::FFLOOR: return "ffloor";
5069 case ISD::FCEIL: return "fceil";
5070 case ISD::FRINT: return "frint";
5071 case ISD::FNEARBYINT: return "fnearbyint";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005072
5073 // Binary operators
5074 case ISD::ADD: return "add";
5075 case ISD::SUB: return "sub";
5076 case ISD::MUL: return "mul";
5077 case ISD::MULHU: return "mulhu";
5078 case ISD::MULHS: return "mulhs";
5079 case ISD::SDIV: return "sdiv";
5080 case ISD::UDIV: return "udiv";
5081 case ISD::SREM: return "srem";
5082 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00005083 case ISD::SMUL_LOHI: return "smul_lohi";
5084 case ISD::UMUL_LOHI: return "umul_lohi";
5085 case ISD::SDIVREM: return "sdivrem";
Dan Gohman2e66b052008-09-08 16:30:29 +00005086 case ISD::UDIVREM: return "udivrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087 case ISD::AND: return "and";
5088 case ISD::OR: return "or";
5089 case ISD::XOR: return "xor";
5090 case ISD::SHL: return "shl";
5091 case ISD::SRA: return "sra";
5092 case ISD::SRL: return "srl";
5093 case ISD::ROTL: return "rotl";
5094 case ISD::ROTR: return "rotr";
5095 case ISD::FADD: return "fadd";
5096 case ISD::FSUB: return "fsub";
5097 case ISD::FMUL: return "fmul";
5098 case ISD::FDIV: return "fdiv";
5099 case ISD::FREM: return "frem";
5100 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00005101 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005102
5103 case ISD::SETCC: return "setcc";
Nate Begeman9a1ce152008-05-12 19:40:03 +00005104 case ISD::VSETCC: return "vsetcc";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005105 case ISD::SELECT: return "select";
5106 case ISD::SELECT_CC: return "select_cc";
5107 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
5108 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
5109 case ISD::CONCAT_VECTORS: return "concat_vectors";
5110 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
5111 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
5112 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
5113 case ISD::CARRY_FALSE: return "carry_false";
5114 case ISD::ADDC: return "addc";
5115 case ISD::ADDE: return "adde";
Bill Wendlingea8b7c92008-11-21 02:12:42 +00005116 case ISD::SADDO: return "saddo";
5117 case ISD::UADDO: return "uaddo";
Bill Wendling7e04be62008-12-09 22:08:41 +00005118 case ISD::SSUBO: return "ssubo";
5119 case ISD::USUBO: return "usubo";
5120 case ISD::SMULO: return "smulo";
5121 case ISD::UMULO: return "umulo";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005122 case ISD::SUBC: return "subc";
5123 case ISD::SUBE: return "sube";
5124 case ISD::SHL_PARTS: return "shl_parts";
5125 case ISD::SRA_PARTS: return "sra_parts";
5126 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lambb768c2e2007-07-26 07:34:40 +00005127
5128 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5129 case ISD::INSERT_SUBREG: return "insert_subreg";
5130
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005131 // Conversion operators.
5132 case ISD::SIGN_EXTEND: return "sign_extend";
5133 case ISD::ZERO_EXTEND: return "zero_extend";
5134 case ISD::ANY_EXTEND: return "any_extend";
5135 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
5136 case ISD::TRUNCATE: return "truncate";
5137 case ISD::FP_ROUND: return "fp_round";
Dan Gohman819574c2008-01-31 00:41:03 +00005138 case ISD::FLT_ROUNDS_: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005139 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
5140 case ISD::FP_EXTEND: return "fp_extend";
5141
5142 case ISD::SINT_TO_FP: return "sint_to_fp";
5143 case ISD::UINT_TO_FP: return "uint_to_fp";
5144 case ISD::FP_TO_SINT: return "fp_to_sint";
5145 case ISD::FP_TO_UINT: return "fp_to_uint";
5146 case ISD::BIT_CONVERT: return "bit_convert";
Mon P Wang73d31542008-11-10 20:54:11 +00005147
5148 case ISD::CONVERT_RNDSAT: {
5149 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
5150 default: assert(0 && "Unknown cvt code!");
5151 case ISD::CVT_FF: return "cvt_ff";
5152 case ISD::CVT_FS: return "cvt_fs";
5153 case ISD::CVT_FU: return "cvt_fu";
5154 case ISD::CVT_SF: return "cvt_sf";
5155 case ISD::CVT_UF: return "cvt_uf";
5156 case ISD::CVT_SS: return "cvt_ss";
5157 case ISD::CVT_SU: return "cvt_su";
5158 case ISD::CVT_US: return "cvt_us";
5159 case ISD::CVT_UU: return "cvt_uu";
5160 }
5161 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005162
5163 // Control flow instructions
5164 case ISD::BR: return "br";
5165 case ISD::BRIND: return "brind";
5166 case ISD::BR_JT: return "br_jt";
5167 case ISD::BRCOND: return "brcond";
5168 case ISD::BR_CC: return "br_cc";
5169 case ISD::RET: return "ret";
5170 case ISD::CALLSEQ_START: return "callseq_start";
5171 case ISD::CALLSEQ_END: return "callseq_end";
5172
5173 // Other operators
5174 case ISD::LOAD: return "load";
5175 case ISD::STORE: return "store";
5176 case ISD::VAARG: return "vaarg";
5177 case ISD::VACOPY: return "vacopy";
5178 case ISD::VAEND: return "vaend";
5179 case ISD::VASTART: return "vastart";
5180 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
5181 case ISD::EXTRACT_ELEMENT: return "extract_element";
5182 case ISD::BUILD_PAIR: return "build_pair";
5183 case ISD::STACKSAVE: return "stacksave";
5184 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00005185 case ISD::TRAP: return "trap";
5186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005187 // Bit manipulation
5188 case ISD::BSWAP: return "bswap";
5189 case ISD::CTPOP: return "ctpop";
5190 case ISD::CTTZ: return "cttz";
5191 case ISD::CTLZ: return "ctlz";
5192
5193 // Debug info
Dan Gohman472d12c2008-06-30 20:59:49 +00005194 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005195 case ISD::DEBUG_LOC: return "debug_loc";
5196
Duncan Sands38947cd2007-07-27 12:58:54 +00005197 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00005198 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00005199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005200 case ISD::CONDCODE:
5201 switch (cast<CondCodeSDNode>(this)->get()) {
5202 default: assert(0 && "Unknown setcc condition!");
5203 case ISD::SETOEQ: return "setoeq";
5204 case ISD::SETOGT: return "setogt";
5205 case ISD::SETOGE: return "setoge";
5206 case ISD::SETOLT: return "setolt";
5207 case ISD::SETOLE: return "setole";
5208 case ISD::SETONE: return "setone";
5209
5210 case ISD::SETO: return "seto";
5211 case ISD::SETUO: return "setuo";
5212 case ISD::SETUEQ: return "setue";
5213 case ISD::SETUGT: return "setugt";
5214 case ISD::SETUGE: return "setuge";
5215 case ISD::SETULT: return "setult";
5216 case ISD::SETULE: return "setule";
5217 case ISD::SETUNE: return "setune";
5218
5219 case ISD::SETEQ: return "seteq";
5220 case ISD::SETGT: return "setgt";
5221 case ISD::SETGE: return "setge";
5222 case ISD::SETLT: return "setlt";
5223 case ISD::SETLE: return "setle";
5224 case ISD::SETNE: return "setne";
5225 }
5226 }
5227}
5228
5229const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
5230 switch (AM) {
5231 default:
5232 return "";
5233 case ISD::PRE_INC:
5234 return "<pre-inc>";
5235 case ISD::PRE_DEC:
5236 return "<pre-dec>";
5237 case ISD::POST_INC:
5238 return "<post-inc>";
5239 case ISD::POST_DEC:
5240 return "<post-dec>";
5241 }
5242}
5243
Duncan Sandsc93fae32008-03-21 09:14:45 +00005244std::string ISD::ArgFlagsTy::getArgFlagsString() {
5245 std::string S = "< ";
5246
5247 if (isZExt())
5248 S += "zext ";
5249 if (isSExt())
5250 S += "sext ";
5251 if (isInReg())
5252 S += "inreg ";
5253 if (isSRet())
5254 S += "sret ";
5255 if (isByVal())
5256 S += "byval ";
5257 if (isNest())
5258 S += "nest ";
5259 if (getByValAlign())
5260 S += "byval-align:" + utostr(getByValAlign()) + " ";
5261 if (getOrigAlign())
5262 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5263 if (getByValSize())
5264 S += "byval-size:" + utostr(getByValSize()) + " ";
5265 return S + ">";
5266}
5267
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005268void SDNode::dump() const { dump(0); }
5269void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005270 print(errs(), G);
Chris Lattnerb7876202008-08-24 18:28:30 +00005271 errs().flush();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005272}
5273
5274void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5275 OS << (void*)this << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005276
5277 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005278 if (i) OS << ",";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005279 if (getValueType(i) == MVT::Other)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005280 OS << "ch";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005281 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005282 OS << getValueType(i).getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005283 }
Chris Lattner1fefaac2008-08-23 22:23:09 +00005284 OS << " = " << getOperationName(G);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005285
Chris Lattner1fefaac2008-08-23 22:23:09 +00005286 OS << " ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005287 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005288 if (i) OS << ", ";
Gabor Greif1c80d112008-08-28 21:40:38 +00005289 OS << (void*)getOperand(i).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00005290 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005291 OS << ":" << RN;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005292 }
5293
Evan Chengaad43a02007-12-11 02:08:35 +00005294 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005295 SDNode *Mask = getOperand(2).getNode();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005296 OS << "<";
Evan Chengaad43a02007-12-11 02:08:35 +00005297 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005298 if (i) OS << ",";
Evan Chengaad43a02007-12-11 02:08:35 +00005299 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005300 OS << "u";
Evan Chengaad43a02007-12-11 02:08:35 +00005301 else
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005302 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengaad43a02007-12-11 02:08:35 +00005303 }
Chris Lattner1fefaac2008-08-23 22:23:09 +00005304 OS << ">";
Evan Chengaad43a02007-12-11 02:08:35 +00005305 }
5306
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005307 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005308 OS << '<' << CSDN->getAPIntValue() << '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00005310 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005311 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen2fc20782007-09-14 22:26:36 +00005312 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005313 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen2fc20782007-09-14 22:26:36 +00005314 else {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005315 OS << "<APFloat(";
Dale Johannesen6e547b42008-10-09 23:00:39 +00005316 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005317 OS << ")>";
Dale Johannesen2fc20782007-09-14 22:26:36 +00005318 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005319 } else if (const GlobalAddressSDNode *GADN =
5320 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohmancfe43232008-10-18 18:22:42 +00005321 int64_t offset = GADN->getOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005322 OS << '<';
5323 WriteAsOperand(OS, GADN->getGlobal());
5324 OS << '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 if (offset > 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005326 OS << " + " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005328 OS << " " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005330 OS << "<" << FIDN->getIndex() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005331 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005332 OS << "<" << JTDN->getIndex() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005333 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
5334 int offset = CP->getOffset();
5335 if (CP->isMachineConstantPoolEntry())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005336 OS << "<" << *CP->getMachineCPVal() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005337 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005338 OS << "<" << *CP->getConstVal() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005339 if (offset > 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005340 OS << " + " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005341 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005342 OS << " " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005343 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005344 OS << "<";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005345 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5346 if (LBB)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005347 OS << LBB->getName() << " ";
5348 OS << (const void*)BBDN->getBasicBlock() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman1e57df32008-02-10 18:45:23 +00005350 if (G && R->getReg() &&
5351 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005352 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005353 } else {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005354 OS << " #" << R->getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005355 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005356 } else if (const ExternalSymbolSDNode *ES =
5357 dyn_cast<ExternalSymbolSDNode>(this)) {
5358 OS << "'" << ES->getSymbol() << "'";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005359 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5360 if (M->getValue())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005361 OS << "<" << M->getValue() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005362 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005363 OS << "<null>";
Dan Gohman12a9c082008-02-06 22:27:42 +00005364 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5365 if (M->MO.getValue())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005366 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman12a9c082008-02-06 22:27:42 +00005367 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005368 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sandsc93fae32008-03-21 09:14:45 +00005369 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005370 OS << N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005371 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005372 OS << ":" << N->getVT().getMVTString();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005373 }
5374 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng034c4f82007-12-18 19:06:30 +00005375 const Value *SrcValue = LD->getSrcValue();
5376 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005377 OS << " <";
Evan Cheng034c4f82007-12-18 19:06:30 +00005378 if (SrcValue)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005379 OS << SrcValue;
Evan Cheng034c4f82007-12-18 19:06:30 +00005380 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005381 OS << "null";
5382 OS << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00005383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384 bool doExt = true;
5385 switch (LD->getExtensionType()) {
5386 default: doExt = false; break;
Chris Lattner1fefaac2008-08-23 22:23:09 +00005387 case ISD::EXTLOAD: OS << " <anyext "; break;
5388 case ISD::SEXTLOAD: OS << " <sext "; break;
5389 case ISD::ZEXTLOAD: OS << " <zext "; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005390 }
5391 if (doExt)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005392 OS << LD->getMemoryVT().getMVTString() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005393
5394 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00005395 if (*AM)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005396 OS << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00005397 if (LD->isVolatile())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005398 OS << " <volatile>";
5399 OS << " alignment=" << LD->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005400 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng7196a7b2007-12-18 07:02:08 +00005401 const Value *SrcValue = ST->getSrcValue();
5402 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005403 OS << " <";
Evan Cheng7196a7b2007-12-18 07:02:08 +00005404 if (SrcValue)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005405 OS << SrcValue;
Evan Cheng7196a7b2007-12-18 07:02:08 +00005406 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005407 OS << "null";
5408 OS << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00005409
5410 if (ST->isTruncatingStore())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005411 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00005412
5413 const char *AM = getIndexedModeName(ST->getAddressingMode());
5414 if (*AM)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005415 OS << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00005416 if (ST->isVolatile())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005417 OS << " <volatile>";
5418 OS << " alignment=" << ST->getAlignment();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005419 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5420 const Value *SrcValue = AT->getSrcValue();
5421 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005422 OS << " <";
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005423 if (SrcValue)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005424 OS << SrcValue;
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005425 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005426 OS << "null";
5427 OS << ":" << SrcOffset << ">";
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005428 if (AT->isVolatile())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005429 OS << " <volatile>";
5430 OS << " alignment=" << AT->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005431 }
5432}
5433
5434static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
5435 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00005436 if (N->getOperand(i).getNode()->hasOneUse())
5437 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005438 else
5439 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greif1c80d112008-08-28 21:40:38 +00005440 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005441
5442
5443 cerr << "\n" << std::string(indent, ' ');
5444 N->dump(G);
5445}
5446
5447void SelectionDAG::dump() const {
5448 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449
Dan Gohman5fafd422008-07-15 18:18:54 +00005450 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5451 I != E; ++I) {
5452 const SDNode *N = I;
Gabor Greif1c80d112008-08-28 21:40:38 +00005453 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman5fafd422008-07-15 18:18:54 +00005454 DumpNodes(N, 2, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455 }
5456
Gabor Greif1c80d112008-08-28 21:40:38 +00005457 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458
5459 cerr << "\n\n";
5460}
5461
5462const Type *ConstantPoolSDNode::getType() const {
5463 if (isMachineConstantPoolEntry())
5464 return Val.MachineCPVal->getType();
5465 return Val.ConstVal->getType();
5466}