blob: fa32aabf6b039b04d59727a29d1c5e392c0c96f0 [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) {
djgc2517d32009-01-26 04:35:06 +0000345 ID.AddPointer(Ops->getNode());
346 ID.AddInteger(Ops->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);
Duncan Sands3f5d2432008-06-06 12:49:32 +0000432 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +0000433 ID.AddInteger(LD->getRawSubclassData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 break;
435 }
436 case ISD::STORE: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000437 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3f5d2432008-06-06 12:49:32 +0000438 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +0000439 ID.AddInteger(ST->getRawSubclassData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 break;
441 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000442 case ISD::ATOMIC_CMP_SWAP:
443 case ISD::ATOMIC_SWAP:
444 case ISD::ATOMIC_LOAD_ADD:
445 case ISD::ATOMIC_LOAD_SUB:
446 case ISD::ATOMIC_LOAD_AND:
447 case ISD::ATOMIC_LOAD_OR:
448 case ISD::ATOMIC_LOAD_XOR:
449 case ISD::ATOMIC_LOAD_NAND:
450 case ISD::ATOMIC_LOAD_MIN:
451 case ISD::ATOMIC_LOAD_MAX:
452 case ISD::ATOMIC_LOAD_UMIN:
453 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000454 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman9d0f5022009-02-03 00:08:45 +0000455 ID.AddInteger(AT->getMemoryVT().getRawBits());
456 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000457 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000459 } // end switch (N->getOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460}
461
Duncan Sands1e450d42008-10-27 15:30:53 +0000462/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
463/// data.
464static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
465 AddNodeIDOpcode(ID, N->getOpcode());
466 // Add the return value info.
467 AddNodeIDValueTypes(ID, N->getVTList());
468 // Add the operand info.
469 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
470
471 // Handle SDNode leafs with special info.
472 AddNodeIDCustom(ID, N);
473}
474
Dan Gohman98beebe2008-08-20 15:58:01 +0000475/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
Dan Gohman9d0f5022009-02-03 00:08:45 +0000476/// the CSE map that carries alignment, volatility, indexing mode, and
477/// extension/truncation information.
Dan Gohman98beebe2008-08-20 15:58:01 +0000478///
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000479static inline unsigned
Dan Gohman9d0f5022009-02-03 00:08:45 +0000480encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM,
481 bool isVolatile, unsigned Alignment) {
482 assert((ConvType & 3) == ConvType &&
483 "ConvType may not require more than 2 bits!");
484 assert((AM & 7) == AM &&
485 "AM may not require more than 3 bits!");
486 return ConvType |
487 (AM << 2) |
488 (isVolatile << 5) |
489 ((Log2_32(Alignment) + 1) << 6);
Dan Gohman98beebe2008-08-20 15:58:01 +0000490}
491
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492//===----------------------------------------------------------------------===//
493// SelectionDAG Class
494//===----------------------------------------------------------------------===//
495
Duncan Sands1e450d42008-10-27 15:30:53 +0000496/// doNotCSE - Return true if CSE should not be performed for this node.
497static bool doNotCSE(SDNode *N) {
498 if (N->getValueType(0) == MVT::Flag)
499 return true; // Never CSE anything that produces a flag.
500
501 switch (N->getOpcode()) {
502 default: break;
503 case ISD::HANDLENODE:
504 case ISD::DBG_LABEL:
505 case ISD::DBG_STOPPOINT:
506 case ISD::EH_LABEL:
507 case ISD::DECLARE:
508 return true; // Never CSE these nodes.
509 }
510
511 // Check that remaining values produced are not flags.
512 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
513 if (N->getValueType(i) == MVT::Flag)
514 return true; // Never CSE anything that produces a flag.
515
516 return false;
517}
518
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519/// RemoveDeadNodes - This method deletes all unreachable nodes in the
520/// SelectionDAG.
521void SelectionDAG::RemoveDeadNodes() {
522 // Create a dummy node (which is not added to allnodes), that adds a reference
523 // to the root node, preventing it from being deleted.
524 HandleSDNode Dummy(getRoot());
525
526 SmallVector<SDNode*, 128> DeadNodes;
527
528 // Add all obviously-dead nodes to the DeadNodes worklist.
529 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
530 if (I->use_empty())
531 DeadNodes.push_back(I);
532
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000533 RemoveDeadNodes(DeadNodes);
534
535 // If the root changed (e.g. it was a dead load, update the root).
536 setRoot(Dummy.getValue());
537}
538
539/// RemoveDeadNodes - This method deletes the unreachable nodes in the
540/// given list, and any nodes that become unreachable as a result.
541void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
542 DAGUpdateListener *UpdateListener) {
543
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 // Process the worklist, deleting the nodes and adding their uses to the
545 // worklist.
546 while (!DeadNodes.empty()) {
djgc2517d32009-01-26 04:35:06 +0000547 SDNode *N = DeadNodes.pop_back_val();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000549 if (UpdateListener)
550 UpdateListener->NodeDeleted(N, 0);
551
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 // Take the node out of the appropriate CSE map.
553 RemoveNodeFromCSEMaps(N);
554
555 // Next, brutally remove the operand list. This is safe to do, as there are
556 // no cycles in the graph.
djgc2517d32009-01-26 04:35:06 +0000557 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
558 SDUse &Use = *I++;
559 SDNode *Operand = Use.getNode();
560 Use.set(SDValue());
561
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 // Now that we removed this operand, see if there are no uses of it left.
563 if (Operand->use_empty())
564 DeadNodes.push_back(Operand);
565 }
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000566
Dan Gohman98a355c2009-01-19 22:39:36 +0000567 DeallocateNode(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569}
570
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000571void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmanbd68c792008-07-17 19:10:17 +0000572 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000573 RemoveDeadNodes(DeadNodes, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574}
575
576void SelectionDAG::DeleteNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577 // First take this out of the appropriate CSE map.
578 RemoveNodeFromCSEMaps(N);
579
580 // Finally, remove uses due to operands of this node, remove from the
581 // AllNodes list, and delete the node.
582 DeleteNodeNotInCSEMaps(N);
583}
584
585void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
djgf03abfe2009-01-25 16:20:37 +0000586 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
587 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman98a355c2009-01-19 22:39:36 +0000588
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000589 // Drop all of the operands and decrement used node's use counts.
djgc2517d32009-01-26 04:35:06 +0000590 N->DropOperands();
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000591
Dan Gohman98a355c2009-01-19 22:39:36 +0000592 DeallocateNode(N);
593}
594
595void SelectionDAG::DeallocateNode(SDNode *N) {
596 if (N->OperandsNeedDelete)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 delete[] N->OperandList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598
Dan Gohman98a355c2009-01-19 22:39:36 +0000599 // Set the opcode to DELETED_NODE to help catch bugs when node
600 // memory is reallocated.
601 N->NodeType = ISD::DELETED_NODE;
602
Dan Gohman0b08b152008-08-26 01:44:34 +0000603 NodeAllocator.Deallocate(AllNodes.remove(N));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604}
605
606/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
607/// correspond to it. This is useful when we're about to delete or repurpose
608/// the node. We don't want future request for structurally identical nodes
609/// to return N anymore.
Dan Gohman705e3f72008-09-13 01:54:27 +0000610bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 bool Erased = false;
612 switch (N->getOpcode()) {
Dan Gohman14a66442008-08-23 02:25:05 +0000613 case ISD::EntryToken:
614 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman705e3f72008-09-13 01:54:27 +0000615 return false;
616 case ISD::HANDLENODE: return false; // noop.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 case ISD::CONDCODE:
618 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
619 "Cond code doesn't exist!");
620 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
621 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
622 break;
Bill Wendlingfef06052008-09-16 21:48:12 +0000623 case ISD::ExternalSymbol:
624 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 break;
Bill Wendlingfef06052008-09-16 21:48:12 +0000626 case ISD::TargetExternalSymbol:
627 Erased =
628 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000630 case ISD::VALUETYPE: {
Duncan Sands92c43912008-06-06 12:08:01 +0000631 MVT VT = cast<VTSDNode>(N)->getVT();
632 if (VT.isExtended()) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000633 Erased = ExtendedValueTypeNodes.erase(VT);
634 } else {
Duncan Sands92c43912008-06-06 12:08:01 +0000635 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
636 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000637 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000639 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 default:
641 // Remove it from the CSE Map.
642 Erased = CSEMap.RemoveNode(N);
643 break;
644 }
645#ifndef NDEBUG
646 // Verify that the node was actually in one of the CSE maps, unless it has a
647 // flag result (which cannot be CSE'd) or is one of the special cases that are
648 // not subject to CSE.
649 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands1e450d42008-10-27 15:30:53 +0000650 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 N->dump(this);
652 cerr << "\n";
653 assert(0 && "Node is not in map!");
654 }
655#endif
Dan Gohman705e3f72008-09-13 01:54:27 +0000656 return Erased;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657}
658
djg943376a2009-01-25 16:29:12 +0000659/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
660/// maps and modified in place. Add it back to the CSE maps, unless an identical
661/// node already exists, in which case transfer all its users to the existing
662/// node. This transfer can potentially trigger recursive merging.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663///
djg943376a2009-01-25 16:29:12 +0000664void
665SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
666 DAGUpdateListener *UpdateListener) {
667 // For node types that aren't CSE'd, just act as if no identical node
668 // already exists.
669 if (!doNotCSE(N)) {
670 SDNode *Existing = CSEMap.GetOrInsertNode(N);
671 if (Existing != N) {
672 // If there was already an existing matching node, use ReplaceAllUsesWith
673 // to replace the dead one with the existing one. This can cause
674 // recursive merging of other unrelated nodes down the line.
675 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Chengd6f57682008-07-08 20:06:39 +0000676
djg943376a2009-01-25 16:29:12 +0000677 // N is now dead. Inform the listener if it exists and delete it.
678 if (UpdateListener)
679 UpdateListener->NodeDeleted(N, Existing);
680 DeleteNodeNotInCSEMaps(N);
681 return;
682 }
683 }
Evan Chengd6f57682008-07-08 20:06:39 +0000684
djg943376a2009-01-25 16:29:12 +0000685 // If the node doesn't already exist, we updated it. Inform a listener if
686 // it exists.
687 if (UpdateListener)
688 UpdateListener->NodeUpdated(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689}
690
691/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
692/// were replaced with those specified. If this node is never memoized,
693/// return null, otherwise return a pointer to the slot it would take. If a
694/// node already exists with these operands, the slot will be non-null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000695SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000697 if (doNotCSE(N))
698 return 0;
Evan Chengd6f57682008-07-08 20:06:39 +0000699
Dan Gohman8181bd12008-07-27 21:46:04 +0000700 SDValue Ops[] = { Op };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 FoldingSetNodeID ID;
702 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands1e450d42008-10-27 15:30:53 +0000703 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
705}
706
707/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
708/// were replaced with those specified. If this node is never memoized,
709/// return null, otherwise return a pointer to the slot it would take. If a
710/// node already exists with these operands, the slot will be non-null.
711SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000712 SDValue Op1, SDValue Op2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000714 if (doNotCSE(N))
715 return 0;
716
Dan Gohman8181bd12008-07-27 21:46:04 +0000717 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 FoldingSetNodeID ID;
719 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands1e450d42008-10-27 15:30:53 +0000720 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
722}
723
724
725/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
726/// were replaced with those specified. If this node is never memoized,
727/// return null, otherwise return a pointer to the slot it would take. If a
728/// node already exists with these operands, the slot will be non-null.
729SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000730 const SDValue *Ops,unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000732 if (doNotCSE(N))
733 return 0;
Evan Chengd6f57682008-07-08 20:06:39 +0000734
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 FoldingSetNodeID ID;
736 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands1e450d42008-10-27 15:30:53 +0000737 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
739}
740
Duncan Sandsd3ace282008-07-21 10:20:31 +0000741/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
742void SelectionDAG::VerifyNode(SDNode *N) {
743 switch (N->getOpcode()) {
744 default:
745 break;
Duncan Sands70269a72008-10-29 14:22:20 +0000746 case ISD::BUILD_PAIR: {
747 MVT VT = N->getValueType(0);
748 assert(N->getNumValues() == 1 && "Too many results!");
749 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
750 "Wrong return type!");
751 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
752 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
753 "Mismatched operand types!");
754 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
755 "Wrong operand type!");
756 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
757 "Wrong return type size");
758 break;
759 }
Duncan Sandsd3ace282008-07-21 10:20:31 +0000760 case ISD::BUILD_VECTOR: {
Duncan Sands70269a72008-10-29 14:22:20 +0000761 assert(N->getNumValues() == 1 && "Too many results!");
762 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd3ace282008-07-21 10:20:31 +0000763 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands70269a72008-10-29 14:22:20 +0000764 "Wrong number of operands!");
Duncan Sands944d0d52008-10-22 09:00:33 +0000765 // FIXME: Change vector_shuffle to a variadic node with mask elements being
766 // operands of the node. Currently the mask is a BUILD_VECTOR passed as an
767 // operand, and it is not always possible to legalize it. Turning off the
768 // following checks at least makes it possible to legalize most of the time.
769// MVT EltVT = N->getValueType(0).getVectorElementType();
770// for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
djgc2517d32009-01-26 04:35:06 +0000771// assert(I->getValueType() == EltVT &&
Duncan Sands70269a72008-10-29 14:22:20 +0000772// "Wrong operand type!");
Duncan Sandsd3ace282008-07-21 10:20:31 +0000773 break;
774 }
775 }
776}
777
Dan Gohman9e3a3422008-07-08 23:46:32 +0000778/// getMVTAlignment - Compute the default alignment value for the
779/// given type.
780///
781unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
782 const Type *Ty = VT == MVT::iPTR ?
783 PointerType::get(Type::Int8Ty, 0) :
784 VT.getTypeForMVT();
785
786 return TLI.getTargetData()->getABITypeAlignment(Ty);
787}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000789SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
Devang Pateleb4e0bd2009-02-03 18:46:32 +0000790 : TLI(tli), FLI(fli), DW(0),
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000791 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohman14a66442008-08-23 02:25:05 +0000792 Root(getEntryNode()) {
793 AllNodes.push_back(&EntryNode);
Dan Gohman03405562008-08-23 00:50:30 +0000794}
795
Devang Patel10e63332009-01-09 19:11:50 +0000796void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
797 DwarfWriter *dw) {
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000798 MF = &mf;
799 MMI = mmi;
Devang Patel10e63332009-01-09 19:11:50 +0000800 DW = dw;
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000801}
802
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803SelectionDAG::~SelectionDAG() {
Dan Gohman14a66442008-08-23 02:25:05 +0000804 allnodes_clear();
805}
806
807void SelectionDAG::allnodes_clear() {
Dan Gohman0b08b152008-08-26 01:44:34 +0000808 assert(&*AllNodes.begin() == &EntryNode);
809 AllNodes.remove(AllNodes.begin());
Dan Gohman98a355c2009-01-19 22:39:36 +0000810 while (!AllNodes.empty())
811 DeallocateNode(AllNodes.begin());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812}
813
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000814void SelectionDAG::clear() {
Dan Gohman14a66442008-08-23 02:25:05 +0000815 allnodes_clear();
816 OperandAllocator.Reset();
817 CSEMap.clear();
818
819 ExtendedValueTypeNodes.clear();
Bill Wendlingfef06052008-09-16 21:48:12 +0000820 ExternalSymbols.clear();
821 TargetExternalSymbols.clear();
Dan Gohman14a66442008-08-23 02:25:05 +0000822 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
823 static_cast<CondCodeSDNode*>(0));
824 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
825 static_cast<SDNode*>(0));
826
djgc2517d32009-01-26 04:35:06 +0000827 EntryNode.UseList = 0;
Dan Gohman14a66442008-08-23 02:25:05 +0000828 AllNodes.push_back(&EntryNode);
829 Root = getEntryNode();
830}
831
Dan Gohman8181bd12008-07-27 21:46:04 +0000832SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 if (Op.getValueType() == VT) return Op;
Dan Gohman161652c2008-02-29 01:47:35 +0000834 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +0000835 VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 return getNode(ISD::AND, Op.getValueType(), Op,
837 getConstant(Imm, Op.getValueType()));
838}
839
Bill Wendling085b2072009-01-30 22:23:15 +0000840SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, MVT VT) {
841 if (Op.getValueType() == VT) return Op;
842 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
843 VT.getSizeInBits());
844 return getNode(ISD::AND, DL, Op.getValueType(), Op,
845 getConstant(Imm, Op.getValueType()));
846}
847
Bob Wilson81a42cf2009-01-22 17:39:32 +0000848/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
849///
Bill Wendling4b8dd442009-01-30 22:11:22 +0000850SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, MVT VT) {
851 SDValue NegOne;
852 if (VT.isVector()) {
853 MVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +0000854 SDValue NegOneElt =
855 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), EltVT);
Bill Wendling4b8dd442009-01-30 22:11:22 +0000856 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOneElt);
Dale Johannesenf2103ee2009-02-03 01:55:44 +0000857 NegOne = getNode(ISD::BUILD_VECTOR, DL, VT, &NegOnes[0], NegOnes.size());
Bill Wendling4b8dd442009-01-30 22:11:22 +0000858 } else {
Duncan Sands505ba942009-02-01 18:06:53 +0000859 NegOne = getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Bill Wendling4b8dd442009-01-30 22:11:22 +0000860 }
Bill Wendling4b8dd442009-01-30 22:11:22 +0000861 return getNode(ISD::XOR, DL, VT, Val, NegOne);
862}
863
Dan Gohman8181bd12008-07-27 21:46:04 +0000864SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng833501d2008-06-30 07:31:25 +0000865 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
djg51bef2e2009-01-27 20:39:34 +0000866 assert((EltVT.getSizeInBits() >= 64 ||
867 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
868 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands92c43912008-06-06 12:08:01 +0000869 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohmandc458cf2008-02-08 22:59:30 +0000870}
871
Dan Gohman8181bd12008-07-27 21:46:04 +0000872SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000873 return getConstant(*ConstantInt::get(Val), VT, isT);
874}
875
876SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000877 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000878
Evan Cheng833501d2008-06-30 07:31:25 +0000879 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands92c43912008-06-06 12:08:01 +0000880 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohmandc458cf2008-02-08 22:59:30 +0000881 "APInt size does not match type size!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882
883 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
884 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000885 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000886 ID.AddPointer(&Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000888 SDNode *N = NULL;
889 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000890 if (!VT.isVector())
Dan Gohman8181bd12008-07-27 21:46:04 +0000891 return SDValue(N, 0);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000892 if (!N) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000893 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000894 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000895 CSEMap.InsertNode(N, IP);
896 AllNodes.push_back(N);
897 }
898
Dan Gohman8181bd12008-07-27 21:46:04 +0000899 SDValue Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000900 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000901 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000902 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000903 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
904 }
905 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906}
907
Dan Gohman8181bd12008-07-27 21:46:04 +0000908SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner5872a362008-01-17 07:00:52 +0000909 return getConstant(Val, TLI.getPointerTy(), isTarget);
910}
911
912
Dan Gohman8181bd12008-07-27 21:46:04 +0000913SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000914 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
915}
916
917SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands92c43912008-06-06 12:08:01 +0000918 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000919
Duncan Sands92c43912008-06-06 12:08:01 +0000920 MVT EltVT =
921 VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922
923 // Do the map lookup using the actual bit pattern for the floating point
924 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
925 // we don't have issues with SNANs.
926 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
927 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000928 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000929 ID.AddPointer(&V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 void *IP = 0;
931 SDNode *N = NULL;
932 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000933 if (!VT.isVector())
Dan Gohman8181bd12008-07-27 21:46:04 +0000934 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 if (!N) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000936 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000937 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 CSEMap.InsertNode(N, IP);
939 AllNodes.push_back(N);
940 }
941
Dan Gohman8181bd12008-07-27 21:46:04 +0000942 SDValue Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000943 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000944 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000945 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
947 }
948 return Result;
949}
950
Dan Gohman8181bd12008-07-27 21:46:04 +0000951SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands92c43912008-06-06 12:08:01 +0000952 MVT EltVT =
953 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000954 if (EltVT==MVT::f32)
955 return getConstantFP(APFloat((float)Val), VT, isTarget);
956 else
957 return getConstantFP(APFloat(Val), VT, isTarget);
958}
959
Dan Gohman8181bd12008-07-27 21:46:04 +0000960SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +0000961 MVT VT, int64_t Offset,
Dan Gohman8181bd12008-07-27 21:46:04 +0000962 bool isTargetGA) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 unsigned Opc;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000964
Dan Gohman36322c72008-10-18 02:06:02 +0000965 // Truncate (with sign-extension) the offset value to the pointer size.
Dan Gohman3d5257c2008-10-21 03:38:42 +0000966 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman36322c72008-10-18 02:06:02 +0000967 if (BitWidth < 64)
968 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
969
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000970 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
971 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000972 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000973 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000974 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000975 }
976
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 if (GVar && GVar->isThreadLocal())
978 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
979 else
980 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000981
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000983 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 ID.AddPointer(GV);
985 ID.AddInteger(Offset);
986 void *IP = 0;
987 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
djg08f48f52009-01-25 16:21:38 +0000988 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000989 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +0000990 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 CSEMap.InsertNode(N, IP);
992 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000993 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994}
995
Dan Gohman8181bd12008-07-27 21:46:04 +0000996SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
998 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000999 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 ID.AddInteger(FI);
1001 void *IP = 0;
1002 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001003 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001004 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001005 new (N) FrameIndexSDNode(FI, VT, isTarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 CSEMap.InsertNode(N, IP);
1007 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001008 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009}
1010
Dan Gohman8181bd12008-07-27 21:46:04 +00001011SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1013 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001014 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 ID.AddInteger(JTI);
1016 void *IP = 0;
1017 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001018 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001019 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001020 new (N) JumpTableSDNode(JTI, VT, isTarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 CSEMap.InsertNode(N, IP);
1022 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001023 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024}
1025
Dan Gohman8181bd12008-07-27 21:46:04 +00001026SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1027 unsigned Alignment, int Offset,
1028 bool isTarget) {
Dan Gohman04637d12008-09-16 22:05:41 +00001029 if (Alignment == 0)
1030 Alignment =
1031 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1033 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001034 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 ID.AddInteger(Alignment);
1036 ID.AddInteger(Offset);
1037 ID.AddPointer(C);
1038 void *IP = 0;
1039 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001040 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001041 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001042 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043 CSEMap.InsertNode(N, IP);
1044 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001045 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046}
1047
1048
Dan Gohman8181bd12008-07-27 21:46:04 +00001049SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1050 unsigned Alignment, int Offset,
1051 bool isTarget) {
Dan Gohman04637d12008-09-16 22:05:41 +00001052 if (Alignment == 0)
1053 Alignment =
1054 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1056 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001057 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 ID.AddInteger(Alignment);
1059 ID.AddInteger(Offset);
1060 C->AddSelectionDAGCSEId(ID);
1061 void *IP = 0;
1062 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001063 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001064 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001065 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 CSEMap.InsertNode(N, IP);
1067 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001068 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069}
1070
Dan Gohman8181bd12008-07-27 21:46:04 +00001071SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001073 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 ID.AddPointer(MBB);
1075 void *IP = 0;
1076 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001077 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001078 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001079 new (N) BasicBlockSDNode(MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 CSEMap.InsertNode(N, IP);
1081 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001082 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083}
1084
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001085SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl) {
1086 FoldingSetNodeID ID;
1087 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
1088 ID.AddPointer(MBB);
1089 void *IP = 0;
1090 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1091 return SDValue(E, 0);
1092 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
1093 new (N) BasicBlockSDNode(MBB, dl);
1094 CSEMap.InsertNode(N, IP);
1095 AllNodes.push_back(N);
1096 return SDValue(N, 0);
1097}
1098
Dan Gohman8181bd12008-07-27 21:46:04 +00001099SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001100 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001101 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001102 ID.AddInteger(Flags.getRawBits());
1103 void *IP = 0;
1104 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001105 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001106 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001107 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001108 CSEMap.InsertNode(N, IP);
1109 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001110 return SDValue(N, 0);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001111}
1112
Dan Gohman8181bd12008-07-27 21:46:04 +00001113SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands92c43912008-06-06 12:08:01 +00001114 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1115 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116
Duncan Sands92c43912008-06-06 12:08:01 +00001117 SDNode *&N = VT.isExtended() ?
1118 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsd7307a92007-10-17 13:49:58 +00001119
Dan Gohman8181bd12008-07-27 21:46:04 +00001120 if (N) return SDValue(N, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001121 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001122 new (N) VTSDNode(VT);
Duncan Sandsd7307a92007-10-17 13:49:58 +00001123 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001124 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125}
1126
Bill Wendlingfef06052008-09-16 21:48:12 +00001127SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1128 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman8181bd12008-07-27 21:46:04 +00001129 if (N) return SDValue(N, 0);
Bill Wendlingfef06052008-09-16 21:48:12 +00001130 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1131 new (N) ExternalSymbolSDNode(false, Sym, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001133 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134}
1135
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001136SDValue SelectionDAG::getExternalSymbol(const char *Sym, DebugLoc dl, MVT VT) {
1137 SDNode *&N = ExternalSymbols[Sym];
1138 if (N) return SDValue(N, 0);
1139 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1140 new (N) ExternalSymbolSDNode(false, dl, Sym, VT);
1141 AllNodes.push_back(N);
1142 return SDValue(N, 0);
1143}
1144
Bill Wendlingfef06052008-09-16 21:48:12 +00001145SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
1146 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman8181bd12008-07-27 21:46:04 +00001147 if (N) return SDValue(N, 0);
Bill Wendlingfef06052008-09-16 21:48:12 +00001148 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1149 new (N) ExternalSymbolSDNode(true, Sym, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001151 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152}
1153
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001154SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, DebugLoc dl,
1155 MVT VT) {
1156 SDNode *&N = TargetExternalSymbols[Sym];
1157 if (N) return SDValue(N, 0);
1158 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1159 new (N) ExternalSymbolSDNode(true, dl, Sym, VT);
1160 AllNodes.push_back(N);
1161 return SDValue(N, 0);
1162}
1163
Dan Gohman8181bd12008-07-27 21:46:04 +00001164SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 if ((unsigned)Cond >= CondCodeNodes.size())
1166 CondCodeNodes.resize(Cond+1);
Duncan Sands92c43912008-06-06 12:08:01 +00001167
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168 if (CondCodeNodes[Cond] == 0) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001169 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001170 new (N) CondCodeSDNode(Cond);
1171 CondCodeNodes[Cond] = N;
1172 AllNodes.push_back(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001174 return SDValue(CondCodeNodes[Cond], 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175}
1176
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001177SDValue SelectionDAG::getConvertRndSat(MVT VT, DebugLoc dl,
1178 SDValue Val, SDValue DTy,
1179 SDValue STy, SDValue Rnd, SDValue Sat,
1180 ISD::CvtCode Code) {
1181 // If the src and dest types are the same, no conversion is necessary.
1182 if (DTy == STy)
1183 return Val;
1184
1185 FoldingSetNodeID ID;
1186 void* IP = 0;
1187 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1188 return SDValue(E, 0);
1189 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1190 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1191 new (N) CvtRndSatSDNode(VT, dl, Ops, 5, Code);
1192 CSEMap.InsertNode(N, IP);
1193 AllNodes.push_back(N);
1194 return SDValue(N, 0);
1195}
1196
Dan Gohman8181bd12008-07-27 21:46:04 +00001197SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001199 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 ID.AddInteger(RegNo);
1201 void *IP = 0;
1202 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001203 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001204 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001205 new (N) RegisterSDNode(RegNo, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 CSEMap.InsertNode(N, IP);
1207 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001208 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209}
1210
Dan Gohman8181bd12008-07-27 21:46:04 +00001211SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Devang Patelfcf1c752009-01-13 00:35:13 +00001212 unsigned Line, unsigned Col,
1213 Value *CU) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001214 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001215 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman472d12c2008-06-30 20:59:49 +00001216 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001217 return SDValue(N, 0);
Dan Gohman472d12c2008-06-30 20:59:49 +00001218}
1219
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001220SDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl,
1221 SDValue Root,
1222 unsigned LabelID) {
1223 FoldingSetNodeID ID;
1224 SDValue Ops[] = { Root };
1225 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1226 ID.AddInteger(LabelID);
1227 void *IP = 0;
1228 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1229 return SDValue(E, 0);
1230 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
1231 new (N) LabelSDNode(Opcode, dl, Root, LabelID);
1232 CSEMap.InsertNode(N, IP);
1233 AllNodes.push_back(N);
1234 return SDValue(N, 0);
1235}
1236
Dan Gohman8181bd12008-07-27 21:46:04 +00001237SDValue SelectionDAG::getSrcValue(const Value *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001238 assert((!V || isa<PointerType>(V->getType())) &&
1239 "SrcValue is not a pointer?");
1240
1241 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001242 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 ID.AddPointer(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001244
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245 void *IP = 0;
1246 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001247 return SDValue(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001248
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001249 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001250 new (N) SrcValueSDNode(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001251 CSEMap.InsertNode(N, IP);
1252 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001253 return SDValue(N, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001254}
1255
Dan Gohman8181bd12008-07-27 21:46:04 +00001256SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Chengcf576fd2008-11-24 07:09:49 +00001257#ifndef NDEBUG
Dan Gohman12a9c082008-02-06 22:27:42 +00001258 const Value *v = MO.getValue();
1259 assert((!v || isa<PointerType>(v->getType())) &&
1260 "SrcValue is not a pointer?");
Evan Chengcf576fd2008-11-24 07:09:49 +00001261#endif
Dan Gohman12a9c082008-02-06 22:27:42 +00001262
1263 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001264 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohman98beebe2008-08-20 15:58:01 +00001265 MO.Profile(ID);
Dan Gohman12a9c082008-02-06 22:27:42 +00001266
1267 void *IP = 0;
1268 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001269 return SDValue(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001270
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001271 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001272 new (N) MemOperandSDNode(MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 CSEMap.InsertNode(N, IP);
1274 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001275 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276}
1277
Duncan Sands7d9e3612009-01-31 15:50:11 +00001278/// getShiftAmountOperand - Return the specified value casted to
1279/// the target's desired shift amount type.
1280SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
1281 MVT OpTy = Op.getValueType();
1282 MVT ShTy = TLI.getShiftAmountTy();
1283 if (OpTy == ShTy || OpTy.isVector()) return Op;
1284
1285 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1286 return getNode(Opcode, ShTy, Op);
1287}
1288
Chris Lattner53f5aee2007-10-15 17:47:20 +00001289/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1290/// specified value type.
Dan Gohman8181bd12008-07-27 21:46:04 +00001291SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner53f5aee2007-10-15 17:47:20 +00001292 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands76099772008-12-04 18:08:40 +00001293 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Duncan Sands92c43912008-06-06 12:08:01 +00001294 const Type *Ty = VT.getTypeForMVT();
Mon P Wang55854cc2008-07-05 20:40:31 +00001295 unsigned StackAlign =
1296 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1297
Chris Lattner53f5aee2007-10-15 17:47:20 +00001298 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1299 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1300}
1301
Duncan Sands871e55f2008-12-09 21:33:20 +00001302/// CreateStackTemporary - Create a stack temporary suitable for holding
1303/// either of the specified value types.
1304SDValue SelectionDAG::CreateStackTemporary(MVT VT1, MVT VT2) {
1305 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1306 VT2.getStoreSizeInBits())/8;
1307 const Type *Ty1 = VT1.getTypeForMVT();
1308 const Type *Ty2 = VT2.getTypeForMVT();
1309 const TargetData *TD = TLI.getTargetData();
1310 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1311 TD->getPrefTypeAlignment(Ty2));
1312
1313 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1314 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1315 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1316}
1317
Dan Gohman8181bd12008-07-27 21:46:04 +00001318SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
Dale Johannesen38496eb2009-02-03 00:47:48 +00001319 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 // These setcc operations always fold.
1321 switch (Cond) {
1322 default: break;
1323 case ISD::SETFALSE:
1324 case ISD::SETFALSE2: return getConstant(0, VT);
1325 case ISD::SETTRUE:
1326 case ISD::SETTRUE2: return getConstant(1, VT);
1327
1328 case ISD::SETOEQ:
1329 case ISD::SETOGT:
1330 case ISD::SETOGE:
1331 case ISD::SETOLT:
1332 case ISD::SETOLE:
1333 case ISD::SETONE:
1334 case ISD::SETO:
1335 case ISD::SETUO:
1336 case ISD::SETUEQ:
1337 case ISD::SETUNE:
Duncan Sands92c43912008-06-06 12:08:01 +00001338 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 break;
1340 }
1341
Gabor Greif1c80d112008-08-28 21:40:38 +00001342 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00001343 const APInt &C2 = N2C->getAPIntValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00001344 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00001345 const APInt &C1 = N1C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346
1347 switch (Cond) {
1348 default: assert(0 && "Unknown integer setcc!");
1349 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1350 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00001351 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1352 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1353 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1354 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1355 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1356 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1357 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1358 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 }
1360 }
1361 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001362 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1363 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001364 // No compile time operations on this type yet.
1365 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman8181bd12008-07-27 21:46:04 +00001366 return SDValue();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001367
1368 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001370 default: break;
Dale Johannesen76844472007-08-31 17:03:33 +00001371 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen38496eb2009-02-03 00:47:48 +00001372 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001373 // fall through
1374 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1375 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen38496eb2009-02-03 00:47:48 +00001376 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001377 // fall through
1378 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001379 R==APFloat::cmpLessThan, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001380 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen38496eb2009-02-03 00:47:48 +00001381 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001382 // fall through
1383 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1384 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen38496eb2009-02-03 00:47:48 +00001385 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001386 // fall through
1387 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1388 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen38496eb2009-02-03 00:47:48 +00001389 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001390 // fall through
1391 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001392 R==APFloat::cmpEqual, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001393 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen38496eb2009-02-03 00:47:48 +00001394 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001395 // fall through
1396 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001397 R==APFloat::cmpEqual, VT);
1398 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1399 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1400 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1401 R==APFloat::cmpEqual, VT);
1402 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1403 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1404 R==APFloat::cmpLessThan, VT);
1405 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1406 R==APFloat::cmpUnordered, VT);
1407 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1408 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 }
1410 } else {
1411 // Ensure that the constant occurs on the RHS.
Dale Johannesen38496eb2009-02-03 00:47:48 +00001412 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001414 }
1415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001416 // Could not fold it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001417 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418}
1419
Dan Gohman07961cd2008-02-25 21:11:39 +00001420/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1421/// use this predicate to simplify operations downstream.
Dan Gohman8181bd12008-07-27 21:46:04 +00001422bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001423 unsigned BitWidth = Op.getValueSizeInBits();
1424 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1425}
1426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1428/// this predicate to simplify operations downstream. Mask is known to be zero
1429/// for bits that V cannot have.
Dan Gohman8181bd12008-07-27 21:46:04 +00001430bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001432 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1434 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1435 return (KnownZero & Mask) == Mask;
1436}
1437
1438/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1439/// known to be either zero or one and return them in the KnownZero/KnownOne
1440/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1441/// processing.
Dan Gohman8181bd12008-07-27 21:46:04 +00001442void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00001443 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 unsigned Depth) const {
Dan Gohman229fa052008-02-13 00:35:47 +00001445 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands92c43912008-06-06 12:08:01 +00001446 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohman56eaab32008-02-13 23:13:32 +00001447 "Mask size mismatches value type size!");
1448
Dan Gohman229fa052008-02-13 00:35:47 +00001449 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 if (Depth == 6 || Mask == 0)
1451 return; // Limit search depth.
1452
Dan Gohman229fa052008-02-13 00:35:47 +00001453 APInt KnownZero2, KnownOne2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454
1455 switch (Op.getOpcode()) {
1456 case ISD::Constant:
1457 // We know all of the bits for a constant!
Dan Gohman229fa052008-02-13 00:35:47 +00001458 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001459 KnownZero = ~KnownOne & Mask;
1460 return;
1461 case ISD::AND:
1462 // If either the LHS or the RHS are Zero, the result is zero.
1463 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001464 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1465 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1467 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1468
1469 // Output known-1 bits are only known if set in both the LHS & RHS.
1470 KnownOne &= KnownOne2;
1471 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1472 KnownZero |= KnownZero2;
1473 return;
1474 case ISD::OR:
1475 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001476 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1477 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1479 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1480
1481 // Output known-0 bits are only known if clear in both the LHS & RHS.
1482 KnownZero &= KnownZero2;
1483 // Output known-1 are known to be set if set in either the LHS | RHS.
1484 KnownOne |= KnownOne2;
1485 return;
1486 case ISD::XOR: {
1487 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1488 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1489 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1490 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1491
1492 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohman229fa052008-02-13 00:35:47 +00001493 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1495 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1496 KnownZero = KnownZeroOut;
1497 return;
1498 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001499 case ISD::MUL: {
1500 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1501 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1502 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1503 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1504 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1505
1506 // If low bits are zero in either operand, output low known-0 bits.
1507 // Also compute a conserative estimate for high known-0 bits.
1508 // More trickiness is possible, but this is sufficient for the
1509 // interesting case of alignment computation.
1510 KnownOne.clear();
1511 unsigned TrailZ = KnownZero.countTrailingOnes() +
1512 KnownZero2.countTrailingOnes();
1513 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman4c451852008-05-07 00:35:55 +00001514 KnownZero2.countLeadingOnes(),
1515 BitWidth) - BitWidth;
Dan Gohmanbec16052008-04-28 17:02:21 +00001516
1517 TrailZ = std::min(TrailZ, BitWidth);
1518 LeadZ = std::min(LeadZ, BitWidth);
1519 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1520 APInt::getHighBitsSet(BitWidth, LeadZ);
1521 KnownZero &= Mask;
1522 return;
1523 }
1524 case ISD::UDIV: {
1525 // For the purposes of computing leading zeros we can conservatively
1526 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001527 // be less than the denominator.
Dan Gohmanbec16052008-04-28 17:02:21 +00001528 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1529 ComputeMaskedBits(Op.getOperand(0),
1530 AllOnes, KnownZero2, KnownOne2, Depth+1);
1531 unsigned LeadZ = KnownZero2.countLeadingOnes();
1532
1533 KnownOne2.clear();
1534 KnownZero2.clear();
1535 ComputeMaskedBits(Op.getOperand(1),
1536 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001537 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1538 if (RHSUnknownLeadingOnes != BitWidth)
1539 LeadZ = std::min(BitWidth,
1540 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001541
1542 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1543 return;
1544 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 case ISD::SELECT:
1546 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1547 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1548 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1549 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1550
1551 // Only known if known in both the LHS and RHS.
1552 KnownOne &= KnownOne2;
1553 KnownZero &= KnownZero2;
1554 return;
1555 case ISD::SELECT_CC:
1556 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1557 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1558 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1559 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1560
1561 // Only known if known in both the LHS and RHS.
1562 KnownOne &= KnownOne2;
1563 KnownZero &= KnownZero2;
1564 return;
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001565 case ISD::SADDO:
1566 case ISD::UADDO:
Bill Wendling7e04be62008-12-09 22:08:41 +00001567 case ISD::SSUBO:
1568 case ISD::USUBO:
1569 case ISD::SMULO:
1570 case ISD::UMULO:
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001571 if (Op.getResNo() != 1)
1572 return;
Duncan Sands8cf4a822008-11-23 15:47:28 +00001573 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 case ISD::SETCC:
1575 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands8cf4a822008-11-23 15:47:28 +00001576 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohman229fa052008-02-13 00:35:47 +00001577 BitWidth > 1)
1578 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579 return;
1580 case ISD::SHL:
1581 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1582 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001583 unsigned ShAmt = SA->getZExtValue();
Dan Gohman4b8d0002008-02-26 18:50:50 +00001584
1585 // If the shift count is an invalid immediate, don't do anything.
1586 if (ShAmt >= BitWidth)
1587 return;
1588
1589 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001590 KnownZero, KnownOne, Depth+1);
1591 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman4b8d0002008-02-26 18:50:50 +00001592 KnownZero <<= ShAmt;
1593 KnownOne <<= ShAmt;
Dan Gohman229fa052008-02-13 00:35:47 +00001594 // low bits known zero.
Dan Gohman4b8d0002008-02-26 18:50:50 +00001595 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001596 }
1597 return;
1598 case ISD::SRL:
1599 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1600 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001601 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001602
Dan Gohman4b8d0002008-02-26 18:50:50 +00001603 // If the shift count is an invalid immediate, don't do anything.
1604 if (ShAmt >= BitWidth)
1605 return;
1606
Dan Gohman229fa052008-02-13 00:35:47 +00001607 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 KnownZero, KnownOne, Depth+1);
1609 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001610 KnownZero = KnownZero.lshr(ShAmt);
1611 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612
Dan Gohman4d81a742008-02-13 22:43:25 +00001613 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 KnownZero |= HighBits; // High bits known zero.
1615 }
1616 return;
1617 case ISD::SRA:
1618 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001619 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001620
Dan Gohman4b8d0002008-02-26 18:50:50 +00001621 // If the shift count is an invalid immediate, don't do anything.
1622 if (ShAmt >= BitWidth)
1623 return;
1624
Dan Gohman229fa052008-02-13 00:35:47 +00001625 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001626 // If any of the demanded bits are produced by the sign extension, we also
1627 // demand the input sign bit.
Dan Gohman4d81a742008-02-13 22:43:25 +00001628 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1629 if (HighBits.getBoolValue())
Dan Gohman229fa052008-02-13 00:35:47 +00001630 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001631
1632 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1633 Depth+1);
1634 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001635 KnownZero = KnownZero.lshr(ShAmt);
1636 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001637
1638 // Handle the sign bits.
Dan Gohman229fa052008-02-13 00:35:47 +00001639 APInt SignBit = APInt::getSignBit(BitWidth);
1640 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001641
Dan Gohman91507292008-02-20 16:30:17 +00001642 if (KnownZero.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001643 KnownZero |= HighBits; // New bits are known zero.
Dan Gohman91507292008-02-20 16:30:17 +00001644 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001645 KnownOne |= HighBits; // New bits are known one.
1646 }
1647 }
1648 return;
1649 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001650 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1651 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001652
1653 // Sign extension. Compute the demanded bits in the result that are not
1654 // present in the input.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001655 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001656
Dan Gohmand0dfc772008-02-13 22:28:48 +00001657 APInt InSignBit = APInt::getSignBit(EBits);
1658 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001659
1660 // If the sign extended bits are demanded, we know that the sign
1661 // bit is demanded.
Dan Gohman229fa052008-02-13 00:35:47 +00001662 InSignBit.zext(BitWidth);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001663 if (NewBits.getBoolValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001664 InputDemandedBits |= InSignBit;
1665
1666 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1667 KnownZero, KnownOne, Depth+1);
1668 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1669
1670 // If the sign bit of the input is known set or clear, then we know the
1671 // top bits of the result.
Dan Gohman91507292008-02-20 16:30:17 +00001672 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001673 KnownZero |= NewBits;
1674 KnownOne &= ~NewBits;
Dan Gohman91507292008-02-20 16:30:17 +00001675 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676 KnownOne |= NewBits;
1677 KnownZero &= ~NewBits;
1678 } else { // Input sign bit unknown
1679 KnownZero &= ~NewBits;
1680 KnownOne &= ~NewBits;
1681 }
1682 return;
1683 }
1684 case ISD::CTTZ:
1685 case ISD::CTLZ:
1686 case ISD::CTPOP: {
Dan Gohman229fa052008-02-13 00:35:47 +00001687 unsigned LowBits = Log2_32(BitWidth)+1;
1688 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman75e24fb2008-06-21 22:02:15 +00001689 KnownOne.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 return;
1691 }
1692 case ISD::LOAD: {
Gabor Greif1c80d112008-08-28 21:40:38 +00001693 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands92c43912008-06-06 12:08:01 +00001695 MVT VT = LD->getMemoryVT();
1696 unsigned MemBits = VT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001697 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001698 }
1699 return;
1700 }
1701 case ISD::ZERO_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001702 MVT InVT = Op.getOperand(0).getValueType();
1703 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001704 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1705 APInt InMask = Mask;
1706 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001707 KnownZero.trunc(InBits);
1708 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001709 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001710 KnownZero.zext(BitWidth);
1711 KnownOne.zext(BitWidth);
1712 KnownZero |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 return;
1714 }
1715 case ISD::SIGN_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001716 MVT InVT = Op.getOperand(0).getValueType();
1717 unsigned InBits = InVT.getSizeInBits();
Dan Gohman229fa052008-02-13 00:35:47 +00001718 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001719 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1720 APInt InMask = Mask;
1721 InMask.trunc(InBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722
1723 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohmand0dfc772008-02-13 22:28:48 +00001724 // bit is demanded. Temporarily set this bit in the mask for our callee.
1725 if (NewBits.getBoolValue())
1726 InMask |= InSignBit;
Dan Gohman229fa052008-02-13 00:35:47 +00001727
Dan Gohman229fa052008-02-13 00:35:47 +00001728 KnownZero.trunc(InBits);
1729 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001730 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1731
1732 // Note if the sign bit is known to be zero or one.
1733 bool SignBitKnownZero = KnownZero.isNegative();
1734 bool SignBitKnownOne = KnownOne.isNegative();
1735 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1736 "Sign bit can't be known to be both zero and one!");
1737
1738 // If the sign bit wasn't actually demanded by our caller, we don't
1739 // want it set in the KnownZero and KnownOne result values. Reset the
1740 // mask and reapply it to the result values.
1741 InMask = Mask;
1742 InMask.trunc(InBits);
1743 KnownZero &= InMask;
1744 KnownOne &= InMask;
1745
Dan Gohman229fa052008-02-13 00:35:47 +00001746 KnownZero.zext(BitWidth);
1747 KnownOne.zext(BitWidth);
1748
Dan Gohmand0dfc772008-02-13 22:28:48 +00001749 // If the sign bit is known zero or one, the top bits match.
1750 if (SignBitKnownZero)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 KnownZero |= NewBits;
Dan Gohmand0dfc772008-02-13 22:28:48 +00001752 else if (SignBitKnownOne)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001753 KnownOne |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 return;
1755 }
1756 case ISD::ANY_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001757 MVT InVT = Op.getOperand(0).getValueType();
1758 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001759 APInt InMask = Mask;
1760 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001761 KnownZero.trunc(InBits);
1762 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001763 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001764 KnownZero.zext(BitWidth);
1765 KnownOne.zext(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001766 return;
1767 }
1768 case ISD::TRUNCATE: {
Duncan Sands92c43912008-06-06 12:08:01 +00001769 MVT InVT = Op.getOperand(0).getValueType();
1770 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001771 APInt InMask = Mask;
1772 InMask.zext(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001773 KnownZero.zext(InBits);
1774 KnownOne.zext(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001775 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001777 KnownZero.trunc(BitWidth);
1778 KnownOne.trunc(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779 break;
1780 }
1781 case ISD::AssertZext: {
Duncan Sands92c43912008-06-06 12:08:01 +00001782 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1783 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1785 KnownOne, Depth+1);
1786 KnownZero |= (~InMask) & Mask;
1787 return;
1788 }
Chris Lattner13f06832007-12-22 21:26:52 +00001789 case ISD::FGETSIGN:
1790 // All bits are zero except the low bit.
Dan Gohman229fa052008-02-13 00:35:47 +00001791 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattner13f06832007-12-22 21:26:52 +00001792 return;
1793
Dan Gohmanbec16052008-04-28 17:02:21 +00001794 case ISD::SUB: {
1795 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1796 // We know that the top bits of C-X are clear if X contains less bits
1797 // than C (i.e. no wrap-around can happen). For example, 20-X is
1798 // positive if we can prove that X is >= 0 and < 16.
1799 if (CLHS->getAPIntValue().isNonNegative()) {
1800 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1801 // NLZ can't be BitWidth with no sign bit
1802 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1803 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1804 Depth+1);
1805
1806 // If all of the MaskV bits are known to be zero, then we know the
1807 // output top bits are zero, because we now know that the output is
1808 // from [0-C].
1809 if ((KnownZero2 & MaskV) == MaskV) {
1810 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1811 // Top bits known zero.
1812 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1813 }
1814 }
1815 }
1816 }
1817 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001818 case ISD::ADD: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001819 // Output known-0 bits are known if clear or set in both the low clear bits
1820 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1821 // low 3 bits clear.
Dan Gohmanbec16052008-04-28 17:02:21 +00001822 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1823 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1824 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1825 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1826
1827 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1828 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1829 KnownZeroOut = std::min(KnownZeroOut,
1830 KnownZero2.countTrailingOnes());
1831
1832 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 return;
1834 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001835 case ISD::SREM:
1836 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman1b1a3c62008-07-03 00:52:03 +00001837 const APInt &RA = Rem->getAPIntValue();
Dan Gohmanbec16052008-04-28 17:02:21 +00001838 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman5a154a12008-05-06 00:51:48 +00001839 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohmanbec16052008-04-28 17:02:21 +00001840 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1841 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001842
Dan Gohman2275be02008-08-13 23:12:35 +00001843 // If the sign bit of the first operand is zero, the sign bit of
1844 // the result is zero. If the first operand has no one bits below
1845 // the second operand's single 1 bit, its sign will be zero.
Dan Gohmanbec16052008-04-28 17:02:21 +00001846 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1847 KnownZero2 |= ~LowBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001848
Dan Gohmanbec16052008-04-28 17:02:21 +00001849 KnownZero |= KnownZero2 & Mask;
Dan Gohmanbec16052008-04-28 17:02:21 +00001850
1851 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001852 }
1853 }
1854 return;
Dan Gohmanbec16052008-04-28 17:02:21 +00001855 case ISD::UREM: {
1856 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman1b1a3c62008-07-03 00:52:03 +00001857 const APInt &RA = Rem->getAPIntValue();
Dan Gohman5a154a12008-05-06 00:51:48 +00001858 if (RA.isPowerOf2()) {
1859 APInt LowBits = (RA - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001860 APInt Mask2 = LowBits & Mask;
1861 KnownZero |= ~LowBits & Mask;
1862 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1863 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1864 break;
1865 }
1866 }
1867
1868 // Since the result is less than or equal to either operand, any leading
1869 // zero bits in either operand must also exist in the result.
1870 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1871 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1872 Depth+1);
1873 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1874 Depth+1);
1875
1876 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1877 KnownZero2.countLeadingOnes());
1878 KnownOne.clear();
1879 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1880 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 }
1882 default:
1883 // Allow the target to implement this method for its nodes.
1884 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1885 case ISD::INTRINSIC_WO_CHAIN:
1886 case ISD::INTRINSIC_W_CHAIN:
1887 case ISD::INTRINSIC_VOID:
1888 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1889 }
1890 return;
1891 }
1892}
1893
1894/// ComputeNumSignBits - Return the number of times the sign bit of the
1895/// register is replicated into the other bits. We know that at least 1 bit
1896/// is always equal to the sign bit (itself), but other cases can give us
1897/// information. For example, immediately after an "SRA X, 2", we know that
1898/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman8181bd12008-07-27 21:46:04 +00001899unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands92c43912008-06-06 12:08:01 +00001900 MVT VT = Op.getValueType();
1901 assert(VT.isInteger() && "Invalid VT!");
1902 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001903 unsigned Tmp, Tmp2;
Dan Gohman4afc4252008-05-23 02:28:01 +00001904 unsigned FirstAnswer = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001905
1906 if (Depth == 6)
1907 return 1; // Limit search depth.
1908
1909 switch (Op.getOpcode()) {
1910 default: break;
1911 case ISD::AssertSext:
Duncan Sands92c43912008-06-06 12:08:01 +00001912 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001913 return VTBits-Tmp+1;
1914 case ISD::AssertZext:
Duncan Sands92c43912008-06-06 12:08:01 +00001915 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001916 return VTBits-Tmp;
1917
1918 case ISD::Constant: {
Dan Gohman463db8c2008-03-03 23:35:36 +00001919 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1920 // If negative, return # leading ones.
1921 if (Val.isNegative())
1922 return Val.countLeadingOnes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001923
Dan Gohman463db8c2008-03-03 23:35:36 +00001924 // Return # leading zeros.
1925 return Val.countLeadingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001926 }
1927
1928 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00001929 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001930 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1931
1932 case ISD::SIGN_EXTEND_INREG:
1933 // Max of the input and what this extends.
Duncan Sands92c43912008-06-06 12:08:01 +00001934 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001935 Tmp = VTBits-Tmp+1;
1936
1937 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1938 return std::max(Tmp, Tmp2);
1939
1940 case ISD::SRA:
1941 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1942 // SRA X, C -> adds C sign bits.
1943 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001944 Tmp += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001945 if (Tmp > VTBits) Tmp = VTBits;
1946 }
1947 return Tmp;
1948 case ISD::SHL:
1949 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1950 // shl destroys sign bits.
1951 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001952 if (C->getZExtValue() >= VTBits || // Bad shift.
1953 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1954 return Tmp - C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001955 }
1956 break;
1957 case ISD::AND:
1958 case ISD::OR:
1959 case ISD::XOR: // NOT is handled here.
Dan Gohman4afc4252008-05-23 02:28:01 +00001960 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001961 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman4afc4252008-05-23 02:28:01 +00001962 if (Tmp != 1) {
1963 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1964 FirstAnswer = std::min(Tmp, Tmp2);
1965 // We computed what we know about the sign bits as our first
1966 // answer. Now proceed to the generic code that uses
1967 // ComputeMaskedBits, and pick whichever answer is better.
1968 }
1969 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001970
1971 case ISD::SELECT:
Dan Gohman6ffcf262008-05-20 20:59:51 +00001972 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001973 if (Tmp == 1) return 1; // Early out.
Dan Gohman6ffcf262008-05-20 20:59:51 +00001974 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 return std::min(Tmp, Tmp2);
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001976
1977 case ISD::SADDO:
1978 case ISD::UADDO:
Bill Wendling7e04be62008-12-09 22:08:41 +00001979 case ISD::SSUBO:
1980 case ISD::USUBO:
1981 case ISD::SMULO:
1982 case ISD::UMULO:
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001983 if (Op.getResNo() != 1)
1984 break;
Duncan Sands8cf4a822008-11-23 15:47:28 +00001985 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001986 case ISD::SETCC:
1987 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands8cf4a822008-11-23 15:47:28 +00001988 if (TLI.getBooleanContents() ==
1989 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001990 return VTBits;
1991 break;
1992 case ISD::ROTL:
1993 case ISD::ROTR:
1994 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001995 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001996
1997 // Handle rotate right by N like a rotate left by 32-N.
1998 if (Op.getOpcode() == ISD::ROTR)
1999 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2000
2001 // If we aren't rotating out all of the known-in sign bits, return the
2002 // number that are left. This handles rotl(sext(x), 1) for example.
2003 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2004 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2005 }
2006 break;
2007 case ISD::ADD:
2008 // Add can have at most one carry bit. Thus we know that the output
2009 // is, at worst, one more bit than the inputs.
2010 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2011 if (Tmp == 1) return 1; // Early out.
2012
2013 // Special case decrementing a value (ADD X, -1):
2014 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
2015 if (CRHS->isAllOnesValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00002016 APInt KnownZero, KnownOne;
2017 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002018 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
2019
2020 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2021 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00002022 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002023 return VTBits;
2024
2025 // If we are subtracting one from a positive number, there is no carry
2026 // out of the result.
Dan Gohman63f4e462008-02-27 01:23:58 +00002027 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002028 return Tmp;
2029 }
2030
2031 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2032 if (Tmp2 == 1) return 1;
2033 return std::min(Tmp, Tmp2)-1;
2034 break;
2035
2036 case ISD::SUB:
2037 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2038 if (Tmp2 == 1) return 1;
2039
2040 // Handle NEG.
2041 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00002042 if (CLHS->isNullValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00002043 APInt KnownZero, KnownOne;
2044 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002045 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2046 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2047 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00002048 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002049 return VTBits;
2050
2051 // If the input is known to be positive (the sign bit is known clear),
2052 // the output of the NEG has the same number of sign bits as the input.
Dan Gohman63f4e462008-02-27 01:23:58 +00002053 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054 return Tmp2;
2055
2056 // Otherwise, we treat this like a SUB.
2057 }
2058
2059 // Sub can have at most one carry bit. Thus we know that the output
2060 // is, at worst, one more bit than the inputs.
2061 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2062 if (Tmp == 1) return 1; // Early out.
2063 return std::min(Tmp, Tmp2)-1;
2064 break;
2065 case ISD::TRUNCATE:
2066 // FIXME: it's tricky to do anything useful for this, but it is an important
2067 // case for targets like X86.
2068 break;
2069 }
2070
2071 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2072 if (Op.getOpcode() == ISD::LOAD) {
2073 LoadSDNode *LD = cast<LoadSDNode>(Op);
2074 unsigned ExtType = LD->getExtensionType();
2075 switch (ExtType) {
2076 default: break;
2077 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands92c43912008-06-06 12:08:01 +00002078 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002079 return VTBits-Tmp+1;
2080 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands92c43912008-06-06 12:08:01 +00002081 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002082 return VTBits-Tmp;
2083 }
2084 }
2085
2086 // Allow the target to implement this method for its nodes.
2087 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2088 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2089 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2090 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2091 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman4afc4252008-05-23 02:28:01 +00002092 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093 }
2094
2095 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2096 // use this information.
Dan Gohman63f4e462008-02-27 01:23:58 +00002097 APInt KnownZero, KnownOne;
2098 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
2100
Dan Gohman63f4e462008-02-27 01:23:58 +00002101 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 Mask = KnownZero;
Dan Gohman63f4e462008-02-27 01:23:58 +00002103 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002104 Mask = KnownOne;
2105 } else {
2106 // Nothing known.
Dan Gohman4afc4252008-05-23 02:28:01 +00002107 return FirstAnswer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002108 }
2109
2110 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2111 // the number of identical bits in the top of the input value.
Dan Gohman63f4e462008-02-27 01:23:58 +00002112 Mask = ~Mask;
2113 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002114 // Return # leading zeros. We use 'min' here in case Val was zero before
2115 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman4afc4252008-05-23 02:28:01 +00002116 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117}
2118
2119
Dan Gohman8181bd12008-07-27 21:46:04 +00002120bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Cheng2e28d622008-02-02 04:07:54 +00002121 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2122 if (!GA) return false;
Dan Gohman36322c72008-10-18 02:06:02 +00002123 if (GA->getOffset() != 0) return false;
Evan Cheng2e28d622008-02-02 04:07:54 +00002124 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2125 if (!GV) return false;
Devang Patelfb3a4882009-01-13 22:54:57 +00002126 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patel42f6bed2009-01-13 23:54:55 +00002127 return MMI && MMI->hasDebugInfo();
Evan Cheng2e28d622008-02-02 04:07:54 +00002128}
2129
2130
Evan Cheng411fc172008-05-13 08:35:03 +00002131/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2132/// element of the result of the vector shuffle.
Dan Gohman8181bd12008-07-27 21:46:04 +00002133SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands92c43912008-06-06 12:08:01 +00002134 MVT VT = N->getValueType(0);
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002135 DebugLoc dl = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00002136 SDValue PermMask = N->getOperand(2);
2137 SDValue Idx = PermMask.getOperand(i);
Evan Cheng57db53b2008-06-25 20:52:59 +00002138 if (Idx.getOpcode() == ISD::UNDEF)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002139 return getNode(ISD::UNDEF, dl, VT.getVectorElementType());
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002140 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002141 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002142 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Cheng57db53b2008-06-25 20:52:59 +00002143 Index %= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00002144
2145 if (V.getOpcode() == ISD::BIT_CONVERT) {
2146 V = V.getOperand(0);
Mon P Wang9901e732008-12-09 05:46:39 +00002147 MVT VVT = V.getValueType();
2148 if (!VVT.isVector() || VVT.getVectorNumElements() != NumElems)
Dan Gohman8181bd12008-07-27 21:46:04 +00002149 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002150 }
Evan Chengdea99362008-05-29 08:22:04 +00002151 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00002152 return (Index == 0) ? V.getOperand(0)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002153 : getNode(ISD::UNDEF, dl, VT.getVectorElementType());
Evan Chengdea99362008-05-29 08:22:04 +00002154 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00002155 return V.getOperand(Index);
2156 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greif1c80d112008-08-28 21:40:38 +00002157 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman8181bd12008-07-27 21:46:04 +00002158 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002159}
2160
2161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002162/// getNode - Gets or creates the specified node.
2163///
Dan Gohman8181bd12008-07-27 21:46:04 +00002164SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00002165 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT);
2166}
2167
2168SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002169 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00002170 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171 void *IP = 0;
2172 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002173 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002174 SDNode *N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002175 new (N) SDNode(Opcode, DL, SDNode::getSDVTList(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002176 CSEMap.InsertNode(N, IP);
2177
2178 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002179#ifndef NDEBUG
2180 VerifyNode(N);
2181#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002182 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002183}
2184
Dan Gohman8181bd12008-07-27 21:46:04 +00002185SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00002186 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Operand);
2187}
2188
2189SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
2190 MVT VT, SDValue Operand) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 // Constant fold unary operations with an integer constant operand.
Gabor Greif1c80d112008-08-28 21:40:38 +00002192 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00002193 const APInt &Val = C->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002194 unsigned BitWidth = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002195 switch (Opcode) {
2196 default: break;
Evan Chengeadaf442008-03-06 17:42:34 +00002197 case ISD::SIGN_EXTEND:
2198 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002199 case ISD::ANY_EXTEND:
Dan Gohman161652c2008-02-29 01:47:35 +00002200 case ISD::ZERO_EXTEND:
Evan Chengeadaf442008-03-06 17:42:34 +00002201 case ISD::TRUNCATE:
2202 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00002203 case ISD::UINT_TO_FP:
2204 case ISD::SINT_TO_FP: {
2205 const uint64_t zero[] = {0, 0};
Dale Johannesenb89072e2007-10-16 23:38:29 +00002206 // No compile time operations on this type.
2207 if (VT==MVT::ppcf128)
2208 break;
Dan Gohman161652c2008-02-29 01:47:35 +00002209 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2210 (void)apf.convertFromAPInt(Val,
2211 Opcode==ISD::SINT_TO_FP,
2212 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00002213 return getConstantFP(apf, VT);
2214 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002215 case ISD::BIT_CONVERT:
2216 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman161652c2008-02-29 01:47:35 +00002217 return getConstantFP(Val.bitsToFloat(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002218 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman161652c2008-02-29 01:47:35 +00002219 return getConstantFP(Val.bitsToDouble(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002220 break;
2221 case ISD::BSWAP:
Dan Gohman161652c2008-02-29 01:47:35 +00002222 return getConstant(Val.byteSwap(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002223 case ISD::CTPOP:
Dan Gohman161652c2008-02-29 01:47:35 +00002224 return getConstant(Val.countPopulation(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002225 case ISD::CTLZ:
Dan Gohman161652c2008-02-29 01:47:35 +00002226 return getConstant(Val.countLeadingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002227 case ISD::CTTZ:
Dan Gohman161652c2008-02-29 01:47:35 +00002228 return getConstant(Val.countTrailingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 }
2230 }
2231
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002232 // Constant fold unary operations with a floating point constant operand.
Gabor Greif1c80d112008-08-28 21:40:38 +00002233 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002234 APFloat V = C->getValueAPF(); // make copy
Chris Lattner5872a362008-01-17 07:00:52 +00002235 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00002236 switch (Opcode) {
2237 case ISD::FNEG:
2238 V.changeSign();
2239 return getConstantFP(V, VT);
2240 case ISD::FABS:
2241 V.clearSign();
2242 return getConstantFP(V, VT);
2243 case ISD::FP_ROUND:
Dale Johannesen6e547b42008-10-09 23:00:39 +00002244 case ISD::FP_EXTEND: {
2245 bool ignored;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002246 // This can return overflow, underflow, or inexact; we don't care.
2247 // FIXME need to be more flexible about rounding mode.
Chris Lattnerd037d482008-03-05 06:48:13 +00002248 (void)V.convert(*MVTToAPFloatSemantics(VT),
Dale Johannesen6e547b42008-10-09 23:00:39 +00002249 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002250 return getConstantFP(V, VT);
Dale Johannesen6e547b42008-10-09 23:00:39 +00002251 }
Dale Johannesenb89072e2007-10-16 23:38:29 +00002252 case ISD::FP_TO_SINT:
2253 case ISD::FP_TO_UINT: {
2254 integerPart x;
Dale Johannesen6e547b42008-10-09 23:00:39 +00002255 bool ignored;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002256 assert(integerPartWidth >= 64);
2257 // FIXME need to be more flexible about rounding mode.
2258 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2259 Opcode==ISD::FP_TO_SINT,
Dale Johannesen6e547b42008-10-09 23:00:39 +00002260 APFloat::rmTowardZero, &ignored);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002261 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2262 break;
2263 return getConstant(x, VT);
2264 }
2265 case ISD::BIT_CONVERT:
2266 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen6e547b42008-10-09 23:00:39 +00002267 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002268 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen6e547b42008-10-09 23:00:39 +00002269 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002270 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002271 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002273 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274
Gabor Greif1c80d112008-08-28 21:40:38 +00002275 unsigned OpOpcode = Operand.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002276 switch (Opcode) {
2277 case ISD::TokenFactor:
Duncan Sands42d7bb82008-12-01 11:41:29 +00002278 case ISD::MERGE_VALUES:
Dan Gohman29c3cef2008-08-14 20:04:46 +00002279 case ISD::CONCAT_VECTORS:
Duncan Sands42d7bb82008-12-01 11:41:29 +00002280 return Operand; // Factor, merge or concat of one node? No need.
Chris Lattner5872a362008-01-17 07:00:52 +00002281 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002282 case ISD::FP_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002283 assert(VT.isFloatingPoint() &&
2284 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00002285 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnere6465dd2008-03-11 06:21:08 +00002286 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002287 return getNode(ISD::UNDEF, DL, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 break;
Chris Lattnere6465dd2008-03-11 06:21:08 +00002289 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002290 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291 "Invalid SIGN_EXTEND!");
2292 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002293 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002294 && "Invalid sext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002295 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002296 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 break;
2298 case ISD::ZERO_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002299 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002300 "Invalid ZERO_EXTEND!");
2301 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002302 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002303 && "Invalid zext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002304 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002305 return getNode(ISD::ZERO_EXTEND, DL, VT,
2306 Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002307 break;
2308 case ISD::ANY_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002309 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310 "Invalid ANY_EXTEND!");
2311 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002312 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002313 && "Invalid anyext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002314 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2315 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002316 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 break;
2318 case ISD::TRUNCATE:
Duncan Sands92c43912008-06-06 12:08:01 +00002319 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002320 "Invalid TRUNCATE!");
2321 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsec142ee2008-06-08 20:54:56 +00002322 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002323 && "Invalid truncate node, src < dst!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002324 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002325 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002326 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2327 OpOpcode == ISD::ANY_EXTEND) {
2328 // If the source is smaller than the dest, we still need an extend.
Gabor Greif1c80d112008-08-28 21:40:38 +00002329 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002330 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00002331 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002332 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002333 else
Gabor Greif1c80d112008-08-28 21:40:38 +00002334 return Operand.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 }
2336 break;
2337 case ISD::BIT_CONVERT:
2338 // Basic sanity checking.
Duncan Sands92c43912008-06-06 12:08:01 +00002339 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002340 && "Cannot BIT_CONVERT between types of different sizes!");
2341 if (VT == Operand.getValueType()) return Operand; // noop conversion.
2342 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002343 return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002344 if (OpOpcode == ISD::UNDEF)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002345 return getNode(ISD::UNDEF, DL, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346 break;
2347 case ISD::SCALAR_TO_VECTOR:
Duncan Sands92c43912008-06-06 12:08:01 +00002348 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2349 VT.getVectorElementType() == Operand.getValueType() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002350 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattner9f0705c2008-03-08 23:43:36 +00002351 if (OpOpcode == ISD::UNDEF)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002352 return getNode(ISD::UNDEF, DL, VT);
Chris Lattner9f0705c2008-03-08 23:43:36 +00002353 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2354 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2355 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2356 Operand.getConstantOperandVal(1) == 0 &&
2357 Operand.getOperand(0).getValueType() == VT)
2358 return Operand.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 break;
2360 case ISD::FNEG:
Mon P Wang4340d5d2009-01-31 06:07:45 +00002361 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
2362 if (UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002363 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greif1c80d112008-08-28 21:40:38 +00002364 Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002365 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greif1c80d112008-08-28 21:40:38 +00002366 return Operand.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367 break;
2368 case ISD::FABS:
2369 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002370 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002371 break;
2372 }
2373
2374 SDNode *N;
2375 SDVTList VTs = getVTList(VT);
2376 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
2377 FoldingSetNodeID ID;
Dan Gohman8181bd12008-07-27 21:46:04 +00002378 SDValue Ops[1] = { Operand };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
2380 void *IP = 0;
2381 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002382 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002383 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002384 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 CSEMap.InsertNode(N, IP);
2386 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002387 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002388 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 }
Duncan Sandsd3ace282008-07-21 10:20:31 +00002390
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002391 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002392#ifndef NDEBUG
2393 VerifyNode(N);
2394#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002395 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002396}
2397
Bill Wendling16bc18c2008-09-24 10:16:24 +00002398SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
2399 MVT VT,
2400 ConstantSDNode *Cst1,
2401 ConstantSDNode *Cst2) {
2402 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2403
2404 switch (Opcode) {
2405 case ISD::ADD: return getConstant(C1 + C2, VT);
2406 case ISD::SUB: return getConstant(C1 - C2, VT);
2407 case ISD::MUL: return getConstant(C1 * C2, VT);
2408 case ISD::UDIV:
2409 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2410 break;
2411 case ISD::UREM:
2412 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2413 break;
2414 case ISD::SDIV:
2415 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2416 break;
2417 case ISD::SREM:
2418 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2419 break;
2420 case ISD::AND: return getConstant(C1 & C2, VT);
2421 case ISD::OR: return getConstant(C1 | C2, VT);
2422 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2423 case ISD::SHL: return getConstant(C1 << C2, VT);
2424 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2425 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2426 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2427 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2428 default: break;
2429 }
2430
2431 return SDValue();
2432}
2433
Dan Gohman8181bd12008-07-27 21:46:04 +00002434SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2435 SDValue N1, SDValue N2) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00002436 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2);
2437}
2438
2439SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2440 SDValue N1, SDValue N2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002441 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2442 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002444 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 case ISD::TokenFactor:
2446 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2447 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002448 // Fold trivial token factors.
2449 if (N1.getOpcode() == ISD::EntryToken) return N2;
2450 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanc0e18d62008-10-01 15:11:19 +00002451 if (N1 == N2) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452 break;
Dan Gohman29c3cef2008-08-14 20:04:46 +00002453 case ISD::CONCAT_VECTORS:
2454 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2455 // one big BUILD_VECTOR.
2456 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2457 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002458 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2459 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002460 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman29c3cef2008-08-14 20:04:46 +00002461 }
2462 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002463 case ISD::AND:
Duncan Sands92c43912008-06-06 12:08:01 +00002464 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002465 N1.getValueType() == VT && "Binary operator types must match!");
2466 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2467 // worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002468 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002469 return N2;
Chris Lattner8aa8a5e2008-01-26 01:05:42 +00002470 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2471 return N1;
Chris Lattnercc126e32008-01-22 19:09:33 +00002472 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 case ISD::OR:
2474 case ISD::XOR:
Dan Gohman3f76aed2008-06-02 22:27:05 +00002475 case ISD::ADD:
2476 case ISD::SUB:
Duncan Sands92c43912008-06-06 12:08:01 +00002477 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002478 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman3f76aed2008-06-02 22:27:05 +00002479 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2480 // it's worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002481 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002482 return N1;
2483 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 case ISD::UDIV:
2485 case ISD::UREM:
2486 case ISD::MULHU:
2487 case ISD::MULHS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 case ISD::MUL:
2489 case ISD::SDIV:
2490 case ISD::SREM:
Dan Gohman3d015562009-01-22 21:58:43 +00002491 assert(VT.isInteger() && "This operator does not apply to FP types!");
2492 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002493 case ISD::FADD:
2494 case ISD::FSUB:
2495 case ISD::FMUL:
2496 case ISD::FDIV:
2497 case ISD::FREM:
Dan Gohman46ef3eb2009-01-23 19:10:37 +00002498 if (UnsafeFPMath) {
2499 if (Opcode == ISD::FADD) {
2500 // 0+x --> x
2501 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2502 if (CFP->getValueAPF().isZero())
2503 return N2;
2504 // x+0 --> x
2505 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2506 if (CFP->getValueAPF().isZero())
2507 return N1;
2508 } else if (Opcode == ISD::FSUB) {
2509 // x-0 --> x
2510 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2511 if (CFP->getValueAPF().isZero())
2512 return N1;
2513 }
Dan Gohman3d015562009-01-22 21:58:43 +00002514 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 assert(N1.getValueType() == N2.getValueType() &&
2516 N1.getValueType() == VT && "Binary operator types must match!");
2517 break;
2518 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2519 assert(N1.getValueType() == VT &&
Duncan Sands92c43912008-06-06 12:08:01 +00002520 N1.getValueType().isFloatingPoint() &&
2521 N2.getValueType().isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002522 "Invalid FCOPYSIGN!");
2523 break;
2524 case ISD::SHL:
2525 case ISD::SRA:
2526 case ISD::SRL:
2527 case ISD::ROTL:
2528 case ISD::ROTR:
2529 assert(VT == N1.getValueType() &&
2530 "Shift operators return type must be the same as their first arg");
Duncan Sands92c43912008-06-06 12:08:01 +00002531 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattnera113d3e2008-07-02 17:01:57 +00002532 "Shifts only work on integers");
2533
2534 // Always fold shifts of i1 values so the code generator doesn't need to
2535 // handle them. Since we know the size of the shift has to be less than the
2536 // size of the value, the shift/rotate count is guaranteed to be zero.
2537 if (VT == MVT::i1)
2538 return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 break;
2540 case ISD::FP_ROUND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00002541 MVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002542 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands92c43912008-06-06 12:08:01 +00002543 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002545 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002546 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002547 break;
2548 }
Chris Lattner5872a362008-01-17 07:00:52 +00002549 case ISD::FP_ROUND:
Duncan Sands92c43912008-06-06 12:08:01 +00002550 assert(VT.isFloatingPoint() &&
2551 N1.getValueType().isFloatingPoint() &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00002552 VT.bitsLE(N1.getValueType()) &&
Chris Lattner5872a362008-01-17 07:00:52 +00002553 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002554 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00002555 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00002557 case ISD::AssertZext: {
Duncan Sands92c43912008-06-06 12:08:01 +00002558 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattnercc126e32008-01-22 19:09:33 +00002559 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002560 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002561 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002562 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands539510b2008-02-10 10:08:52 +00002563 if (VT == EVT) return N1; // noop assertion.
Chris Lattnercc126e32008-01-22 19:09:33 +00002564 break;
2565 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002566 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00002567 MVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002569 assert(VT.isInteger() && EVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002571 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002572 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573
Chris Lattnercc126e32008-01-22 19:09:33 +00002574 if (N1C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002575 APInt Val = N1C->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002576 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman161652c2008-02-29 01:47:35 +00002577 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng7faa1d72008-03-06 08:20:51 +00002578 Val = Val.ashr(Val.getBitWidth()-FromBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579 return getConstant(Val, VT);
2580 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002581 break;
2582 }
2583 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattner9f0705c2008-03-08 23:43:36 +00002584 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2585 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002586 return getNode(ISD::UNDEF, DL, VT);
Chris Lattner9f0705c2008-03-08 23:43:36 +00002587
Chris Lattnercc126e32008-01-22 19:09:33 +00002588 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2589 // expanding copies of large vectors from registers.
Dan Gohman101124c2008-08-13 21:51:37 +00002590 if (N2C &&
2591 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002592 N1.getNumOperands() > 0) {
2593 unsigned Factor =
Duncan Sands92c43912008-06-06 12:08:01 +00002594 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002595 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002596 N1.getOperand(N2C->getZExtValue() / Factor),
2597 getConstant(N2C->getZExtValue() % Factor,
2598 N2.getValueType()));
Chris Lattnercc126e32008-01-22 19:09:33 +00002599 }
2600
2601 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2602 // expanding large vector constants.
Dan Gohman101124c2008-08-13 21:51:37 +00002603 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002604 return N1.getOperand(N2C->getZExtValue());
Chris Lattner9f0705c2008-03-08 23:43:36 +00002605
Chris Lattnercc126e32008-01-22 19:09:33 +00002606 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2607 // operations are lowered to scalars.
Dan Gohman101124c2008-08-13 21:51:37 +00002608 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Dan Gohmanb38eabf2009-01-29 16:10:46 +00002609 // If the indices are the same, return the inserted element.
Dan Gohman101124c2008-08-13 21:51:37 +00002610 if (N1.getOperand(2) == N2)
2611 return N1.getOperand(1);
Dan Gohmanb38eabf2009-01-29 16:10:46 +00002612 // If the indices are known different, extract the element from
2613 // the original vector.
2614 else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2615 isa<ConstantSDNode>(N2))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002616 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Dan Gohman101124c2008-08-13 21:51:37 +00002617 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002618 break;
2619 case ISD::EXTRACT_ELEMENT:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002620 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands1568af82008-06-25 20:24:48 +00002621 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2622 (N1.getValueType().isInteger() == VT.isInteger()) &&
2623 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sandsc4d85172008-03-12 20:30:08 +00002624
Chris Lattnercc126e32008-01-22 19:09:33 +00002625 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2626 // 64-bit integers into 32-bit parts. Instead of building the extract of
2627 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2628 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002629 return N1.getOperand(N2C->getZExtValue());
Duncan Sandsc4d85172008-03-12 20:30:08 +00002630
Chris Lattnercc126e32008-01-22 19:09:33 +00002631 // EXTRACT_ELEMENT of a constant int is also very common.
2632 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002633 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002634 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman452b4ce2008-03-24 16:38:05 +00002635 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2636 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattnercc126e32008-01-22 19:09:33 +00002637 }
2638 break;
Duncan Sandsbd13a812008-02-20 17:38:09 +00002639 case ISD::EXTRACT_SUBVECTOR:
2640 if (N1.getValueType() == VT) // Trivial extraction.
2641 return N1;
2642 break;
Chris Lattnercc126e32008-01-22 19:09:33 +00002643 }
2644
2645 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002646 if (N2C) {
Bill Wendling16bc18c2008-09-24 10:16:24 +00002647 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2648 if (SV.getNode()) return SV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649 } else { // Cannonicalize constant to RHS if commutative
2650 if (isCommutativeBinOp(Opcode)) {
2651 std::swap(N1C, N2C);
2652 std::swap(N1, N2);
2653 }
2654 }
2655 }
2656
Chris Lattnercc126e32008-01-22 19:09:33 +00002657 // Constant fold FP operations.
Gabor Greif1c80d112008-08-28 21:40:38 +00002658 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2659 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002660 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002661 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2662 // Cannonicalize constant to RHS if commutative
2663 std::swap(N1CFP, N2CFP);
2664 std::swap(N1, N2);
2665 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002666 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2667 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668 switch (Opcode) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002669 case ISD::FADD:
2670 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002671 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002672 return getConstantFP(V1, VT);
2673 break;
2674 case ISD::FSUB:
2675 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2676 if (s!=APFloat::opInvalidOp)
2677 return getConstantFP(V1, VT);
2678 break;
2679 case ISD::FMUL:
2680 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2681 if (s!=APFloat::opInvalidOp)
2682 return getConstantFP(V1, VT);
2683 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002684 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002685 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2686 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2687 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002688 break;
2689 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002690 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2691 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2692 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002694 case ISD::FCOPYSIGN:
2695 V1.copySign(V2);
2696 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 default: break;
2698 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002699 }
2700 }
2701
2702 // Canonicalize an UNDEF to the RHS, even over a constant.
2703 if (N1.getOpcode() == ISD::UNDEF) {
2704 if (isCommutativeBinOp(Opcode)) {
2705 std::swap(N1, N2);
2706 } else {
2707 switch (Opcode) {
2708 case ISD::FP_ROUND_INREG:
2709 case ISD::SIGN_EXTEND_INREG:
2710 case ISD::SUB:
2711 case ISD::FSUB:
2712 case ISD::FDIV:
2713 case ISD::FREM:
2714 case ISD::SRA:
2715 return N1; // fold op(undef, arg2) -> undef
2716 case ISD::UDIV:
2717 case ISD::SDIV:
2718 case ISD::UREM:
2719 case ISD::SREM:
2720 case ISD::SRL:
2721 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002722 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2724 // For vectors, we can't easily build an all zero vector, just return
2725 // the LHS.
2726 return N2;
2727 }
2728 }
2729 }
2730
2731 // Fold a bunch of operators when the RHS is undef.
2732 if (N2.getOpcode() == ISD::UNDEF) {
2733 switch (Opcode) {
Evan Cheng5d00cb42008-03-25 20:08:07 +00002734 case ISD::XOR:
2735 if (N1.getOpcode() == ISD::UNDEF)
2736 // Handle undef ^ undef -> 0 special case. This is a common
2737 // idiom (misuse).
2738 return getConstant(0, VT);
2739 // fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002740 case ISD::ADD:
2741 case ISD::ADDC:
2742 case ISD::ADDE:
2743 case ISD::SUB:
2744 case ISD::FADD:
2745 case ISD::FSUB:
2746 case ISD::FMUL:
2747 case ISD::FDIV:
2748 case ISD::FREM:
2749 case ISD::UDIV:
2750 case ISD::SDIV:
2751 case ISD::UREM:
2752 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 return N2; // fold op(arg1, undef) -> undef
2754 case ISD::MUL:
2755 case ISD::AND:
2756 case ISD::SRL:
2757 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002758 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2760 // For vectors, we can't easily build an all zero vector, just return
2761 // the LHS.
2762 return N1;
2763 case ISD::OR:
Duncan Sands92c43912008-06-06 12:08:01 +00002764 if (!VT.isVector())
Duncan Sands505ba942009-02-01 18:06:53 +00002765 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002766 // For vectors, we can't easily build an all one vector, just return
2767 // the LHS.
2768 return N1;
2769 case ISD::SRA:
2770 return N1;
2771 }
2772 }
2773
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774 // Memoize this node if possible.
2775 SDNode *N;
2776 SDVTList VTs = getVTList(VT);
2777 if (VT != MVT::Flag) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002778 SDValue Ops[] = { N1, N2 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 FoldingSetNodeID ID;
2780 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2781 void *IP = 0;
2782 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002783 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002784 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002785 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002786 CSEMap.InsertNode(N, IP);
2787 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002788 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002789 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002790 }
2791
2792 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002793#ifndef NDEBUG
2794 VerifyNode(N);
2795#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002796 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797}
2798
Dan Gohman8181bd12008-07-27 21:46:04 +00002799SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2800 SDValue N1, SDValue N2, SDValue N3) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00002801 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3);
2802}
2803
2804SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2805 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002806 // Perform various simplifications.
Gabor Greif1c80d112008-08-28 21:40:38 +00002807 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2808 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002809 switch (Opcode) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00002810 case ISD::CONCAT_VECTORS:
2811 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2812 // one big BUILD_VECTOR.
2813 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2814 N2.getOpcode() == ISD::BUILD_VECTOR &&
2815 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002816 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2817 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2818 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dale Johannesen38496eb2009-02-03 00:47:48 +00002819 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman29c3cef2008-08-14 20:04:46 +00002820 }
2821 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002822 case ISD::SETCC: {
2823 // Use FoldSetCC to simplify SETCC's.
Dale Johannesen38496eb2009-02-03 00:47:48 +00002824 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002825 if (Simp.getNode()) return Simp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002826 break;
2827 }
2828 case ISD::SELECT:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002829 if (N1C) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002830 if (N1C->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002831 return N2; // select true, X, Y -> X
2832 else
2833 return N3; // select false, X, Y -> Y
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002834 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835
2836 if (N2 == N3) return N2; // select C, X, X -> X
2837 break;
2838 case ISD::BRCOND:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002839 if (N2C) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002840 if (N2C->getZExtValue()) // Unconditional branch
Dale Johannesen38496eb2009-02-03 00:47:48 +00002841 return getNode(ISD::BR, DL, MVT::Other, N1, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002842 else
2843 return N1; // Never-taken branch
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002844 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 break;
2846 case ISD::VECTOR_SHUFFLE:
Mon P Wangbff5d9c2008-11-10 04:46:22 +00002847 assert(N1.getValueType() == N2.getValueType() &&
2848 N1.getValueType().isVector() &&
Duncan Sands92c43912008-06-06 12:08:01 +00002849 VT.isVector() && N3.getValueType().isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands92c43912008-06-06 12:08:01 +00002851 VT.getVectorNumElements() == N3.getNumOperands() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 "Illegal VECTOR_SHUFFLE node!");
2853 break;
2854 case ISD::BIT_CONVERT:
2855 // Fold bit_convert nodes from a type to themselves.
2856 if (N1.getValueType() == VT)
2857 return N1;
2858 break;
2859 }
2860
2861 // Memoize node if it doesn't produce a flag.
2862 SDNode *N;
2863 SDVTList VTs = getVTList(VT);
2864 if (VT != MVT::Flag) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002865 SDValue Ops[] = { N1, N2, N3 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 FoldingSetNodeID ID;
2867 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2868 void *IP = 0;
2869 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002870 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002871 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002872 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 CSEMap.InsertNode(N, IP);
2874 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002875 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002876 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002877 }
2878 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002879#ifndef NDEBUG
2880 VerifyNode(N);
2881#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002882 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883}
2884
Dan Gohman8181bd12008-07-27 21:46:04 +00002885SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2886 SDValue N1, SDValue N2, SDValue N3,
2887 SDValue N4) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00002888 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3, N4);
2889}
2890
2891SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2892 SDValue N1, SDValue N2, SDValue N3,
2893 SDValue N4) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002894 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00002895 return getNode(Opcode, DL, VT, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002896}
2897
Dan Gohman8181bd12008-07-27 21:46:04 +00002898SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2899 SDValue N1, SDValue N2, SDValue N3,
2900 SDValue N4, SDValue N5) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00002901 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3, N4, N5);
2902}
2903
2904SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2905 SDValue N1, SDValue N2, SDValue N3,
2906 SDValue N4, SDValue N5) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002907 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00002908 return getNode(Opcode, DL, VT, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909}
2910
Dan Gohmane8b391e2008-04-12 04:36:06 +00002911/// getMemsetValue - Vectorized representation of the memset value
2912/// operand.
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002913static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG,
2914 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002915 unsigned NumBits = VT.isVector() ?
2916 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohmane8b391e2008-04-12 04:36:06 +00002917 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002918 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002919 unsigned Shift = 8;
Evan Cheng8c590372008-05-15 08:39:06 +00002920 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002921 Val = (Val << Shift) | Val;
2922 Shift <<= 1;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002923 }
Duncan Sands92c43912008-06-06 12:08:01 +00002924 if (VT.isInteger())
Evan Cheng8c590372008-05-15 08:39:06 +00002925 return DAG.getConstant(Val, VT);
2926 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002927 }
Evan Cheng8c590372008-05-15 08:39:06 +00002928
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00002929 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002930 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Evan Cheng8c590372008-05-15 08:39:06 +00002931 unsigned Shift = 8;
2932 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002933 Value = DAG.getNode(ISD::OR, dl, VT,
2934 DAG.getNode(ISD::SHL, dl, VT, Value,
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00002935 DAG.getConstant(Shift,
2936 TLI.getShiftAmountTy())),
2937 Value);
Evan Cheng8c590372008-05-15 08:39:06 +00002938 Shift <<= 1;
2939 }
2940
2941 return Value;
Rafael Espindola80825902007-10-19 10:41:11 +00002942}
2943
Dan Gohmane8b391e2008-04-12 04:36:06 +00002944/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2945/// used when a memcpy is turned into a memset when the source is a constant
2946/// string ptr.
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002947static SDValue getMemsetStringVal(MVT VT, DebugLoc dl, SelectionDAG &DAG,
Dan Gohmane8b391e2008-04-12 04:36:06 +00002948 const TargetLowering &TLI,
2949 std::string &Str, unsigned Offset) {
Evan Cheng833501d2008-06-30 07:31:25 +00002950 // Handle vector with all elements zero.
2951 if (Str.empty()) {
2952 if (VT.isInteger())
2953 return DAG.getConstant(0, VT);
2954 unsigned NumElts = VT.getVectorNumElements();
2955 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002956 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Evan Cheng833501d2008-06-30 07:31:25 +00002957 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2958 }
2959
Duncan Sands92c43912008-06-06 12:08:01 +00002960 assert(!VT.isVector() && "Can't handle vector type here!");
2961 unsigned NumBits = VT.getSizeInBits();
Evan Cheng8c590372008-05-15 08:39:06 +00002962 unsigned MSB = NumBits / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002963 uint64_t Val = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002964 if (TLI.isLittleEndian())
2965 Offset = Offset + MSB - 1;
2966 for (unsigned i = 0; i != MSB; ++i) {
2967 Val = (Val << 8) | (unsigned char)Str[Offset];
2968 Offset += TLI.isLittleEndian() ? -1 : 1;
2969 }
2970 return DAG.getConstant(Val, VT);
Rafael Espindola80825902007-10-19 10:41:11 +00002971}
2972
Dan Gohmane8b391e2008-04-12 04:36:06 +00002973/// getMemBasePlusOffset - Returns base and offset node for the
Evan Cheng8c590372008-05-15 08:39:06 +00002974///
Dan Gohman8181bd12008-07-27 21:46:04 +00002975static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohmane8b391e2008-04-12 04:36:06 +00002976 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002977 MVT VT = Base.getValueType();
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002978 return DAG.getNode(ISD::ADD, Base.getNode()->getDebugLoc(),
2979 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohmane8b391e2008-04-12 04:36:06 +00002980}
2981
Evan Cheng8c590372008-05-15 08:39:06 +00002982/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2983///
Dan Gohman8181bd12008-07-27 21:46:04 +00002984static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Cheng8c590372008-05-15 08:39:06 +00002985 unsigned SrcDelta = 0;
2986 GlobalAddressSDNode *G = NULL;
2987 if (Src.getOpcode() == ISD::GlobalAddress)
2988 G = cast<GlobalAddressSDNode>(Src);
2989 else if (Src.getOpcode() == ISD::ADD &&
2990 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2991 Src.getOperand(1).getOpcode() == ISD::Constant) {
2992 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002993 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Cheng8c590372008-05-15 08:39:06 +00002994 }
2995 if (!G)
2996 return false;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002997
Evan Cheng8c590372008-05-15 08:39:06 +00002998 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng833501d2008-06-30 07:31:25 +00002999 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3000 return true;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003001
Evan Cheng8c590372008-05-15 08:39:06 +00003002 return false;
3003}
Dan Gohmane8b391e2008-04-12 04:36:06 +00003004
Evan Cheng8c590372008-05-15 08:39:06 +00003005/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3006/// to replace the memset / memcpy is below the threshold. It also returns the
3007/// types of the sequence of memory ops to perform memset / memcpy.
3008static
Duncan Sands92c43912008-06-06 12:08:01 +00003009bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman8181bd12008-07-27 21:46:04 +00003010 SDValue Dst, SDValue Src,
Evan Cheng8c590372008-05-15 08:39:06 +00003011 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng833501d2008-06-30 07:31:25 +00003012 std::string &Str, bool &isSrcStr,
Evan Cheng8c590372008-05-15 08:39:06 +00003013 SelectionDAG &DAG,
3014 const TargetLowering &TLI) {
Evan Cheng833501d2008-06-30 07:31:25 +00003015 isSrcStr = isMemSrcFromString(Src, Str);
Evan Cheng8c590372008-05-15 08:39:06 +00003016 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng833501d2008-06-30 07:31:25 +00003017 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Chris Lattner23d3bdf2008-10-28 07:10:51 +00003018 MVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Cheng8c590372008-05-15 08:39:06 +00003019 if (VT != MVT::iAny) {
3020 unsigned NewAlign = (unsigned)
Duncan Sands92c43912008-06-06 12:08:01 +00003021 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng8c590372008-05-15 08:39:06 +00003022 // If source is a string constant, this will require an unaligned load.
3023 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
3024 if (Dst.getOpcode() != ISD::FrameIndex) {
3025 // Can't change destination alignment. It requires a unaligned store.
3026 if (AllowUnalign)
3027 VT = MVT::iAny;
3028 } else {
3029 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
3030 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3031 if (MFI->isFixedObjectIndex(FI)) {
3032 // Can't change destination alignment. It requires a unaligned store.
3033 if (AllowUnalign)
3034 VT = MVT::iAny;
3035 } else {
Evan Cheng36cd8862008-06-04 23:37:54 +00003036 // Give the stack frame object a larger alignment if needed.
3037 if (MFI->getObjectAlignment(FI) < NewAlign)
3038 MFI->setObjectAlignment(FI, NewAlign);
Evan Cheng8c590372008-05-15 08:39:06 +00003039 Align = NewAlign;
3040 }
3041 }
3042 }
3043 }
3044
3045 if (VT == MVT::iAny) {
3046 if (AllowUnalign) {
3047 VT = MVT::i64;
3048 } else {
3049 switch (Align & 7) {
3050 case 0: VT = MVT::i64; break;
3051 case 4: VT = MVT::i32; break;
3052 case 2: VT = MVT::i16; break;
3053 default: VT = MVT::i8; break;
3054 }
3055 }
3056
Duncan Sands92c43912008-06-06 12:08:01 +00003057 MVT LVT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00003058 while (!TLI.isTypeLegal(LVT))
Duncan Sands92c43912008-06-06 12:08:01 +00003059 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
3060 assert(LVT.isInteger());
Evan Cheng8c590372008-05-15 08:39:06 +00003061
Duncan Sandsec142ee2008-06-08 20:54:56 +00003062 if (VT.bitsGT(LVT))
Evan Cheng8c590372008-05-15 08:39:06 +00003063 VT = LVT;
3064 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003065
3066 unsigned NumMemOps = 0;
3067 while (Size != 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00003068 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003069 while (VTSize > Size) {
Evan Cheng8c590372008-05-15 08:39:06 +00003070 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands92c43912008-06-06 12:08:01 +00003071 if (VT.isVector()) {
Evan Cheng8c590372008-05-15 08:39:06 +00003072 VT = MVT::i64;
3073 while (!TLI.isTypeLegal(VT))
Duncan Sands92c43912008-06-06 12:08:01 +00003074 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
3075 VTSize = VT.getSizeInBits() / 8;
Evan Cheng8c590372008-05-15 08:39:06 +00003076 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00003077 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Cheng8c590372008-05-15 08:39:06 +00003078 VTSize >>= 1;
3079 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003080 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003081
3082 if (++NumMemOps > Limit)
3083 return false;
3084 MemOps.push_back(VT);
3085 Size -= VTSize;
3086 }
3087
3088 return true;
3089}
3090
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003091static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00003092 SDValue Chain, SDValue Dst,
3093 SDValue Src, uint64_t Size,
Evan Cheng8c590372008-05-15 08:39:06 +00003094 unsigned Align, bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00003095 const Value *DstSV, uint64_t DstSVOff,
3096 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00003097 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3098
Dan Gohman42d311c2008-05-29 19:42:22 +00003099 // Expand memcpy to a series of load and store ops if the size operand falls
3100 // below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00003101 std::vector<MVT> MemOps;
Dan Gohman88566ae2008-10-03 17:56:45 +00003102 uint64_t Limit = -1ULL;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003103 if (!AlwaysInline)
3104 Limit = TLI.getMaxStoresPerMemcpy();
Evan Cheng8c590372008-05-15 08:39:06 +00003105 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng833501d2008-06-30 07:31:25 +00003106 std::string Str;
3107 bool CopyFromStr;
Evan Cheng8c590372008-05-15 08:39:06 +00003108 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng833501d2008-06-30 07:31:25 +00003109 Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003110 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003111
Dan Gohmane8b391e2008-04-12 04:36:06 +00003112
Evan Cheng833501d2008-06-30 07:31:25 +00003113 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman8181bd12008-07-27 21:46:04 +00003114 SmallVector<SDValue, 8> OutChains;
Evan Cheng8c590372008-05-15 08:39:06 +00003115 unsigned NumMemOps = MemOps.size();
Evan Cheng833501d2008-06-30 07:31:25 +00003116 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003117 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00003118 MVT VT = MemOps[i];
3119 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003120 SDValue Value, Store;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003121
Evan Cheng833501d2008-06-30 07:31:25 +00003122 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Cheng8c590372008-05-15 08:39:06 +00003123 // It's unlikely a store of a vector immediate can be done in a single
3124 // instruction. It would require a load from a constantpool first.
Evan Cheng833501d2008-06-30 07:31:25 +00003125 // We also handle store a vector with all zero's.
3126 // FIXME: Handle other cases where store of vector immediate is done in
3127 // a single instruction.
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003128 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
3129 Store = DAG.getStore(Chain, dl, Value,
Evan Cheng8c590372008-05-15 08:39:06 +00003130 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng757ec312008-07-09 06:38:06 +00003131 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003132 } else {
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003133 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003134 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman65118f42008-04-28 17:15:20 +00003135 SrcSV, SrcSVOff + SrcOff, false, Align);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003136 Store = DAG.getStore(Chain, dl, Value,
Evan Cheng8c590372008-05-15 08:39:06 +00003137 getMemBasePlusOffset(Dst, DstOff, DAG),
3138 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003139 }
3140 OutChains.push_back(Store);
3141 SrcOff += VTSize;
3142 DstOff += VTSize;
3143 }
3144
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003145 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003146 &OutChains[0], OutChains.size());
3147}
3148
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003149static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00003150 SDValue Chain, SDValue Dst,
3151 SDValue Src, uint64_t Size,
Dan Gohman42d311c2008-05-29 19:42:22 +00003152 unsigned Align, bool AlwaysInline,
3153 const Value *DstSV, uint64_t DstSVOff,
3154 const Value *SrcSV, uint64_t SrcSVOff){
3155 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3156
3157 // Expand memmove to a series of load and store ops if the size operand falls
3158 // below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00003159 std::vector<MVT> MemOps;
Dan Gohman88566ae2008-10-03 17:56:45 +00003160 uint64_t Limit = -1ULL;
Dan Gohman42d311c2008-05-29 19:42:22 +00003161 if (!AlwaysInline)
3162 Limit = TLI.getMaxStoresPerMemmove();
3163 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng833501d2008-06-30 07:31:25 +00003164 std::string Str;
3165 bool CopyFromStr;
Dan Gohman42d311c2008-05-29 19:42:22 +00003166 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng833501d2008-06-30 07:31:25 +00003167 Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003168 return SDValue();
Dan Gohman42d311c2008-05-29 19:42:22 +00003169
Dan Gohman42d311c2008-05-29 19:42:22 +00003170 uint64_t SrcOff = 0, DstOff = 0;
3171
Dan Gohman8181bd12008-07-27 21:46:04 +00003172 SmallVector<SDValue, 8> LoadValues;
3173 SmallVector<SDValue, 8> LoadChains;
3174 SmallVector<SDValue, 8> OutChains;
Dan Gohman42d311c2008-05-29 19:42:22 +00003175 unsigned NumMemOps = MemOps.size();
3176 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00003177 MVT VT = MemOps[i];
3178 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003179 SDValue Value, Store;
Dan Gohman42d311c2008-05-29 19:42:22 +00003180
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003181 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman42d311c2008-05-29 19:42:22 +00003182 getMemBasePlusOffset(Src, SrcOff, DAG),
3183 SrcSV, SrcSVOff + SrcOff, false, Align);
3184 LoadValues.push_back(Value);
3185 LoadChains.push_back(Value.getValue(1));
3186 SrcOff += VTSize;
3187 }
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003188 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman42d311c2008-05-29 19:42:22 +00003189 &LoadChains[0], LoadChains.size());
3190 OutChains.clear();
3191 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00003192 MVT VT = MemOps[i];
3193 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003194 SDValue Value, Store;
Dan Gohman42d311c2008-05-29 19:42:22 +00003195
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003196 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman42d311c2008-05-29 19:42:22 +00003197 getMemBasePlusOffset(Dst, DstOff, DAG),
3198 DstSV, DstSVOff + DstOff, false, DstAlign);
3199 OutChains.push_back(Store);
3200 DstOff += VTSize;
3201 }
3202
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003203 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman42d311c2008-05-29 19:42:22 +00003204 &OutChains[0], OutChains.size());
3205}
3206
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003207static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00003208 SDValue Chain, SDValue Dst,
3209 SDValue Src, uint64_t Size,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003210 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00003211 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003212 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3213
3214 // Expand memset to a series of load/store ops if the size operand
3215 // falls below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00003216 std::vector<MVT> MemOps;
Evan Cheng833501d2008-06-30 07:31:25 +00003217 std::string Str;
3218 bool CopyFromStr;
Evan Cheng8c590372008-05-15 08:39:06 +00003219 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng833501d2008-06-30 07:31:25 +00003220 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003221 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003222
Dan Gohman8181bd12008-07-27 21:46:04 +00003223 SmallVector<SDValue, 8> OutChains;
Dan Gohman65118f42008-04-28 17:15:20 +00003224 uint64_t DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003225
3226 unsigned NumMemOps = MemOps.size();
3227 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00003228 MVT VT = MemOps[i];
3229 unsigned VTSize = VT.getSizeInBits() / 8;
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003230 SDValue Value = getMemsetValue(Src, VT, DAG, dl);
3231 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner23d3bdf2008-10-28 07:10:51 +00003232 getMemBasePlusOffset(Dst, DstOff, DAG),
3233 DstSV, DstSVOff + DstOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003234 OutChains.push_back(Store);
3235 DstOff += VTSize;
3236 }
3237
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003238 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003239 &OutChains[0], OutChains.size());
3240}
3241
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003242SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003243 SDValue Src, SDValue Size,
3244 unsigned Align, bool AlwaysInline,
3245 const Value *DstSV, uint64_t DstSVOff,
3246 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003247
3248 // Check to see if we should lower the memcpy to loads and stores first.
3249 // For cases within the target-specified limits, this is the best choice.
3250 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3251 if (ConstantSize) {
3252 // Memcpy with size zero? Just return the original chain.
3253 if (ConstantSize->isNullValue())
3254 return Chain;
3255
Dan Gohman8181bd12008-07-27 21:46:04 +00003256 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003257 getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003258 ConstantSize->getZExtValue(),
Dan Gohman65118f42008-04-28 17:15:20 +00003259 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
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 memcpy 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 =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003267 TLI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003268 AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00003269 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003270 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003271 return Result;
3272
3273 // If we really need inline code and the target declined to provide it,
3274 // use a (potentially long) sequence of loads and stores.
3275 if (AlwaysInline) {
3276 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003277 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003278 ConstantSize->getZExtValue(), Align, true,
Dan Gohman65118f42008-04-28 17:15:20 +00003279 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003280 }
3281
3282 // Emit a library call.
3283 TargetLowering::ArgListTy Args;
3284 TargetLowering::ArgListEntry Entry;
3285 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3286 Entry.Node = Dst; Args.push_back(Entry);
3287 Entry.Node = Src; Args.push_back(Entry);
3288 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003289 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003290 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003291 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00003292 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00003293 getExternalSymbol("memcpy", TLI.getPointerTy()),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003294 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003295 return CallResult.second;
3296}
3297
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003298SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003299 SDValue Src, SDValue Size,
3300 unsigned Align,
3301 const Value *DstSV, uint64_t DstSVOff,
3302 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003303
Dan Gohman42d311c2008-05-29 19:42:22 +00003304 // Check to see if we should lower the memmove to loads and stores first.
3305 // For cases within the target-specified limits, this is the best choice.
3306 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3307 if (ConstantSize) {
3308 // Memmove with size zero? Just return the original chain.
3309 if (ConstantSize->isNullValue())
3310 return Chain;
3311
Dan Gohman8181bd12008-07-27 21:46:04 +00003312 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003313 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003314 ConstantSize->getZExtValue(),
Dan Gohman42d311c2008-05-29 19:42:22 +00003315 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003316 if (Result.getNode())
Dan Gohman42d311c2008-05-29 19:42:22 +00003317 return Result;
3318 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003319
3320 // Then check to see if we should lower the memmove with target-specific
3321 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003322 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003323 TLI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman65118f42008-04-28 17:15:20 +00003324 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003325 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003326 return Result;
3327
3328 // Emit a library call.
3329 TargetLowering::ArgListTy Args;
3330 TargetLowering::ArgListEntry Entry;
3331 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3332 Entry.Node = Dst; Args.push_back(Entry);
3333 Entry.Node = Src; Args.push_back(Entry);
3334 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003335 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003336 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003337 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00003338 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00003339 getExternalSymbol("memmove", TLI.getPointerTy()),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003340 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003341 return CallResult.second;
3342}
3343
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003344SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003345 SDValue Src, SDValue Size,
3346 unsigned Align,
3347 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003348
3349 // Check to see if we should lower the memset to stores first.
3350 // For cases within the target-specified limits, this is the best choice.
3351 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3352 if (ConstantSize) {
3353 // Memset with size zero? Just return the original chain.
3354 if (ConstantSize->isNullValue())
3355 return Chain;
3356
Dan Gohman8181bd12008-07-27 21:46:04 +00003357 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003358 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003359 Align, DstSV, DstSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003360 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003361 return Result;
3362 }
3363
3364 // Then check to see if we should lower the memset with target-specific
3365 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003366 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003367 TLI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align,
Bill Wendling4b2e3782008-10-01 00:59:58 +00003368 DstSV, DstSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003369 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003370 return Result;
3371
3372 // Emit a library call.
3373 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3374 TargetLowering::ArgListTy Args;
3375 TargetLowering::ArgListEntry Entry;
3376 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3377 Args.push_back(Entry);
3378 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sandsec142ee2008-06-08 20:54:56 +00003379 if (Src.getValueType().bitsGT(MVT::i32))
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003380 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003381 else
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003382 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003383 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3384 Args.push_back(Entry);
3385 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3386 Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003387 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003388 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00003389 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen67cc9b62008-09-26 19:31:26 +00003390 false, false, false, false, CallingConv::C, false,
Bill Wendlingfef06052008-09-16 21:48:12 +00003391 getExternalSymbol("memset", TLI.getPointerTy()),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003392 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003393 return CallResult.second;
Rafael Espindola80825902007-10-19 10:41:11 +00003394}
3395
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003396SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3397 SDValue Chain,
3398 SDValue Ptr, SDValue Cmp,
3399 SDValue Swp, const Value* PtrVal,
3400 unsigned Alignment) {
3401 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3402 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3403
3404 MVT VT = Cmp.getValueType();
3405
3406 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3407 Alignment = getMVTAlignment(MemVT);
3408
3409 SDVTList VTs = getVTList(VT, MVT::Other);
3410 FoldingSetNodeID ID;
Dan Gohman9d0f5022009-02-03 00:08:45 +00003411 ID.AddInteger(MemVT.getRawBits());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003412 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3413 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3414 void* IP = 0;
3415 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3416 return SDValue(E, 0);
3417 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3418 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3419 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
3420 CSEMap.InsertNode(N, IP);
3421 AllNodes.push_back(N);
3422 return SDValue(N, 0);
3423}
3424
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003425SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3426 SDValue Chain,
3427 SDValue Ptr, SDValue Val,
3428 const Value* PtrVal,
3429 unsigned Alignment) {
3430 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3431 Opcode == ISD::ATOMIC_LOAD_SUB ||
3432 Opcode == ISD::ATOMIC_LOAD_AND ||
3433 Opcode == ISD::ATOMIC_LOAD_OR ||
3434 Opcode == ISD::ATOMIC_LOAD_XOR ||
3435 Opcode == ISD::ATOMIC_LOAD_NAND ||
3436 Opcode == ISD::ATOMIC_LOAD_MIN ||
3437 Opcode == ISD::ATOMIC_LOAD_MAX ||
3438 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3439 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3440 Opcode == ISD::ATOMIC_SWAP) &&
3441 "Invalid Atomic Op");
3442
3443 MVT VT = Val.getValueType();
3444
3445 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3446 Alignment = getMVTAlignment(MemVT);
3447
3448 SDVTList VTs = getVTList(VT, MVT::Other);
3449 FoldingSetNodeID ID;
Dan Gohman9d0f5022009-02-03 00:08:45 +00003450 ID.AddInteger(MemVT.getRawBits());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003451 SDValue Ops[] = {Chain, Ptr, Val};
3452 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3453 void* IP = 0;
3454 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3455 return SDValue(E, 0);
3456 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3457 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3458 Chain, Ptr, Val, PtrVal, Alignment);
3459 CSEMap.InsertNode(N, IP);
3460 AllNodes.push_back(N);
3461 return SDValue(N, 0);
3462}
3463
Duncan Sands698842f2008-07-02 17:40:58 +00003464/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3465/// Allowed to return something different (and simpler) if Simplify is true.
Duncan Sands42d7bb82008-12-01 11:41:29 +00003466SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps) {
3467 if (NumOps == 1)
Duncan Sands698842f2008-07-02 17:40:58 +00003468 return Ops[0];
3469
3470 SmallVector<MVT, 4> VTs;
3471 VTs.reserve(NumOps);
3472 for (unsigned i = 0; i < NumOps; ++i)
3473 VTs.push_back(Ops[i].getValueType());
3474 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3475}
3476
Dale Johannesenf3b91132009-02-02 20:47:48 +00003477/// DebugLoc-aware version.
3478SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3479 DebugLoc dl) {
3480 if (NumOps == 1)
3481 return Ops[0];
3482
3483 SmallVector<MVT, 4> VTs;
3484 VTs.reserve(NumOps);
3485 for (unsigned i = 0; i < NumOps; ++i)
3486 VTs.push_back(Ops[i].getValueType());
3487 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
3488 Ops, NumOps);
3489}
3490
Dan Gohman8181bd12008-07-27 21:46:04 +00003491SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003492SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
3493 const MVT *VTs, unsigned NumVTs,
3494 const SDValue *Ops, unsigned NumOps,
3495 MVT MemVT, const Value *srcValue, int SVOff,
3496 unsigned Align, bool Vol,
3497 bool ReadMem, bool WriteMem) {
3498 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3499 MemVT, srcValue, SVOff, Align, Vol,
3500 ReadMem, WriteMem);
3501}
3502
3503SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003504SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3505 const SDValue *Ops, unsigned NumOps,
3506 MVT MemVT, const Value *srcValue, int SVOff,
3507 unsigned Align, bool Vol,
3508 bool ReadMem, bool WriteMem) {
3509 // Memoize the node unless it returns a flag.
3510 MemIntrinsicSDNode *N;
3511 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3512 FoldingSetNodeID ID;
3513 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3514 void *IP = 0;
3515 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3516 return SDValue(E, 0);
3517
3518 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3519 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3520 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3521 CSEMap.InsertNode(N, IP);
3522 } else {
3523 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3524 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3525 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3526 }
3527 AllNodes.push_back(N);
3528 return SDValue(N, 0);
3529}
3530
3531SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003532SelectionDAG::getCall(unsigned CallingConv, DebugLoc dl, bool IsVarArgs,
3533 bool IsTailCall, bool IsInreg, SDVTList VTs,
3534 const SDValue *Operands, unsigned NumOperands) {
3535 // Do not include isTailCall in the folding set profile.
3536 FoldingSetNodeID ID;
3537 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3538 ID.AddInteger(CallingConv);
3539 ID.AddInteger(IsVarArgs);
3540 void *IP = 0;
3541 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3542 // Instead of including isTailCall in the folding set, we just
3543 // set the flag of the existing node.
3544 if (!IsTailCall)
3545 cast<CallSDNode>(E)->setNotTailCall();
3546 return SDValue(E, 0);
3547 }
3548 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
3549 new (N) CallSDNode(CallingConv, dl, IsVarArgs, IsTailCall, IsInreg,
3550 VTs, Operands, NumOperands);
3551 CSEMap.InsertNode(N, IP);
3552 AllNodes.push_back(N);
3553 return SDValue(N, 0);
3554}
3555
3556SDValue
Duncan Sandsb2589712008-03-28 09:45:24 +00003557SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman8181bd12008-07-27 21:46:04 +00003558 MVT VT, SDValue Chain,
3559 SDValue Ptr, SDValue Offset,
Duncan Sands92c43912008-06-06 12:08:01 +00003560 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandsb2589712008-03-28 09:45:24 +00003561 bool isVolatile, unsigned Alignment) {
Dan Gohman9e3a3422008-07-08 23:46:32 +00003562 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3563 Alignment = getMVTAlignment(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003564
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003565 if (VT == EVT) {
3566 ExtType = ISD::NON_EXTLOAD;
3567 } else if (ExtType == ISD::NON_EXTLOAD) {
3568 assert(VT == EVT && "Non-extending load from different memory type!");
3569 } else {
3570 // Extending load.
Duncan Sands92c43912008-06-06 12:08:01 +00003571 if (VT.isVector())
Dan Gohman29c3cef2008-08-14 20:04:46 +00003572 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3573 "Invalid vector extload!");
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003574 else
Duncan Sandsec142ee2008-06-08 20:54:56 +00003575 assert(EVT.bitsLT(VT) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003576 "Should only be an extending load, not truncating!");
Duncan Sands92c43912008-06-06 12:08:01 +00003577 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003578 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands92c43912008-06-06 12:08:01 +00003579 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003580 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003581 }
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003582
3583 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sandsf6890712008-05-27 11:50:51 +00003584 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003585 "Unindexed load with an offset!");
3586
3587 SDVTList VTs = Indexed ?
3588 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00003589 SDValue Ops[] = { Chain, Ptr, Offset };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003590 FoldingSetNodeID ID;
3591 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003592 ID.AddInteger(EVT.getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003593 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, isVolatile, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003594 void *IP = 0;
3595 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003596 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003597 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003598 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3599 Alignment, isVolatile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003600 CSEMap.InsertNode(N, IP);
3601 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003602 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003603}
3604
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003605SDValue
3606SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
3607 ISD::LoadExtType ExtType, MVT VT, SDValue Chain,
3608 SDValue Ptr, SDValue Offset,
3609 const Value *SV, int SVOffset, MVT EVT,
3610 bool isVolatile, unsigned Alignment) {
3611 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3612 Alignment = getMVTAlignment(VT);
3613
3614 if (VT == EVT) {
3615 ExtType = ISD::NON_EXTLOAD;
3616 } else if (ExtType == ISD::NON_EXTLOAD) {
3617 assert(VT == EVT && "Non-extending load from different memory type!");
3618 } else {
3619 // Extending load.
3620 if (VT.isVector())
3621 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3622 "Invalid vector extload!");
3623 else
3624 assert(EVT.bitsLT(VT) &&
3625 "Should only be an extending load, not truncating!");
3626 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
3627 "Cannot sign/zero extend a FP/Vector load!");
3628 assert(VT.isInteger() == EVT.isInteger() &&
3629 "Cannot convert from FP to Int or Int -> FP!");
3630 }
3631
3632 bool Indexed = AM != ISD::UNINDEXED;
3633 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3634 "Unindexed load with an offset!");
3635
3636 SDVTList VTs = Indexed ?
3637 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3638 SDValue Ops[] = { Chain, Ptr, Offset };
3639 FoldingSetNodeID ID;
3640 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003641 ID.AddInteger(EVT.getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003642 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, isVolatile, Alignment));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003643 void *IP = 0;
3644 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3645 return SDValue(E, 0);
3646 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
3647 new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, EVT, SV, SVOffset,
3648 Alignment, isVolatile);
3649 CSEMap.InsertNode(N, IP);
3650 AllNodes.push_back(N);
3651 return SDValue(N, 0);
3652}
3653
Dan Gohman8181bd12008-07-27 21:46:04 +00003654SDValue SelectionDAG::getLoad(MVT VT,
3655 SDValue Chain, SDValue Ptr,
3656 const Value *SV, int SVOffset,
3657 bool isVolatile, unsigned Alignment) {
3658 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandsb2589712008-03-28 09:45:24 +00003659 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3660 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003661}
3662
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003663SDValue SelectionDAG::getLoad(MVT VT, DebugLoc dl,
3664 SDValue Chain, SDValue Ptr,
3665 const Value *SV, int SVOffset,
3666 bool isVolatile, unsigned Alignment) {
3667 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3668 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3669 SV, SVOffset, VT, isVolatile, Alignment);
3670}
3671
Dan Gohman8181bd12008-07-27 21:46:04 +00003672SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3673 SDValue Chain, SDValue Ptr,
3674 const Value *SV,
3675 int SVOffset, MVT EVT,
3676 bool isVolatile, unsigned Alignment) {
3677 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandsb2589712008-03-28 09:45:24 +00003678 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3679 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003680}
3681
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003682SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, MVT VT,
3683 SDValue Chain, SDValue Ptr,
3684 const Value *SV,
3685 int SVOffset, MVT EVT,
3686 bool isVolatile, unsigned Alignment) {
3687 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3688 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
3689 SV, SVOffset, EVT, isVolatile, Alignment);
3690}
3691
Dan Gohman8181bd12008-07-27 21:46:04 +00003692SDValue
3693SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3694 SDValue Offset, ISD::MemIndexedMode AM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003695 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3696 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3697 "Load is already a indexed load!");
Duncan Sandsb2589712008-03-28 09:45:24 +00003698 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3699 LD->getChain(), Base, Offset, LD->getSrcValue(),
3700 LD->getSrcValueOffset(), LD->getMemoryVT(),
3701 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003702}
3703
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003704SDValue
3705SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3706 SDValue Offset, ISD::MemIndexedMode AM) {
3707 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3708 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3709 "Load is already a indexed load!");
3710 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3711 LD->getChain(), Base, Offset, LD->getSrcValue(),
3712 LD->getSrcValueOffset(), LD->getMemoryVT(),
3713 LD->isVolatile(), LD->getAlignment());
3714}
3715
Dan Gohman8181bd12008-07-27 21:46:04 +00003716SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3717 SDValue Ptr, const Value *SV, int SVOffset,
3718 bool isVolatile, unsigned Alignment) {
Duncan Sands92c43912008-06-06 12:08:01 +00003719 MVT VT = Val.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003720
Dan Gohman9e3a3422008-07-08 23:46:32 +00003721 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3722 Alignment = getMVTAlignment(VT);
3723
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003724 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00003725 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3726 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003727 FoldingSetNodeID ID;
3728 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003729 ID.AddInteger(VT.getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003730 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED,
3731 isVolatile, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003732 void *IP = 0;
3733 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003734 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003735 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003736 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3737 VT, SV, SVOffset, Alignment, isVolatile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003738 CSEMap.InsertNode(N, IP);
3739 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003740 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003741}
3742
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003743SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3744 SDValue Ptr, const Value *SV, int SVOffset,
3745 bool isVolatile, unsigned Alignment) {
3746 MVT VT = Val.getValueType();
3747
3748 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3749 Alignment = getMVTAlignment(VT);
3750
3751 SDVTList VTs = getVTList(MVT::Other);
3752 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3753 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3754 FoldingSetNodeID ID;
3755 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003756 ID.AddInteger(VT.getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003757 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED,
3758 isVolatile, Alignment));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003759 void *IP = 0;
3760 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3761 return SDValue(E, 0);
3762 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3763 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false,
3764 VT, SV, SVOffset, Alignment, isVolatile);
3765 CSEMap.InsertNode(N, IP);
3766 AllNodes.push_back(N);
3767 return SDValue(N, 0);
3768}
3769
Dan Gohman8181bd12008-07-27 21:46:04 +00003770SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3771 SDValue Ptr, const Value *SV,
3772 int SVOffset, MVT SVT,
3773 bool isVolatile, unsigned Alignment) {
Duncan Sands92c43912008-06-06 12:08:01 +00003774 MVT VT = Val.getValueType();
Duncan Sands06fcf652007-10-30 12:40:58 +00003775
3776 if (VT == SVT)
3777 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778
Duncan Sandsec142ee2008-06-08 20:54:56 +00003779 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands92c43912008-06-06 12:08:01 +00003780 assert(VT.isInteger() == SVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003781 "Can't do FP-INT conversion!");
3782
Dan Gohman9e3a3422008-07-08 23:46:32 +00003783 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3784 Alignment = getMVTAlignment(VT);
3785
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003786 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00003787 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3788 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 FoldingSetNodeID ID;
3790 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003791 ID.AddInteger(SVT.getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003792 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED,
3793 isVolatile, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003794 void *IP = 0;
3795 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003796 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003797 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003798 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3799 SVT, SV, SVOffset, Alignment, isVolatile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003800 CSEMap.InsertNode(N, IP);
3801 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003802 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003803}
3804
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003805SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3806 SDValue Ptr, const Value *SV,
3807 int SVOffset, MVT SVT,
3808 bool isVolatile, unsigned Alignment) {
3809 MVT VT = Val.getValueType();
3810
3811 if (VT == SVT)
3812 return getStore(Chain, dl, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
3813
3814 assert(VT.bitsGT(SVT) && "Not a truncation?");
3815 assert(VT.isInteger() == SVT.isInteger() &&
3816 "Can't do FP-INT conversion!");
3817
3818 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3819 Alignment = getMVTAlignment(VT);
3820
3821 SDVTList VTs = getVTList(MVT::Other);
3822 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3823 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3824 FoldingSetNodeID ID;
3825 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003826 ID.AddInteger(SVT.getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003827 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED,
3828 isVolatile, Alignment));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003829 void *IP = 0;
3830 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3831 return SDValue(E, 0);
3832 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3833 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true,
3834 SVT, SV, SVOffset, Alignment, isVolatile);
3835 CSEMap.InsertNode(N, IP);
3836 AllNodes.push_back(N);
3837 return SDValue(N, 0);
3838}
3839
Dan Gohman8181bd12008-07-27 21:46:04 +00003840SDValue
3841SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3842 SDValue Offset, ISD::MemIndexedMode AM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3844 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3845 "Store is already a indexed store!");
3846 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00003847 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003848 FoldingSetNodeID ID;
3849 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003850 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003851 ID.AddInteger(ST->getRawSubclassData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003852 void *IP = 0;
3853 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003854 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003855 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00003856 new (N) StoreSDNode(Ops, VTs, AM,
3857 ST->isTruncatingStore(), ST->getMemoryVT(),
3858 ST->getSrcValue(), ST->getSrcValueOffset(),
3859 ST->getAlignment(), ST->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003860 CSEMap.InsertNode(N, IP);
3861 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00003862 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003863}
3864
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003865SDValue
3866SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
3867 SDValue Offset, ISD::MemIndexedMode AM) {
3868 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3869 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3870 "Store is already a indexed store!");
3871 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3872 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3873 FoldingSetNodeID ID;
3874 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003875 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003876 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003877 void *IP = 0;
3878 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3879 return SDValue(E, 0);
3880 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3881 new (N) StoreSDNode(Ops, dl, VTs, AM,
3882 ST->isTruncatingStore(), ST->getMemoryVT(),
3883 ST->getSrcValue(), ST->getSrcValueOffset(),
3884 ST->getAlignment(), ST->isVolatile());
3885 CSEMap.InsertNode(N, IP);
3886 AllNodes.push_back(N);
3887 return SDValue(N, 0);
3888}
3889
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003890SDValue SelectionDAG::getVAArg(MVT VT, DebugLoc dl,
3891 SDValue Chain, SDValue Ptr,
3892 SDValue SV) {
3893 SDValue Ops[] = { Chain, Ptr, SV };
3894 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 3);
3895}
3896
Dan Gohman8181bd12008-07-27 21:46:04 +00003897SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3898 const SDUse *Ops, unsigned NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00003899 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Ops, NumOps);
3900}
3901
3902SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
3903 const SDUse *Ops, unsigned NumOps) {
Dan Gohman703ce5d2008-07-07 18:26:29 +00003904 switch (NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00003905 case 0: return getNode(Opcode, DL, VT);
3906 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3907 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3908 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman703ce5d2008-07-07 18:26:29 +00003909 default: break;
3910 }
3911
Dan Gohman8181bd12008-07-27 21:46:04 +00003912 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman703ce5d2008-07-07 18:26:29 +00003913 // the regular getNode logic.
Dan Gohman8181bd12008-07-27 21:46:04 +00003914 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendlingdf4de112009-01-28 22:17:52 +00003915 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman703ce5d2008-07-07 18:26:29 +00003916}
3917
Dan Gohman8181bd12008-07-27 21:46:04 +00003918SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3919 const SDValue *Ops, unsigned NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00003920 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Ops, NumOps);
3921}
3922
3923SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
3924 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003925 switch (NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00003926 case 0: return getNode(Opcode, DL, VT);
3927 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3928 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3929 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003930 default: break;
3931 }
3932
3933 switch (Opcode) {
3934 default: break;
3935 case ISD::SELECT_CC: {
3936 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
3937 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3938 "LHS and RHS of condition must have same type!");
3939 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3940 "True and False arms of SelectCC must have same type!");
3941 assert(Ops[2].getValueType() == VT &&
3942 "select_cc node must be of same type as true and false value!");
3943 break;
3944 }
3945 case ISD::BR_CC: {
3946 assert(NumOps == 5 && "BR_CC takes 5 operands!");
3947 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3948 "LHS/RHS of comparison should match types!");
3949 break;
3950 }
3951 }
3952
3953 // Memoize nodes.
3954 SDNode *N;
3955 SDVTList VTs = getVTList(VT);
Bill Wendlingdf4de112009-01-28 22:17:52 +00003956
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957 if (VT != MVT::Flag) {
3958 FoldingSetNodeID ID;
3959 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
3960 void *IP = 0;
Bill Wendlingdf4de112009-01-28 22:17:52 +00003961
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003963 return SDValue(E, 0);
Bill Wendlingdf4de112009-01-28 22:17:52 +00003964
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003965 N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00003966 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003967 CSEMap.InsertNode(N, IP);
3968 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003969 N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00003970 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003971 }
Bill Wendlingdf4de112009-01-28 22:17:52 +00003972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003974#ifndef NDEBUG
3975 VerifyNode(N);
3976#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00003977 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978}
3979
Dan Gohman8181bd12008-07-27 21:46:04 +00003980SDValue SelectionDAG::getNode(unsigned Opcode,
3981 const std::vector<MVT> &ResultTys,
3982 const SDValue *Ops, unsigned NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00003983 return getNode(Opcode, DebugLoc::getUnknownLoc(), ResultTys, Ops, NumOps);
3984}
3985
3986SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
3987 const std::vector<MVT> &ResultTys,
3988 const SDValue *Ops, unsigned NumOps) {
3989 return getNode(Opcode, DL, getNodeValueTypes(ResultTys), ResultTys.size(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003990 Ops, NumOps);
3991}
3992
Dan Gohman8181bd12008-07-27 21:46:04 +00003993SDValue SelectionDAG::getNode(unsigned Opcode,
3994 const MVT *VTs, unsigned NumVTs,
3995 const SDValue *Ops, unsigned NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00003996 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTs, NumVTs, Ops, NumOps);
3997}
3998
3999SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
4000 const MVT *VTs, unsigned NumVTs,
4001 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002 if (NumVTs == 1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004003 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4004 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005}
4006
Dan Gohman8181bd12008-07-27 21:46:04 +00004007SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4008 const SDValue *Ops, unsigned NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00004009 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, Ops, NumOps);
4010}
4011
4012SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4013 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004014 if (VTList.NumVTs == 1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004015 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016
4017 switch (Opcode) {
4018 // FIXME: figure out how to safely handle things like
4019 // int foo(int x) { return 1 << (x & 255); }
4020 // int bar() { return foo(256); }
4021#if 0
4022 case ISD::SRA_PARTS:
4023 case ISD::SRL_PARTS:
4024 case ISD::SHL_PARTS:
4025 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
4026 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004027 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028 else if (N3.getOpcode() == ISD::AND)
4029 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4030 // If the and is only masking out bits that cannot effect the shift,
4031 // eliminate the and.
Duncan Sands92c43912008-06-06 12:08:01 +00004032 unsigned NumBits = VT.getSizeInBits()*2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004033 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004034 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 }
4036 break;
4037#endif
4038 }
4039
4040 // Memoize the node unless it returns a flag.
4041 SDNode *N;
4042 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4043 FoldingSetNodeID ID;
4044 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4045 void *IP = 0;
4046 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00004047 return SDValue(E, 0);
Dan Gohmaned825d12008-07-07 23:02:41 +00004048 if (NumOps == 1) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004049 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004050 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004051 } else if (NumOps == 2) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004052 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004053 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004054 } else if (NumOps == 3) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004055 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004056 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004057 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004058 N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004059 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohmaned825d12008-07-07 23:02:41 +00004060 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004061 CSEMap.InsertNode(N, IP);
4062 } else {
Dan Gohmaned825d12008-07-07 23:02:41 +00004063 if (NumOps == 1) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004064 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004065 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004066 } else if (NumOps == 2) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004067 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004068 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004069 } else if (NumOps == 3) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004070 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004071 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004072 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004073 N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004074 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohmaned825d12008-07-07 23:02:41 +00004075 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004076 }
4077 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00004078#ifndef NDEBUG
4079 VerifyNode(N);
4080#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00004081 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004082}
4083
Dan Gohman8181bd12008-07-27 21:46:04 +00004084SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00004085 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList);
4086}
4087
4088SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4089 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman798d1272007-10-08 15:49:58 +00004090}
4091
Dan Gohman8181bd12008-07-27 21:46:04 +00004092SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Bill Wendlingdf4de112009-01-28 22:17:52 +00004093 SDValue N1) {
4094 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1);
4095}
4096
4097SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4098 SDValue N1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004099 SDValue Ops[] = { N1 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004100 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman798d1272007-10-08 15:49:58 +00004101}
4102
Dan Gohman8181bd12008-07-27 21:46:04 +00004103SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4104 SDValue N1, SDValue N2) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00004105 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2);
4106}
4107
4108SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4109 SDValue N1, SDValue N2) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004110 SDValue Ops[] = { N1, N2 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004111 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman798d1272007-10-08 15:49:58 +00004112}
4113
Dan Gohman8181bd12008-07-27 21:46:04 +00004114SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4115 SDValue N1, SDValue N2, SDValue N3) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00004116 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3);
4117}
4118
4119SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4120 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004121 SDValue Ops[] = { N1, N2, N3 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004122 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman798d1272007-10-08 15:49:58 +00004123}
4124
Dan Gohman8181bd12008-07-27 21:46:04 +00004125SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4126 SDValue N1, SDValue N2, SDValue N3,
4127 SDValue N4) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00004128 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3, N4);
4129}
4130
4131SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4132 SDValue N1, SDValue N2, SDValue N3,
4133 SDValue N4) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004134 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004135 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman798d1272007-10-08 15:49:58 +00004136}
4137
Dan Gohman8181bd12008-07-27 21:46:04 +00004138SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4139 SDValue N1, SDValue N2, SDValue N3,
4140 SDValue N4, SDValue N5) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00004141 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3, N4, N5);
4142}
4143
4144SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4145 SDValue N1, SDValue N2, SDValue N3,
4146 SDValue N4, SDValue N5) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004147 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004148 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman798d1272007-10-08 15:49:58 +00004149}
4150
Duncan Sands92c43912008-06-06 12:08:01 +00004151SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00004152 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004153}
4154
Duncan Sands92c43912008-06-06 12:08:01 +00004155SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004156 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4157 E = VTList.rend(); I != E; ++I)
4158 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4159 return *I;
4160
4161 MVT *Array = Allocator.Allocate<MVT>(2);
4162 Array[0] = VT1;
4163 Array[1] = VT2;
4164 SDVTList Result = makeVTList(Array, 2);
4165 VTList.push_back(Result);
4166 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167}
Dan Gohmanbd68c792008-07-17 19:10:17 +00004168
4169SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
4170 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4171 E = VTList.rend(); I != E; ++I)
4172 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4173 I->VTs[2] == VT3)
4174 return *I;
4175
4176 MVT *Array = Allocator.Allocate<MVT>(3);
4177 Array[0] = VT1;
4178 Array[1] = VT2;
4179 Array[2] = VT3;
4180 SDVTList Result = makeVTList(Array, 3);
4181 VTList.push_back(Result);
4182 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004183}
4184
Bill Wendling7a4076f2008-12-01 23:28:22 +00004185SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
4186 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4187 E = VTList.rend(); I != E; ++I)
4188 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4189 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4190 return *I;
4191
4192 MVT *Array = Allocator.Allocate<MVT>(3);
4193 Array[0] = VT1;
4194 Array[1] = VT2;
4195 Array[2] = VT3;
4196 Array[3] = VT4;
4197 SDVTList Result = makeVTList(Array, 4);
4198 VTList.push_back(Result);
4199 return Result;
4200}
4201
Duncan Sands92c43912008-06-06 12:08:01 +00004202SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203 switch (NumVTs) {
4204 case 0: assert(0 && "Cannot have nodes without results!");
4205 case 1: return getVTList(VTs[0]);
4206 case 2: return getVTList(VTs[0], VTs[1]);
4207 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
4208 default: break;
4209 }
4210
Dan Gohmanbd68c792008-07-17 19:10:17 +00004211 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4212 E = VTList.rend(); I != E; ++I) {
4213 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4214 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215
4216 bool NoMatch = false;
4217 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmanbd68c792008-07-17 19:10:17 +00004218 if (VTs[i] != I->VTs[i]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 NoMatch = true;
4220 break;
4221 }
4222 if (!NoMatch)
Dan Gohmanbd68c792008-07-17 19:10:17 +00004223 return *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224 }
4225
Dan Gohmanbd68c792008-07-17 19:10:17 +00004226 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
4227 std::copy(VTs, VTs+NumVTs, Array);
4228 SDVTList Result = makeVTList(Array, NumVTs);
4229 VTList.push_back(Result);
4230 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004231}
4232
4233
4234/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4235/// specified operands. If the resultant node already exists in the DAG,
4236/// this does not modify the specified node, instead it returns the node that
4237/// already exists. If the resultant node does not exist in the DAG, the
4238/// input node is returned. As a degenerate case, if you specify the same
4239/// input operands as the node already has, the input node is returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00004240SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004241 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
4243
4244 // Check to see if there is no change.
4245 if (Op == N->getOperand(0)) return InN;
4246
4247 // See if the modified node already exists.
4248 void *InsertPos = 0;
4249 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004250 return SDValue(Existing, InN.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251
Dan Gohman7d919ea2008-07-21 22:38:59 +00004252 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004254 if (!RemoveNodeFromCSEMaps(N))
4255 InsertPos = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256
4257 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004258 N->OperandList[0].set(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259
4260 // If this gets put into a CSE map, add it.
4261 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4262 return InN;
4263}
4264
Dan Gohman8181bd12008-07-27 21:46:04 +00004265SDValue SelectionDAG::
4266UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004267 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004268 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
4269
4270 // Check to see if there is no change.
4271 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4272 return InN; // No operands changed, just return the input node.
4273
4274 // See if the modified node already exists.
4275 void *InsertPos = 0;
4276 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004277 return SDValue(Existing, InN.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278
Dan Gohman7d919ea2008-07-21 22:38:59 +00004279 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004280 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004281 if (!RemoveNodeFromCSEMaps(N))
4282 InsertPos = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004283
4284 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004285 if (N->OperandList[0] != Op1)
4286 N->OperandList[0].set(Op1);
4287 if (N->OperandList[1] != Op2)
4288 N->OperandList[1].set(Op2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289
4290 // If this gets put into a CSE map, add it.
4291 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4292 return InN;
4293}
4294
Dan Gohman8181bd12008-07-27 21:46:04 +00004295SDValue SelectionDAG::
4296UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4297 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004298 return UpdateNodeOperands(N, Ops, 3);
4299}
4300
Dan Gohman8181bd12008-07-27 21:46:04 +00004301SDValue SelectionDAG::
4302UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4303 SDValue Op3, SDValue Op4) {
4304 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305 return UpdateNodeOperands(N, Ops, 4);
4306}
4307
Dan Gohman8181bd12008-07-27 21:46:04 +00004308SDValue SelectionDAG::
4309UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4310 SDValue Op3, SDValue Op4, SDValue Op5) {
4311 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 return UpdateNodeOperands(N, Ops, 5);
4313}
4314
Dan Gohman8181bd12008-07-27 21:46:04 +00004315SDValue SelectionDAG::
4316UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004317 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004318 assert(N->getNumOperands() == NumOps &&
4319 "Update with wrong number of operands");
4320
4321 // Check to see if there is no change.
4322 bool AnyChange = false;
4323 for (unsigned i = 0; i != NumOps; ++i) {
4324 if (Ops[i] != N->getOperand(i)) {
4325 AnyChange = true;
4326 break;
4327 }
4328 }
4329
4330 // No operands changed, just return the input node.
4331 if (!AnyChange) return InN;
4332
4333 // See if the modified node already exists.
4334 void *InsertPos = 0;
4335 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004336 return SDValue(Existing, InN.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337
Dan Gohman14a027d2008-05-02 00:05:03 +00004338 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004340 if (!RemoveNodeFromCSEMaps(N))
4341 InsertPos = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342
4343 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004344 for (unsigned i = 0; i != NumOps; ++i)
4345 if (N->OperandList[i] != Ops[i])
4346 N->OperandList[i].set(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347
4348 // If this gets put into a CSE map, add it.
4349 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4350 return InN;
4351}
4352
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004353/// DropOperands - Release the operands and set this node to have
Dan Gohmanbd68c792008-07-17 19:10:17 +00004354/// zero operands.
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004355void SDNode::DropOperands() {
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004356 // Unlike the code in MorphNodeTo that does this, we don't need to
4357 // watch for dead nodes here.
djgc2517d32009-01-26 04:35:06 +00004358 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4359 SDUse &Use = *I++;
4360 Use.set(SDValue());
4361 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362}
4363
Dan Gohmanbd68c792008-07-17 19:10:17 +00004364/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4365/// machine opcode.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366///
Dan Gohmanbd68c792008-07-17 19:10:17 +00004367SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00004368 MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004369 SDVTList VTs = getVTList(VT);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004370 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371}
4372
Dan Gohmanbd68c792008-07-17 19:10:17 +00004373SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004374 MVT VT, SDValue Op1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004375 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004376 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004377 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378}
4379
Dan Gohmanbd68c792008-07-17 19:10:17 +00004380SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004381 MVT VT, SDValue Op1,
4382 SDValue Op2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004383 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004384 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004385 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004386}
4387
Dan Gohmanbd68c792008-07-17 19:10:17 +00004388SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004389 MVT VT, SDValue Op1,
4390 SDValue Op2, SDValue Op3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004391 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004392 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004393 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004394}
4395
Dan Gohmanbd68c792008-07-17 19:10:17 +00004396SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004397 MVT VT, const SDValue *Ops,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399 SDVTList VTs = getVTList(VT);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004400 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004401}
4402
Dan Gohmanbd68c792008-07-17 19:10:17 +00004403SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004404 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohman7eced112008-07-02 23:23:19 +00004405 unsigned NumOps) {
4406 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004407 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004408}
4409
Dan Gohmanbd68c792008-07-17 19:10:17 +00004410SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman7eced112008-07-02 23:23:19 +00004411 MVT VT1, MVT VT2) {
4412 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004413 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohman7eced112008-07-02 23:23:19 +00004414}
4415
Dan Gohmanbd68c792008-07-17 19:10:17 +00004416SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman703ce5d2008-07-07 18:26:29 +00004417 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004418 const SDValue *Ops, unsigned NumOps) {
Dan Gohman7eced112008-07-02 23:23:19 +00004419 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004420 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004421}
4422
Bill Wendling7a4076f2008-12-01 23:28:22 +00004423SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
4424 MVT VT1, MVT VT2, MVT VT3, MVT VT4,
4425 const SDValue *Ops, unsigned NumOps) {
4426 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4427 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4428}
4429
Dan Gohmanbd68c792008-07-17 19:10:17 +00004430SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman7eced112008-07-02 23:23:19 +00004431 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004432 SDValue Op1) {
Dan Gohman7eced112008-07-02 23:23:19 +00004433 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004434 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004435 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436}
4437
Dan Gohmanbd68c792008-07-17 19:10:17 +00004438SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00004439 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004440 SDValue Op1, SDValue Op2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004441 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004442 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004443 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444}
4445
Dan Gohmanbd68c792008-07-17 19:10:17 +00004446SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00004447 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004448 SDValue Op1, SDValue Op2,
4449 SDValue Op3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004450 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004451 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004452 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman7eced112008-07-02 23:23:19 +00004453}
4454
Dan Gohmanbd68c792008-07-17 19:10:17 +00004455SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Bill Wendling7a4076f2008-12-01 23:28:22 +00004456 MVT VT1, MVT VT2, MVT VT3,
4457 SDValue Op1, SDValue Op2,
4458 SDValue Op3) {
4459 SDVTList VTs = getVTList(VT1, VT2, VT3);
4460 SDValue Ops[] = { Op1, Op2, Op3 };
4461 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4462}
4463
4464SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004465 SDVTList VTs, const SDValue *Ops,
Dan Gohman7eced112008-07-02 23:23:19 +00004466 unsigned NumOps) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004467 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4468}
4469
4470SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4471 MVT VT) {
4472 SDVTList VTs = getVTList(VT);
4473 return MorphNodeTo(N, Opc, VTs, 0, 0);
4474}
4475
4476SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004477 MVT VT, SDValue Op1) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004478 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004479 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004480 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4481}
4482
4483SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004484 MVT VT, SDValue Op1,
4485 SDValue Op2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004486 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004487 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004488 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4489}
4490
4491SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004492 MVT VT, SDValue Op1,
4493 SDValue Op2, SDValue Op3) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004494 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004495 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004496 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4497}
4498
4499SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004500 MVT VT, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004501 unsigned NumOps) {
4502 SDVTList VTs = getVTList(VT);
4503 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4504}
4505
4506SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004507 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004508 unsigned NumOps) {
4509 SDVTList VTs = getVTList(VT1, VT2);
4510 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4511}
4512
4513SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4514 MVT VT1, MVT VT2) {
4515 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004516 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004517}
4518
4519SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4520 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004521 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004522 SDVTList VTs = getVTList(VT1, VT2, VT3);
4523 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4524}
4525
4526SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4527 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004528 SDValue Op1) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004529 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004530 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004531 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4532}
4533
4534SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4535 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004536 SDValue Op1, SDValue Op2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004537 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004538 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004539 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4540}
4541
4542SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4543 MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004544 SDValue Op1, SDValue Op2,
4545 SDValue Op3) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004546 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004547 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004548 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4549}
4550
4551/// MorphNodeTo - These *mutate* the specified node to have the specified
4552/// return type, opcode, and operands.
4553///
4554/// Note that MorphNodeTo returns the resultant node. If there is already a
4555/// node of the specified opcode and operands, it returns that node instead of
4556/// the current one.
4557///
4558/// Using MorphNodeTo is faster than creating a new node and swapping it in
4559/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif8b2235c2008-08-30 22:16:05 +00004560/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmanbd68c792008-07-17 19:10:17 +00004561/// the node's users.
4562///
4563SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004564 SDVTList VTs, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004565 unsigned NumOps) {
Dan Gohman7eced112008-07-02 23:23:19 +00004566 // If an identical node already exists, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004567 void *IP = 0;
Dan Gohmanbd68c792008-07-17 19:10:17 +00004568 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4569 FoldingSetNodeID ID;
4570 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4571 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4572 return ON;
4573 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574
Dan Gohman705e3f72008-09-13 01:54:27 +00004575 if (!RemoveNodeFromCSEMaps(N))
4576 IP = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004577
Dan Gohmanbd68c792008-07-17 19:10:17 +00004578 // Start the morphing.
4579 N->NodeType = Opc;
4580 N->ValueList = VTs.VTs;
4581 N->NumValues = VTs.NumVTs;
4582
4583 // Clear the operands list, updating used nodes to remove this from their
4584 // use list. Keep track of any operands that become dead as a result.
4585 SmallPtrSet<SDNode*, 16> DeadNodeSet;
djgc2517d32009-01-26 04:35:06 +00004586 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4587 SDUse &Use = *I++;
4588 SDNode *Used = Use.getNode();
4589 Use.set(SDValue());
Dan Gohmanbd68c792008-07-17 19:10:17 +00004590 if (Used->use_empty())
4591 DeadNodeSet.insert(Used);
4592 }
4593
4594 // If NumOps is larger than the # of operands we currently have, reallocate
4595 // the operand list.
4596 if (NumOps > N->NumOperands) {
4597 if (N->OperandsNeedDelete)
4598 delete[] N->OperandList;
Bill Wendlingb75ddd22008-10-19 20:51:12 +00004599
Dan Gohmanbd68c792008-07-17 19:10:17 +00004600 if (N->isMachineOpcode()) {
4601 // We're creating a final node that will live unmorphed for the
Dan Gohman14a66442008-08-23 02:25:05 +00004602 // remainder of the current SelectionDAG iteration, so we can allocate
4603 // the operands directly out of a pool with no recycling metadata.
4604 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004605 N->OperandsNeedDelete = false;
4606 } else {
4607 N->OperandList = new SDUse[NumOps];
4608 N->OperandsNeedDelete = true;
4609 }
4610 }
4611
4612 // Assign the new operands.
4613 N->NumOperands = NumOps;
4614 for (unsigned i = 0, e = NumOps; i != e; ++i) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004615 N->OperandList[i].setUser(N);
djgc2517d32009-01-26 04:35:06 +00004616 N->OperandList[i].setInitial(Ops[i]);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004617 }
4618
4619 // Delete any nodes that are still dead after adding the uses for the
4620 // new operands.
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004621 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmanbd68c792008-07-17 19:10:17 +00004622 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4623 E = DeadNodeSet.end(); I != E; ++I)
4624 if ((*I)->use_empty())
4625 DeadNodes.push_back(*I);
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004626 RemoveDeadNodes(DeadNodes);
4627
Dan Gohmanbd68c792008-07-17 19:10:17 +00004628 if (IP)
4629 CSEMap.InsertNode(N, IP); // Memoize the new node.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630 return N;
4631}
4632
4633
4634/// getTargetNode - These are used for target selectors to create a new node
4635/// with specified return type(s), target opcode, and operands.
4636///
4637/// Note that getTargetNode returns the resultant node. If there is already a
4638/// node of the specified opcode and operands, it returns that node instead of
4639/// the current one.
Duncan Sands92c43912008-06-06 12:08:01 +00004640SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004641 return getNode(~Opcode, VT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004642}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004643SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT) {
4644 return getNode(~Opcode, dl, VT).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004645}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004646
Dan Gohman8181bd12008-07-27 21:46:04 +00004647SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004648 return getNode(~Opcode, VT, Op1).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004650SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4651 SDValue Op1) {
4652 return getNode(~Opcode, dl, VT, Op1).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004653}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004654
Duncan Sands92c43912008-06-06 12:08:01 +00004655SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00004656 SDValue Op1, SDValue Op2) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004657 return getNode(~Opcode, VT, Op1, Op2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004659SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004660 SDValue Op1, SDValue Op2) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004661 return getNode(~Opcode, dl, VT, Op1, Op2).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004662}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004663
Duncan Sands92c43912008-06-06 12:08:01 +00004664SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Bill Wendling6ed1f082009-01-29 09:01:55 +00004665 SDValue Op1, SDValue Op2,
4666 SDValue Op3) {
4667 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004668}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004669SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4670 SDValue Op1, SDValue Op2,
4671 SDValue Op3) {
4672 return getNode(~Opcode, dl, VT, Op1, Op2, Op3).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004673}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004674
Duncan Sands92c43912008-06-06 12:08:01 +00004675SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00004676 const SDValue *Ops, unsigned NumOps) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004677 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004678}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004679SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004680 const SDValue *Ops, unsigned NumOps) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004681 return getNode(~Opcode, dl, VT, Ops, NumOps).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004682}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004683
4684SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4685 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4686 SDValue Op;
4687 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen3d8578b2007-10-10 01:01:31 +00004688}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004689SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004690 MVT VT1, MVT VT2) {
4691 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4692 SDValue Op;
Bill Wendling6ed1f082009-01-29 09:01:55 +00004693 return getNode(~Opcode, dl, VTs, 2, &Op, 0).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004694}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004695
Duncan Sands92c43912008-06-06 12:08:01 +00004696SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004697 MVT VT2, SDValue Op1) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004698 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4699 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004701SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004702 MVT VT2, SDValue Op1) {
4703 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004704 return getNode(~Opcode, dl, VTs, 2, &Op1, 1).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004705}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004706
Duncan Sands92c43912008-06-06 12:08:01 +00004707SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004708 MVT VT2, SDValue Op1,
Bill Wendling6ed1f082009-01-29 09:01:55 +00004709 SDValue Op2) {
4710 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4711 SDValue Ops[] = { Op1, Op2 };
4712 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Bill Wendling66968012009-01-29 05:27:31 +00004713}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004714SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4715 MVT VT2, SDValue Op1,
4716 SDValue Op2) {
4717 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4718 SDValue Ops[] = { Op1, Op2 };
4719 return getNode(~Opcode, dl, VTs, 2, Ops, 2).getNode();
4720}
4721
4722SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Bill Wendling66968012009-01-29 05:27:31 +00004723 MVT VT2, SDValue Op1,
4724 SDValue Op2, SDValue Op3) {
Duncan Sands92c43912008-06-06 12:08:01 +00004725 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004726 SDValue Ops[] = { Op1, Op2, Op3 };
Bill Wendling6ed1f082009-01-29 09:01:55 +00004727 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004729SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4730 MVT VT2, SDValue Op1,
4731 SDValue Op2, SDValue Op3) {
4732 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4733 SDValue Ops[] = { Op1, Op2, Op3 };
4734 return getNode(~Opcode, dl, VTs, 2, Ops, 3).getNode();
4735}
4736
4737SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004738 const SDValue *Ops, unsigned NumOps) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004739 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4740 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004742SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004743 MVT VT1, MVT VT2,
4744 const SDValue *Ops, unsigned NumOps) {
4745 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004746 return getNode(~Opcode, dl, VTs, 2, Ops, NumOps).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004747}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004748
4749SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004750 SDValue Op1, SDValue Op2) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004751 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4752 SDValue Ops[] = { Op1, Op2 };
4753 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004754}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004755SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004756 MVT VT1, MVT VT2, MVT VT3,
4757 SDValue Op1, SDValue Op2) {
4758 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4759 SDValue Ops[] = { Op1, Op2 };
Bill Wendling6ed1f082009-01-29 09:01:55 +00004760 return getNode(~Opcode, dl, VTs, 3, Ops, 2).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004761}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004762
4763SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
4764 SDValue Op1, SDValue Op2,
4765 SDValue Op3) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004766 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4767 SDValue Ops[] = { Op1, Op2, Op3 };
Bill Wendling6ed1f082009-01-29 09:01:55 +00004768 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004769}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004770SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Bill Wendling66968012009-01-29 05:27:31 +00004771 MVT VT1, MVT VT2, MVT VT3,
Bill Wendling6ed1f082009-01-29 09:01:55 +00004772 SDValue Op1, SDValue Op2,
4773 SDValue Op3) {
4774 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4775 SDValue Ops[] = { Op1, Op2, Op3 };
4776 return getNode(~Opcode, dl, VTs, 3, Ops, 3).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004777}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004778
4779SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
4780 const SDValue *Ops, unsigned NumOps) {
4781 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4782 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
4783}
4784SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004785 MVT VT1, MVT VT2, MVT VT3,
4786 const SDValue *Ops, unsigned NumOps) {
4787 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dale Johannesenf2103ee2009-02-03 01:55:44 +00004788 return getNode(~Opcode, dl, VTs, 3, Ops, NumOps).getNode();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004789}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004790
4791SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4792 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman8181bd12008-07-27 21:46:04 +00004793 const SDValue *Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00004794 std::vector<MVT> VTList;
Evan Chenge1d067e2007-09-12 23:39:49 +00004795 VTList.push_back(VT1);
4796 VTList.push_back(VT2);
4797 VTList.push_back(VT3);
4798 VTList.push_back(VT4);
Duncan Sands92c43912008-06-06 12:08:01 +00004799 const MVT *VTs = getNodeValueTypes(VTList);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004800 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Chenge1d067e2007-09-12 23:39:49 +00004801}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004802SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4803 MVT VT2, MVT VT3, MVT VT4,
4804 const SDValue *Ops, unsigned NumOps) {
4805 std::vector<MVT> VTList;
4806 VTList.push_back(VT1);
4807 VTList.push_back(VT2);
4808 VTList.push_back(VT3);
4809 VTList.push_back(VT4);
4810 const MVT *VTs = getNodeValueTypes(VTList);
4811 return getNode(~Opcode, dl, VTs, 4, Ops, NumOps).getNode();
4812}
4813
Evan Chenge3940912007-10-05 01:10:49 +00004814SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmana36422a2008-07-09 00:00:42 +00004815 const std::vector<MVT> &ResultTys,
Dan Gohman8181bd12008-07-27 21:46:04 +00004816 const SDValue *Ops, unsigned NumOps) {
Bill Wendling6ed1f082009-01-29 09:01:55 +00004817 const MVT *VTs = getNodeValueTypes(ResultTys);
4818 return getNode(~Opcode, VTs, ResultTys.size(),
4819 Ops, NumOps).getNode();
Evan Chenge3940912007-10-05 01:10:49 +00004820}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004821SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004822 const std::vector<MVT> &ResultTys,
4823 const SDValue *Ops, unsigned NumOps) {
4824 const MVT *VTs = getNodeValueTypes(ResultTys);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004825 return getNode(~Opcode, dl, VTs, ResultTys.size(),
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004826 Ops, NumOps).getNode();
4827}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004828
Evan Chengd1113582008-03-22 01:55:50 +00004829/// getNodeIfExists - Get the specified node if it's already available, or
4830/// else return NULL.
4831SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman8181bd12008-07-27 21:46:04 +00004832 const SDValue *Ops, unsigned NumOps) {
Evan Chengd1113582008-03-22 01:55:50 +00004833 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4834 FoldingSetNodeID ID;
4835 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4836 void *IP = 0;
4837 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4838 return E;
4839 }
4840 return NULL;
4841}
4842
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4844/// This can cause recursive merging of nodes in the DAG.
4845///
Chris Lattnerdca329f2008-02-03 03:35:22 +00004846/// This version assumes From has a single result value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847///
Dan Gohman8181bd12008-07-27 21:46:04 +00004848void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004849 DAGUpdateListener *UpdateListener) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004850 SDNode *From = FromN.getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00004851 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004852 "Cannot replace with this method!");
Gabor Greif1c80d112008-08-28 21:40:38 +00004853 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein05650fd2008-04-07 10:06:32 +00004854
djg943376a2009-01-25 16:29:12 +00004855 // Iterate over all the existing uses of From. New uses will be added
4856 // to the beginning of the use list, which we avoid visiting.
4857 // This specifically avoids visiting uses of From that arise while the
4858 // replacement is happening, because any such uses would be the result
4859 // of CSE: If an existing node looks like From after one of its operands
4860 // is replaced by To, we don't want to replace of all its users with To
4861 // too. See PR3018 for more info.
Dan Gohman86bf1392009-01-19 21:44:21 +00004862 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4863 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004864 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004865
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004866 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004867 RemoveNodeFromCSEMaps(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868
djg943376a2009-01-25 16:29:12 +00004869 // A user can appear in a use list multiple times, and when this
4870 // happens the uses are usually next to each other in the list.
4871 // To help reduce the number of CSE recomputations, process all
4872 // the uses of this user that we can find this way.
4873 do {
djgc2517d32009-01-26 04:35:06 +00004874 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00004875 ++UI;
djgc2517d32009-01-26 04:35:06 +00004876 Use.set(To);
djg943376a2009-01-25 16:29:12 +00004877 } while (UI != UE && *UI == User);
4878
4879 // Now that we have modified User, add it back to the CSE maps. If it
4880 // already exists there, recursively merge the results together.
4881 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004882 }
4883}
4884
4885/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4886/// This can cause recursive merging of nodes in the DAG.
4887///
4888/// This version assumes From/To have matching types and numbers of result
4889/// values.
4890///
4891void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004892 DAGUpdateListener *UpdateListener) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004893 assert(From->getVTList().VTs == To->getVTList().VTs &&
4894 From->getNumValues() == To->getNumValues() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004895 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmanbd68c792008-07-17 19:10:17 +00004896
4897 // Handle the trivial case.
4898 if (From == To)
4899 return;
4900
Dan Gohman86bf1392009-01-19 21:44:21 +00004901 // Iterate over just the existing users of From. See the comments in
4902 // the ReplaceAllUsesWith above.
4903 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4904 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004905 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004906
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004907 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004908 RemoveNodeFromCSEMaps(User);
Roman Levenstein05650fd2008-04-07 10:06:32 +00004909
djg943376a2009-01-25 16:29:12 +00004910 // A user can appear in a use list multiple times, and when this
4911 // happens the uses are usually next to each other in the list.
4912 // To help reduce the number of CSE recomputations, process all
4913 // the uses of this user that we can find this way.
4914 do {
djgc2517d32009-01-26 04:35:06 +00004915 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00004916 ++UI;
djgc2517d32009-01-26 04:35:06 +00004917 Use.setNode(To);
djg943376a2009-01-25 16:29:12 +00004918 } while (UI != UE && *UI == User);
4919
4920 // Now that we have modified User, add it back to the CSE maps. If it
4921 // already exists there, recursively merge the results together.
4922 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004923 }
4924}
4925
4926/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4927/// This can cause recursive merging of nodes in the DAG.
4928///
4929/// This version can replace From with any result values. To must match the
4930/// number and types of values returned by From.
4931void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman8181bd12008-07-27 21:46:04 +00004932 const SDValue *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004933 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00004934 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman8181bd12008-07-27 21:46:04 +00004935 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004936
Dan Gohman86bf1392009-01-19 21:44:21 +00004937 // Iterate over just the existing users of From. See the comments in
4938 // the ReplaceAllUsesWith above.
4939 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4940 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004941 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004942
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004943 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004944 RemoveNodeFromCSEMaps(User);
Roman Levenstein05650fd2008-04-07 10:06:32 +00004945
djg943376a2009-01-25 16:29:12 +00004946 // A user can appear in a use list multiple times, and when this
4947 // happens the uses are usually next to each other in the list.
4948 // To help reduce the number of CSE recomputations, process all
4949 // the uses of this user that we can find this way.
4950 do {
djgc2517d32009-01-26 04:35:06 +00004951 SDUse &Use = UI.getUse();
4952 const SDValue &ToOp = To[Use.getResNo()];
djg943376a2009-01-25 16:29:12 +00004953 ++UI;
djgc2517d32009-01-26 04:35:06 +00004954 Use.set(ToOp);
djg943376a2009-01-25 16:29:12 +00004955 } while (UI != UE && *UI == User);
4956
4957 // Now that we have modified User, add it back to the CSE maps. If it
4958 // already exists there, recursively merge the results together.
4959 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960 }
4961}
4962
4963/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
djgc2517d32009-01-26 04:35:06 +00004964/// uses of other values produced by From.getNode() alone. The Deleted
4965/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman8181bd12008-07-27 21:46:04 +00004966void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004967 DAGUpdateListener *UpdateListener){
Dan Gohmanbd68c792008-07-17 19:10:17 +00004968 // Handle the really simple, really trivial case efficiently.
4969 if (From == To) return;
4970
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 // Handle the simple, trivial, case efficiently.
Gabor Greif1c80d112008-08-28 21:40:38 +00004972 if (From.getNode()->getNumValues() == 1) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004973 ReplaceAllUsesWith(From, To, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 return;
4975 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004976
djg943376a2009-01-25 16:29:12 +00004977 // Iterate over just the existing users of From. See the comments in
4978 // the ReplaceAllUsesWith above.
4979 SDNode::use_iterator UI = From.getNode()->use_begin(),
4980 UE = From.getNode()->use_end();
4981 while (UI != UE) {
4982 SDNode *User = *UI;
4983 bool UserRemovedFromCSEMaps = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984
djg943376a2009-01-25 16:29:12 +00004985 // A user can appear in a use list multiple times, and when this
4986 // happens the uses are usually next to each other in the list.
4987 // To help reduce the number of CSE recomputations, process all
4988 // the uses of this user that we can find this way.
4989 do {
djgc2517d32009-01-26 04:35:06 +00004990 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00004991
4992 // Skip uses of different values from the same node.
djgc2517d32009-01-26 04:35:06 +00004993 if (Use.getResNo() != From.getResNo()) {
djg943376a2009-01-25 16:29:12 +00004994 ++UI;
4995 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 }
djg943376a2009-01-25 16:29:12 +00004997
4998 // If this node hasn't been modified yet, it's still in the CSE maps,
4999 // so remove its old self from the CSE maps.
5000 if (!UserRemovedFromCSEMaps) {
5001 RemoveNodeFromCSEMaps(User);
5002 UserRemovedFromCSEMaps = true;
5003 }
5004
5005 ++UI;
djgc2517d32009-01-26 04:35:06 +00005006 Use.set(To);
djg943376a2009-01-25 16:29:12 +00005007 } while (UI != UE && *UI == User);
5008
5009 // We are iterating over all uses of the From node, so if a use
5010 // doesn't use the specific value, no changes are made.
5011 if (!UserRemovedFromCSEMaps)
5012 continue;
5013
Chris Lattner8a258202007-10-15 06:10:22 +00005014 // Now that we have modified User, add it back to the CSE maps. If it
5015 // already exists there, recursively merge the results together.
djg943376a2009-01-25 16:29:12 +00005016 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanbd68c792008-07-17 19:10:17 +00005017 }
5018}
5019
djg943376a2009-01-25 16:29:12 +00005020namespace {
5021 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5022 /// to record information about a use.
5023 struct UseMemo {
5024 SDNode *User;
5025 unsigned Index;
djgc2517d32009-01-26 04:35:06 +00005026 SDUse *Use;
djg943376a2009-01-25 16:29:12 +00005027 };
djgc2517d32009-01-26 04:35:06 +00005028
5029 /// operator< - Sort Memos by User.
5030 bool operator<(const UseMemo &L, const UseMemo &R) {
5031 return (intptr_t)L.User < (intptr_t)R.User;
5032 }
djg943376a2009-01-25 16:29:12 +00005033}
5034
Dan Gohmanbd68c792008-07-17 19:10:17 +00005035/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
djgc2517d32009-01-26 04:35:06 +00005036/// uses of other values produced by From.getNode() alone. The same value
5037/// may appear in both the From and To list. The Deleted vector is
Dan Gohmanbd68c792008-07-17 19:10:17 +00005038/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman8181bd12008-07-27 21:46:04 +00005039void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5040 const SDValue *To,
Dan Gohmanbd68c792008-07-17 19:10:17 +00005041 unsigned Num,
5042 DAGUpdateListener *UpdateListener){
5043 // Handle the simple, trivial case efficiently.
5044 if (Num == 1)
5045 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5046
djg943376a2009-01-25 16:29:12 +00005047 // Read up all the uses and make records of them. This helps
5048 // processing new uses that are introduced during the
5049 // replacement process.
5050 SmallVector<UseMemo, 4> Uses;
5051 for (unsigned i = 0; i != Num; ++i) {
5052 unsigned FromResNo = From[i].getResNo();
5053 SDNode *FromNode = From[i].getNode();
5054 for (SDNode::use_iterator UI = FromNode->use_begin(),
djgc2517d32009-01-26 04:35:06 +00005055 E = FromNode->use_end(); UI != E; ++UI) {
5056 SDUse &Use = UI.getUse();
5057 if (Use.getResNo() == FromResNo) {
5058 UseMemo Memo = { *UI, i, &Use };
djg943376a2009-01-25 16:29:12 +00005059 Uses.push_back(Memo);
5060 }
djgc2517d32009-01-26 04:35:06 +00005061 }
djg943376a2009-01-25 16:29:12 +00005062 }
Dan Gohmanbd68c792008-07-17 19:10:17 +00005063
djgc2517d32009-01-26 04:35:06 +00005064 // Sort the uses, so that all the uses from a given User are together.
5065 std::sort(Uses.begin(), Uses.end());
5066
djg943376a2009-01-25 16:29:12 +00005067 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5068 UseIndex != UseIndexEnd; ) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00005069 // We know that this user uses some value of From. If it is the right
5070 // value, update it.
djg943376a2009-01-25 16:29:12 +00005071 SDNode *User = Uses[UseIndex].User;
5072
5073 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanbd68c792008-07-17 19:10:17 +00005074 RemoveNodeFromCSEMaps(User);
djg943376a2009-01-25 16:29:12 +00005075
djgc2517d32009-01-26 04:35:06 +00005076 // The Uses array is sorted, so all the uses for a given User
5077 // are next to each other in the list.
djg943376a2009-01-25 16:29:12 +00005078 // To help reduce the number of CSE recomputations, process all
5079 // the uses of this user that we can find this way.
5080 do {
5081 unsigned i = Uses[UseIndex].Index;
djgc2517d32009-01-26 04:35:06 +00005082 SDUse &Use = *Uses[UseIndex].Use;
djg943376a2009-01-25 16:29:12 +00005083 ++UseIndex;
5084
djgc2517d32009-01-26 04:35:06 +00005085 Use.set(To[i]);
djg943376a2009-01-25 16:29:12 +00005086 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5087
Dan Gohmanbd68c792008-07-17 19:10:17 +00005088 // Now that we have modified User, add it back to the CSE maps. If it
5089 // already exists there, recursively merge the results together.
djg943376a2009-01-25 16:29:12 +00005090 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005091 }
5092}
5093
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005094/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
5095/// based on their topological order. It returns the maximum id and a vector
5096/// of the SDNodes* in assigned order by reference.
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005097unsigned SelectionDAG::AssignTopologicalOrder() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005098
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005099 unsigned DAGSize = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005101 // SortedPos tracks the progress of the algorithm. Nodes before it are
5102 // sorted, nodes after it are unsorted. When the algorithm completes
5103 // it is at the end of the list.
5104 allnodes_iterator SortedPos = allnodes_begin();
5105
Dan Gohman6e780312008-11-21 19:10:41 +00005106 // Visit all the nodes. Move nodes with no operands to the front of
5107 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005108 // operand count. Before we do this, the Node Id fields of the nodes
5109 // may contain arbitrary values. After, the Node Id fields for nodes
5110 // before SortedPos will contain the topological sort index, and the
5111 // Node Id fields for nodes At SortedPos and after will contain the
5112 // count of outstanding operands.
5113 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5114 SDNode *N = I++;
5115 unsigned Degree = N->getNumOperands();
5116 if (Degree == 0) {
5117 // A node with no uses, add it to the result array immediately.
5118 N->setNodeId(DAGSize++);
5119 allnodes_iterator Q = N;
5120 if (Q != SortedPos)
5121 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
5122 ++SortedPos;
5123 } else {
5124 // Temporarily use the Node Id as scratch space for the degree count.
5125 N->setNodeId(Degree);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005126 }
5127 }
5128
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005129 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5130 // such that by the time the end is reached all nodes will be sorted.
5131 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5132 SDNode *N = I;
5133 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5134 UI != UE; ++UI) {
5135 SDNode *P = *UI;
5136 unsigned Degree = P->getNodeId();
5137 --Degree;
5138 if (Degree == 0) {
5139 // All of P's operands are sorted, so P may sorted now.
5140 P->setNodeId(DAGSize++);
5141 if (P != SortedPos)
5142 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
5143 ++SortedPos;
5144 } else {
5145 // Update P's outstanding operand count.
5146 P->setNodeId(Degree);
5147 }
5148 }
5149 }
5150
5151 assert(SortedPos == AllNodes.end() &&
5152 "Topological sort incomplete!");
5153 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5154 "First node in topological sort is not the entry token!");
5155 assert(AllNodes.front().getNodeId() == 0 &&
5156 "First node in topological sort has non-zero id!");
5157 assert(AllNodes.front().getNumOperands() == 0 &&
5158 "First node in topological sort has operands!");
5159 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5160 "Last node in topologic sort has unexpected id!");
5161 assert(AllNodes.back().use_empty() &&
5162 "Last node in topologic sort has users!");
Dan Gohman6e780312008-11-21 19:10:41 +00005163 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005164 return DAGSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005165}
5166
5167
5168
5169//===----------------------------------------------------------------------===//
5170// SDNode Class
5171//===----------------------------------------------------------------------===//
5172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005173HandleSDNode::~HandleSDNode() {
Dan Gohmanb997a4e2008-07-07 20:57:48 +00005174 DropOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005175}
5176
5177GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Dan Gohman36322c72008-10-18 02:06:02 +00005178 MVT VT, int64_t o)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005179 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman53491e92007-07-23 20:24:29 +00005180 cast<GlobalVariable>(GA)->isThreadLocal() ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 // Thread Local
5182 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
5183 // Non Thread Local
5184 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
5185 getSDVTList(VT)), Offset(o) {
5186 TheGlobal = const_cast<GlobalValue*>(GA);
5187}
5188
Dan Gohman7ad1cca2008-07-09 22:08:04 +00005189MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman846947a2008-07-09 21:23:02 +00005190 const Value *srcValue, int SVO,
5191 unsigned alignment, bool vol)
Dan Gohman9d0f5022009-02-03 00:08:45 +00005192 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5193 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dan Gohman846947a2008-07-09 21:23:02 +00005194 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5195 assert(getAlignment() == alignment && "Alignment representation error!");
5196 assert(isVolatile() == vol && "Volatile representation error!");
5197}
5198
Mon P Wang89423382008-10-17 18:22:58 +00005199MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops,
5200 unsigned NumOps, MVT memvt, const Value *srcValue,
5201 int SVO, unsigned alignment, bool vol)
5202 : SDNode(Opc, VTs, Ops, NumOps),
Dan Gohman9d0f5022009-02-03 00:08:45 +00005203 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5204 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Mon P Wang89423382008-10-17 18:22:58 +00005205 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5206 assert(getAlignment() == alignment && "Alignment representation error!");
5207 assert(isVolatile() == vol && "Volatile representation error!");
5208}
5209
Dale Johannesen08f344e2009-01-28 21:18:29 +00005210MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, MVT memvt,
5211 const Value *srcValue, int SVO,
5212 unsigned alignment, bool vol)
Dan Gohman9d0f5022009-02-03 00:08:45 +00005213 : SDNode(Opc, dl, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5214 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen08f344e2009-01-28 21:18:29 +00005215 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5216 assert(getAlignment() == alignment && "Alignment representation error!");
5217 assert(isVolatile() == vol && "Volatile representation error!");
5218}
5219
5220MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
5221 const SDValue *Ops,
5222 unsigned NumOps, MVT memvt, const Value *srcValue,
5223 int SVO, unsigned alignment, bool vol)
5224 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohman9d0f5022009-02-03 00:08:45 +00005225 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5226 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen08f344e2009-01-28 21:18:29 +00005227 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5228 assert(getAlignment() == alignment && "Alignment representation error!");
5229 assert(isVolatile() == vol && "Volatile representation error!");
5230}
5231
Dan Gohman1fad9e62008-04-07 19:35:22 +00005232/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman7ad1cca2008-07-09 22:08:04 +00005233/// reference performed by this memory reference.
5234MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesen92434a52008-10-24 01:06:58 +00005235 int Flags = 0;
Dan Gohman7ad1cca2008-07-09 22:08:04 +00005236 if (isa<LoadSDNode>(this))
5237 Flags = MachineMemOperand::MOLoad;
5238 else if (isa<StoreSDNode>(this))
5239 Flags = MachineMemOperand::MOStore;
Mon P Wang89423382008-10-17 18:22:58 +00005240 else if (isa<AtomicSDNode>(this)) {
Dan Gohman7ad1cca2008-07-09 22:08:04 +00005241 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
5242 }
Mon P Wang89423382008-10-17 18:22:58 +00005243 else {
5244 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
5245 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
5246 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
5247 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
5248 }
Dan Gohman7ad1cca2008-07-09 22:08:04 +00005249
5250 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005251 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
5252
Dan Gohman7ad1cca2008-07-09 22:08:04 +00005253 // Check if the memory reference references a frame index
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005254 const FrameIndexSDNode *FI =
Gabor Greif1c80d112008-08-28 21:40:38 +00005255 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005256 if (!getSrcValue() && FI)
Dan Gohman1fc34bc2008-07-11 22:44:52 +00005257 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
5258 Flags, 0, Size, getAlignment());
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005259 else
5260 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
5261 Size, getAlignment());
5262}
5263
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005264/// Profile - Gather unique data for the node.
5265///
Dan Gohman98beebe2008-08-20 15:58:01 +00005266void SDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005267 AddNodeIDNode(ID, this);
5268}
5269
5270/// getValueTypeList - Return a pointer to the specified value type.
5271///
Duncan Sands92c43912008-06-06 12:08:01 +00005272const MVT *SDNode::getValueTypeList(MVT VT) {
5273 if (VT.isExtended()) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00005274 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman8cdf7892008-02-08 03:26:46 +00005275 return &(*EVTs.insert(VT).first);
Duncan Sandsa9810f32007-10-16 09:56:48 +00005276 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00005277 static MVT VTs[MVT::LAST_VALUETYPE];
5278 VTs[VT.getSimpleVT()] = VT;
5279 return &VTs[VT.getSimpleVT()];
Duncan Sandsa9810f32007-10-16 09:56:48 +00005280 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005281}
Duncan Sandsa9810f32007-10-16 09:56:48 +00005282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005283/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5284/// indicated value. This method ignores uses of other values defined by this
5285/// operation.
5286bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
5287 assert(Value < getNumValues() && "Bad value!");
5288
Roman Levenstein05650fd2008-04-07 10:06:32 +00005289 // TODO: Only iterate over uses of a given value of the node
5290 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
djgc2517d32009-01-26 04:35:06 +00005291 if (UI.getUse().getResNo() == Value) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00005292 if (NUses == 0)
5293 return false;
5294 --NUses;
5295 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 }
5297
5298 // Found exactly the right number of uses?
5299 return NUses == 0;
5300}
5301
5302
Evan Cheng0af04f72007-08-02 05:29:38 +00005303/// hasAnyUseOfValue - Return true if there are any use of the indicated
5304/// value. This method ignores uses of other values defined by this operation.
5305bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5306 assert(Value < getNumValues() && "Bad value!");
5307
Dan Gohmana9651732008-07-09 22:39:01 +00005308 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
djgc2517d32009-01-26 04:35:06 +00005309 if (UI.getUse().getResNo() == Value)
Dan Gohmana9651732008-07-09 22:39:01 +00005310 return true;
Evan Cheng0af04f72007-08-02 05:29:38 +00005311
5312 return false;
5313}
5314
5315
Dan Gohman07fadb02008-07-27 18:06:42 +00005316/// isOnlyUserOf - Return true if this node is the only use of N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005317///
Dan Gohman07fadb02008-07-27 18:06:42 +00005318bool SDNode::isOnlyUserOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005319 bool Seen = false;
5320 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00005321 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005322 if (User == this)
5323 Seen = true;
5324 else
5325 return false;
5326 }
5327
5328 return Seen;
5329}
5330
5331/// isOperand - Return true if this node is an operand of N.
5332///
Dan Gohman8181bd12008-07-27 21:46:04 +00005333bool SDValue::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5335 if (*this == N->getOperand(i))
5336 return true;
5337 return false;
5338}
5339
Evan Chengd9387682008-03-04 00:41:45 +00005340bool SDNode::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005341 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
djgc2517d32009-01-26 04:35:06 +00005342 if (this == N->OperandList[i].getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005343 return true;
5344 return false;
5345}
5346
Chris Lattner10d94f92008-01-16 05:49:24 +00005347/// reachesChainWithoutSideEffects - Return true if this operand (which must
5348/// be a chain) reaches the specified operand without crossing any
5349/// side-effecting instructions. In practice, this looks through token
5350/// factors and non-volatile loads. In order to remain efficient, this only
5351/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman8181bd12008-07-27 21:46:04 +00005352bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner10d94f92008-01-16 05:49:24 +00005353 unsigned Depth) const {
5354 if (*this == Dest) return true;
5355
5356 // Don't search too deeply, we just want to be able to see through
5357 // TokenFactor's etc.
5358 if (Depth == 0) return false;
5359
5360 // If this is a token factor, all inputs to the TF happen in parallel. If any
5361 // of the operands of the TF reach dest, then we can do the xform.
5362 if (getOpcode() == ISD::TokenFactor) {
5363 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5364 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5365 return true;
5366 return false;
5367 }
5368
5369 // Loads don't have side effects, look through them.
5370 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5371 if (!Ld->isVolatile())
5372 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5373 }
5374 return false;
5375}
5376
5377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005378static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
5379 SmallPtrSet<SDNode *, 32> &Visited) {
5380 if (found || !Visited.insert(N))
5381 return;
5382
5383 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005384 SDNode *Op = N->getOperand(i).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005385 if (Op == P) {
5386 found = true;
5387 return;
5388 }
5389 findPredecessor(Op, P, found, Visited);
5390 }
5391}
5392
Evan Chengd9387682008-03-04 00:41:45 +00005393/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005394/// is either an operand of N or it can be reached by recursively traversing
5395/// up the operands.
5396/// NOTE: this is an expensive method. Use it carefully.
Evan Chengd9387682008-03-04 00:41:45 +00005397bool SDNode::isPredecessorOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005398 SmallPtrSet<SDNode *, 32> Visited;
5399 bool found = false;
5400 findPredecessor(N, this, found, Visited);
5401 return found;
5402}
5403
5404uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5405 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005406 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005407}
5408
5409std::string SDNode::getOperationName(const SelectionDAG *G) const {
5410 switch (getOpcode()) {
5411 default:
5412 if (getOpcode() < ISD::BUILTIN_OP_END)
5413 return "<<Unknown DAG Node>>";
Dan Gohmanbd68c792008-07-17 19:10:17 +00005414 if (isMachineOpcode()) {
5415 if (G)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005416 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmanbd68c792008-07-17 19:10:17 +00005417 if (getMachineOpcode() < TII->getNumOpcodes())
5418 return TII->get(getMachineOpcode()).getName();
5419 return "<<Unknown Machine Node>>";
5420 }
5421 if (G) {
Dan Gohmana0c429e2009-01-15 16:58:17 +00005422 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmanbd68c792008-07-17 19:10:17 +00005423 const char *Name = TLI.getTargetNodeName(getOpcode());
5424 if (Name) return Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005425 return "<<Unknown Target Node>>";
5426 }
Dan Gohmanbd68c792008-07-17 19:10:17 +00005427 return "<<Unknown Node>>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005428
Dan Gohmanbd68c792008-07-17 19:10:17 +00005429#ifndef NDEBUG
5430 case ISD::DELETED_NODE:
5431 return "<<Deleted Node!>>";
5432#endif
Evan Chengd1d68072008-03-08 00:58:38 +00005433 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth785610d2008-02-16 01:24:58 +00005434 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohmanbebba8d2008-12-23 21:37:04 +00005435 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5436 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5437 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5438 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5439 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5440 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5441 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5442 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5443 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5444 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5445 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5446 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005447 case ISD::PCMARKER: return "PCMarker";
5448 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
5449 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman12a9c082008-02-06 22:27:42 +00005450 case ISD::MEMOPERAND: return "MemOperand";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005451 case ISD::EntryToken: return "EntryToken";
5452 case ISD::TokenFactor: return "TokenFactor";
5453 case ISD::AssertSext: return "AssertSext";
5454 case ISD::AssertZext: return "AssertZext";
5455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005456 case ISD::BasicBlock: return "BasicBlock";
Duncan Sandsc93fae32008-03-21 09:14:45 +00005457 case ISD::ARG_FLAGS: return "ArgFlags";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458 case ISD::VALUETYPE: return "ValueType";
5459 case ISD::Register: return "Register";
5460
5461 case ISD::Constant: return "Constant";
5462 case ISD::ConstantFP: return "ConstantFP";
5463 case ISD::GlobalAddress: return "GlobalAddress";
5464 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
5465 case ISD::FrameIndex: return "FrameIndex";
5466 case ISD::JumpTable: return "JumpTable";
5467 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendlingfef06052008-09-16 21:48:12 +00005468 case ISD::RETURNADDR: return "RETURNADDR";
5469 case ISD::FRAMEADDR: return "FRAMEADDR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005470 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
5471 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendlingfef06052008-09-16 21:48:12 +00005472 case ISD::EHSELECTION: return "EHSELECTION";
5473 case ISD::EH_RETURN: return "EH_RETURN";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005474 case ISD::ConstantPool: return "ConstantPool";
Bill Wendlingfef06052008-09-16 21:48:12 +00005475 case ISD::ExternalSymbol: return "ExternalSymbol";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005477 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005478 return Intrinsic::getName((Intrinsic::ID)IID);
5479 }
5480 case ISD::INTRINSIC_VOID:
5481 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005482 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005483 return Intrinsic::getName((Intrinsic::ID)IID);
5484 }
5485
5486 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
5487 case ISD::TargetConstant: return "TargetConstant";
5488 case ISD::TargetConstantFP:return "TargetConstantFP";
5489 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
5490 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
5491 case ISD::TargetFrameIndex: return "TargetFrameIndex";
5492 case ISD::TargetJumpTable: return "TargetJumpTable";
5493 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendlingfef06052008-09-16 21:48:12 +00005494 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005495
5496 case ISD::CopyToReg: return "CopyToReg";
5497 case ISD::CopyFromReg: return "CopyFromReg";
5498 case ISD::UNDEF: return "undef";
5499 case ISD::MERGE_VALUES: return "merge_values";
5500 case ISD::INLINEASM: return "inlineasm";
Dan Gohmanfa607c92008-07-01 00:05:16 +00005501 case ISD::DBG_LABEL: return "dbg_label";
5502 case ISD::EH_LABEL: return "eh_label";
Evan Cheng2e28d622008-02-02 04:07:54 +00005503 case ISD::DECLARE: return "declare";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005504 case ISD::HANDLENODE: return "handlenode";
5505 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
5506 case ISD::CALL: return "call";
5507
5508 // Unary operators
5509 case ISD::FABS: return "fabs";
5510 case ISD::FNEG: return "fneg";
5511 case ISD::FSQRT: return "fsqrt";
5512 case ISD::FSIN: return "fsin";
5513 case ISD::FCOS: return "fcos";
5514 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00005515 case ISD::FPOW: return "fpow";
Dan Gohmanc8b20e22008-08-21 17:55:02 +00005516 case ISD::FTRUNC: return "ftrunc";
5517 case ISD::FFLOOR: return "ffloor";
5518 case ISD::FCEIL: return "fceil";
5519 case ISD::FRINT: return "frint";
5520 case ISD::FNEARBYINT: return "fnearbyint";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005521
5522 // Binary operators
5523 case ISD::ADD: return "add";
5524 case ISD::SUB: return "sub";
5525 case ISD::MUL: return "mul";
5526 case ISD::MULHU: return "mulhu";
5527 case ISD::MULHS: return "mulhs";
5528 case ISD::SDIV: return "sdiv";
5529 case ISD::UDIV: return "udiv";
5530 case ISD::SREM: return "srem";
5531 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00005532 case ISD::SMUL_LOHI: return "smul_lohi";
5533 case ISD::UMUL_LOHI: return "umul_lohi";
5534 case ISD::SDIVREM: return "sdivrem";
Dan Gohman2e66b052008-09-08 16:30:29 +00005535 case ISD::UDIVREM: return "udivrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536 case ISD::AND: return "and";
5537 case ISD::OR: return "or";
5538 case ISD::XOR: return "xor";
5539 case ISD::SHL: return "shl";
5540 case ISD::SRA: return "sra";
5541 case ISD::SRL: return "srl";
5542 case ISD::ROTL: return "rotl";
5543 case ISD::ROTR: return "rotr";
5544 case ISD::FADD: return "fadd";
5545 case ISD::FSUB: return "fsub";
5546 case ISD::FMUL: return "fmul";
5547 case ISD::FDIV: return "fdiv";
5548 case ISD::FREM: return "frem";
5549 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00005550 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005551
5552 case ISD::SETCC: return "setcc";
Nate Begeman9a1ce152008-05-12 19:40:03 +00005553 case ISD::VSETCC: return "vsetcc";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554 case ISD::SELECT: return "select";
5555 case ISD::SELECT_CC: return "select_cc";
5556 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
5557 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
5558 case ISD::CONCAT_VECTORS: return "concat_vectors";
5559 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
5560 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
5561 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
5562 case ISD::CARRY_FALSE: return "carry_false";
5563 case ISD::ADDC: return "addc";
5564 case ISD::ADDE: return "adde";
Bill Wendlingea8b7c92008-11-21 02:12:42 +00005565 case ISD::SADDO: return "saddo";
5566 case ISD::UADDO: return "uaddo";
Bill Wendling7e04be62008-12-09 22:08:41 +00005567 case ISD::SSUBO: return "ssubo";
5568 case ISD::USUBO: return "usubo";
5569 case ISD::SMULO: return "smulo";
5570 case ISD::UMULO: return "umulo";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 case ISD::SUBC: return "subc";
5572 case ISD::SUBE: return "sube";
5573 case ISD::SHL_PARTS: return "shl_parts";
5574 case ISD::SRA_PARTS: return "sra_parts";
5575 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lambb768c2e2007-07-26 07:34:40 +00005576
5577 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5578 case ISD::INSERT_SUBREG: return "insert_subreg";
5579
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005580 // Conversion operators.
5581 case ISD::SIGN_EXTEND: return "sign_extend";
5582 case ISD::ZERO_EXTEND: return "zero_extend";
5583 case ISD::ANY_EXTEND: return "any_extend";
5584 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
5585 case ISD::TRUNCATE: return "truncate";
5586 case ISD::FP_ROUND: return "fp_round";
Dan Gohman819574c2008-01-31 00:41:03 +00005587 case ISD::FLT_ROUNDS_: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
5589 case ISD::FP_EXTEND: return "fp_extend";
5590
5591 case ISD::SINT_TO_FP: return "sint_to_fp";
5592 case ISD::UINT_TO_FP: return "uint_to_fp";
5593 case ISD::FP_TO_SINT: return "fp_to_sint";
5594 case ISD::FP_TO_UINT: return "fp_to_uint";
5595 case ISD::BIT_CONVERT: return "bit_convert";
Mon P Wang73d31542008-11-10 20:54:11 +00005596
5597 case ISD::CONVERT_RNDSAT: {
5598 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
5599 default: assert(0 && "Unknown cvt code!");
5600 case ISD::CVT_FF: return "cvt_ff";
5601 case ISD::CVT_FS: return "cvt_fs";
5602 case ISD::CVT_FU: return "cvt_fu";
5603 case ISD::CVT_SF: return "cvt_sf";
5604 case ISD::CVT_UF: return "cvt_uf";
5605 case ISD::CVT_SS: return "cvt_ss";
5606 case ISD::CVT_SU: return "cvt_su";
5607 case ISD::CVT_US: return "cvt_us";
5608 case ISD::CVT_UU: return "cvt_uu";
5609 }
5610 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005611
5612 // Control flow instructions
5613 case ISD::BR: return "br";
5614 case ISD::BRIND: return "brind";
5615 case ISD::BR_JT: return "br_jt";
5616 case ISD::BRCOND: return "brcond";
5617 case ISD::BR_CC: return "br_cc";
5618 case ISD::RET: return "ret";
5619 case ISD::CALLSEQ_START: return "callseq_start";
5620 case ISD::CALLSEQ_END: return "callseq_end";
5621
5622 // Other operators
5623 case ISD::LOAD: return "load";
5624 case ISD::STORE: return "store";
5625 case ISD::VAARG: return "vaarg";
5626 case ISD::VACOPY: return "vacopy";
5627 case ISD::VAEND: return "vaend";
5628 case ISD::VASTART: return "vastart";
5629 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
5630 case ISD::EXTRACT_ELEMENT: return "extract_element";
5631 case ISD::BUILD_PAIR: return "build_pair";
5632 case ISD::STACKSAVE: return "stacksave";
5633 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00005634 case ISD::TRAP: return "trap";
5635
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005636 // Bit manipulation
5637 case ISD::BSWAP: return "bswap";
5638 case ISD::CTPOP: return "ctpop";
5639 case ISD::CTTZ: return "cttz";
5640 case ISD::CTLZ: return "ctlz";
5641
5642 // Debug info
Dan Gohman472d12c2008-06-30 20:59:49 +00005643 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 case ISD::DEBUG_LOC: return "debug_loc";
5645
Duncan Sands38947cd2007-07-27 12:58:54 +00005646 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00005647 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00005648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005649 case ISD::CONDCODE:
5650 switch (cast<CondCodeSDNode>(this)->get()) {
5651 default: assert(0 && "Unknown setcc condition!");
5652 case ISD::SETOEQ: return "setoeq";
5653 case ISD::SETOGT: return "setogt";
5654 case ISD::SETOGE: return "setoge";
5655 case ISD::SETOLT: return "setolt";
5656 case ISD::SETOLE: return "setole";
5657 case ISD::SETONE: return "setone";
5658
5659 case ISD::SETO: return "seto";
5660 case ISD::SETUO: return "setuo";
5661 case ISD::SETUEQ: return "setue";
5662 case ISD::SETUGT: return "setugt";
5663 case ISD::SETUGE: return "setuge";
5664 case ISD::SETULT: return "setult";
5665 case ISD::SETULE: return "setule";
5666 case ISD::SETUNE: return "setune";
5667
5668 case ISD::SETEQ: return "seteq";
5669 case ISD::SETGT: return "setgt";
5670 case ISD::SETGE: return "setge";
5671 case ISD::SETLT: return "setlt";
5672 case ISD::SETLE: return "setle";
5673 case ISD::SETNE: return "setne";
5674 }
5675 }
5676}
5677
5678const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
5679 switch (AM) {
5680 default:
5681 return "";
5682 case ISD::PRE_INC:
5683 return "<pre-inc>";
5684 case ISD::PRE_DEC:
5685 return "<pre-dec>";
5686 case ISD::POST_INC:
5687 return "<post-inc>";
5688 case ISD::POST_DEC:
5689 return "<post-dec>";
5690 }
5691}
5692
Duncan Sandsc93fae32008-03-21 09:14:45 +00005693std::string ISD::ArgFlagsTy::getArgFlagsString() {
5694 std::string S = "< ";
5695
5696 if (isZExt())
5697 S += "zext ";
5698 if (isSExt())
5699 S += "sext ";
5700 if (isInReg())
5701 S += "inreg ";
5702 if (isSRet())
5703 S += "sret ";
5704 if (isByVal())
5705 S += "byval ";
5706 if (isNest())
5707 S += "nest ";
5708 if (getByValAlign())
5709 S += "byval-align:" + utostr(getByValAlign()) + " ";
5710 if (getOrigAlign())
5711 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5712 if (getByValSize())
5713 S += "byval-size:" + utostr(getByValSize()) + " ";
5714 return S + ">";
5715}
5716
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005717void SDNode::dump() const { dump(0); }
5718void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005719 print(errs(), G);
Chris Lattnerb7876202008-08-24 18:28:30 +00005720 errs().flush();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005721}
5722
5723void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5724 OS << (void*)this << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005725
5726 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005727 if (i) OS << ",";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005728 if (getValueType(i) == MVT::Other)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005729 OS << "ch";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005730 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005731 OS << getValueType(i).getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 }
Chris Lattner1fefaac2008-08-23 22:23:09 +00005733 OS << " = " << getOperationName(G);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005734
Chris Lattner1fefaac2008-08-23 22:23:09 +00005735 OS << " ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005736 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005737 if (i) OS << ", ";
Gabor Greif1c80d112008-08-28 21:40:38 +00005738 OS << (void*)getOperand(i).getNode();
Gabor Greif46bf5472008-08-26 22:36:50 +00005739 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005740 OS << ":" << RN;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005741 }
5742
Evan Chengaad43a02007-12-11 02:08:35 +00005743 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005744 SDNode *Mask = getOperand(2).getNode();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005745 OS << "<";
Evan Chengaad43a02007-12-11 02:08:35 +00005746 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005747 if (i) OS << ",";
Evan Chengaad43a02007-12-11 02:08:35 +00005748 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005749 OS << "u";
Evan Chengaad43a02007-12-11 02:08:35 +00005750 else
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005751 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengaad43a02007-12-11 02:08:35 +00005752 }
Chris Lattner1fefaac2008-08-23 22:23:09 +00005753 OS << ">";
Evan Chengaad43a02007-12-11 02:08:35 +00005754 }
5755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005756 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005757 OS << '<' << CSDN->getAPIntValue() << '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005758 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00005759 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005760 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen2fc20782007-09-14 22:26:36 +00005761 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005762 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen2fc20782007-09-14 22:26:36 +00005763 else {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005764 OS << "<APFloat(";
Dale Johannesen6e547b42008-10-09 23:00:39 +00005765 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005766 OS << ")>";
Dale Johannesen2fc20782007-09-14 22:26:36 +00005767 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005768 } else if (const GlobalAddressSDNode *GADN =
5769 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohmancfe43232008-10-18 18:22:42 +00005770 int64_t offset = GADN->getOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005771 OS << '<';
5772 WriteAsOperand(OS, GADN->getGlobal());
5773 OS << '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005774 if (offset > 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005775 OS << " + " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005776 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005777 OS << " " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005778 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005779 OS << "<" << FIDN->getIndex() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005780 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005781 OS << "<" << JTDN->getIndex() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005782 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
5783 int offset = CP->getOffset();
5784 if (CP->isMachineConstantPoolEntry())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005785 OS << "<" << *CP->getMachineCPVal() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005787 OS << "<" << *CP->getConstVal() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005788 if (offset > 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005789 OS << " + " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005790 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005791 OS << " " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005792 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005793 OS << "<";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005794 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5795 if (LBB)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005796 OS << LBB->getName() << " ";
5797 OS << (const void*)BBDN->getBasicBlock() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005798 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman1e57df32008-02-10 18:45:23 +00005799 if (G && R->getReg() &&
5800 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005801 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005802 } else {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005803 OS << " #" << R->getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005804 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005805 } else if (const ExternalSymbolSDNode *ES =
5806 dyn_cast<ExternalSymbolSDNode>(this)) {
5807 OS << "'" << ES->getSymbol() << "'";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005808 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5809 if (M->getValue())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005810 OS << "<" << M->getValue() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005812 OS << "<null>";
Dan Gohman12a9c082008-02-06 22:27:42 +00005813 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5814 if (M->MO.getValue())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005815 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman12a9c082008-02-06 22:27:42 +00005816 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005817 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sandsc93fae32008-03-21 09:14:45 +00005818 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005819 OS << N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005820 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005821 OS << ":" << N->getVT().getMVTString();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005822 }
5823 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng034c4f82007-12-18 19:06:30 +00005824 const Value *SrcValue = LD->getSrcValue();
5825 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005826 OS << " <";
Evan Cheng034c4f82007-12-18 19:06:30 +00005827 if (SrcValue)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005828 OS << SrcValue;
Evan Cheng034c4f82007-12-18 19:06:30 +00005829 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005830 OS << "null";
5831 OS << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00005832
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005833 bool doExt = true;
5834 switch (LD->getExtensionType()) {
5835 default: doExt = false; break;
Chris Lattner1fefaac2008-08-23 22:23:09 +00005836 case ISD::EXTLOAD: OS << " <anyext "; break;
5837 case ISD::SEXTLOAD: OS << " <sext "; break;
5838 case ISD::ZEXTLOAD: OS << " <zext "; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005839 }
5840 if (doExt)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005841 OS << LD->getMemoryVT().getMVTString() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005842
5843 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00005844 if (*AM)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005845 OS << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00005846 if (LD->isVolatile())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005847 OS << " <volatile>";
5848 OS << " alignment=" << LD->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005849 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng7196a7b2007-12-18 07:02:08 +00005850 const Value *SrcValue = ST->getSrcValue();
5851 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005852 OS << " <";
Evan Cheng7196a7b2007-12-18 07:02:08 +00005853 if (SrcValue)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005854 OS << SrcValue;
Evan Cheng7196a7b2007-12-18 07:02:08 +00005855 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005856 OS << "null";
5857 OS << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00005858
5859 if (ST->isTruncatingStore())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005860 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00005861
5862 const char *AM = getIndexedModeName(ST->getAddressingMode());
5863 if (*AM)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005864 OS << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00005865 if (ST->isVolatile())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005866 OS << " <volatile>";
5867 OS << " alignment=" << ST->getAlignment();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005868 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5869 const Value *SrcValue = AT->getSrcValue();
5870 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005871 OS << " <";
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005872 if (SrcValue)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005873 OS << SrcValue;
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005874 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005875 OS << "null";
5876 OS << ":" << SrcOffset << ">";
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005877 if (AT->isVolatile())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005878 OS << " <volatile>";
5879 OS << " alignment=" << AT->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005880 }
5881}
5882
5883static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
5884 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00005885 if (N->getOperand(i).getNode()->hasOneUse())
5886 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005887 else
5888 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greif1c80d112008-08-28 21:40:38 +00005889 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005890
5891
5892 cerr << "\n" << std::string(indent, ' ');
5893 N->dump(G);
5894}
5895
5896void SelectionDAG::dump() const {
5897 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005898
Dan Gohman5fafd422008-07-15 18:18:54 +00005899 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5900 I != E; ++I) {
5901 const SDNode *N = I;
Gabor Greif1c80d112008-08-28 21:40:38 +00005902 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman5fafd422008-07-15 18:18:54 +00005903 DumpNodes(N, 2, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005904 }
5905
Gabor Greif1c80d112008-08-28 21:40:38 +00005906 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005907
5908 cerr << "\n\n";
5909}
5910
5911const Type *ConstantPoolSDNode::getType() const {
5912 if (isMachineConstantPoolEntry())
5913 return Val.MachineCPVal->getType();
5914 return Val.ConstVal->getType();
5915}