blob: 023be5c695470da4b9b15bd4cb4071d6b8be3b26 [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 Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Support/MathExtras.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetLowering.h"
31#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/ADT/SetVector.h"
34#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsa9810f32007-10-16 09:56:48 +000035#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/ADT/SmallVector.h"
37#include "llvm/ADT/StringExtras.h"
38#include <algorithm>
39#include <cmath>
40using namespace llvm;
41
42/// makeVTList - Return an instance of the SDVTList struct initialized with the
43/// specified members.
Duncan Sands92c43912008-06-06 12:08:01 +000044static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 SDVTList Res = {VTs, NumVTs};
46 return Res;
47}
48
Duncan Sands92c43912008-06-06 12:08:01 +000049static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
50 switch (VT.getSimpleVT()) {
Chris Lattnerd037d482008-03-05 06:48:13 +000051 default: assert(0 && "Unknown FP format");
52 case MVT::f32: return &APFloat::IEEEsingle;
53 case MVT::f64: return &APFloat::IEEEdouble;
54 case MVT::f80: return &APFloat::x87DoubleExtended;
55 case MVT::f128: return &APFloat::IEEEquad;
56 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
57 }
58}
59
Chris Lattner7bcb18f2008-02-03 06:49:24 +000060SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
61
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062//===----------------------------------------------------------------------===//
63// ConstantFPSDNode Class
64//===----------------------------------------------------------------------===//
65
66/// isExactlyValue - We don't rely on operator== working on double values, as
67/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
68/// As such, this method can be used to do an exact bit-for-bit comparison of
69/// two floating point values.
Dale Johannesenc53301c2007-08-26 01:18:27 +000070bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen7f2c1d12007-08-25 22:10:57 +000071 return Value.bitwiseIsEqual(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072}
73
Duncan Sands92c43912008-06-06 12:08:01 +000074bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenbbe2b702007-08-30 00:23:21 +000075 const APFloat& Val) {
Duncan Sands92c43912008-06-06 12:08:01 +000076 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerd037d482008-03-05 06:48:13 +000077
Dale Johannesen90c25d12008-04-20 18:23:46 +000078 // PPC long double cannot be converted to any other type.
79 if (VT == MVT::ppcf128 ||
80 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerd037d482008-03-05 06:48:13 +000081 return false;
82
Dale Johannesenbbe2b702007-08-30 00:23:21 +000083 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
Chris Lattnerd037d482008-03-05 06:48:13 +000085 return Val2.convert(*MVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenbbe2b702007-08-30 00:23:21 +000087}
88
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089//===----------------------------------------------------------------------===//
90// ISD Namespace
91//===----------------------------------------------------------------------===//
92
93/// isBuildVectorAllOnes - Return true if the specified node is a
94/// BUILD_VECTOR where all of the elements are ~0 or undef.
95bool ISD::isBuildVectorAllOnes(const SDNode *N) {
96 // Look through a bit convert.
97 if (N->getOpcode() == ISD::BIT_CONVERT)
98 N = N->getOperand(0).Val;
99
100 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
101
102 unsigned i = 0, e = N->getNumOperands();
103
104 // Skip over all of the undef values.
105 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
106 ++i;
107
108 // Do not accept an all-undef vector.
109 if (i == e) return false;
110
111 // Do not accept build_vectors that aren't all constants or which have non-~0
112 // elements.
113 SDOperand NotZero = N->getOperand(i);
114 if (isa<ConstantSDNode>(NotZero)) {
115 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
116 return false;
117 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman161652c2008-02-29 01:47:35 +0000118 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
119 convertToAPInt().isAllOnesValue())
120 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 } else
122 return false;
123
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
125 // undefs.
126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
129 return false;
130 return true;
131}
132
133
134/// isBuildVectorAllZeros - Return true if the specified node is a
135/// BUILD_VECTOR where all of the elements are 0 or undef.
136bool ISD::isBuildVectorAllZeros(const SDNode *N) {
137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
140
141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
142
143 unsigned i = 0, e = N->getNumOperands();
144
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
147 ++i;
148
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
151
152 // Do not accept build_vectors that aren't all constants or which have non-~0
153 // elements.
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
157 return false;
158 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 return false;
161 } else
162 return false;
163
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
165 // undefs.
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
169 return false;
170 return true;
171}
172
Evan Chengd1045a62008-02-18 23:04:32 +0000173/// isScalarToVector - Return true if the specified node is a
174/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
175/// element is not an undef.
176bool ISD::isScalarToVector(const SDNode *N) {
177 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
178 return true;
179
180 if (N->getOpcode() != ISD::BUILD_VECTOR)
181 return false;
182 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
183 return false;
184 unsigned NumElems = N->getNumOperands();
185 for (unsigned i = 1; i < NumElems; ++i) {
186 SDOperand V = N->getOperand(i);
187 if (V.getOpcode() != ISD::UNDEF)
188 return false;
189 }
190 return true;
191}
192
193
Evan Cheng13d1c292008-01-31 09:59:15 +0000194/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengee6db0f2008-02-04 23:10:38 +0000195/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Cheng13d1c292008-01-31 09:59:15 +0000196/// is 0).
197bool ISD::isDebugLabel(const SDNode *N) {
198 SDOperand Zero;
199 if (N->getOpcode() == ISD::LABEL)
200 Zero = N->getOperand(2);
201 else if (N->isTargetOpcode() &&
202 N->getTargetOpcode() == TargetInstrInfo::LABEL)
203 // Chain moved to last operand.
204 Zero = N->getOperand(1);
205 else
206 return false;
207 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
208}
209
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
211/// when given the operation for (X op Y).
212ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
213 // To perform this operation, we just need to swap the L and G bits of the
214 // operation.
215 unsigned OldL = (Operation >> 2) & 1;
216 unsigned OldG = (Operation >> 1) & 1;
217 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
218 (OldL << 1) | // New G bit
219 (OldG << 2)); // New L bit.
220}
221
222/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
223/// 'op' is a valid SetCC operation.
224ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
225 unsigned Operation = Op;
226 if (isInteger)
227 Operation ^= 7; // Flip L, G, E bits, but not U.
228 else
229 Operation ^= 15; // Flip all of the condition bits.
230 if (Operation > ISD::SETTRUE2)
231 Operation &= ~8; // Don't let N and U bits get set.
232 return ISD::CondCode(Operation);
233}
234
235
236/// isSignedOp - For an integer comparison, return 1 if the comparison is a
237/// signed operation and 2 if the result is an unsigned comparison. Return zero
238/// if the operation does not depend on the sign of the input (setne and seteq).
239static int isSignedOp(ISD::CondCode Opcode) {
240 switch (Opcode) {
241 default: assert(0 && "Illegal integer setcc operation!");
242 case ISD::SETEQ:
243 case ISD::SETNE: return 0;
244 case ISD::SETLT:
245 case ISD::SETLE:
246 case ISD::SETGT:
247 case ISD::SETGE: return 1;
248 case ISD::SETULT:
249 case ISD::SETULE:
250 case ISD::SETUGT:
251 case ISD::SETUGE: return 2;
252 }
253}
254
255/// getSetCCOrOperation - Return the result of a logical OR between different
256/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
257/// returns SETCC_INVALID if it is not possible to represent the resultant
258/// comparison.
259ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
260 bool isInteger) {
261 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
262 // Cannot fold a signed integer setcc with an unsigned integer setcc.
263 return ISD::SETCC_INVALID;
264
265 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
266
267 // If the N and U bits get set then the resultant comparison DOES suddenly
268 // care about orderedness, and is true when ordered.
269 if (Op > ISD::SETTRUE2)
270 Op &= ~16; // Clear the U bit if the N bit is set.
271
272 // Canonicalize illegal integer setcc's.
273 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
274 Op = ISD::SETNE;
275
276 return ISD::CondCode(Op);
277}
278
279/// getSetCCAndOperation - Return the result of a logical AND between different
280/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
281/// function returns zero if it is not possible to represent the resultant
282/// comparison.
283ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
284 bool isInteger) {
285 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
286 // Cannot fold a signed setcc with an unsigned setcc.
287 return ISD::SETCC_INVALID;
288
289 // Combine all of the condition bits.
290 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
291
292 // Canonicalize illegal integer setcc's.
293 if (isInteger) {
294 switch (Result) {
295 default: break;
296 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmanf1e8e552008-05-14 18:17:09 +0000297 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
299 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
300 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
301 }
302 }
303
304 return Result;
305}
306
307const TargetMachine &SelectionDAG::getTarget() const {
308 return TLI.getTargetMachine();
309}
310
311//===----------------------------------------------------------------------===//
312// SDNode Profile Support
313//===----------------------------------------------------------------------===//
314
315/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
316///
317static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
318 ID.AddInteger(OpC);
319}
320
321/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
322/// solely with their pointer.
Dan Gohman089efff2008-05-13 00:00:25 +0000323static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 ID.AddPointer(VTList.VTs);
325}
326
327/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
328///
329static void AddNodeIDOperands(FoldingSetNodeID &ID,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000330 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 for (; NumOps; --NumOps, ++Ops) {
332 ID.AddPointer(Ops->Val);
333 ID.AddInteger(Ops->ResNo);
334 }
335}
336
337static void AddNodeIDNode(FoldingSetNodeID &ID,
338 unsigned short OpC, SDVTList VTList,
Dan Gohman1d47f5c2008-04-17 23:02:12 +0000339 SDOperandPtr OpList, unsigned N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 AddNodeIDOpcode(ID, OpC);
341 AddNodeIDValueTypes(ID, VTList);
342 AddNodeIDOperands(ID, OpList, N);
343}
344
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000345
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
347/// data.
348static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
349 AddNodeIDOpcode(ID, N->getOpcode());
350 // Add the return value info.
351 AddNodeIDValueTypes(ID, N->getVTList());
352 // Add the operand info.
353 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
354
355 // Handle SDNode leafs with special info.
356 switch (N->getOpcode()) {
357 default: break; // Normal nodes don't need extra info.
Duncan Sandsc93fae32008-03-21 09:14:45 +0000358 case ISD::ARG_FLAGS:
359 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
360 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 case ISD::TargetConstant:
362 case ISD::Constant:
Chris Lattnerf5e3e182008-02-20 06:28:01 +0000363 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 break;
365 case ISD::TargetConstantFP:
Dale Johannesendf8a8312007-08-31 04:03:46 +0000366 case ISD::ConstantFP: {
Ted Kremenekdc71c802008-02-11 17:24:50 +0000367 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 break;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000369 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 case ISD::TargetGlobalAddress:
371 case ISD::GlobalAddress:
372 case ISD::TargetGlobalTLSAddress:
373 case ISD::GlobalTLSAddress: {
374 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
375 ID.AddPointer(GA->getGlobal());
376 ID.AddInteger(GA->getOffset());
377 break;
378 }
379 case ISD::BasicBlock:
380 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
381 break;
382 case ISD::Register:
383 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
384 break;
Dan Gohman12a9c082008-02-06 22:27:42 +0000385 case ISD::SRCVALUE:
386 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
387 break;
388 case ISD::MEMOPERAND: {
Dan Gohman1fad9e62008-04-07 19:35:22 +0000389 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman12a9c082008-02-06 22:27:42 +0000390 ID.AddPointer(MO.getValue());
391 ID.AddInteger(MO.getFlags());
392 ID.AddInteger(MO.getOffset());
393 ID.AddInteger(MO.getSize());
394 ID.AddInteger(MO.getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 break;
396 }
397 case ISD::FrameIndex:
398 case ISD::TargetFrameIndex:
399 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
400 break;
401 case ISD::JumpTable:
402 case ISD::TargetJumpTable:
403 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
404 break;
405 case ISD::ConstantPool:
406 case ISD::TargetConstantPool: {
407 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
408 ID.AddInteger(CP->getAlignment());
409 ID.AddInteger(CP->getOffset());
410 if (CP->isMachineConstantPoolEntry())
411 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
412 else
413 ID.AddPointer(CP->getConstVal());
414 break;
415 }
416 case ISD::LOAD: {
417 LoadSDNode *LD = cast<LoadSDNode>(N);
418 ID.AddInteger(LD->getAddressingMode());
419 ID.AddInteger(LD->getExtensionType());
Duncan Sands3f5d2432008-06-06 12:49:32 +0000420 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 ID.AddInteger(LD->getAlignment());
422 ID.AddInteger(LD->isVolatile());
423 break;
424 }
425 case ISD::STORE: {
426 StoreSDNode *ST = cast<StoreSDNode>(N);
427 ID.AddInteger(ST->getAddressingMode());
428 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3f5d2432008-06-06 12:49:32 +0000429 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 ID.AddInteger(ST->getAlignment());
431 ID.AddInteger(ST->isVolatile());
432 break;
433 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000434 case ISD::ATOMIC_CMP_SWAP:
435 case ISD::ATOMIC_LOAD_ADD:
436 case ISD::ATOMIC_SWAP:
437 case ISD::ATOMIC_LOAD_SUB:
438 case ISD::ATOMIC_LOAD_AND:
439 case ISD::ATOMIC_LOAD_OR:
440 case ISD::ATOMIC_LOAD_XOR:
441 case ISD::ATOMIC_LOAD_NAND:
442 case ISD::ATOMIC_LOAD_MIN:
443 case ISD::ATOMIC_LOAD_MAX:
444 case ISD::ATOMIC_LOAD_UMIN:
445 case ISD::ATOMIC_LOAD_UMAX: {
446 AtomicSDNode *AT = cast<AtomicSDNode>(N);
447 ID.AddInteger(AT->getAlignment());
448 ID.AddInteger(AT->isVolatile());
449 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000451 } // end switch (N->getOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452}
453
454//===----------------------------------------------------------------------===//
455// SelectionDAG Class
456//===----------------------------------------------------------------------===//
457
458/// RemoveDeadNodes - This method deletes all unreachable nodes in the
459/// SelectionDAG.
460void SelectionDAG::RemoveDeadNodes() {
461 // Create a dummy node (which is not added to allnodes), that adds a reference
462 // to the root node, preventing it from being deleted.
463 HandleSDNode Dummy(getRoot());
464
465 SmallVector<SDNode*, 128> DeadNodes;
466
467 // Add all obviously-dead nodes to the DeadNodes worklist.
468 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
469 if (I->use_empty())
470 DeadNodes.push_back(I);
471
472 // Process the worklist, deleting the nodes and adding their uses to the
473 // worklist.
474 while (!DeadNodes.empty()) {
475 SDNode *N = DeadNodes.back();
476 DeadNodes.pop_back();
477
478 // Take the node out of the appropriate CSE map.
479 RemoveNodeFromCSEMaps(N);
480
481 // Next, brutally remove the operand list. This is safe to do, as there are
482 // no cycles in the graph.
483 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000484 SDNode *Operand = I->getVal();
Roman Levenstein05650fd2008-04-07 10:06:32 +0000485 Operand->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486
487 // Now that we removed this operand, see if there are no uses of it left.
488 if (Operand->use_empty())
489 DeadNodes.push_back(Operand);
490 }
Roman Levenstein05650fd2008-04-07 10:06:32 +0000491 if (N->OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 delete[] N->OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +0000493 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 N->OperandList = 0;
495 N->NumOperands = 0;
496
497 // Finally, remove N itself.
498 AllNodes.erase(N);
499 }
500
501 // If the root changed (e.g. it was a dead load, update the root).
502 setRoot(Dummy.getValue());
503}
504
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000505void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 SmallVector<SDNode*, 16> DeadNodes;
507 DeadNodes.push_back(N);
508
509 // Process the worklist, deleting the nodes and adding their uses to the
510 // worklist.
511 while (!DeadNodes.empty()) {
512 SDNode *N = DeadNodes.back();
513 DeadNodes.pop_back();
514
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000515 if (UpdateListener)
Duncan Sands3866b1c2008-06-11 11:42:12 +0000516 UpdateListener->NodeDeleted(N, 0);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000517
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 // Take the node out of the appropriate CSE map.
519 RemoveNodeFromCSEMaps(N);
520
521 // Next, brutally remove the operand list. This is safe to do, as there are
522 // no cycles in the graph.
523 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000524 SDNode *Operand = I->getVal();
Roman Levenstein05650fd2008-04-07 10:06:32 +0000525 Operand->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526
527 // Now that we removed this operand, see if there are no uses of it left.
528 if (Operand->use_empty())
529 DeadNodes.push_back(Operand);
530 }
Roman Levenstein05650fd2008-04-07 10:06:32 +0000531 if (N->OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 delete[] N->OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +0000533 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 N->OperandList = 0;
535 N->NumOperands = 0;
536
537 // Finally, remove N itself.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 AllNodes.erase(N);
539 }
540}
541
542void SelectionDAG::DeleteNode(SDNode *N) {
543 assert(N->use_empty() && "Cannot delete a node that is not dead!");
544
545 // First take this out of the appropriate CSE map.
546 RemoveNodeFromCSEMaps(N);
547
548 // Finally, remove uses due to operands of this node, remove from the
549 // AllNodes list, and delete the node.
550 DeleteNodeNotInCSEMaps(N);
551}
552
553void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
554
555 // Remove it from the AllNodes list.
556 AllNodes.remove(N);
557
558 // Drop all of the operands and decrement used nodes use counts.
559 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000560 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levenstein05650fd2008-04-07 10:06:32 +0000561 if (N->OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 delete[] N->OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +0000563 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 N->OperandList = 0;
565 N->NumOperands = 0;
566
567 delete N;
568}
569
570/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
571/// correspond to it. This is useful when we're about to delete or repurpose
572/// the node. We don't want future request for structurally identical nodes
573/// to return N anymore.
574void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
575 bool Erased = false;
576 switch (N->getOpcode()) {
577 case ISD::HANDLENODE: return; // noop.
578 case ISD::STRING:
579 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
580 break;
581 case ISD::CONDCODE:
582 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
583 "Cond code doesn't exist!");
584 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
585 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
586 break;
587 case ISD::ExternalSymbol:
588 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
589 break;
590 case ISD::TargetExternalSymbol:
591 Erased =
592 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
593 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000594 case ISD::VALUETYPE: {
Duncan Sands92c43912008-06-06 12:08:01 +0000595 MVT VT = cast<VTSDNode>(N)->getVT();
596 if (VT.isExtended()) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000597 Erased = ExtendedValueTypeNodes.erase(VT);
598 } else {
Duncan Sands92c43912008-06-06 12:08:01 +0000599 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
600 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000601 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000603 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 default:
605 // Remove it from the CSE Map.
606 Erased = CSEMap.RemoveNode(N);
607 break;
608 }
609#ifndef NDEBUG
610 // Verify that the node was actually in one of the CSE maps, unless it has a
611 // flag result (which cannot be CSE'd) or is one of the special cases that are
612 // not subject to CSE.
613 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
614 !N->isTargetOpcode()) {
615 N->dump(this);
616 cerr << "\n";
617 assert(0 && "Node is not in map!");
618 }
619#endif
620}
621
622/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
623/// has been taken out and modified in some way. If the specified node already
624/// exists in the CSE maps, do not modify the maps, but return the existing node
625/// instead. If it doesn't exist, add it and return null.
626///
627SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
628 assert(N->getNumOperands() && "This is a leaf node!");
629 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
630 return 0; // Never add these nodes.
631
632 // Check that remaining values produced are not flags.
633 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
634 if (N->getValueType(i) == MVT::Flag)
635 return 0; // Never CSE anything that produces a flag.
636
637 SDNode *New = CSEMap.GetOrInsertNode(N);
638 if (New != N) return New; // Node already existed.
639 return 0;
640}
641
642/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
643/// were replaced with those specified. If this node is never memoized,
644/// return null, otherwise return a pointer to the slot it would take. If a
645/// node already exists with these operands, the slot will be non-null.
646SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
647 void *&InsertPos) {
648 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
649 return 0; // Never add these nodes.
650
651 // Check that remaining values produced are not flags.
652 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
653 if (N->getValueType(i) == MVT::Flag)
654 return 0; // Never CSE anything that produces a flag.
655
656 SDOperand Ops[] = { Op };
657 FoldingSetNodeID ID;
658 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
659 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
660}
661
662/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
663/// were replaced with those specified. If this node is never memoized,
664/// return null, otherwise return a pointer to the slot it would take. If a
665/// node already exists with these operands, the slot will be non-null.
666SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
667 SDOperand Op1, SDOperand Op2,
668 void *&InsertPos) {
669 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
670 return 0; // Never add these nodes.
671
672 // Check that remaining values produced are not flags.
673 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
674 if (N->getValueType(i) == MVT::Flag)
675 return 0; // Never CSE anything that produces a flag.
676
677 SDOperand Ops[] = { Op1, Op2 };
678 FoldingSetNodeID ID;
679 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
680 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
681}
682
683
684/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
685/// were replaced with those specified. If this node is never memoized,
686/// return null, otherwise return a pointer to the slot it would take. If a
687/// node already exists with these operands, the slot will be non-null.
688SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000689 SDOperandPtr Ops,unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 void *&InsertPos) {
691 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
692 return 0; // Never add these nodes.
693
694 // Check that remaining values produced are not flags.
695 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
696 if (N->getValueType(i) == MVT::Flag)
697 return 0; // Never CSE anything that produces a flag.
698
699 FoldingSetNodeID ID;
700 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
701
702 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
703 ID.AddInteger(LD->getAddressingMode());
704 ID.AddInteger(LD->getExtensionType());
Duncan Sands3f5d2432008-06-06 12:49:32 +0000705 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 ID.AddInteger(LD->getAlignment());
707 ID.AddInteger(LD->isVolatile());
708 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
709 ID.AddInteger(ST->getAddressingMode());
710 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3f5d2432008-06-06 12:49:32 +0000711 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 ID.AddInteger(ST->getAlignment());
713 ID.AddInteger(ST->isVolatile());
714 }
715
716 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
717}
718
719
720SelectionDAG::~SelectionDAG() {
721 while (!AllNodes.empty()) {
722 SDNode *N = AllNodes.begin();
723 N->SetNextInBucket(0);
Roman Levenstein05650fd2008-04-07 10:06:32 +0000724 if (N->OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 delete [] N->OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +0000726 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 N->OperandList = 0;
728 N->NumOperands = 0;
729 AllNodes.pop_front();
730 }
731}
732
Duncan Sands92c43912008-06-06 12:08:01 +0000733SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 if (Op.getValueType() == VT) return Op;
Dan Gohman161652c2008-02-29 01:47:35 +0000735 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +0000736 VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 return getNode(ISD::AND, Op.getValueType(), Op,
738 getConstant(Imm, Op.getValueType()));
739}
740
741SDOperand SelectionDAG::getString(const std::string &Val) {
742 StringSDNode *&N = StringNodes[Val];
743 if (!N) {
744 N = new StringSDNode(Val);
745 AllNodes.push_back(N);
746 }
747 return SDOperand(N, 0);
748}
749
Duncan Sands92c43912008-06-06 12:08:01 +0000750SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng833501d2008-06-30 07:31:25 +0000751 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands92c43912008-06-06 12:08:01 +0000752 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohmandc458cf2008-02-08 22:59:30 +0000753}
754
Duncan Sands92c43912008-06-06 12:08:01 +0000755SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
756 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000757
Evan Cheng833501d2008-06-30 07:31:25 +0000758 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands92c43912008-06-06 12:08:01 +0000759 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohmandc458cf2008-02-08 22:59:30 +0000760 "APInt size does not match type size!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761
762 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
763 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000764 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenekdc71c802008-02-11 17:24:50 +0000765 ID.Add(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000767 SDNode *N = NULL;
768 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000769 if (!VT.isVector())
Dan Gohman5b9d6412007-12-12 22:21:26 +0000770 return SDOperand(N, 0);
771 if (!N) {
772 N = new ConstantSDNode(isT, Val, EltVT);
773 CSEMap.InsertNode(N, IP);
774 AllNodes.push_back(N);
775 }
776
777 SDOperand Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000778 if (VT.isVector()) {
Dan Gohman5b9d6412007-12-12 22:21:26 +0000779 SmallVector<SDOperand, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000780 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000781 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
782 }
783 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784}
785
Chris Lattner5872a362008-01-17 07:00:52 +0000786SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
787 return getConstant(Val, TLI.getPointerTy(), isTarget);
788}
789
790
Duncan Sands92c43912008-06-06 12:08:01 +0000791SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
792 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000793
Duncan Sands92c43912008-06-06 12:08:01 +0000794 MVT EltVT =
795 VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796
797 // Do the map lookup using the actual bit pattern for the floating point
798 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
799 // we don't have issues with SNANs.
800 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
801 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000802 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenekdc71c802008-02-11 17:24:50 +0000803 ID.Add(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 void *IP = 0;
805 SDNode *N = NULL;
806 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000807 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 return SDOperand(N, 0);
809 if (!N) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000810 N = new ConstantFPSDNode(isTarget, V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 CSEMap.InsertNode(N, IP);
812 AllNodes.push_back(N);
813 }
814
815 SDOperand Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000816 if (VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 SmallVector<SDOperand, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000818 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
820 }
821 return Result;
822}
823
Duncan Sands92c43912008-06-06 12:08:01 +0000824SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
825 MVT EltVT =
826 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000827 if (EltVT==MVT::f32)
828 return getConstantFP(APFloat((float)Val), VT, isTarget);
829 else
830 return getConstantFP(APFloat(Val), VT, isTarget);
831}
832
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands92c43912008-06-06 12:08:01 +0000834 MVT VT, int Offset,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 bool isTargetGA) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 unsigned Opc;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000837
838 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
839 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000840 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000841 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
842 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
843 }
844
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 if (GVar && GVar->isThreadLocal())
846 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
847 else
848 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000851 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 ID.AddPointer(GV);
853 ID.AddInteger(Offset);
854 void *IP = 0;
855 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
856 return SDOperand(E, 0);
857 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
858 CSEMap.InsertNode(N, IP);
859 AllNodes.push_back(N);
860 return SDOperand(N, 0);
861}
862
Duncan Sands92c43912008-06-06 12:08:01 +0000863SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
865 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000866 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 ID.AddInteger(FI);
868 void *IP = 0;
869 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
870 return SDOperand(E, 0);
871 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
872 CSEMap.InsertNode(N, IP);
873 AllNodes.push_back(N);
874 return SDOperand(N, 0);
875}
876
Duncan Sands92c43912008-06-06 12:08:01 +0000877SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
879 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000880 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 ID.AddInteger(JTI);
882 void *IP = 0;
883 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
884 return SDOperand(E, 0);
885 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
886 CSEMap.InsertNode(N, IP);
887 AllNodes.push_back(N);
888 return SDOperand(N, 0);
889}
890
Duncan Sands92c43912008-06-06 12:08:01 +0000891SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 unsigned Alignment, int Offset,
893 bool isTarget) {
894 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
895 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000896 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 ID.AddInteger(Alignment);
898 ID.AddInteger(Offset);
899 ID.AddPointer(C);
900 void *IP = 0;
901 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
902 return SDOperand(E, 0);
903 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
904 CSEMap.InsertNode(N, IP);
905 AllNodes.push_back(N);
906 return SDOperand(N, 0);
907}
908
909
Duncan Sands92c43912008-06-06 12:08:01 +0000910SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 unsigned Alignment, int Offset,
912 bool isTarget) {
913 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
914 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000915 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 ID.AddInteger(Alignment);
917 ID.AddInteger(Offset);
918 C->AddSelectionDAGCSEId(ID);
919 void *IP = 0;
920 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
921 return SDOperand(E, 0);
922 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
923 CSEMap.InsertNode(N, IP);
924 AllNodes.push_back(N);
925 return SDOperand(N, 0);
926}
927
928
929SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
930 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000931 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 ID.AddPointer(MBB);
933 void *IP = 0;
934 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
935 return SDOperand(E, 0);
936 SDNode *N = new BasicBlockSDNode(MBB);
937 CSEMap.InsertNode(N, IP);
938 AllNodes.push_back(N);
939 return SDOperand(N, 0);
940}
941
Duncan Sandsc93fae32008-03-21 09:14:45 +0000942SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
943 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000944 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sandsc93fae32008-03-21 09:14:45 +0000945 ID.AddInteger(Flags.getRawBits());
946 void *IP = 0;
947 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
948 return SDOperand(E, 0);
949 SDNode *N = new ARG_FLAGSSDNode(Flags);
950 CSEMap.InsertNode(N, IP);
951 AllNodes.push_back(N);
952 return SDOperand(N, 0);
953}
954
Duncan Sands92c43912008-06-06 12:08:01 +0000955SDOperand SelectionDAG::getValueType(MVT VT) {
956 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
957 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958
Duncan Sands92c43912008-06-06 12:08:01 +0000959 SDNode *&N = VT.isExtended() ?
960 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsd7307a92007-10-17 13:49:58 +0000961
962 if (N) return SDOperand(N, 0);
963 N = new VTSDNode(VT);
964 AllNodes.push_back(N);
965 return SDOperand(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966}
967
Duncan Sands92c43912008-06-06 12:08:01 +0000968SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 SDNode *&N = ExternalSymbols[Sym];
970 if (N) return SDOperand(N, 0);
971 N = new ExternalSymbolSDNode(false, Sym, VT);
972 AllNodes.push_back(N);
973 return SDOperand(N, 0);
974}
975
Duncan Sands92c43912008-06-06 12:08:01 +0000976SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 SDNode *&N = TargetExternalSymbols[Sym];
978 if (N) return SDOperand(N, 0);
979 N = new ExternalSymbolSDNode(true, Sym, VT);
980 AllNodes.push_back(N);
981 return SDOperand(N, 0);
982}
983
984SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
985 if ((unsigned)Cond >= CondCodeNodes.size())
986 CondCodeNodes.resize(Cond+1);
Duncan Sands92c43912008-06-06 12:08:01 +0000987
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 if (CondCodeNodes[Cond] == 0) {
989 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
990 AllNodes.push_back(CondCodeNodes[Cond]);
991 }
992 return SDOperand(CondCodeNodes[Cond], 0);
993}
994
Duncan Sands92c43912008-06-06 12:08:01 +0000995SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000997 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 ID.AddInteger(RegNo);
999 void *IP = 0;
1000 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1001 return SDOperand(E, 0);
1002 SDNode *N = new RegisterSDNode(RegNo, VT);
1003 CSEMap.InsertNode(N, IP);
1004 AllNodes.push_back(N);
1005 return SDOperand(N, 0);
1006}
1007
Dan Gohman12a9c082008-02-06 22:27:42 +00001008SDOperand SelectionDAG::getSrcValue(const Value *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 assert((!V || isa<PointerType>(V->getType())) &&
1010 "SrcValue is not a pointer?");
1011
1012 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001013 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 ID.AddPointer(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016 void *IP = 0;
1017 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1018 return SDOperand(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001019
1020 SDNode *N = new SrcValueSDNode(V);
1021 CSEMap.InsertNode(N, IP);
1022 AllNodes.push_back(N);
1023 return SDOperand(N, 0);
1024}
1025
Dan Gohman1fad9e62008-04-07 19:35:22 +00001026SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman12a9c082008-02-06 22:27:42 +00001027 const Value *v = MO.getValue();
1028 assert((!v || isa<PointerType>(v->getType())) &&
1029 "SrcValue is not a pointer?");
1030
1031 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001032 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001033 ID.AddPointer(v);
1034 ID.AddInteger(MO.getFlags());
1035 ID.AddInteger(MO.getOffset());
1036 ID.AddInteger(MO.getSize());
1037 ID.AddInteger(MO.getAlignment());
1038
1039 void *IP = 0;
1040 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1041 return SDOperand(E, 0);
1042
1043 SDNode *N = new MemOperandSDNode(MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 CSEMap.InsertNode(N, IP);
1045 AllNodes.push_back(N);
1046 return SDOperand(N, 0);
1047}
1048
Chris Lattner53f5aee2007-10-15 17:47:20 +00001049/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1050/// specified value type.
Duncan Sands92c43912008-06-06 12:08:01 +00001051SDOperand SelectionDAG::CreateStackTemporary(MVT VT) {
Chris Lattner53f5aee2007-10-15 17:47:20 +00001052 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00001053 unsigned ByteSize = VT.getSizeInBits()/8;
1054 const Type *Ty = VT.getTypeForMVT();
Chris Lattner53f5aee2007-10-15 17:47:20 +00001055 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1056 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1057 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1058}
1059
1060
Duncan Sands92c43912008-06-06 12:08:01 +00001061SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 SDOperand N2, ISD::CondCode Cond) {
1063 // These setcc operations always fold.
1064 switch (Cond) {
1065 default: break;
1066 case ISD::SETFALSE:
1067 case ISD::SETFALSE2: return getConstant(0, VT);
1068 case ISD::SETTRUE:
1069 case ISD::SETTRUE2: return getConstant(1, VT);
1070
1071 case ISD::SETOEQ:
1072 case ISD::SETOGT:
1073 case ISD::SETOGE:
1074 case ISD::SETOLT:
1075 case ISD::SETOLE:
1076 case ISD::SETONE:
1077 case ISD::SETO:
1078 case ISD::SETUO:
1079 case ISD::SETUEQ:
1080 case ISD::SETUNE:
Duncan Sands92c43912008-06-06 12:08:01 +00001081 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 break;
1083 }
1084
1085 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001086 const APInt &C2 = N2C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001088 const APInt &C1 = N1C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089
1090 switch (Cond) {
1091 default: assert(0 && "Unknown integer setcc!");
1092 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1093 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00001094 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1095 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1096 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1097 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1098 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1099 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1100 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1101 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102 }
1103 }
1104 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001105 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001107 // No compile time operations on this type yet.
1108 if (N1C->getValueType(0) == MVT::ppcf128)
1109 return SDOperand();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001110
1111 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001113 default: break;
Dale Johannesen76844472007-08-31 17:03:33 +00001114 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1115 return getNode(ISD::UNDEF, VT);
1116 // fall through
1117 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1118 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1119 return getNode(ISD::UNDEF, VT);
1120 // fall through
1121 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001122 R==APFloat::cmpLessThan, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001123 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1124 return getNode(ISD::UNDEF, VT);
1125 // fall through
1126 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1127 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1128 return getNode(ISD::UNDEF, VT);
1129 // fall through
1130 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1131 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1132 return getNode(ISD::UNDEF, VT);
1133 // fall through
1134 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001135 R==APFloat::cmpEqual, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001136 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1137 return getNode(ISD::UNDEF, VT);
1138 // fall through
1139 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001140 R==APFloat::cmpEqual, VT);
1141 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1142 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1143 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1144 R==APFloat::cmpEqual, VT);
1145 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1146 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1147 R==APFloat::cmpLessThan, VT);
1148 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1149 R==APFloat::cmpUnordered, VT);
1150 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1151 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 }
1153 } else {
1154 // Ensure that the constant occurs on the RHS.
1155 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1156 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001157 }
1158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159 // Could not fold it.
1160 return SDOperand();
1161}
1162
Dan Gohman07961cd2008-02-25 21:11:39 +00001163/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1164/// use this predicate to simplify operations downstream.
1165bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1166 unsigned BitWidth = Op.getValueSizeInBits();
1167 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1168}
1169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1171/// this predicate to simplify operations downstream. Mask is known to be zero
1172/// for bits that V cannot have.
Dan Gohman07961cd2008-02-25 21:11:39 +00001173bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001175 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1177 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1178 return (KnownZero & Mask) == Mask;
1179}
1180
1181/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1182/// known to be either zero or one and return them in the KnownZero/KnownOne
1183/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1184/// processing.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001185void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00001186 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 unsigned Depth) const {
Dan Gohman229fa052008-02-13 00:35:47 +00001188 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands92c43912008-06-06 12:08:01 +00001189 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohman56eaab32008-02-13 23:13:32 +00001190 "Mask size mismatches value type size!");
1191
Dan Gohman229fa052008-02-13 00:35:47 +00001192 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 if (Depth == 6 || Mask == 0)
1194 return; // Limit search depth.
1195
Dan Gohman229fa052008-02-13 00:35:47 +00001196 APInt KnownZero2, KnownOne2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197
1198 switch (Op.getOpcode()) {
1199 case ISD::Constant:
1200 // We know all of the bits for a constant!
Dan Gohman229fa052008-02-13 00:35:47 +00001201 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 KnownZero = ~KnownOne & Mask;
1203 return;
1204 case ISD::AND:
1205 // If either the LHS or the RHS are Zero, the result is zero.
1206 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001207 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1208 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1210 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1211
1212 // Output known-1 bits are only known if set in both the LHS & RHS.
1213 KnownOne &= KnownOne2;
1214 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1215 KnownZero |= KnownZero2;
1216 return;
1217 case ISD::OR:
1218 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001219 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1220 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1222 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1223
1224 // Output known-0 bits are only known if clear in both the LHS & RHS.
1225 KnownZero &= KnownZero2;
1226 // Output known-1 are known to be set if set in either the LHS | RHS.
1227 KnownOne |= KnownOne2;
1228 return;
1229 case ISD::XOR: {
1230 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1231 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1232 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1233 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1234
1235 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohman229fa052008-02-13 00:35:47 +00001236 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1238 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1239 KnownZero = KnownZeroOut;
1240 return;
1241 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001242 case ISD::MUL: {
1243 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1244 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1245 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1246 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1247 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1248
1249 // If low bits are zero in either operand, output low known-0 bits.
1250 // Also compute a conserative estimate for high known-0 bits.
1251 // More trickiness is possible, but this is sufficient for the
1252 // interesting case of alignment computation.
1253 KnownOne.clear();
1254 unsigned TrailZ = KnownZero.countTrailingOnes() +
1255 KnownZero2.countTrailingOnes();
1256 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman4c451852008-05-07 00:35:55 +00001257 KnownZero2.countLeadingOnes(),
1258 BitWidth) - BitWidth;
Dan Gohmanbec16052008-04-28 17:02:21 +00001259
1260 TrailZ = std::min(TrailZ, BitWidth);
1261 LeadZ = std::min(LeadZ, BitWidth);
1262 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1263 APInt::getHighBitsSet(BitWidth, LeadZ);
1264 KnownZero &= Mask;
1265 return;
1266 }
1267 case ISD::UDIV: {
1268 // For the purposes of computing leading zeros we can conservatively
1269 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001270 // be less than the denominator.
Dan Gohmanbec16052008-04-28 17:02:21 +00001271 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1272 ComputeMaskedBits(Op.getOperand(0),
1273 AllOnes, KnownZero2, KnownOne2, Depth+1);
1274 unsigned LeadZ = KnownZero2.countLeadingOnes();
1275
1276 KnownOne2.clear();
1277 KnownZero2.clear();
1278 ComputeMaskedBits(Op.getOperand(1),
1279 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001280 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1281 if (RHSUnknownLeadingOnes != BitWidth)
1282 LeadZ = std::min(BitWidth,
1283 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001284
1285 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1286 return;
1287 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 case ISD::SELECT:
1289 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1290 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1291 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1292 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1293
1294 // Only known if known in both the LHS and RHS.
1295 KnownOne &= KnownOne2;
1296 KnownZero &= KnownZero2;
1297 return;
1298 case ISD::SELECT_CC:
1299 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1300 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1301 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1302 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1303
1304 // Only known if known in both the LHS and RHS.
1305 KnownOne &= KnownOne2;
1306 KnownZero &= KnownZero2;
1307 return;
1308 case ISD::SETCC:
1309 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohman229fa052008-02-13 00:35:47 +00001310 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1311 BitWidth > 1)
1312 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313 return;
1314 case ISD::SHL:
1315 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1316 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman4b8d0002008-02-26 18:50:50 +00001317 unsigned ShAmt = SA->getValue();
1318
1319 // If the shift count is an invalid immediate, don't do anything.
1320 if (ShAmt >= BitWidth)
1321 return;
1322
1323 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 KnownZero, KnownOne, Depth+1);
1325 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman4b8d0002008-02-26 18:50:50 +00001326 KnownZero <<= ShAmt;
1327 KnownOne <<= ShAmt;
Dan Gohman229fa052008-02-13 00:35:47 +00001328 // low bits known zero.
Dan Gohman4b8d0002008-02-26 18:50:50 +00001329 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 }
1331 return;
1332 case ISD::SRL:
1333 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1334 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 unsigned ShAmt = SA->getValue();
1336
Dan Gohman4b8d0002008-02-26 18:50:50 +00001337 // If the shift count is an invalid immediate, don't do anything.
1338 if (ShAmt >= BitWidth)
1339 return;
1340
Dan Gohman229fa052008-02-13 00:35:47 +00001341 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001342 KnownZero, KnownOne, Depth+1);
1343 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001344 KnownZero = KnownZero.lshr(ShAmt);
1345 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346
Dan Gohman4d81a742008-02-13 22:43:25 +00001347 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348 KnownZero |= HighBits; // High bits known zero.
1349 }
1350 return;
1351 case ISD::SRA:
1352 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 unsigned ShAmt = SA->getValue();
1354
Dan Gohman4b8d0002008-02-26 18:50:50 +00001355 // If the shift count is an invalid immediate, don't do anything.
1356 if (ShAmt >= BitWidth)
1357 return;
1358
Dan Gohman229fa052008-02-13 00:35:47 +00001359 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 // If any of the demanded bits are produced by the sign extension, we also
1361 // demand the input sign bit.
Dan Gohman4d81a742008-02-13 22:43:25 +00001362 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1363 if (HighBits.getBoolValue())
Dan Gohman229fa052008-02-13 00:35:47 +00001364 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365
1366 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1367 Depth+1);
1368 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001369 KnownZero = KnownZero.lshr(ShAmt);
1370 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001371
1372 // Handle the sign bits.
Dan Gohman229fa052008-02-13 00:35:47 +00001373 APInt SignBit = APInt::getSignBit(BitWidth);
1374 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375
Dan Gohman91507292008-02-20 16:30:17 +00001376 if (KnownZero.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 KnownZero |= HighBits; // New bits are known zero.
Dan Gohman91507292008-02-20 16:30:17 +00001378 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 KnownOne |= HighBits; // New bits are known one.
1380 }
1381 }
1382 return;
1383 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00001384 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1385 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386
1387 // Sign extension. Compute the demanded bits in the result that are not
1388 // present in the input.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001389 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390
Dan Gohmand0dfc772008-02-13 22:28:48 +00001391 APInt InSignBit = APInt::getSignBit(EBits);
1392 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393
1394 // If the sign extended bits are demanded, we know that the sign
1395 // bit is demanded.
Dan Gohman229fa052008-02-13 00:35:47 +00001396 InSignBit.zext(BitWidth);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001397 if (NewBits.getBoolValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 InputDemandedBits |= InSignBit;
1399
1400 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1401 KnownZero, KnownOne, Depth+1);
1402 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1403
1404 // If the sign bit of the input is known set or clear, then we know the
1405 // top bits of the result.
Dan Gohman91507292008-02-20 16:30:17 +00001406 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001407 KnownZero |= NewBits;
1408 KnownOne &= ~NewBits;
Dan Gohman91507292008-02-20 16:30:17 +00001409 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 KnownOne |= NewBits;
1411 KnownZero &= ~NewBits;
1412 } else { // Input sign bit unknown
1413 KnownZero &= ~NewBits;
1414 KnownOne &= ~NewBits;
1415 }
1416 return;
1417 }
1418 case ISD::CTTZ:
1419 case ISD::CTLZ:
1420 case ISD::CTPOP: {
Dan Gohman229fa052008-02-13 00:35:47 +00001421 unsigned LowBits = Log2_32(BitWidth)+1;
1422 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman75e24fb2008-06-21 22:02:15 +00001423 KnownOne.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424 return;
1425 }
1426 case ISD::LOAD: {
1427 if (ISD::isZEXTLoad(Op.Val)) {
1428 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands92c43912008-06-06 12:08:01 +00001429 MVT VT = LD->getMemoryVT();
1430 unsigned MemBits = VT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001431 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432 }
1433 return;
1434 }
1435 case ISD::ZERO_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001436 MVT InVT = Op.getOperand(0).getValueType();
1437 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001438 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1439 APInt InMask = Mask;
1440 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001441 KnownZero.trunc(InBits);
1442 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001443 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001444 KnownZero.zext(BitWidth);
1445 KnownOne.zext(BitWidth);
1446 KnownZero |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 return;
1448 }
1449 case ISD::SIGN_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001450 MVT InVT = Op.getOperand(0).getValueType();
1451 unsigned InBits = InVT.getSizeInBits();
Dan Gohman229fa052008-02-13 00:35:47 +00001452 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001453 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1454 APInt InMask = Mask;
1455 InMask.trunc(InBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456
1457 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohmand0dfc772008-02-13 22:28:48 +00001458 // bit is demanded. Temporarily set this bit in the mask for our callee.
1459 if (NewBits.getBoolValue())
1460 InMask |= InSignBit;
Dan Gohman229fa052008-02-13 00:35:47 +00001461
Dan Gohman229fa052008-02-13 00:35:47 +00001462 KnownZero.trunc(InBits);
1463 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001464 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1465
1466 // Note if the sign bit is known to be zero or one.
1467 bool SignBitKnownZero = KnownZero.isNegative();
1468 bool SignBitKnownOne = KnownOne.isNegative();
1469 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1470 "Sign bit can't be known to be both zero and one!");
1471
1472 // If the sign bit wasn't actually demanded by our caller, we don't
1473 // want it set in the KnownZero and KnownOne result values. Reset the
1474 // mask and reapply it to the result values.
1475 InMask = Mask;
1476 InMask.trunc(InBits);
1477 KnownZero &= InMask;
1478 KnownOne &= InMask;
1479
Dan Gohman229fa052008-02-13 00:35:47 +00001480 KnownZero.zext(BitWidth);
1481 KnownOne.zext(BitWidth);
1482
Dan Gohmand0dfc772008-02-13 22:28:48 +00001483 // If the sign bit is known zero or one, the top bits match.
1484 if (SignBitKnownZero)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485 KnownZero |= NewBits;
Dan Gohmand0dfc772008-02-13 22:28:48 +00001486 else if (SignBitKnownOne)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 KnownOne |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488 return;
1489 }
1490 case ISD::ANY_EXTEND: {
Duncan Sands92c43912008-06-06 12:08:01 +00001491 MVT InVT = Op.getOperand(0).getValueType();
1492 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001493 APInt InMask = Mask;
1494 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001495 KnownZero.trunc(InBits);
1496 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001497 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001498 KnownZero.zext(BitWidth);
1499 KnownOne.zext(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500 return;
1501 }
1502 case ISD::TRUNCATE: {
Duncan Sands92c43912008-06-06 12:08:01 +00001503 MVT InVT = Op.getOperand(0).getValueType();
1504 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001505 APInt InMask = Mask;
1506 InMask.zext(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001507 KnownZero.zext(InBits);
1508 KnownOne.zext(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001509 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001511 KnownZero.trunc(BitWidth);
1512 KnownOne.trunc(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513 break;
1514 }
1515 case ISD::AssertZext: {
Duncan Sands92c43912008-06-06 12:08:01 +00001516 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1517 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1519 KnownOne, Depth+1);
1520 KnownZero |= (~InMask) & Mask;
1521 return;
1522 }
Chris Lattner13f06832007-12-22 21:26:52 +00001523 case ISD::FGETSIGN:
1524 // All bits are zero except the low bit.
Dan Gohman229fa052008-02-13 00:35:47 +00001525 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattner13f06832007-12-22 21:26:52 +00001526 return;
1527
Dan Gohmanbec16052008-04-28 17:02:21 +00001528 case ISD::SUB: {
1529 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1530 // We know that the top bits of C-X are clear if X contains less bits
1531 // than C (i.e. no wrap-around can happen). For example, 20-X is
1532 // positive if we can prove that X is >= 0 and < 16.
1533 if (CLHS->getAPIntValue().isNonNegative()) {
1534 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1535 // NLZ can't be BitWidth with no sign bit
1536 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1537 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1538 Depth+1);
1539
1540 // If all of the MaskV bits are known to be zero, then we know the
1541 // output top bits are zero, because we now know that the output is
1542 // from [0-C].
1543 if ((KnownZero2 & MaskV) == MaskV) {
1544 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1545 // Top bits known zero.
1546 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1547 }
1548 }
1549 }
1550 }
1551 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552 case ISD::ADD: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553 // Output known-0 bits are known if clear or set in both the low clear bits
1554 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1555 // low 3 bits clear.
Dan Gohmanbec16052008-04-28 17:02:21 +00001556 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1557 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1558 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1559 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1560
1561 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1562 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1563 KnownZeroOut = std::min(KnownZeroOut,
1564 KnownZero2.countTrailingOnes());
1565
1566 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001567 return;
1568 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001569 case ISD::SREM:
1570 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1571 APInt RA = Rem->getAPIntValue();
1572 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman5a154a12008-05-06 00:51:48 +00001573 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohmanbec16052008-04-28 17:02:21 +00001574 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1575 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576
Dan Gohmanbec16052008-04-28 17:02:21 +00001577 // The sign of a remainder is equal to the sign of the first
1578 // operand (zero being positive).
1579 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1580 KnownZero2 |= ~LowBits;
1581 else if (KnownOne2[BitWidth-1])
1582 KnownOne2 |= ~LowBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583
Dan Gohmanbec16052008-04-28 17:02:21 +00001584 KnownZero |= KnownZero2 & Mask;
1585 KnownOne |= KnownOne2 & Mask;
1586
1587 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 }
1589 }
1590 return;
Dan Gohmanbec16052008-04-28 17:02:21 +00001591 case ISD::UREM: {
1592 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1593 APInt RA = Rem->getAPIntValue();
Dan Gohman5a154a12008-05-06 00:51:48 +00001594 if (RA.isPowerOf2()) {
1595 APInt LowBits = (RA - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001596 APInt Mask2 = LowBits & Mask;
1597 KnownZero |= ~LowBits & Mask;
1598 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1599 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1600 break;
1601 }
1602 }
1603
1604 // Since the result is less than or equal to either operand, any leading
1605 // zero bits in either operand must also exist in the result.
1606 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1607 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1608 Depth+1);
1609 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1610 Depth+1);
1611
1612 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1613 KnownZero2.countLeadingOnes());
1614 KnownOne.clear();
1615 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1616 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 }
1618 default:
1619 // Allow the target to implement this method for its nodes.
1620 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1621 case ISD::INTRINSIC_WO_CHAIN:
1622 case ISD::INTRINSIC_W_CHAIN:
1623 case ISD::INTRINSIC_VOID:
1624 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1625 }
1626 return;
1627 }
1628}
1629
1630/// ComputeNumSignBits - Return the number of times the sign bit of the
1631/// register is replicated into the other bits. We know that at least 1 bit
1632/// is always equal to the sign bit (itself), but other cases can give us
1633/// information. For example, immediately after an "SRA X, 2", we know that
1634/// the top 3 bits are all equal to each other, so we return 3.
1635unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands92c43912008-06-06 12:08:01 +00001636 MVT VT = Op.getValueType();
1637 assert(VT.isInteger() && "Invalid VT!");
1638 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 unsigned Tmp, Tmp2;
Dan Gohman4afc4252008-05-23 02:28:01 +00001640 unsigned FirstAnswer = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001641
1642 if (Depth == 6)
1643 return 1; // Limit search depth.
1644
1645 switch (Op.getOpcode()) {
1646 default: break;
1647 case ISD::AssertSext:
Duncan Sands92c43912008-06-06 12:08:01 +00001648 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 return VTBits-Tmp+1;
1650 case ISD::AssertZext:
Duncan Sands92c43912008-06-06 12:08:01 +00001651 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001652 return VTBits-Tmp;
1653
1654 case ISD::Constant: {
Dan Gohman463db8c2008-03-03 23:35:36 +00001655 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1656 // If negative, return # leading ones.
1657 if (Val.isNegative())
1658 return Val.countLeadingOnes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001659
Dan Gohman463db8c2008-03-03 23:35:36 +00001660 // Return # leading zeros.
1661 return Val.countLeadingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001662 }
1663
1664 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00001665 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001666 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1667
1668 case ISD::SIGN_EXTEND_INREG:
1669 // Max of the input and what this extends.
Duncan Sands92c43912008-06-06 12:08:01 +00001670 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001671 Tmp = VTBits-Tmp+1;
1672
1673 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1674 return std::max(Tmp, Tmp2);
1675
1676 case ISD::SRA:
1677 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1678 // SRA X, C -> adds C sign bits.
1679 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1680 Tmp += C->getValue();
1681 if (Tmp > VTBits) Tmp = VTBits;
1682 }
1683 return Tmp;
1684 case ISD::SHL:
1685 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1686 // shl destroys sign bits.
1687 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1688 if (C->getValue() >= VTBits || // Bad shift.
1689 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1690 return Tmp - C->getValue();
1691 }
1692 break;
1693 case ISD::AND:
1694 case ISD::OR:
1695 case ISD::XOR: // NOT is handled here.
Dan Gohman4afc4252008-05-23 02:28:01 +00001696 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001697 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman4afc4252008-05-23 02:28:01 +00001698 if (Tmp != 1) {
1699 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1700 FirstAnswer = std::min(Tmp, Tmp2);
1701 // We computed what we know about the sign bits as our first
1702 // answer. Now proceed to the generic code that uses
1703 // ComputeMaskedBits, and pick whichever answer is better.
1704 }
1705 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001706
1707 case ISD::SELECT:
Dan Gohman6ffcf262008-05-20 20:59:51 +00001708 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 if (Tmp == 1) return 1; // Early out.
Dan Gohman6ffcf262008-05-20 20:59:51 +00001710 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001711 return std::min(Tmp, Tmp2);
1712
1713 case ISD::SETCC:
1714 // If setcc returns 0/-1, all bits are sign bits.
1715 if (TLI.getSetCCResultContents() ==
1716 TargetLowering::ZeroOrNegativeOneSetCCResult)
1717 return VTBits;
1718 break;
1719 case ISD::ROTL:
1720 case ISD::ROTR:
1721 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1722 unsigned RotAmt = C->getValue() & (VTBits-1);
1723
1724 // Handle rotate right by N like a rotate left by 32-N.
1725 if (Op.getOpcode() == ISD::ROTR)
1726 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1727
1728 // If we aren't rotating out all of the known-in sign bits, return the
1729 // number that are left. This handles rotl(sext(x), 1) for example.
1730 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1731 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1732 }
1733 break;
1734 case ISD::ADD:
1735 // Add can have at most one carry bit. Thus we know that the output
1736 // is, at worst, one more bit than the inputs.
1737 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1738 if (Tmp == 1) return 1; // Early out.
1739
1740 // Special case decrementing a value (ADD X, -1):
1741 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1742 if (CRHS->isAllOnesValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00001743 APInt KnownZero, KnownOne;
1744 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001745 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1746
1747 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1748 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00001749 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001750 return VTBits;
1751
1752 // If we are subtracting one from a positive number, there is no carry
1753 // out of the result.
Dan Gohman63f4e462008-02-27 01:23:58 +00001754 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 return Tmp;
1756 }
1757
1758 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1759 if (Tmp2 == 1) return 1;
1760 return std::min(Tmp, Tmp2)-1;
1761 break;
1762
1763 case ISD::SUB:
1764 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1765 if (Tmp2 == 1) return 1;
1766
1767 // Handle NEG.
1768 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00001769 if (CLHS->isNullValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00001770 APInt KnownZero, KnownOne;
1771 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1773 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1774 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00001775 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 return VTBits;
1777
1778 // If the input is known to be positive (the sign bit is known clear),
1779 // the output of the NEG has the same number of sign bits as the input.
Dan Gohman63f4e462008-02-27 01:23:58 +00001780 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001781 return Tmp2;
1782
1783 // Otherwise, we treat this like a SUB.
1784 }
1785
1786 // Sub can have at most one carry bit. Thus we know that the output
1787 // is, at worst, one more bit than the inputs.
1788 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1789 if (Tmp == 1) return 1; // Early out.
1790 return std::min(Tmp, Tmp2)-1;
1791 break;
1792 case ISD::TRUNCATE:
1793 // FIXME: it's tricky to do anything useful for this, but it is an important
1794 // case for targets like X86.
1795 break;
1796 }
1797
1798 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1799 if (Op.getOpcode() == ISD::LOAD) {
1800 LoadSDNode *LD = cast<LoadSDNode>(Op);
1801 unsigned ExtType = LD->getExtensionType();
1802 switch (ExtType) {
1803 default: break;
1804 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands92c43912008-06-06 12:08:01 +00001805 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806 return VTBits-Tmp+1;
1807 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands92c43912008-06-06 12:08:01 +00001808 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001809 return VTBits-Tmp;
1810 }
1811 }
1812
1813 // Allow the target to implement this method for its nodes.
1814 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1815 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1816 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1817 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1818 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman4afc4252008-05-23 02:28:01 +00001819 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001820 }
1821
1822 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1823 // use this information.
Dan Gohman63f4e462008-02-27 01:23:58 +00001824 APInt KnownZero, KnownOne;
1825 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1827
Dan Gohman63f4e462008-02-27 01:23:58 +00001828 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829 Mask = KnownZero;
Dan Gohman63f4e462008-02-27 01:23:58 +00001830 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 Mask = KnownOne;
1832 } else {
1833 // Nothing known.
Dan Gohman4afc4252008-05-23 02:28:01 +00001834 return FirstAnswer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835 }
1836
1837 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1838 // the number of identical bits in the top of the input value.
Dan Gohman63f4e462008-02-27 01:23:58 +00001839 Mask = ~Mask;
1840 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841 // Return # leading zeros. We use 'min' here in case Val was zero before
1842 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman4afc4252008-05-23 02:28:01 +00001843 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001844}
1845
1846
Evan Cheng2e28d622008-02-02 04:07:54 +00001847bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1848 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1849 if (!GA) return false;
1850 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1851 if (!GV) return false;
1852 MachineModuleInfo *MMI = getMachineModuleInfo();
1853 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1854}
1855
1856
Evan Cheng411fc172008-05-13 08:35:03 +00001857/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1858/// element of the result of the vector shuffle.
Evan Cheng57db53b2008-06-25 20:52:59 +00001859SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001860 MVT VT = N->getValueType(0);
Evan Cheng411fc172008-05-13 08:35:03 +00001861 SDOperand PermMask = N->getOperand(2);
Evan Cheng57db53b2008-06-25 20:52:59 +00001862 SDOperand Idx = PermMask.getOperand(i);
1863 if (Idx.getOpcode() == ISD::UNDEF)
1864 return getNode(ISD::UNDEF, VT.getVectorElementType());
1865 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng411fc172008-05-13 08:35:03 +00001866 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng57db53b2008-06-25 20:52:59 +00001867 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1868 Index %= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00001869
1870 if (V.getOpcode() == ISD::BIT_CONVERT) {
1871 V = V.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00001872 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengdea99362008-05-29 08:22:04 +00001873 return SDOperand();
Evan Cheng411fc172008-05-13 08:35:03 +00001874 }
Evan Chengdea99362008-05-29 08:22:04 +00001875 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00001876 return (Index == 0) ? V.getOperand(0)
Duncan Sands92c43912008-06-06 12:08:01 +00001877 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengdea99362008-05-29 08:22:04 +00001878 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00001879 return V.getOperand(Index);
1880 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1881 return getShuffleScalarElt(V.Val, Index);
Evan Cheng411fc172008-05-13 08:35:03 +00001882 return SDOperand();
1883}
1884
1885
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001886/// getNode - Gets or creates the specified node.
1887///
Duncan Sands92c43912008-06-06 12:08:01 +00001888SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001889 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001890 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001891 void *IP = 0;
1892 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1893 return SDOperand(E, 0);
1894 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1895 CSEMap.InsertNode(N, IP);
1896
1897 AllNodes.push_back(N);
1898 return SDOperand(N, 0);
1899}
1900
Duncan Sands92c43912008-06-06 12:08:01 +00001901SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001902 // Constant fold unary operations with an integer constant operand.
1903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001904 const APInt &Val = C->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00001905 unsigned BitWidth = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001906 switch (Opcode) {
1907 default: break;
Evan Chengeadaf442008-03-06 17:42:34 +00001908 case ISD::SIGN_EXTEND:
1909 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001910 case ISD::ANY_EXTEND:
Dan Gohman161652c2008-02-29 01:47:35 +00001911 case ISD::ZERO_EXTEND:
Evan Chengeadaf442008-03-06 17:42:34 +00001912 case ISD::TRUNCATE:
1913 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001914 case ISD::UINT_TO_FP:
1915 case ISD::SINT_TO_FP: {
1916 const uint64_t zero[] = {0, 0};
Dale Johannesenb89072e2007-10-16 23:38:29 +00001917 // No compile time operations on this type.
1918 if (VT==MVT::ppcf128)
1919 break;
Dan Gohman161652c2008-02-29 01:47:35 +00001920 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1921 (void)apf.convertFromAPInt(Val,
1922 Opcode==ISD::SINT_TO_FP,
1923 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001924 return getConstantFP(apf, VT);
1925 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001926 case ISD::BIT_CONVERT:
1927 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman161652c2008-02-29 01:47:35 +00001928 return getConstantFP(Val.bitsToFloat(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman161652c2008-02-29 01:47:35 +00001930 return getConstantFP(Val.bitsToDouble(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931 break;
1932 case ISD::BSWAP:
Dan Gohman161652c2008-02-29 01:47:35 +00001933 return getConstant(Val.byteSwap(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001934 case ISD::CTPOP:
Dan Gohman161652c2008-02-29 01:47:35 +00001935 return getConstant(Val.countPopulation(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001936 case ISD::CTLZ:
Dan Gohman161652c2008-02-29 01:47:35 +00001937 return getConstant(Val.countLeadingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001938 case ISD::CTTZ:
Dan Gohman161652c2008-02-29 01:47:35 +00001939 return getConstant(Val.countTrailingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001940 }
1941 }
1942
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001943 // Constant fold unary operations with a floating point constant operand.
1944 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1945 APFloat V = C->getValueAPF(); // make copy
Chris Lattner5872a362008-01-17 07:00:52 +00001946 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00001947 switch (Opcode) {
1948 case ISD::FNEG:
1949 V.changeSign();
1950 return getConstantFP(V, VT);
1951 case ISD::FABS:
1952 V.clearSign();
1953 return getConstantFP(V, VT);
1954 case ISD::FP_ROUND:
1955 case ISD::FP_EXTEND:
1956 // This can return overflow, underflow, or inexact; we don't care.
1957 // FIXME need to be more flexible about rounding mode.
Chris Lattnerd037d482008-03-05 06:48:13 +00001958 (void)V.convert(*MVTToAPFloatSemantics(VT),
1959 APFloat::rmNearestTiesToEven);
Dale Johannesenb89072e2007-10-16 23:38:29 +00001960 return getConstantFP(V, VT);
1961 case ISD::FP_TO_SINT:
1962 case ISD::FP_TO_UINT: {
1963 integerPart x;
1964 assert(integerPartWidth >= 64);
1965 // FIXME need to be more flexible about rounding mode.
1966 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1967 Opcode==ISD::FP_TO_SINT,
1968 APFloat::rmTowardZero);
1969 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1970 break;
1971 return getConstant(x, VT);
1972 }
1973 case ISD::BIT_CONVERT:
1974 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1975 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1976 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1977 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001978 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00001979 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001980 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001981 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001982
1983 unsigned OpOpcode = Operand.Val->getOpcode();
1984 switch (Opcode) {
1985 case ISD::TokenFactor:
Dan Gohman849d7c62008-04-14 18:43:25 +00001986 case ISD::MERGE_VALUES:
1987 return Operand; // Factor or merge of one node? No need.
Chris Lattner5872a362008-01-17 07:00:52 +00001988 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001989 case ISD::FP_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00001990 assert(VT.isFloatingPoint() &&
1991 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00001992 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnere6465dd2008-03-11 06:21:08 +00001993 if (Operand.getOpcode() == ISD::UNDEF)
1994 return getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001995 break;
Chris Lattnere6465dd2008-03-11 06:21:08 +00001996 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00001997 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001998 "Invalid SIGN_EXTEND!");
1999 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002000 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002001 && "Invalid sext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002002 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2003 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2004 break;
2005 case ISD::ZERO_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002006 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007 "Invalid ZERO_EXTEND!");
2008 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002009 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002010 && "Invalid zext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
2012 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
2013 break;
2014 case ISD::ANY_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002015 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 "Invalid ANY_EXTEND!");
2017 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002018 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002019 && "Invalid anyext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002020 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2021 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2022 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2023 break;
2024 case ISD::TRUNCATE:
Duncan Sands92c43912008-06-06 12:08:01 +00002025 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 "Invalid TRUNCATE!");
2027 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsec142ee2008-06-08 20:54:56 +00002028 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002029 && "Invalid truncate node, src < dst!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 if (OpOpcode == ISD::TRUNCATE)
2031 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2032 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2033 OpOpcode == ISD::ANY_EXTEND) {
2034 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsec142ee2008-06-08 20:54:56 +00002035 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002036 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00002037 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002038 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2039 else
2040 return Operand.Val->getOperand(0);
2041 }
2042 break;
2043 case ISD::BIT_CONVERT:
2044 // Basic sanity checking.
Duncan Sands92c43912008-06-06 12:08:01 +00002045 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046 && "Cannot BIT_CONVERT between types of different sizes!");
2047 if (VT == Operand.getValueType()) return Operand; // noop conversion.
2048 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2049 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
2050 if (OpOpcode == ISD::UNDEF)
2051 return getNode(ISD::UNDEF, VT);
2052 break;
2053 case ISD::SCALAR_TO_VECTOR:
Duncan Sands92c43912008-06-06 12:08:01 +00002054 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2055 VT.getVectorElementType() == Operand.getValueType() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002056 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattner9f0705c2008-03-08 23:43:36 +00002057 if (OpOpcode == ISD::UNDEF)
2058 return getNode(ISD::UNDEF, VT);
2059 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2060 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2061 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2062 Operand.getConstantOperandVal(1) == 0 &&
2063 Operand.getOperand(0).getValueType() == VT)
2064 return Operand.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002065 break;
2066 case ISD::FNEG:
2067 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2068 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
2069 Operand.Val->getOperand(0));
2070 if (OpOpcode == ISD::FNEG) // --X -> X
2071 return Operand.Val->getOperand(0);
2072 break;
2073 case ISD::FABS:
2074 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2075 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2076 break;
2077 }
2078
2079 SDNode *N;
2080 SDVTList VTs = getVTList(VT);
2081 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
2082 FoldingSetNodeID ID;
2083 SDOperand Ops[1] = { Operand };
2084 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
2085 void *IP = 0;
2086 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2087 return SDOperand(E, 0);
2088 N = new UnarySDNode(Opcode, VTs, Operand);
2089 CSEMap.InsertNode(N, IP);
2090 } else {
2091 N = new UnarySDNode(Opcode, VTs, Operand);
2092 }
2093 AllNodes.push_back(N);
2094 return SDOperand(N, 0);
2095}
2096
2097
2098
Duncan Sands92c43912008-06-06 12:08:01 +00002099SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002100 SDOperand N1, SDOperand N2) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002101 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2102 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002104 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 case ISD::TokenFactor:
2106 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2107 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002108 // Fold trivial token factors.
2109 if (N1.getOpcode() == ISD::EntryToken) return N2;
2110 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002111 break;
2112 case ISD::AND:
Duncan Sands92c43912008-06-06 12:08:01 +00002113 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002114 N1.getValueType() == VT && "Binary operator types must match!");
2115 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2116 // worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002117 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002118 return N2;
Chris Lattner8aa8a5e2008-01-26 01:05:42 +00002119 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2120 return N1;
Chris Lattnercc126e32008-01-22 19:09:33 +00002121 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 case ISD::OR:
2123 case ISD::XOR:
Dan Gohman3f76aed2008-06-02 22:27:05 +00002124 case ISD::ADD:
2125 case ISD::SUB:
Duncan Sands92c43912008-06-06 12:08:01 +00002126 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002127 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman3f76aed2008-06-02 22:27:05 +00002128 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2129 // it's worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002130 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002131 return N1;
2132 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002133 case ISD::UDIV:
2134 case ISD::UREM:
2135 case ISD::MULHU:
2136 case ISD::MULHS:
Duncan Sands92c43912008-06-06 12:08:01 +00002137 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002138 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002139 case ISD::MUL:
2140 case ISD::SDIV:
2141 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002142 case ISD::FADD:
2143 case ISD::FSUB:
2144 case ISD::FMUL:
2145 case ISD::FDIV:
2146 case ISD::FREM:
2147 assert(N1.getValueType() == N2.getValueType() &&
2148 N1.getValueType() == VT && "Binary operator types must match!");
2149 break;
2150 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2151 assert(N1.getValueType() == VT &&
Duncan Sands92c43912008-06-06 12:08:01 +00002152 N1.getValueType().isFloatingPoint() &&
2153 N2.getValueType().isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 "Invalid FCOPYSIGN!");
2155 break;
2156 case ISD::SHL:
2157 case ISD::SRA:
2158 case ISD::SRL:
2159 case ISD::ROTL:
2160 case ISD::ROTR:
2161 assert(VT == N1.getValueType() &&
2162 "Shift operators return type must be the same as their first arg");
Duncan Sands92c43912008-06-06 12:08:01 +00002163 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002164 VT != MVT::i1 && "Shifts only work on integers");
2165 break;
2166 case ISD::FP_ROUND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00002167 MVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002168 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands92c43912008-06-06 12:08:01 +00002169 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002171 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002172 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002173 break;
2174 }
Chris Lattner5872a362008-01-17 07:00:52 +00002175 case ISD::FP_ROUND:
Duncan Sands92c43912008-06-06 12:08:01 +00002176 assert(VT.isFloatingPoint() &&
2177 N1.getValueType().isFloatingPoint() &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00002178 VT.bitsLE(N1.getValueType()) &&
Chris Lattner5872a362008-01-17 07:00:52 +00002179 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002180 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00002181 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00002183 case ISD::AssertZext: {
Duncan Sands92c43912008-06-06 12:08:01 +00002184 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattnercc126e32008-01-22 19:09:33 +00002185 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002186 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002187 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002188 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands539510b2008-02-10 10:08:52 +00002189 if (VT == EVT) return N1; // noop assertion.
Chris Lattnercc126e32008-01-22 19:09:33 +00002190 break;
2191 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002192 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands92c43912008-06-06 12:08:01 +00002193 MVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002195 assert(VT.isInteger() && EVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002196 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002197 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002198 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002199
Chris Lattnercc126e32008-01-22 19:09:33 +00002200 if (N1C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002201 APInt Val = N1C->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002202 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman161652c2008-02-29 01:47:35 +00002203 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng7faa1d72008-03-06 08:20:51 +00002204 Val = Val.ashr(Val.getBitWidth()-FromBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002205 return getConstant(Val, VT);
2206 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002207 break;
2208 }
2209 case ISD::EXTRACT_VECTOR_ELT:
2210 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2211
Chris Lattner9f0705c2008-03-08 23:43:36 +00002212 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2213 if (N1.getOpcode() == ISD::UNDEF)
2214 return getNode(ISD::UNDEF, VT);
2215
Chris Lattnercc126e32008-01-22 19:09:33 +00002216 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2217 // expanding copies of large vectors from registers.
2218 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2219 N1.getNumOperands() > 0) {
2220 unsigned Factor =
Duncan Sands92c43912008-06-06 12:08:01 +00002221 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattnercc126e32008-01-22 19:09:33 +00002222 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2223 N1.getOperand(N2C->getValue() / Factor),
2224 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2225 }
2226
2227 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2228 // expanding large vector constants.
2229 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2230 return N1.getOperand(N2C->getValue());
Chris Lattner9f0705c2008-03-08 23:43:36 +00002231
Chris Lattnercc126e32008-01-22 19:09:33 +00002232 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2233 // operations are lowered to scalars.
2234 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2235 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2236 if (IEC == N2C)
2237 return N1.getOperand(1);
2238 else
2239 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2240 }
2241 break;
2242 case ISD::EXTRACT_ELEMENT:
2243 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands1568af82008-06-25 20:24:48 +00002244 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2245 (N1.getValueType().isInteger() == VT.isInteger()) &&
2246 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sandsc4d85172008-03-12 20:30:08 +00002247
Chris Lattnercc126e32008-01-22 19:09:33 +00002248 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2249 // 64-bit integers into 32-bit parts. Instead of building the extract of
2250 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2251 if (N1.getOpcode() == ISD::BUILD_PAIR)
2252 return N1.getOperand(N2C->getValue());
Duncan Sandsc4d85172008-03-12 20:30:08 +00002253
Chris Lattnercc126e32008-01-22 19:09:33 +00002254 // EXTRACT_ELEMENT of a constant int is also very common.
2255 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002256 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman452b4ce2008-03-24 16:38:05 +00002257 unsigned Shift = ElementSize * N2C->getValue();
2258 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2259 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattnercc126e32008-01-22 19:09:33 +00002260 }
2261 break;
Duncan Sandsbd13a812008-02-20 17:38:09 +00002262 case ISD::EXTRACT_SUBVECTOR:
2263 if (N1.getValueType() == VT) // Trivial extraction.
2264 return N1;
2265 break;
Chris Lattnercc126e32008-01-22 19:09:33 +00002266 }
2267
2268 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269 if (N2C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002270 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002271 switch (Opcode) {
2272 case ISD::ADD: return getConstant(C1 + C2, VT);
2273 case ISD::SUB: return getConstant(C1 - C2, VT);
2274 case ISD::MUL: return getConstant(C1 * C2, VT);
2275 case ISD::UDIV:
Dan Gohman161652c2008-02-29 01:47:35 +00002276 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002277 break;
2278 case ISD::UREM :
Dan Gohman161652c2008-02-29 01:47:35 +00002279 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280 break;
2281 case ISD::SDIV :
Dan Gohman161652c2008-02-29 01:47:35 +00002282 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 break;
2284 case ISD::SREM :
Dan Gohman161652c2008-02-29 01:47:35 +00002285 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002286 break;
2287 case ISD::AND : return getConstant(C1 & C2, VT);
2288 case ISD::OR : return getConstant(C1 | C2, VT);
2289 case ISD::XOR : return getConstant(C1 ^ C2, VT);
2290 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00002291 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2292 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2293 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2294 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002295 default: break;
2296 }
2297 } else { // Cannonicalize constant to RHS if commutative
2298 if (isCommutativeBinOp(Opcode)) {
2299 std::swap(N1C, N2C);
2300 std::swap(N1, N2);
2301 }
2302 }
2303 }
2304
Chris Lattnercc126e32008-01-22 19:09:33 +00002305 // Constant fold FP operations.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2307 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
2308 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002309 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2310 // Cannonicalize constant to RHS if commutative
2311 std::swap(N1CFP, N2CFP);
2312 std::swap(N1, N2);
2313 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002314 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2315 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002316 switch (Opcode) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002317 case ISD::FADD:
2318 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002319 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002320 return getConstantFP(V1, VT);
2321 break;
2322 case ISD::FSUB:
2323 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2324 if (s!=APFloat::opInvalidOp)
2325 return getConstantFP(V1, VT);
2326 break;
2327 case ISD::FMUL:
2328 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2329 if (s!=APFloat::opInvalidOp)
2330 return getConstantFP(V1, VT);
2331 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002332 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002333 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2334 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2335 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002336 break;
2337 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002338 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2339 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2340 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002341 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002342 case ISD::FCOPYSIGN:
2343 V1.copySign(V2);
2344 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345 default: break;
2346 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 }
2348 }
2349
2350 // Canonicalize an UNDEF to the RHS, even over a constant.
2351 if (N1.getOpcode() == ISD::UNDEF) {
2352 if (isCommutativeBinOp(Opcode)) {
2353 std::swap(N1, N2);
2354 } else {
2355 switch (Opcode) {
2356 case ISD::FP_ROUND_INREG:
2357 case ISD::SIGN_EXTEND_INREG:
2358 case ISD::SUB:
2359 case ISD::FSUB:
2360 case ISD::FDIV:
2361 case ISD::FREM:
2362 case ISD::SRA:
2363 return N1; // fold op(undef, arg2) -> undef
2364 case ISD::UDIV:
2365 case ISD::SDIV:
2366 case ISD::UREM:
2367 case ISD::SREM:
2368 case ISD::SRL:
2369 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002370 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002371 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2372 // For vectors, we can't easily build an all zero vector, just return
2373 // the LHS.
2374 return N2;
2375 }
2376 }
2377 }
2378
2379 // Fold a bunch of operators when the RHS is undef.
2380 if (N2.getOpcode() == ISD::UNDEF) {
2381 switch (Opcode) {
Evan Cheng5d00cb42008-03-25 20:08:07 +00002382 case ISD::XOR:
2383 if (N1.getOpcode() == ISD::UNDEF)
2384 // Handle undef ^ undef -> 0 special case. This is a common
2385 // idiom (misuse).
2386 return getConstant(0, VT);
2387 // fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002388 case ISD::ADD:
2389 case ISD::ADDC:
2390 case ISD::ADDE:
2391 case ISD::SUB:
2392 case ISD::FADD:
2393 case ISD::FSUB:
2394 case ISD::FMUL:
2395 case ISD::FDIV:
2396 case ISD::FREM:
2397 case ISD::UDIV:
2398 case ISD::SDIV:
2399 case ISD::UREM:
2400 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002401 return N2; // fold op(arg1, undef) -> undef
2402 case ISD::MUL:
2403 case ISD::AND:
2404 case ISD::SRL:
2405 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002406 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2408 // For vectors, we can't easily build an all zero vector, just return
2409 // the LHS.
2410 return N1;
2411 case ISD::OR:
Duncan Sands92c43912008-06-06 12:08:01 +00002412 if (!VT.isVector())
2413 return getConstant(VT.getIntegerVTBitMask(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002414 // For vectors, we can't easily build an all one vector, just return
2415 // the LHS.
2416 return N1;
2417 case ISD::SRA:
2418 return N1;
2419 }
2420 }
2421
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 // Memoize this node if possible.
2423 SDNode *N;
2424 SDVTList VTs = getVTList(VT);
2425 if (VT != MVT::Flag) {
2426 SDOperand Ops[] = { N1, N2 };
2427 FoldingSetNodeID ID;
2428 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2429 void *IP = 0;
2430 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2431 return SDOperand(E, 0);
2432 N = new BinarySDNode(Opcode, VTs, N1, N2);
2433 CSEMap.InsertNode(N, IP);
2434 } else {
2435 N = new BinarySDNode(Opcode, VTs, N1, N2);
2436 }
2437
2438 AllNodes.push_back(N);
2439 return SDOperand(N, 0);
2440}
2441
Duncan Sands92c43912008-06-06 12:08:01 +00002442SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 SDOperand N1, SDOperand N2, SDOperand N3) {
2444 // Perform various simplifications.
2445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2446 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2447 switch (Opcode) {
2448 case ISD::SETCC: {
2449 // Use FoldSetCC to simplify SETCC's.
2450 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2451 if (Simp.Val) return Simp;
2452 break;
2453 }
2454 case ISD::SELECT:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002455 if (N1C) {
2456 if (N1C->getValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 return N2; // select true, X, Y -> X
2458 else
2459 return N3; // select false, X, Y -> Y
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002460 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002461
2462 if (N2 == N3) return N2; // select C, X, X -> X
2463 break;
2464 case ISD::BRCOND:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002465 if (N2C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466 if (N2C->getValue()) // Unconditional branch
2467 return getNode(ISD::BR, MVT::Other, N1, N3);
2468 else
2469 return N1; // Never-taken branch
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002470 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002471 break;
2472 case ISD::VECTOR_SHUFFLE:
2473 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands92c43912008-06-06 12:08:01 +00002474 VT.isVector() && N3.getValueType().isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002475 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands92c43912008-06-06 12:08:01 +00002476 VT.getVectorNumElements() == N3.getNumOperands() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 "Illegal VECTOR_SHUFFLE node!");
2478 break;
2479 case ISD::BIT_CONVERT:
2480 // Fold bit_convert nodes from a type to themselves.
2481 if (N1.getValueType() == VT)
2482 return N1;
2483 break;
2484 }
2485
2486 // Memoize node if it doesn't produce a flag.
2487 SDNode *N;
2488 SDVTList VTs = getVTList(VT);
2489 if (VT != MVT::Flag) {
2490 SDOperand Ops[] = { N1, N2, N3 };
2491 FoldingSetNodeID ID;
2492 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2493 void *IP = 0;
2494 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2495 return SDOperand(E, 0);
2496 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2497 CSEMap.InsertNode(N, IP);
2498 } else {
2499 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2500 }
2501 AllNodes.push_back(N);
2502 return SDOperand(N, 0);
2503}
2504
Duncan Sands92c43912008-06-06 12:08:01 +00002505SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002506 SDOperand N1, SDOperand N2, SDOperand N3,
2507 SDOperand N4) {
2508 SDOperand Ops[] = { N1, N2, N3, N4 };
2509 return getNode(Opcode, VT, Ops, 4);
2510}
2511
Duncan Sands92c43912008-06-06 12:08:01 +00002512SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513 SDOperand N1, SDOperand N2, SDOperand N3,
2514 SDOperand N4, SDOperand N5) {
2515 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2516 return getNode(Opcode, VT, Ops, 5);
2517}
2518
Dan Gohmane8b391e2008-04-12 04:36:06 +00002519/// getMemsetValue - Vectorized representation of the memset value
2520/// operand.
Duncan Sands92c43912008-06-06 12:08:01 +00002521static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2522 unsigned NumBits = VT.isVector() ?
2523 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohmane8b391e2008-04-12 04:36:06 +00002524 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Cheng8c590372008-05-15 08:39:06 +00002525 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002526 unsigned Shift = 8;
Evan Cheng8c590372008-05-15 08:39:06 +00002527 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002528 Val = (Val << Shift) | Val;
2529 Shift <<= 1;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002530 }
Duncan Sands92c43912008-06-06 12:08:01 +00002531 if (VT.isInteger())
Evan Cheng8c590372008-05-15 08:39:06 +00002532 return DAG.getConstant(Val, VT);
2533 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002534 }
Evan Cheng8c590372008-05-15 08:39:06 +00002535
2536 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2537 unsigned Shift = 8;
2538 for (unsigned i = NumBits; i > 8; i >>= 1) {
2539 Value = DAG.getNode(ISD::OR, VT,
2540 DAG.getNode(ISD::SHL, VT, Value,
2541 DAG.getConstant(Shift, MVT::i8)), Value);
2542 Shift <<= 1;
2543 }
2544
2545 return Value;
Rafael Espindola80825902007-10-19 10:41:11 +00002546}
2547
Dan Gohmane8b391e2008-04-12 04:36:06 +00002548/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2549/// used when a memcpy is turned into a memset when the source is a constant
2550/// string ptr.
Duncan Sands92c43912008-06-06 12:08:01 +00002551static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohmane8b391e2008-04-12 04:36:06 +00002552 const TargetLowering &TLI,
2553 std::string &Str, unsigned Offset) {
Evan Cheng833501d2008-06-30 07:31:25 +00002554 // Handle vector with all elements zero.
2555 if (Str.empty()) {
2556 if (VT.isInteger())
2557 return DAG.getConstant(0, VT);
2558 unsigned NumElts = VT.getVectorNumElements();
2559 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2560 return DAG.getNode(ISD::BIT_CONVERT, VT,
2561 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2562 }
2563
Duncan Sands92c43912008-06-06 12:08:01 +00002564 assert(!VT.isVector() && "Can't handle vector type here!");
2565 unsigned NumBits = VT.getSizeInBits();
Evan Cheng8c590372008-05-15 08:39:06 +00002566 unsigned MSB = NumBits / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002567 uint64_t Val = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002568 if (TLI.isLittleEndian())
2569 Offset = Offset + MSB - 1;
2570 for (unsigned i = 0; i != MSB; ++i) {
2571 Val = (Val << 8) | (unsigned char)Str[Offset];
2572 Offset += TLI.isLittleEndian() ? -1 : 1;
2573 }
2574 return DAG.getConstant(Val, VT);
Rafael Espindola80825902007-10-19 10:41:11 +00002575}
2576
Dan Gohmane8b391e2008-04-12 04:36:06 +00002577/// getMemBasePlusOffset - Returns base and offset node for the
Evan Cheng8c590372008-05-15 08:39:06 +00002578///
Dan Gohmane8b391e2008-04-12 04:36:06 +00002579static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2580 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002581 MVT VT = Base.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00002582 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2583}
2584
Evan Cheng8c590372008-05-15 08:39:06 +00002585/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2586///
Evan Cheng833501d2008-06-30 07:31:25 +00002587static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Cheng8c590372008-05-15 08:39:06 +00002588 unsigned SrcDelta = 0;
2589 GlobalAddressSDNode *G = NULL;
2590 if (Src.getOpcode() == ISD::GlobalAddress)
2591 G = cast<GlobalAddressSDNode>(Src);
2592 else if (Src.getOpcode() == ISD::ADD &&
2593 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2594 Src.getOperand(1).getOpcode() == ISD::Constant) {
2595 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2596 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2597 }
2598 if (!G)
2599 return false;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002600
Evan Cheng8c590372008-05-15 08:39:06 +00002601 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng833501d2008-06-30 07:31:25 +00002602 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2603 return true;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002604
Evan Cheng8c590372008-05-15 08:39:06 +00002605 return false;
2606}
Dan Gohmane8b391e2008-04-12 04:36:06 +00002607
Evan Cheng8c590372008-05-15 08:39:06 +00002608/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2609/// to replace the memset / memcpy is below the threshold. It also returns the
2610/// types of the sequence of memory ops to perform memset / memcpy.
2611static
Duncan Sands92c43912008-06-06 12:08:01 +00002612bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Cheng8c590372008-05-15 08:39:06 +00002613 SDOperand Dst, SDOperand Src,
2614 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng833501d2008-06-30 07:31:25 +00002615 std::string &Str, bool &isSrcStr,
Evan Cheng8c590372008-05-15 08:39:06 +00002616 SelectionDAG &DAG,
2617 const TargetLowering &TLI) {
Evan Cheng833501d2008-06-30 07:31:25 +00002618 isSrcStr = isMemSrcFromString(Src, Str);
Evan Cheng8c590372008-05-15 08:39:06 +00002619 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng833501d2008-06-30 07:31:25 +00002620 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands92c43912008-06-06 12:08:01 +00002621 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Cheng8c590372008-05-15 08:39:06 +00002622 if (VT != MVT::iAny) {
2623 unsigned NewAlign = (unsigned)
Duncan Sands92c43912008-06-06 12:08:01 +00002624 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng8c590372008-05-15 08:39:06 +00002625 // If source is a string constant, this will require an unaligned load.
2626 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2627 if (Dst.getOpcode() != ISD::FrameIndex) {
2628 // Can't change destination alignment. It requires a unaligned store.
2629 if (AllowUnalign)
2630 VT = MVT::iAny;
2631 } else {
2632 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2633 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2634 if (MFI->isFixedObjectIndex(FI)) {
2635 // Can't change destination alignment. It requires a unaligned store.
2636 if (AllowUnalign)
2637 VT = MVT::iAny;
2638 } else {
Evan Cheng36cd8862008-06-04 23:37:54 +00002639 // Give the stack frame object a larger alignment if needed.
2640 if (MFI->getObjectAlignment(FI) < NewAlign)
2641 MFI->setObjectAlignment(FI, NewAlign);
Evan Cheng8c590372008-05-15 08:39:06 +00002642 Align = NewAlign;
2643 }
2644 }
2645 }
2646 }
2647
2648 if (VT == MVT::iAny) {
2649 if (AllowUnalign) {
2650 VT = MVT::i64;
2651 } else {
2652 switch (Align & 7) {
2653 case 0: VT = MVT::i64; break;
2654 case 4: VT = MVT::i32; break;
2655 case 2: VT = MVT::i16; break;
2656 default: VT = MVT::i8; break;
2657 }
2658 }
2659
Duncan Sands92c43912008-06-06 12:08:01 +00002660 MVT LVT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00002661 while (!TLI.isTypeLegal(LVT))
Duncan Sands92c43912008-06-06 12:08:01 +00002662 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2663 assert(LVT.isInteger());
Evan Cheng8c590372008-05-15 08:39:06 +00002664
Duncan Sandsec142ee2008-06-08 20:54:56 +00002665 if (VT.bitsGT(LVT))
Evan Cheng8c590372008-05-15 08:39:06 +00002666 VT = LVT;
2667 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002668
2669 unsigned NumMemOps = 0;
2670 while (Size != 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00002671 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002672 while (VTSize > Size) {
Evan Cheng8c590372008-05-15 08:39:06 +00002673 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands92c43912008-06-06 12:08:01 +00002674 if (VT.isVector()) {
Evan Cheng8c590372008-05-15 08:39:06 +00002675 VT = MVT::i64;
2676 while (!TLI.isTypeLegal(VT))
Duncan Sands92c43912008-06-06 12:08:01 +00002677 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2678 VTSize = VT.getSizeInBits() / 8;
Evan Cheng8c590372008-05-15 08:39:06 +00002679 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00002680 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Cheng8c590372008-05-15 08:39:06 +00002681 VTSize >>= 1;
2682 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002683 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002684
2685 if (++NumMemOps > Limit)
2686 return false;
2687 MemOps.push_back(VT);
2688 Size -= VTSize;
2689 }
2690
2691 return true;
2692}
2693
2694static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2695 SDOperand Chain, SDOperand Dst,
2696 SDOperand Src, uint64_t Size,
Evan Cheng8c590372008-05-15 08:39:06 +00002697 unsigned Align, bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00002698 const Value *DstSV, uint64_t DstSVOff,
2699 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00002700 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2701
Dan Gohman42d311c2008-05-29 19:42:22 +00002702 // Expand memcpy to a series of load and store ops if the size operand falls
2703 // below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00002704 std::vector<MVT> MemOps;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002705 uint64_t Limit = -1;
2706 if (!AlwaysInline)
2707 Limit = TLI.getMaxStoresPerMemcpy();
Evan Cheng8c590372008-05-15 08:39:06 +00002708 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng833501d2008-06-30 07:31:25 +00002709 std::string Str;
2710 bool CopyFromStr;
Evan Cheng8c590372008-05-15 08:39:06 +00002711 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng833501d2008-06-30 07:31:25 +00002712 Str, CopyFromStr, DAG, TLI))
Dan Gohmane8b391e2008-04-12 04:36:06 +00002713 return SDOperand();
2714
Dan Gohmane8b391e2008-04-12 04:36:06 +00002715
Evan Cheng833501d2008-06-30 07:31:25 +00002716 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng8c590372008-05-15 08:39:06 +00002717 SmallVector<SDOperand, 8> OutChains;
2718 unsigned NumMemOps = MemOps.size();
Evan Cheng833501d2008-06-30 07:31:25 +00002719 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002720 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00002721 MVT VT = MemOps[i];
2722 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002723 SDOperand Value, Store;
2724
Evan Cheng833501d2008-06-30 07:31:25 +00002725 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Cheng8c590372008-05-15 08:39:06 +00002726 // It's unlikely a store of a vector immediate can be done in a single
2727 // instruction. It would require a load from a constantpool first.
Evan Cheng833501d2008-06-30 07:31:25 +00002728 // We also handle store a vector with all zero's.
2729 // FIXME: Handle other cases where store of vector immediate is done in
2730 // a single instruction.
Dan Gohmane8b391e2008-04-12 04:36:06 +00002731 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Cheng8c590372008-05-15 08:39:06 +00002732 Store = DAG.getStore(Chain, Value,
2733 getMemBasePlusOffset(Dst, DstOff, DAG),
2734 DstSV, DstSVOff + DstOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002735 } else {
2736 Value = DAG.getLoad(VT, Chain,
2737 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman65118f42008-04-28 17:15:20 +00002738 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Cheng8c590372008-05-15 08:39:06 +00002739 Store = DAG.getStore(Chain, Value,
2740 getMemBasePlusOffset(Dst, DstOff, DAG),
2741 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002742 }
2743 OutChains.push_back(Store);
2744 SrcOff += VTSize;
2745 DstOff += VTSize;
2746 }
2747
2748 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2749 &OutChains[0], OutChains.size());
2750}
2751
Dan Gohman42d311c2008-05-29 19:42:22 +00002752static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2753 SDOperand Chain, SDOperand Dst,
2754 SDOperand Src, uint64_t Size,
2755 unsigned Align, bool AlwaysInline,
2756 const Value *DstSV, uint64_t DstSVOff,
2757 const Value *SrcSV, uint64_t SrcSVOff){
2758 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2759
2760 // Expand memmove to a series of load and store ops if the size operand falls
2761 // below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00002762 std::vector<MVT> MemOps;
Dan Gohman42d311c2008-05-29 19:42:22 +00002763 uint64_t Limit = -1;
2764 if (!AlwaysInline)
2765 Limit = TLI.getMaxStoresPerMemmove();
2766 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng833501d2008-06-30 07:31:25 +00002767 std::string Str;
2768 bool CopyFromStr;
Dan Gohman42d311c2008-05-29 19:42:22 +00002769 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng833501d2008-06-30 07:31:25 +00002770 Str, CopyFromStr, DAG, TLI))
Dan Gohman42d311c2008-05-29 19:42:22 +00002771 return SDOperand();
2772
Dan Gohman42d311c2008-05-29 19:42:22 +00002773 uint64_t SrcOff = 0, DstOff = 0;
2774
2775 SmallVector<SDOperand, 8> LoadValues;
2776 SmallVector<SDOperand, 8> LoadChains;
2777 SmallVector<SDOperand, 8> OutChains;
2778 unsigned NumMemOps = MemOps.size();
2779 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00002780 MVT VT = MemOps[i];
2781 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman42d311c2008-05-29 19:42:22 +00002782 SDOperand Value, Store;
2783
2784 Value = DAG.getLoad(VT, Chain,
2785 getMemBasePlusOffset(Src, SrcOff, DAG),
2786 SrcSV, SrcSVOff + SrcOff, false, Align);
2787 LoadValues.push_back(Value);
2788 LoadChains.push_back(Value.getValue(1));
2789 SrcOff += VTSize;
2790 }
2791 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2792 &LoadChains[0], LoadChains.size());
2793 OutChains.clear();
2794 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00002795 MVT VT = MemOps[i];
2796 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman42d311c2008-05-29 19:42:22 +00002797 SDOperand Value, Store;
2798
2799 Store = DAG.getStore(Chain, LoadValues[i],
2800 getMemBasePlusOffset(Dst, DstOff, DAG),
2801 DstSV, DstSVOff + DstOff, false, DstAlign);
2802 OutChains.push_back(Store);
2803 DstOff += VTSize;
2804 }
2805
2806 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2807 &OutChains[0], OutChains.size());
2808}
2809
Dan Gohmane8b391e2008-04-12 04:36:06 +00002810static SDOperand getMemsetStores(SelectionDAG &DAG,
2811 SDOperand Chain, SDOperand Dst,
2812 SDOperand Src, uint64_t Size,
2813 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002814 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002815 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2816
2817 // Expand memset to a series of load/store ops if the size operand
2818 // falls below a certain threshold.
Duncan Sands92c43912008-06-06 12:08:01 +00002819 std::vector<MVT> MemOps;
Evan Cheng833501d2008-06-30 07:31:25 +00002820 std::string Str;
2821 bool CopyFromStr;
Evan Cheng8c590372008-05-15 08:39:06 +00002822 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng833501d2008-06-30 07:31:25 +00002823 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohmane8b391e2008-04-12 04:36:06 +00002824 return SDOperand();
2825
2826 SmallVector<SDOperand, 8> OutChains;
Dan Gohman65118f42008-04-28 17:15:20 +00002827 uint64_t DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002828
2829 unsigned NumMemOps = MemOps.size();
2830 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +00002831 MVT VT = MemOps[i];
2832 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002833 SDOperand Value = getMemsetValue(Src, VT, DAG);
2834 SDOperand Store = DAG.getStore(Chain, Value,
2835 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman65118f42008-04-28 17:15:20 +00002836 DstSV, DstSVOff + DstOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002837 OutChains.push_back(Store);
2838 DstOff += VTSize;
2839 }
2840
2841 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2842 &OutChains[0], OutChains.size());
2843}
2844
2845SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola80825902007-10-19 10:41:11 +00002846 SDOperand Src, SDOperand Size,
Dan Gohmane8b391e2008-04-12 04:36:06 +00002847 unsigned Align, bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00002848 const Value *DstSV, uint64_t DstSVOff,
2849 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002850
2851 // Check to see if we should lower the memcpy to loads and stores first.
2852 // For cases within the target-specified limits, this is the best choice.
2853 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2854 if (ConstantSize) {
2855 // Memcpy with size zero? Just return the original chain.
2856 if (ConstantSize->isNullValue())
2857 return Chain;
2858
2859 SDOperand Result =
2860 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman65118f42008-04-28 17:15:20 +00002861 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002862 if (Result.Val)
2863 return Result;
2864 }
2865
2866 // Then check to see if we should lower the memcpy with target-specific
2867 // code. If the target chooses to do this, this is the next best.
2868 SDOperand Result =
2869 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2870 AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00002871 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002872 if (Result.Val)
2873 return Result;
2874
2875 // If we really need inline code and the target declined to provide it,
2876 // use a (potentially long) sequence of loads and stores.
2877 if (AlwaysInline) {
2878 assert(ConstantSize && "AlwaysInline requires a constant size!");
2879 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2880 ConstantSize->getValue(), Align, true,
Dan Gohman65118f42008-04-28 17:15:20 +00002881 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002882 }
2883
2884 // Emit a library call.
2885 TargetLowering::ArgListTy Args;
2886 TargetLowering::ArgListEntry Entry;
2887 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2888 Entry.Node = Dst; Args.push_back(Entry);
2889 Entry.Node = Src; Args.push_back(Entry);
2890 Entry.Node = Size; Args.push_back(Entry);
2891 std::pair<SDOperand,SDOperand> CallResult =
2892 TLI.LowerCallTo(Chain, Type::VoidTy,
2893 false, false, false, CallingConv::C, false,
2894 getExternalSymbol("memcpy", TLI.getPointerTy()),
2895 Args, *this);
2896 return CallResult.second;
2897}
2898
2899SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2900 SDOperand Src, SDOperand Size,
2901 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002902 const Value *DstSV, uint64_t DstSVOff,
2903 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002904
Dan Gohman42d311c2008-05-29 19:42:22 +00002905 // Check to see if we should lower the memmove to loads and stores first.
2906 // For cases within the target-specified limits, this is the best choice.
2907 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2908 if (ConstantSize) {
2909 // Memmove with size zero? Just return the original chain.
2910 if (ConstantSize->isNullValue())
2911 return Chain;
2912
2913 SDOperand Result =
2914 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2915 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2916 if (Result.Val)
2917 return Result;
2918 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002919
2920 // Then check to see if we should lower the memmove with target-specific
2921 // code. If the target chooses to do this, this is the next best.
2922 SDOperand Result =
2923 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002924 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002925 if (Result.Val)
2926 return Result;
2927
2928 // Emit a library call.
2929 TargetLowering::ArgListTy Args;
2930 TargetLowering::ArgListEntry Entry;
2931 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2932 Entry.Node = Dst; Args.push_back(Entry);
2933 Entry.Node = Src; Args.push_back(Entry);
2934 Entry.Node = Size; Args.push_back(Entry);
2935 std::pair<SDOperand,SDOperand> CallResult =
2936 TLI.LowerCallTo(Chain, Type::VoidTy,
2937 false, false, false, CallingConv::C, false,
2938 getExternalSymbol("memmove", TLI.getPointerTy()),
2939 Args, *this);
2940 return CallResult.second;
2941}
2942
2943SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2944 SDOperand Src, SDOperand Size,
2945 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002946 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002947
2948 // Check to see if we should lower the memset to stores first.
2949 // For cases within the target-specified limits, this is the best choice.
2950 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2951 if (ConstantSize) {
2952 // Memset with size zero? Just return the original chain.
2953 if (ConstantSize->isNullValue())
2954 return Chain;
2955
2956 SDOperand Result =
2957 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002958 DstSV, DstSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002959 if (Result.Val)
2960 return Result;
2961 }
2962
2963 // Then check to see if we should lower the memset with target-specific
2964 // code. If the target chooses to do this, this is the next best.
2965 SDOperand Result =
2966 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002967 DstSV, DstSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002968 if (Result.Val)
2969 return Result;
2970
2971 // Emit a library call.
2972 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2973 TargetLowering::ArgListTy Args;
2974 TargetLowering::ArgListEntry Entry;
2975 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2976 Args.push_back(Entry);
2977 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sandsec142ee2008-06-08 20:54:56 +00002978 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohmane8b391e2008-04-12 04:36:06 +00002979 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2980 else
2981 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2982 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2983 Args.push_back(Entry);
2984 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2985 Args.push_back(Entry);
2986 std::pair<SDOperand,SDOperand> CallResult =
2987 TLI.LowerCallTo(Chain, Type::VoidTy,
2988 false, false, false, CallingConv::C, false,
2989 getExternalSymbol("memset", TLI.getPointerTy()),
2990 Args, *this);
2991 return CallResult.second;
Rafael Espindola80825902007-10-19 10:41:11 +00002992}
2993
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002994SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002995 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanc70fa752008-06-25 16:07:49 +00002996 SDOperand Swp, const Value* PtrVal,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00002997 unsigned Alignment) {
2998 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002999 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3000 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003001 FoldingSetNodeID ID;
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00003002 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003003 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003004 void* IP = 0;
3005 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3006 return SDOperand(E, 0);
Dan Gohmanc70fa752008-06-25 16:07:49 +00003007 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00003008 PtrVal, Alignment);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003009 CSEMap.InsertNode(N, IP);
3010 AllNodes.push_back(N);
3011 return SDOperand(N, 0);
3012}
3013
3014SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00003015 SDOperand Ptr, SDOperand Val,
Dan Gohmanc70fa752008-06-25 16:07:49 +00003016 const Value* PtrVal,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00003017 unsigned Alignment) {
3018 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang078a62d2008-05-05 19:05:59 +00003019 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3020 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharthaf02d592008-06-14 05:48:15 +00003021 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang078a62d2008-05-05 19:05:59 +00003022 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3023 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003024 && "Invalid Atomic Op");
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00003025 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003026 FoldingSetNodeID ID;
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00003027 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003028 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003029 void* IP = 0;
3030 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3031 return SDOperand(E, 0);
Dan Gohmanc70fa752008-06-25 16:07:49 +00003032 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00003033 PtrVal, Alignment);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003034 CSEMap.InsertNode(N, IP);
3035 AllNodes.push_back(N);
3036 return SDOperand(N, 0);
3037}
3038
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003039SDOperand
Duncan Sandsb2589712008-03-28 09:45:24 +00003040SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands92c43912008-06-06 12:08:01 +00003041 MVT VT, SDOperand Chain,
Duncan Sandsb2589712008-03-28 09:45:24 +00003042 SDOperand Ptr, SDOperand Offset,
Duncan Sands92c43912008-06-06 12:08:01 +00003043 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandsb2589712008-03-28 09:45:24 +00003044 bool isVolatile, unsigned Alignment) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3046 const Type *Ty = 0;
3047 if (VT != MVT::iPTR) {
Duncan Sands92c43912008-06-06 12:08:01 +00003048 Ty = VT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049 } else if (SV) {
3050 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3051 assert(PT && "Value for load must be a pointer");
3052 Ty = PT->getElementType();
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003053 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054 assert(Ty && "Could not get type information for load");
3055 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3056 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003058 if (VT == EVT) {
3059 ExtType = ISD::NON_EXTLOAD;
3060 } else if (ExtType == ISD::NON_EXTLOAD) {
3061 assert(VT == EVT && "Non-extending load from different memory type!");
3062 } else {
3063 // Extending load.
Duncan Sands92c43912008-06-06 12:08:01 +00003064 if (VT.isVector())
3065 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003066 else
Duncan Sandsec142ee2008-06-08 20:54:56 +00003067 assert(EVT.bitsLT(VT) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003068 "Should only be an extending load, not truncating!");
Duncan Sands92c43912008-06-06 12:08:01 +00003069 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003070 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands92c43912008-06-06 12:08:01 +00003071 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003072 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 }
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003074
3075 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sandsf6890712008-05-27 11:50:51 +00003076 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003077 "Unindexed load with an offset!");
3078
3079 SDVTList VTs = Indexed ?
3080 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3081 SDOperand Ops[] = { Chain, Ptr, Offset };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003082 FoldingSetNodeID ID;
3083 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003084 ID.AddInteger(AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085 ID.AddInteger(ExtType);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003086 ID.AddInteger(EVT.getRawBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 ID.AddInteger(Alignment);
3088 ID.AddInteger(isVolatile);
3089 void *IP = 0;
3090 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3091 return SDOperand(E, 0);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003092 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3093 Alignment, isVolatile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003094 CSEMap.InsertNode(N, IP);
3095 AllNodes.push_back(N);
3096 return SDOperand(N, 0);
3097}
3098
Duncan Sands92c43912008-06-06 12:08:01 +00003099SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003100 SDOperand Chain, SDOperand Ptr,
3101 const Value *SV, int SVOffset,
3102 bool isVolatile, unsigned Alignment) {
3103 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandsb2589712008-03-28 09:45:24 +00003104 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3105 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003106}
3107
Duncan Sands92c43912008-06-06 12:08:01 +00003108SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003109 SDOperand Chain, SDOperand Ptr,
3110 const Value *SV,
Duncan Sands92c43912008-06-06 12:08:01 +00003111 int SVOffset, MVT EVT,
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003112 bool isVolatile, unsigned Alignment) {
3113 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandsb2589712008-03-28 09:45:24 +00003114 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3115 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003116}
3117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118SDOperand
3119SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3120 SDOperand Offset, ISD::MemIndexedMode AM) {
3121 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3122 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3123 "Load is already a indexed load!");
Duncan Sandsb2589712008-03-28 09:45:24 +00003124 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3125 LD->getChain(), Base, Offset, LD->getSrcValue(),
3126 LD->getSrcValueOffset(), LD->getMemoryVT(),
3127 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128}
3129
3130SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
3131 SDOperand Ptr, const Value *SV, int SVOffset,
3132 bool isVolatile, unsigned Alignment) {
Duncan Sands92c43912008-06-06 12:08:01 +00003133 MVT VT = Val.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003134
3135 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3136 const Type *Ty = 0;
3137 if (VT != MVT::iPTR) {
Duncan Sands92c43912008-06-06 12:08:01 +00003138 Ty = VT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003139 } else if (SV) {
3140 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3141 assert(PT && "Value for store must be a pointer");
3142 Ty = PT->getElementType();
3143 }
3144 assert(Ty && "Could not get type information for store");
3145 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3146 }
3147 SDVTList VTs = getVTList(MVT::Other);
3148 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3149 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
3150 FoldingSetNodeID ID;
3151 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3152 ID.AddInteger(ISD::UNINDEXED);
3153 ID.AddInteger(false);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003154 ID.AddInteger(VT.getRawBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003155 ID.AddInteger(Alignment);
3156 ID.AddInteger(isVolatile);
3157 void *IP = 0;
3158 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3159 return SDOperand(E, 0);
3160 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3161 VT, SV, SVOffset, Alignment, isVolatile);
3162 CSEMap.InsertNode(N, IP);
3163 AllNodes.push_back(N);
3164 return SDOperand(N, 0);
3165}
3166
3167SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
3168 SDOperand Ptr, const Value *SV,
Duncan Sands92c43912008-06-06 12:08:01 +00003169 int SVOffset, MVT SVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003170 bool isVolatile, unsigned Alignment) {
Duncan Sands92c43912008-06-06 12:08:01 +00003171 MVT VT = Val.getValueType();
Duncan Sands06fcf652007-10-30 12:40:58 +00003172
3173 if (VT == SVT)
3174 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175
Duncan Sandsec142ee2008-06-08 20:54:56 +00003176 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands92c43912008-06-06 12:08:01 +00003177 assert(VT.isInteger() == SVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003178 "Can't do FP-INT conversion!");
3179
3180 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3181 const Type *Ty = 0;
3182 if (VT != MVT::iPTR) {
Duncan Sands92c43912008-06-06 12:08:01 +00003183 Ty = VT.getTypeForMVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 } else if (SV) {
3185 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3186 assert(PT && "Value for store must be a pointer");
3187 Ty = PT->getElementType();
3188 }
3189 assert(Ty && "Could not get type information for store");
3190 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3191 }
3192 SDVTList VTs = getVTList(MVT::Other);
3193 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3194 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
3195 FoldingSetNodeID ID;
3196 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3197 ID.AddInteger(ISD::UNINDEXED);
Duncan Sands06fcf652007-10-30 12:40:58 +00003198 ID.AddInteger(1);
Duncan Sands3f5d2432008-06-06 12:49:32 +00003199 ID.AddInteger(SVT.getRawBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 ID.AddInteger(Alignment);
3201 ID.AddInteger(isVolatile);
3202 void *IP = 0;
3203 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3204 return SDOperand(E, 0);
Duncan Sands06fcf652007-10-30 12:40:58 +00003205 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003206 SVT, SV, SVOffset, Alignment, isVolatile);
3207 CSEMap.InsertNode(N, IP);
3208 AllNodes.push_back(N);
3209 return SDOperand(N, 0);
3210}
3211
3212SDOperand
3213SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3214 SDOperand Offset, ISD::MemIndexedMode AM) {
3215 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3216 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3217 "Store is already a indexed store!");
3218 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3219 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3220 FoldingSetNodeID ID;
3221 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3222 ID.AddInteger(AM);
3223 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3f5d2432008-06-06 12:49:32 +00003224 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003225 ID.AddInteger(ST->getAlignment());
3226 ID.AddInteger(ST->isVolatile());
3227 void *IP = 0;
3228 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3229 return SDOperand(E, 0);
3230 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003231 ST->isTruncatingStore(), ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232 ST->getSrcValue(), ST->getSrcValueOffset(),
3233 ST->getAlignment(), ST->isVolatile());
3234 CSEMap.InsertNode(N, IP);
3235 AllNodes.push_back(N);
3236 return SDOperand(N, 0);
3237}
3238
Duncan Sands92c43912008-06-06 12:08:01 +00003239SDOperand SelectionDAG::getVAArg(MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003240 SDOperand Chain, SDOperand Ptr,
3241 SDOperand SV) {
3242 SDOperand Ops[] = { Chain, Ptr, SV };
3243 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
3244}
3245
Duncan Sands92c43912008-06-06 12:08:01 +00003246SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003247 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248 switch (NumOps) {
3249 case 0: return getNode(Opcode, VT);
3250 case 1: return getNode(Opcode, VT, Ops[0]);
3251 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3252 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
3253 default: break;
3254 }
3255
3256 switch (Opcode) {
3257 default: break;
3258 case ISD::SELECT_CC: {
3259 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
3260 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3261 "LHS and RHS of condition must have same type!");
3262 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3263 "True and False arms of SelectCC must have same type!");
3264 assert(Ops[2].getValueType() == VT &&
3265 "select_cc node must be of same type as true and false value!");
3266 break;
3267 }
3268 case ISD::BR_CC: {
3269 assert(NumOps == 5 && "BR_CC takes 5 operands!");
3270 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3271 "LHS/RHS of comparison should match types!");
3272 break;
3273 }
3274 }
3275
3276 // Memoize nodes.
3277 SDNode *N;
3278 SDVTList VTs = getVTList(VT);
3279 if (VT != MVT::Flag) {
3280 FoldingSetNodeID ID;
3281 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
3282 void *IP = 0;
3283 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3284 return SDOperand(E, 0);
3285 N = new SDNode(Opcode, VTs, Ops, NumOps);
3286 CSEMap.InsertNode(N, IP);
3287 } else {
3288 N = new SDNode(Opcode, VTs, Ops, NumOps);
3289 }
3290 AllNodes.push_back(N);
3291 return SDOperand(N, 0);
3292}
3293
3294SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands92c43912008-06-06 12:08:01 +00003295 std::vector<MVT> &ResultTys,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003296 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003297 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3298 Ops, NumOps);
3299}
3300
3301SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands92c43912008-06-06 12:08:01 +00003302 const MVT *VTs, unsigned NumVTs,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003303 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304 if (NumVTs == 1)
3305 return getNode(Opcode, VTs[0], Ops, NumOps);
3306 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3307}
3308
3309SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003310 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 if (VTList.NumVTs == 1)
3312 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
3313
3314 switch (Opcode) {
3315 // FIXME: figure out how to safely handle things like
3316 // int foo(int x) { return 1 << (x & 255); }
3317 // int bar() { return foo(256); }
3318#if 0
3319 case ISD::SRA_PARTS:
3320 case ISD::SRL_PARTS:
3321 case ISD::SHL_PARTS:
3322 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3323 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
3324 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3325 else if (N3.getOpcode() == ISD::AND)
3326 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3327 // If the and is only masking out bits that cannot effect the shift,
3328 // eliminate the and.
Duncan Sands92c43912008-06-06 12:08:01 +00003329 unsigned NumBits = VT.getSizeInBits()*2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003330 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3331 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3332 }
3333 break;
3334#endif
3335 }
3336
3337 // Memoize the node unless it returns a flag.
3338 SDNode *N;
3339 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3340 FoldingSetNodeID ID;
3341 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3342 void *IP = 0;
3343 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3344 return SDOperand(E, 0);
3345 if (NumOps == 1)
3346 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3347 else if (NumOps == 2)
3348 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3349 else if (NumOps == 3)
3350 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3351 else
3352 N = new SDNode(Opcode, VTList, Ops, NumOps);
3353 CSEMap.InsertNode(N, IP);
3354 } else {
3355 if (NumOps == 1)
3356 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3357 else if (NumOps == 2)
3358 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3359 else if (NumOps == 3)
3360 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3361 else
3362 N = new SDNode(Opcode, VTList, Ops, NumOps);
3363 }
3364 AllNodes.push_back(N);
3365 return SDOperand(N, 0);
3366}
3367
Dan Gohman798d1272007-10-08 15:49:58 +00003368SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003369 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman798d1272007-10-08 15:49:58 +00003370}
3371
3372SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3373 SDOperand N1) {
3374 SDOperand Ops[] = { N1 };
3375 return getNode(Opcode, VTList, Ops, 1);
3376}
3377
3378SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3379 SDOperand N1, SDOperand N2) {
3380 SDOperand Ops[] = { N1, N2 };
3381 return getNode(Opcode, VTList, Ops, 2);
3382}
3383
3384SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3385 SDOperand N1, SDOperand N2, SDOperand N3) {
3386 SDOperand Ops[] = { N1, N2, N3 };
3387 return getNode(Opcode, VTList, Ops, 3);
3388}
3389
3390SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3391 SDOperand N1, SDOperand N2, SDOperand N3,
3392 SDOperand N4) {
3393 SDOperand Ops[] = { N1, N2, N3, N4 };
3394 return getNode(Opcode, VTList, Ops, 4);
3395}
3396
3397SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3398 SDOperand N1, SDOperand N2, SDOperand N3,
3399 SDOperand N4, SDOperand N5) {
3400 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3401 return getNode(Opcode, VTList, Ops, 5);
3402}
3403
Duncan Sands92c43912008-06-06 12:08:01 +00003404SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00003405 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003406}
3407
Duncan Sands92c43912008-06-06 12:08:01 +00003408SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3409 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410 E = VTList.end(); I != E; ++I) {
3411 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
3412 return makeVTList(&(*I)[0], 2);
3413 }
Duncan Sands92c43912008-06-06 12:08:01 +00003414 std::vector<MVT> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003415 V.push_back(VT1);
3416 V.push_back(VT2);
3417 VTList.push_front(V);
3418 return makeVTList(&(*VTList.begin())[0], 2);
3419}
Duncan Sands92c43912008-06-06 12:08:01 +00003420SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3421 MVT VT3) {
3422 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003423 E = VTList.end(); I != E; ++I) {
3424 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3425 (*I)[2] == VT3)
3426 return makeVTList(&(*I)[0], 3);
3427 }
Duncan Sands92c43912008-06-06 12:08:01 +00003428 std::vector<MVT> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003429 V.push_back(VT1);
3430 V.push_back(VT2);
3431 V.push_back(VT3);
3432 VTList.push_front(V);
3433 return makeVTList(&(*VTList.begin())[0], 3);
3434}
3435
Duncan Sands92c43912008-06-06 12:08:01 +00003436SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 switch (NumVTs) {
3438 case 0: assert(0 && "Cannot have nodes without results!");
3439 case 1: return getVTList(VTs[0]);
3440 case 2: return getVTList(VTs[0], VTs[1]);
3441 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3442 default: break;
3443 }
3444
Duncan Sands92c43912008-06-06 12:08:01 +00003445 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446 E = VTList.end(); I != E; ++I) {
3447 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3448
3449 bool NoMatch = false;
3450 for (unsigned i = 2; i != NumVTs; ++i)
3451 if (VTs[i] != (*I)[i]) {
3452 NoMatch = true;
3453 break;
3454 }
3455 if (!NoMatch)
3456 return makeVTList(&*I->begin(), NumVTs);
3457 }
3458
Duncan Sands92c43912008-06-06 12:08:01 +00003459 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003460 return makeVTList(&*VTList.begin()->begin(), NumVTs);
3461}
3462
3463
3464/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3465/// specified operands. If the resultant node already exists in the DAG,
3466/// this does not modify the specified node, instead it returns the node that
3467/// already exists. If the resultant node does not exist in the DAG, the
3468/// input node is returned. As a degenerate case, if you specify the same
3469/// input operands as the node already has, the input node is returned.
3470SDOperand SelectionDAG::
3471UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3472 SDNode *N = InN.Val;
3473 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3474
3475 // Check to see if there is no change.
3476 if (Op == N->getOperand(0)) return InN;
3477
3478 // See if the modified node already exists.
3479 void *InsertPos = 0;
3480 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3481 return SDOperand(Existing, InN.ResNo);
3482
3483 // Nope it doesn't. Remove the node from it's current place in the maps.
3484 if (InsertPos)
3485 RemoveNodeFromCSEMaps(N);
3486
3487 // Now we update the operands.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003488 N->OperandList[0].getVal()->removeUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003489 N->OperandList[0] = Op;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003490 N->OperandList[0].setUser(N);
3491 Op.Val->addUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003492
3493 // If this gets put into a CSE map, add it.
3494 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3495 return InN;
3496}
3497
3498SDOperand SelectionDAG::
3499UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3500 SDNode *N = InN.Val;
3501 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3502
3503 // Check to see if there is no change.
3504 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3505 return InN; // No operands changed, just return the input node.
3506
3507 // See if the modified node already exists.
3508 void *InsertPos = 0;
3509 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3510 return SDOperand(Existing, InN.ResNo);
3511
3512 // Nope it doesn't. Remove the node from it's current place in the maps.
3513 if (InsertPos)
3514 RemoveNodeFromCSEMaps(N);
3515
3516 // Now we update the operands.
3517 if (N->OperandList[0] != Op1) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003518 N->OperandList[0].getVal()->removeUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003519 N->OperandList[0] = Op1;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003520 N->OperandList[0].setUser(N);
3521 Op1.Val->addUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003522 }
3523 if (N->OperandList[1] != Op2) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003524 N->OperandList[1].getVal()->removeUser(1, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525 N->OperandList[1] = Op2;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003526 N->OperandList[1].setUser(N);
3527 Op2.Val->addUser(1, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528 }
3529
3530 // If this gets put into a CSE map, add it.
3531 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3532 return InN;
3533}
3534
3535SDOperand SelectionDAG::
3536UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
3537 SDOperand Ops[] = { Op1, Op2, Op3 };
3538 return UpdateNodeOperands(N, Ops, 3);
3539}
3540
3541SDOperand SelectionDAG::
3542UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3543 SDOperand Op3, SDOperand Op4) {
3544 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3545 return UpdateNodeOperands(N, Ops, 4);
3546}
3547
3548SDOperand SelectionDAG::
3549UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3550 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
3551 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3552 return UpdateNodeOperands(N, Ops, 5);
3553}
3554
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003555SDOperand SelectionDAG::
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003556UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003557 SDNode *N = InN.Val;
3558 assert(N->getNumOperands() == NumOps &&
3559 "Update with wrong number of operands");
3560
3561 // Check to see if there is no change.
3562 bool AnyChange = false;
3563 for (unsigned i = 0; i != NumOps; ++i) {
3564 if (Ops[i] != N->getOperand(i)) {
3565 AnyChange = true;
3566 break;
3567 }
3568 }
3569
3570 // No operands changed, just return the input node.
3571 if (!AnyChange) return InN;
3572
3573 // See if the modified node already exists.
3574 void *InsertPos = 0;
3575 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
3576 return SDOperand(Existing, InN.ResNo);
3577
Dan Gohman14a027d2008-05-02 00:05:03 +00003578 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003579 if (InsertPos)
3580 RemoveNodeFromCSEMaps(N);
3581
3582 // Now we update the operands.
3583 for (unsigned i = 0; i != NumOps; ++i) {
3584 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003585 N->OperandList[i].getVal()->removeUser(i, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003586 N->OperandList[i] = Ops[i];
Roman Levenstein05650fd2008-04-07 10:06:32 +00003587 N->OperandList[i].setUser(N);
3588 Ops[i].Val->addUser(i, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003589 }
3590 }
3591
3592 // If this gets put into a CSE map, add it.
3593 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3594 return InN;
3595}
3596
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597/// MorphNodeTo - This frees the operands of the current node, resets the
3598/// opcode, types, and operands to the specified value. This should only be
3599/// used by the SelectionDAG class.
3600void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman1d47f5c2008-04-17 23:02:12 +00003601 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003602 NodeType = Opc;
3603 ValueList = L.VTs;
3604 NumValues = L.NumVTs;
3605
3606 // Clear the operands list, updating used nodes to remove this from their
3607 // use list.
3608 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003609 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003610
3611 // If NumOps is larger than the # of operands we currently have, reallocate
3612 // the operand list.
3613 if (NumOps > NumOperands) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003614 if (OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615 delete [] OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003616 }
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003617 OperandList = new SDUse[NumOps];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003618 OperandsNeedDelete = true;
3619 }
3620
3621 // Assign the new operands.
3622 NumOperands = NumOps;
3623
3624 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3625 OperandList[i] = Ops[i];
Roman Levenstein05650fd2008-04-07 10:06:32 +00003626 OperandList[i].setUser(this);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003627 SDNode *N = OperandList[i].getVal();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003628 N->addUser(i, this);
3629 ++N->UsesSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003630 }
3631}
3632
3633/// SelectNodeTo - These are used for target selectors to *mutate* the
3634/// specified node to have the specified return type, Target opcode, and
3635/// operands. Note that target opcodes are stored as
3636/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
3637///
3638/// Note that SelectNodeTo returns the resultant node. If there is already a
3639/// node of the specified opcode and operands, it returns that node instead of
3640/// the current one.
3641SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00003642 MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003643 SDVTList VTs = getVTList(VT);
3644 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003645 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003646 void *IP = 0;
3647 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3648 return ON;
3649
3650 RemoveNodeFromCSEMaps(N);
3651
Dan Gohman1d47f5c2008-04-17 23:02:12 +00003652 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, SDOperandPtr(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003653
3654 CSEMap.InsertNode(N, IP);
3655 return N;
3656}
3657
3658SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00003659 MVT VT, SDOperand Op1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003660 // If an identical node already exists, use it.
3661 SDVTList VTs = getVTList(VT);
3662 SDOperand Ops[] = { Op1 };
3663
3664 FoldingSetNodeID ID;
3665 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3666 void *IP = 0;
3667 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3668 return ON;
3669
3670 RemoveNodeFromCSEMaps(N);
3671 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3672 CSEMap.InsertNode(N, IP);
3673 return N;
3674}
3675
3676SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00003677 MVT VT, SDOperand Op1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003678 SDOperand Op2) {
3679 // If an identical node already exists, use it.
3680 SDVTList VTs = getVTList(VT);
3681 SDOperand Ops[] = { Op1, Op2 };
3682
3683 FoldingSetNodeID ID;
3684 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3685 void *IP = 0;
3686 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3687 return ON;
3688
3689 RemoveNodeFromCSEMaps(N);
3690
3691 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3692
3693 CSEMap.InsertNode(N, IP); // Memoize the new node.
3694 return N;
3695}
3696
3697SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00003698 MVT VT, SDOperand Op1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003699 SDOperand Op2, SDOperand Op3) {
3700 // If an identical node already exists, use it.
3701 SDVTList VTs = getVTList(VT);
3702 SDOperand Ops[] = { Op1, Op2, Op3 };
3703 FoldingSetNodeID ID;
3704 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3705 void *IP = 0;
3706 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3707 return ON;
3708
3709 RemoveNodeFromCSEMaps(N);
3710
3711 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3712
3713 CSEMap.InsertNode(N, IP); // Memoize the new node.
3714 return N;
3715}
3716
3717SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00003718 MVT VT, SDOperandPtr Ops,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003719 unsigned NumOps) {
3720 // If an identical node already exists, use it.
3721 SDVTList VTs = getVTList(VT);
3722 FoldingSetNodeID ID;
3723 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3724 void *IP = 0;
3725 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3726 return ON;
3727
3728 RemoveNodeFromCSEMaps(N);
3729 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3730
3731 CSEMap.InsertNode(N, IP); // Memoize the new node.
3732 return N;
3733}
3734
3735SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00003736 MVT VT1, MVT VT2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737 SDOperand Op1, SDOperand Op2) {
3738 SDVTList VTs = getVTList(VT1, VT2);
3739 FoldingSetNodeID ID;
3740 SDOperand Ops[] = { Op1, Op2 };
3741 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3742 void *IP = 0;
3743 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3744 return ON;
3745
3746 RemoveNodeFromCSEMaps(N);
3747 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3748 CSEMap.InsertNode(N, IP); // Memoize the new node.
3749 return N;
3750}
3751
3752SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands92c43912008-06-06 12:08:01 +00003753 MVT VT1, MVT VT2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003754 SDOperand Op1, SDOperand Op2,
3755 SDOperand Op3) {
3756 // If an identical node already exists, use it.
3757 SDVTList VTs = getVTList(VT1, VT2);
3758 SDOperand Ops[] = { Op1, Op2, Op3 };
3759 FoldingSetNodeID ID;
3760 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3761 void *IP = 0;
3762 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3763 return ON;
3764
3765 RemoveNodeFromCSEMaps(N);
3766
3767 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3768 CSEMap.InsertNode(N, IP); // Memoize the new node.
3769 return N;
3770}
3771
3772
3773/// getTargetNode - These are used for target selectors to create a new node
3774/// with specified return type(s), target opcode, and operands.
3775///
3776/// Note that getTargetNode returns the resultant node. If there is already a
3777/// node of the specified opcode and operands, it returns that node instead of
3778/// the current one.
Duncan Sands92c43912008-06-06 12:08:01 +00003779SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003780 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3781}
Duncan Sands92c43912008-06-06 12:08:01 +00003782SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003783 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3784}
Duncan Sands92c43912008-06-06 12:08:01 +00003785SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003786 SDOperand Op1, SDOperand Op2) {
3787 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3788}
Duncan Sands92c43912008-06-06 12:08:01 +00003789SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790 SDOperand Op1, SDOperand Op2,
3791 SDOperand Op3) {
3792 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3793}
Duncan Sands92c43912008-06-06 12:08:01 +00003794SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003795 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003796 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3797}
Duncan Sands92c43912008-06-06 12:08:01 +00003798SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3799 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003800 SDOperand Op;
3801 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3802}
Duncan Sands92c43912008-06-06 12:08:01 +00003803SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3804 MVT VT2, SDOperand Op1) {
3805 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003806 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3807}
Duncan Sands92c43912008-06-06 12:08:01 +00003808SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3809 MVT VT2, SDOperand Op1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 SDOperand Op2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003811 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003812 SDOperand Ops[] = { Op1, Op2 };
3813 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3814}
Duncan Sands92c43912008-06-06 12:08:01 +00003815SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3816 MVT VT2, SDOperand Op1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003817 SDOperand Op2, SDOperand Op3) {
Duncan Sands92c43912008-06-06 12:08:01 +00003818 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003819 SDOperand Ops[] = { Op1, Op2, Op3 };
3820 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3821}
Duncan Sands92c43912008-06-06 12:08:01 +00003822SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003823 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00003824 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3826}
Duncan Sands92c43912008-06-06 12:08:01 +00003827SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003828 SDOperand Op1, SDOperand Op2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003829 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003830 SDOperand Ops[] = { Op1, Op2 };
3831 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3832}
Duncan Sands92c43912008-06-06 12:08:01 +00003833SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 SDOperand Op1, SDOperand Op2,
3835 SDOperand Op3) {
Duncan Sands92c43912008-06-06 12:08:01 +00003836 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003837 SDOperand Ops[] = { Op1, Op2, Op3 };
3838 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3839}
Duncan Sands92c43912008-06-06 12:08:01 +00003840SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003841 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00003842 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3844}
Duncan Sands92c43912008-06-06 12:08:01 +00003845SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3846 MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003847 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00003848 std::vector<MVT> VTList;
Evan Chenge1d067e2007-09-12 23:39:49 +00003849 VTList.push_back(VT1);
3850 VTList.push_back(VT2);
3851 VTList.push_back(VT3);
3852 VTList.push_back(VT4);
Duncan Sands92c43912008-06-06 12:08:01 +00003853 const MVT *VTs = getNodeValueTypes(VTList);
Evan Chenge1d067e2007-09-12 23:39:49 +00003854 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3855}
Evan Chenge3940912007-10-05 01:10:49 +00003856SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands92c43912008-06-06 12:08:01 +00003857 std::vector<MVT> &ResultTys,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003858 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands92c43912008-06-06 12:08:01 +00003859 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Chenge3940912007-10-05 01:10:49 +00003860 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3861 Ops, NumOps).Val;
3862}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003863
Evan Chengd1113582008-03-22 01:55:50 +00003864/// getNodeIfExists - Get the specified node if it's already available, or
3865/// else return NULL.
3866SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003867 SDOperandPtr Ops, unsigned NumOps) {
Evan Chengd1113582008-03-22 01:55:50 +00003868 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3869 FoldingSetNodeID ID;
3870 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3871 void *IP = 0;
3872 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3873 return E;
3874 }
3875 return NULL;
3876}
3877
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003878
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003879/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3880/// This can cause recursive merging of nodes in the DAG.
3881///
Chris Lattnerdca329f2008-02-03 03:35:22 +00003882/// This version assumes From has a single result value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003883///
Chris Lattnerdca329f2008-02-03 03:35:22 +00003884void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003885 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00003886 SDNode *From = FromN.Val;
Chris Lattnerdca329f2008-02-03 03:35:22 +00003887 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888 "Cannot replace with this method!");
Chris Lattnerdca329f2008-02-03 03:35:22 +00003889 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levenstein05650fd2008-04-07 10:06:32 +00003890
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891 while (!From->use_empty()) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003892 SDNode::use_iterator UI = From->use_begin();
3893 SDNode *U = UI->getUser();
3894
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895 // This node is about to morph, remove its old self from the CSE maps.
3896 RemoveNodeFromCSEMaps(U);
Roman Levenstein05650fd2008-04-07 10:06:32 +00003897 int operandNum = 0;
3898 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3899 I != E; ++I, ++operandNum)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003900 if (I->getVal() == From) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003901 From->removeUser(operandNum, U);
Chris Lattnerdca329f2008-02-03 03:35:22 +00003902 *I = To;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003903 I->setUser(U);
3904 To.Val->addUser(operandNum, U);
3905 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906
3907 // Now that we have modified U, add it back to the CSE maps. If it already
3908 // exists there, recursively merge the results together.
3909 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003910 ReplaceAllUsesWith(U, Existing, UpdateListener);
3911 // U is now dead. Inform the listener if it exists and delete it.
3912 if (UpdateListener)
Duncan Sands3866b1c2008-06-11 11:42:12 +00003913 UpdateListener->NodeDeleted(U, Existing);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003914 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003915 } else {
3916 // If the node doesn't already exist, we updated it. Inform a listener if
3917 // it exists.
3918 if (UpdateListener)
3919 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 }
3921 }
3922}
3923
3924/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3925/// This can cause recursive merging of nodes in the DAG.
3926///
3927/// This version assumes From/To have matching types and numbers of result
3928/// values.
3929///
3930void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003931 DAGUpdateListener *UpdateListener) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932 assert(From != To && "Cannot replace uses of with self");
3933 assert(From->getNumValues() == To->getNumValues() &&
3934 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattnerdca329f2008-02-03 03:35:22 +00003935 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003936 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3937 UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003938
3939 while (!From->use_empty()) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003940 SDNode::use_iterator UI = From->use_begin();
3941 SDNode *U = UI->getUser();
3942
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003943 // This node is about to morph, remove its old self from the CSE maps.
3944 RemoveNodeFromCSEMaps(U);
Roman Levenstein05650fd2008-04-07 10:06:32 +00003945 int operandNum = 0;
3946 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3947 I != E; ++I, ++operandNum)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003948 if (I->getVal() == From) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003949 From->removeUser(operandNum, U);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003950 I->getVal() = To;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003951 To->addUser(operandNum, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003952 }
Roman Levenstein05650fd2008-04-07 10:06:32 +00003953
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954 // Now that we have modified U, add it back to the CSE maps. If it already
3955 // exists there, recursively merge the results together.
3956 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003957 ReplaceAllUsesWith(U, Existing, UpdateListener);
3958 // U is now dead. Inform the listener if it exists and delete it.
3959 if (UpdateListener)
Duncan Sands3866b1c2008-06-11 11:42:12 +00003960 UpdateListener->NodeDeleted(U, Existing);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003961 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003962 } else {
3963 // If the node doesn't already exist, we updated it. Inform a listener if
3964 // it exists.
3965 if (UpdateListener)
3966 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003967 }
3968 }
3969}
3970
3971/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3972/// This can cause recursive merging of nodes in the DAG.
3973///
3974/// This version can replace From with any result values. To must match the
3975/// number and types of values returned by From.
3976void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003977 SDOperandPtr To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003978 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00003979 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003980 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981
3982 while (!From->use_empty()) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003983 SDNode::use_iterator UI = From->use_begin();
3984 SDNode *U = UI->getUser();
3985
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 // This node is about to morph, remove its old self from the CSE maps.
3987 RemoveNodeFromCSEMaps(U);
Roman Levenstein05650fd2008-04-07 10:06:32 +00003988 int operandNum = 0;
3989 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3990 I != E; ++I, ++operandNum)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003991 if (I->getVal() == From) {
3992 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levenstein05650fd2008-04-07 10:06:32 +00003993 From->removeUser(operandNum, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003994 *I = ToOp;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003995 I->setUser(U);
3996 ToOp.Val->addUser(operandNum, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003997 }
Roman Levenstein05650fd2008-04-07 10:06:32 +00003998
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003999 // Now that we have modified U, add it back to the CSE maps. If it already
4000 // exists there, recursively merge the results together.
4001 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004002 ReplaceAllUsesWith(U, Existing, UpdateListener);
4003 // U is now dead. Inform the listener if it exists and delete it.
4004 if (UpdateListener)
Duncan Sands3866b1c2008-06-11 11:42:12 +00004005 UpdateListener->NodeDeleted(U, Existing);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004006 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004007 } else {
4008 // If the node doesn't already exist, we updated it. Inform a listener if
4009 // it exists.
4010 if (UpdateListener)
4011 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012 }
4013 }
4014}
4015
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004016namespace {
4017 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4018 /// any deleted nodes from the set passed into its constructor and recursively
4019 /// notifies another update listener if specified.
4020 class ChainedSetUpdaterListener :
4021 public SelectionDAG::DAGUpdateListener {
4022 SmallSetVector<SDNode*, 16> &Set;
4023 SelectionDAG::DAGUpdateListener *Chain;
4024 public:
4025 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4026 SelectionDAG::DAGUpdateListener *chain)
4027 : Set(set), Chain(chain) {}
Roman Levenstein05650fd2008-04-07 10:06:32 +00004028
Duncan Sands3866b1c2008-06-11 11:42:12 +00004029 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004030 Set.remove(N);
Duncan Sands3866b1c2008-06-11 11:42:12 +00004031 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004032 }
4033 virtual void NodeUpdated(SDNode *N) {
4034 if (Chain) Chain->NodeUpdated(N);
4035 }
4036 };
4037}
4038
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4040/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004041/// handled the same way as for ReplaceAllUsesWith.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004042void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004043 DAGUpdateListener *UpdateListener){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004044 assert(From != To && "Cannot replace a value with itself");
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004045
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004046 // Handle the simple, trivial, case efficiently.
Chris Lattnerdca329f2008-02-03 03:35:22 +00004047 if (From.Val->getNumValues() == 1) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004048 ReplaceAllUsesWith(From, To, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 return;
4050 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004051
4052 if (From.use_empty()) return;
4053
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004054 // Get all of the users of From.Val. We want these in a nice,
4055 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levenstein05650fd2008-04-07 10:06:32 +00004056 SmallSetVector<SDNode*, 16> Users;
4057 for (SDNode::use_iterator UI = From.Val->use_begin(),
4058 E = From.Val->use_end(); UI != E; ++UI) {
4059 SDNode *User = UI->getUser();
4060 if (!Users.count(User))
4061 Users.insert(User);
4062 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004063
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004064 // When one of the recursive merges deletes nodes from the graph, we need to
4065 // make sure that UpdateListener is notified *and* that the node is removed
4066 // from Users if present. CSUL does this.
4067 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner8a258202007-10-15 06:10:22 +00004068
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 while (!Users.empty()) {
4070 // We know that this user uses some value of From. If it is the right
4071 // value, update it.
4072 SDNode *User = Users.back();
4073 Users.pop_back();
4074
Chris Lattner8a258202007-10-15 06:10:22 +00004075 // Scan for an operand that matches From.
Roman Levenstein05650fd2008-04-07 10:06:32 +00004076 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner8a258202007-10-15 06:10:22 +00004077 for (; Op != E; ++Op)
4078 if (*Op == From) break;
4079
4080 // If there are no matches, the user must use some other result of From.
4081 if (Op == E) continue;
4082
4083 // Okay, we know this user needs to be updated. Remove its old self
4084 // from the CSE maps.
4085 RemoveNodeFromCSEMaps(User);
4086
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004087 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner8a258202007-10-15 06:10:22 +00004088 for (; Op != E; ++Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004089 if (*Op == From) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00004090 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif5bd3d9b2008-04-11 09:34:57 +00004091 *Op = To;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004092 Op->setUser(User);
4093 To.Val->addUser(Op-User->op_begin(), User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 }
4095 }
Chris Lattner8a258202007-10-15 06:10:22 +00004096
4097 // Now that we have modified User, add it back to the CSE maps. If it
4098 // already exists there, recursively merge the results together.
4099 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004100 if (!Existing) {
4101 if (UpdateListener) UpdateListener->NodeUpdated(User);
4102 continue; // Continue on to next user.
4103 }
Chris Lattner8a258202007-10-15 06:10:22 +00004104
4105 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004106 // to replace the dead one with the existing one. This can cause
Chris Lattner8a258202007-10-15 06:10:22 +00004107 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004108 // can cause deletion of nodes that used the old value. To handle this, we
4109 // use CSUL to remove them from the Users set.
4110 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner8a258202007-10-15 06:10:22 +00004111
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004112 // User is now dead. Notify a listener if present.
Duncan Sands3866b1c2008-06-11 11:42:12 +00004113 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner8a258202007-10-15 06:10:22 +00004114 DeleteNodeNotInCSEMaps(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004115 }
4116}
4117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4119/// their allnodes order. It returns the maximum id.
4120unsigned SelectionDAG::AssignNodeIds() {
4121 unsigned Id = 0;
4122 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4123 SDNode *N = I;
4124 N->setNodeId(Id++);
4125 }
4126 return Id;
4127}
4128
4129/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
4130/// based on their topological order. It returns the maximum id and a vector
4131/// of the SDNodes* in assigned order by reference.
4132unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
4133 unsigned DAGSize = AllNodes.size();
4134 std::vector<unsigned> InDegree(DAGSize);
4135 std::vector<SDNode*> Sources;
4136
4137 // Use a two pass approach to avoid using a std::map which is slow.
4138 unsigned Id = 0;
4139 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4140 SDNode *N = I;
4141 N->setNodeId(Id++);
4142 unsigned Degree = N->use_size();
4143 InDegree[N->getNodeId()] = Degree;
4144 if (Degree == 0)
4145 Sources.push_back(N);
4146 }
4147
4148 TopOrder.clear();
4149 while (!Sources.empty()) {
4150 SDNode *N = Sources.back();
4151 Sources.pop_back();
4152 TopOrder.push_back(N);
4153 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004154 SDNode *P = I->getVal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 unsigned Degree = --InDegree[P->getNodeId()];
4156 if (Degree == 0)
4157 Sources.push_back(P);
4158 }
4159 }
4160
4161 // Second pass, assign the actual topological order as node ids.
4162 Id = 0;
4163 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4164 TI != TE; ++TI)
4165 (*TI)->setNodeId(Id++);
4166
4167 return Id;
4168}
4169
4170
4171
4172//===----------------------------------------------------------------------===//
4173// SDNode Class
4174//===----------------------------------------------------------------------===//
4175
4176// Out-of-line virtual method to give class a home.
4177void SDNode::ANCHOR() {}
4178void UnarySDNode::ANCHOR() {}
4179void BinarySDNode::ANCHOR() {}
4180void TernarySDNode::ANCHOR() {}
4181void HandleSDNode::ANCHOR() {}
4182void StringSDNode::ANCHOR() {}
4183void ConstantSDNode::ANCHOR() {}
4184void ConstantFPSDNode::ANCHOR() {}
4185void GlobalAddressSDNode::ANCHOR() {}
4186void FrameIndexSDNode::ANCHOR() {}
4187void JumpTableSDNode::ANCHOR() {}
4188void ConstantPoolSDNode::ANCHOR() {}
4189void BasicBlockSDNode::ANCHOR() {}
4190void SrcValueSDNode::ANCHOR() {}
Dan Gohman12a9c082008-02-06 22:27:42 +00004191void MemOperandSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192void RegisterSDNode::ANCHOR() {}
4193void ExternalSymbolSDNode::ANCHOR() {}
4194void CondCodeSDNode::ANCHOR() {}
Duncan Sandsc93fae32008-03-21 09:14:45 +00004195void ARG_FLAGSSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196void VTSDNode::ANCHOR() {}
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004197void MemSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198void LoadSDNode::ANCHOR() {}
4199void StoreSDNode::ANCHOR() {}
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004200void AtomicSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201
4202HandleSDNode::~HandleSDNode() {
4203 SDVTList VTs = { 0, 0 };
Dan Gohman1d47f5c2008-04-17 23:02:12 +00004204 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205}
4206
4207GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands92c43912008-06-06 12:08:01 +00004208 MVT VT, int o)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004209 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman53491e92007-07-23 20:24:29 +00004210 cast<GlobalVariable>(GA)->isThreadLocal() ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211 // Thread Local
4212 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4213 // Non Thread Local
4214 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4215 getSDVTList(VT)), Offset(o) {
4216 TheGlobal = const_cast<GlobalValue*>(GA);
4217}
4218
Dan Gohman1fad9e62008-04-07 19:35:22 +00004219/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004220/// reference performed by this atomic.
4221MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanc70fa752008-06-25 16:07:49 +00004222 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004223 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4224 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4225
4226 // Check if the atomic references a frame index
4227 const FrameIndexSDNode *FI =
4228 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4229 if (!getSrcValue() && FI)
4230 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4231 FI->getIndex(), Size, getAlignment());
4232 else
4233 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4234 Size, getAlignment());
4235}
4236
4237/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman12a9c082008-02-06 22:27:42 +00004238/// reference performed by this load or store.
Dan Gohman1fad9e62008-04-07 19:35:22 +00004239MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands92c43912008-06-06 12:08:01 +00004240 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman12a9c082008-02-06 22:27:42 +00004241 int Flags =
Dan Gohman1fad9e62008-04-07 19:35:22 +00004242 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4243 MachineMemOperand::MOStore;
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004244 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman12a9c082008-02-06 22:27:42 +00004245
4246 // Check if the load references a frame index, and does not have
4247 // an SV attached.
4248 const FrameIndexSDNode *FI =
4249 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4250 if (!getSrcValue() && FI)
Dan Gohman1fad9e62008-04-07 19:35:22 +00004251 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004252 FI->getIndex(), Size, getAlignment());
Dan Gohman12a9c082008-02-06 22:27:42 +00004253 else
Dan Gohman1fad9e62008-04-07 19:35:22 +00004254 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004255 getSrcValueOffset(), Size, getAlignment());
Dan Gohman12a9c082008-02-06 22:27:42 +00004256}
4257
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258/// Profile - Gather unique data for the node.
4259///
4260void SDNode::Profile(FoldingSetNodeID &ID) {
4261 AddNodeIDNode(ID, this);
4262}
4263
4264/// getValueTypeList - Return a pointer to the specified value type.
4265///
Duncan Sands92c43912008-06-06 12:08:01 +00004266const MVT *SDNode::getValueTypeList(MVT VT) {
4267 if (VT.isExtended()) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004268 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman8cdf7892008-02-08 03:26:46 +00004269 return &(*EVTs.insert(VT).first);
Duncan Sandsa9810f32007-10-16 09:56:48 +00004270 } else {
Duncan Sands92c43912008-06-06 12:08:01 +00004271 static MVT VTs[MVT::LAST_VALUETYPE];
4272 VTs[VT.getSimpleVT()] = VT;
4273 return &VTs[VT.getSimpleVT()];
Duncan Sandsa9810f32007-10-16 09:56:48 +00004274 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004275}
Duncan Sandsa9810f32007-10-16 09:56:48 +00004276
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4278/// indicated value. This method ignores uses of other values defined by this
4279/// operation.
4280bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
4281 assert(Value < getNumValues() && "Bad value!");
4282
4283 // If there is only one value, this is easy.
4284 if (getNumValues() == 1)
4285 return use_size() == NUses;
Evan Cheng0af04f72007-08-02 05:29:38 +00004286 if (use_size() < NUses) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287
4288 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4289
4290 SmallPtrSet<SDNode*, 32> UsersHandled;
4291
Roman Levenstein05650fd2008-04-07 10:06:32 +00004292 // TODO: Only iterate over uses of a given value of the node
4293 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4294 if (*UI == TheValue) {
4295 if (NUses == 0)
4296 return false;
4297 --NUses;
4298 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004299 }
4300
4301 // Found exactly the right number of uses?
4302 return NUses == 0;
4303}
4304
4305
Evan Cheng0af04f72007-08-02 05:29:38 +00004306/// hasAnyUseOfValue - Return true if there are any use of the indicated
4307/// value. This method ignores uses of other values defined by this operation.
4308bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4309 assert(Value < getNumValues() && "Bad value!");
4310
Dan Gohman301f4052008-01-29 13:02:09 +00004311 if (use_empty()) return false;
Evan Cheng0af04f72007-08-02 05:29:38 +00004312
4313 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4314
4315 SmallPtrSet<SDNode*, 32> UsersHandled;
4316
Roman Levenstein05650fd2008-04-07 10:06:32 +00004317 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4318 SDNode *User = UI->getUser();
Evan Cheng0af04f72007-08-02 05:29:38 +00004319 if (User->getNumOperands() == 1 ||
4320 UsersHandled.insert(User)) // First time we've seen this?
4321 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4322 if (User->getOperand(i) == TheValue) {
4323 return true;
4324 }
4325 }
4326
4327 return false;
4328}
4329
4330
Evan Chengd9387682008-03-04 00:41:45 +00004331/// isOnlyUseOf - Return true if this node is the only use of N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332///
Evan Chengd9387682008-03-04 00:41:45 +00004333bool SDNode::isOnlyUseOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 bool Seen = false;
4335 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00004336 SDNode *User = I->getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337 if (User == this)
4338 Seen = true;
4339 else
4340 return false;
4341 }
4342
4343 return Seen;
4344}
4345
4346/// isOperand - Return true if this node is an operand of N.
4347///
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004348bool SDOperand::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004349 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4350 if (*this == N->getOperand(i))
4351 return true;
4352 return false;
4353}
4354
Evan Chengd9387682008-03-04 00:41:45 +00004355bool SDNode::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004357 if (this == N->OperandList[i].getVal())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358 return true;
4359 return false;
4360}
4361
Chris Lattner10d94f92008-01-16 05:49:24 +00004362/// reachesChainWithoutSideEffects - Return true if this operand (which must
4363/// be a chain) reaches the specified operand without crossing any
4364/// side-effecting instructions. In practice, this looks through token
4365/// factors and non-volatile loads. In order to remain efficient, this only
4366/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004367bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner10d94f92008-01-16 05:49:24 +00004368 unsigned Depth) const {
4369 if (*this == Dest) return true;
4370
4371 // Don't search too deeply, we just want to be able to see through
4372 // TokenFactor's etc.
4373 if (Depth == 0) return false;
4374
4375 // If this is a token factor, all inputs to the TF happen in parallel. If any
4376 // of the operands of the TF reach dest, then we can do the xform.
4377 if (getOpcode() == ISD::TokenFactor) {
4378 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4379 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4380 return true;
4381 return false;
4382 }
4383
4384 // Loads don't have side effects, look through them.
4385 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4386 if (!Ld->isVolatile())
4387 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4388 }
4389 return false;
4390}
4391
4392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004393static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
4394 SmallPtrSet<SDNode *, 32> &Visited) {
4395 if (found || !Visited.insert(N))
4396 return;
4397
4398 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4399 SDNode *Op = N->getOperand(i).Val;
4400 if (Op == P) {
4401 found = true;
4402 return;
4403 }
4404 findPredecessor(Op, P, found, Visited);
4405 }
4406}
4407
Evan Chengd9387682008-03-04 00:41:45 +00004408/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409/// is either an operand of N or it can be reached by recursively traversing
4410/// up the operands.
4411/// NOTE: this is an expensive method. Use it carefully.
Evan Chengd9387682008-03-04 00:41:45 +00004412bool SDNode::isPredecessorOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413 SmallPtrSet<SDNode *, 32> Visited;
4414 bool found = false;
4415 findPredecessor(N, this, found, Visited);
4416 return found;
4417}
4418
4419uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4420 assert(Num < NumOperands && "Invalid child # of SDNode!");
4421 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4422}
4423
4424std::string SDNode::getOperationName(const SelectionDAG *G) const {
4425 switch (getOpcode()) {
4426 default:
4427 if (getOpcode() < ISD::BUILTIN_OP_END)
4428 return "<<Unknown DAG Node>>";
4429 else {
4430 if (G) {
4431 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
4432 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner0c2a4f32008-01-07 03:13:06 +00004433 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004434
4435 TargetLowering &TLI = G->getTargetLoweringInfo();
4436 const char *Name =
4437 TLI.getTargetNodeName(getOpcode());
4438 if (Name) return Name;
4439 }
4440
4441 return "<<Unknown Target Node>>";
4442 }
4443
Evan Chengd1d68072008-03-08 00:58:38 +00004444 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth785610d2008-02-16 01:24:58 +00004445 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004446 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4447 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4448 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang078a62d2008-05-05 19:05:59 +00004449 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4450 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4451 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharthaf02d592008-06-14 05:48:15 +00004452 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang078a62d2008-05-05 19:05:59 +00004453 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4454 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4455 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4456 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4457 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458 case ISD::PCMARKER: return "PCMarker";
4459 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
4460 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman12a9c082008-02-06 22:27:42 +00004461 case ISD::MEMOPERAND: return "MemOperand";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 case ISD::EntryToken: return "EntryToken";
4463 case ISD::TokenFactor: return "TokenFactor";
4464 case ISD::AssertSext: return "AssertSext";
4465 case ISD::AssertZext: return "AssertZext";
4466
4467 case ISD::STRING: return "String";
4468 case ISD::BasicBlock: return "BasicBlock";
Duncan Sandsc93fae32008-03-21 09:14:45 +00004469 case ISD::ARG_FLAGS: return "ArgFlags";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004470 case ISD::VALUETYPE: return "ValueType";
4471 case ISD::Register: return "Register";
4472
4473 case ISD::Constant: return "Constant";
4474 case ISD::ConstantFP: return "ConstantFP";
4475 case ISD::GlobalAddress: return "GlobalAddress";
4476 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
4477 case ISD::FrameIndex: return "FrameIndex";
4478 case ISD::JumpTable: return "JumpTable";
4479 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
4480 case ISD::RETURNADDR: return "RETURNADDR";
4481 case ISD::FRAMEADDR: return "FRAMEADDR";
4482 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
4483 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4484 case ISD::EHSELECTION: return "EHSELECTION";
4485 case ISD::EH_RETURN: return "EH_RETURN";
4486 case ISD::ConstantPool: return "ConstantPool";
4487 case ISD::ExternalSymbol: return "ExternalSymbol";
4488 case ISD::INTRINSIC_WO_CHAIN: {
4489 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4490 return Intrinsic::getName((Intrinsic::ID)IID);
4491 }
4492 case ISD::INTRINSIC_VOID:
4493 case ISD::INTRINSIC_W_CHAIN: {
4494 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
4495 return Intrinsic::getName((Intrinsic::ID)IID);
4496 }
4497
4498 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
4499 case ISD::TargetConstant: return "TargetConstant";
4500 case ISD::TargetConstantFP:return "TargetConstantFP";
4501 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
4502 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
4503 case ISD::TargetFrameIndex: return "TargetFrameIndex";
4504 case ISD::TargetJumpTable: return "TargetJumpTable";
4505 case ISD::TargetConstantPool: return "TargetConstantPool";
4506 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
4507
4508 case ISD::CopyToReg: return "CopyToReg";
4509 case ISD::CopyFromReg: return "CopyFromReg";
4510 case ISD::UNDEF: return "undef";
4511 case ISD::MERGE_VALUES: return "merge_values";
4512 case ISD::INLINEASM: return "inlineasm";
4513 case ISD::LABEL: return "label";
Evan Cheng2e28d622008-02-02 04:07:54 +00004514 case ISD::DECLARE: return "declare";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004515 case ISD::HANDLENODE: return "handlenode";
4516 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
4517 case ISD::CALL: return "call";
4518
4519 // Unary operators
4520 case ISD::FABS: return "fabs";
4521 case ISD::FNEG: return "fneg";
4522 case ISD::FSQRT: return "fsqrt";
4523 case ISD::FSIN: return "fsin";
4524 case ISD::FCOS: return "fcos";
4525 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00004526 case ISD::FPOW: return "fpow";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527
4528 // Binary operators
4529 case ISD::ADD: return "add";
4530 case ISD::SUB: return "sub";
4531 case ISD::MUL: return "mul";
4532 case ISD::MULHU: return "mulhu";
4533 case ISD::MULHS: return "mulhs";
4534 case ISD::SDIV: return "sdiv";
4535 case ISD::UDIV: return "udiv";
4536 case ISD::SREM: return "srem";
4537 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00004538 case ISD::SMUL_LOHI: return "smul_lohi";
4539 case ISD::UMUL_LOHI: return "umul_lohi";
4540 case ISD::SDIVREM: return "sdivrem";
4541 case ISD::UDIVREM: return "divrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004542 case ISD::AND: return "and";
4543 case ISD::OR: return "or";
4544 case ISD::XOR: return "xor";
4545 case ISD::SHL: return "shl";
4546 case ISD::SRA: return "sra";
4547 case ISD::SRL: return "srl";
4548 case ISD::ROTL: return "rotl";
4549 case ISD::ROTR: return "rotr";
4550 case ISD::FADD: return "fadd";
4551 case ISD::FSUB: return "fsub";
4552 case ISD::FMUL: return "fmul";
4553 case ISD::FDIV: return "fdiv";
4554 case ISD::FREM: return "frem";
4555 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00004556 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557
4558 case ISD::SETCC: return "setcc";
Nate Begeman9a1ce152008-05-12 19:40:03 +00004559 case ISD::VSETCC: return "vsetcc";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560 case ISD::SELECT: return "select";
4561 case ISD::SELECT_CC: return "select_cc";
4562 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
4563 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
4564 case ISD::CONCAT_VECTORS: return "concat_vectors";
4565 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
4566 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
4567 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
4568 case ISD::CARRY_FALSE: return "carry_false";
4569 case ISD::ADDC: return "addc";
4570 case ISD::ADDE: return "adde";
4571 case ISD::SUBC: return "subc";
4572 case ISD::SUBE: return "sube";
4573 case ISD::SHL_PARTS: return "shl_parts";
4574 case ISD::SRA_PARTS: return "sra_parts";
4575 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lambb768c2e2007-07-26 07:34:40 +00004576
4577 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4578 case ISD::INSERT_SUBREG: return "insert_subreg";
4579
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 // Conversion operators.
4581 case ISD::SIGN_EXTEND: return "sign_extend";
4582 case ISD::ZERO_EXTEND: return "zero_extend";
4583 case ISD::ANY_EXTEND: return "any_extend";
4584 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
4585 case ISD::TRUNCATE: return "truncate";
4586 case ISD::FP_ROUND: return "fp_round";
Dan Gohman819574c2008-01-31 00:41:03 +00004587 case ISD::FLT_ROUNDS_: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004588 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
4589 case ISD::FP_EXTEND: return "fp_extend";
4590
4591 case ISD::SINT_TO_FP: return "sint_to_fp";
4592 case ISD::UINT_TO_FP: return "uint_to_fp";
4593 case ISD::FP_TO_SINT: return "fp_to_sint";
4594 case ISD::FP_TO_UINT: return "fp_to_uint";
4595 case ISD::BIT_CONVERT: return "bit_convert";
4596
4597 // Control flow instructions
4598 case ISD::BR: return "br";
4599 case ISD::BRIND: return "brind";
4600 case ISD::BR_JT: return "br_jt";
4601 case ISD::BRCOND: return "brcond";
4602 case ISD::BR_CC: return "br_cc";
4603 case ISD::RET: return "ret";
4604 case ISD::CALLSEQ_START: return "callseq_start";
4605 case ISD::CALLSEQ_END: return "callseq_end";
4606
4607 // Other operators
4608 case ISD::LOAD: return "load";
4609 case ISD::STORE: return "store";
4610 case ISD::VAARG: return "vaarg";
4611 case ISD::VACOPY: return "vacopy";
4612 case ISD::VAEND: return "vaend";
4613 case ISD::VASTART: return "vastart";
4614 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
4615 case ISD::EXTRACT_ELEMENT: return "extract_element";
4616 case ISD::BUILD_PAIR: return "build_pair";
4617 case ISD::STACKSAVE: return "stacksave";
4618 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004619 case ISD::TRAP: return "trap";
4620
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621 // Bit manipulation
4622 case ISD::BSWAP: return "bswap";
4623 case ISD::CTPOP: return "ctpop";
4624 case ISD::CTTZ: return "cttz";
4625 case ISD::CTLZ: return "ctlz";
4626
4627 // Debug info
4628 case ISD::LOCATION: return "location";
4629 case ISD::DEBUG_LOC: return "debug_loc";
4630
Duncan Sands38947cd2007-07-27 12:58:54 +00004631 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00004632 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00004633
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634 case ISD::CONDCODE:
4635 switch (cast<CondCodeSDNode>(this)->get()) {
4636 default: assert(0 && "Unknown setcc condition!");
4637 case ISD::SETOEQ: return "setoeq";
4638 case ISD::SETOGT: return "setogt";
4639 case ISD::SETOGE: return "setoge";
4640 case ISD::SETOLT: return "setolt";
4641 case ISD::SETOLE: return "setole";
4642 case ISD::SETONE: return "setone";
4643
4644 case ISD::SETO: return "seto";
4645 case ISD::SETUO: return "setuo";
4646 case ISD::SETUEQ: return "setue";
4647 case ISD::SETUGT: return "setugt";
4648 case ISD::SETUGE: return "setuge";
4649 case ISD::SETULT: return "setult";
4650 case ISD::SETULE: return "setule";
4651 case ISD::SETUNE: return "setune";
4652
4653 case ISD::SETEQ: return "seteq";
4654 case ISD::SETGT: return "setgt";
4655 case ISD::SETGE: return "setge";
4656 case ISD::SETLT: return "setlt";
4657 case ISD::SETLE: return "setle";
4658 case ISD::SETNE: return "setne";
4659 }
4660 }
4661}
4662
4663const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
4664 switch (AM) {
4665 default:
4666 return "";
4667 case ISD::PRE_INC:
4668 return "<pre-inc>";
4669 case ISD::PRE_DEC:
4670 return "<pre-dec>";
4671 case ISD::POST_INC:
4672 return "<post-inc>";
4673 case ISD::POST_DEC:
4674 return "<post-dec>";
4675 }
4676}
4677
Duncan Sandsc93fae32008-03-21 09:14:45 +00004678std::string ISD::ArgFlagsTy::getArgFlagsString() {
4679 std::string S = "< ";
4680
4681 if (isZExt())
4682 S += "zext ";
4683 if (isSExt())
4684 S += "sext ";
4685 if (isInReg())
4686 S += "inreg ";
4687 if (isSRet())
4688 S += "sret ";
4689 if (isByVal())
4690 S += "byval ";
4691 if (isNest())
4692 S += "nest ";
4693 if (getByValAlign())
4694 S += "byval-align:" + utostr(getByValAlign()) + " ";
4695 if (getOrigAlign())
4696 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4697 if (getByValSize())
4698 S += "byval-size:" + utostr(getByValSize()) + " ";
4699 return S + ">";
4700}
4701
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004702void SDNode::dump() const { dump(0); }
4703void SDNode::dump(const SelectionDAG *G) const {
4704 cerr << (void*)this << ": ";
4705
4706 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
4707 if (i) cerr << ",";
4708 if (getValueType(i) == MVT::Other)
4709 cerr << "ch";
4710 else
Duncan Sands92c43912008-06-06 12:08:01 +00004711 cerr << getValueType(i).getMVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004712 }
4713 cerr << " = " << getOperationName(G);
4714
4715 cerr << " ";
4716 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
4717 if (i) cerr << ", ";
4718 cerr << (void*)getOperand(i).Val;
4719 if (unsigned RN = getOperand(i).ResNo)
4720 cerr << ":" << RN;
4721 }
4722
Evan Chengaad43a02007-12-11 02:08:35 +00004723 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4724 SDNode *Mask = getOperand(2).Val;
4725 cerr << "<";
4726 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4727 if (i) cerr << ",";
4728 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4729 cerr << "u";
4730 else
4731 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4732 }
4733 cerr << ">";
4734 }
4735
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004736 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
4737 cerr << "<" << CSDN->getValue() << ">";
4738 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00004739 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4740 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4741 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4742 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4743 else {
4744 cerr << "<APFloat(";
4745 CSDN->getValueAPF().convertToAPInt().dump();
4746 cerr << ")>";
4747 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748 } else if (const GlobalAddressSDNode *GADN =
4749 dyn_cast<GlobalAddressSDNode>(this)) {
4750 int offset = GADN->getOffset();
4751 cerr << "<";
4752 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
4753 if (offset > 0)
4754 cerr << " + " << offset;
4755 else
4756 cerr << " " << offset;
4757 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
4758 cerr << "<" << FIDN->getIndex() << ">";
4759 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
4760 cerr << "<" << JTDN->getIndex() << ">";
4761 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
4762 int offset = CP->getOffset();
4763 if (CP->isMachineConstantPoolEntry())
4764 cerr << "<" << *CP->getMachineCPVal() << ">";
4765 else
4766 cerr << "<" << *CP->getConstVal() << ">";
4767 if (offset > 0)
4768 cerr << " + " << offset;
4769 else
4770 cerr << " " << offset;
4771 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
4772 cerr << "<";
4773 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4774 if (LBB)
4775 cerr << LBB->getName() << " ";
4776 cerr << (const void*)BBDN->getBasicBlock() << ">";
4777 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman1e57df32008-02-10 18:45:23 +00004778 if (G && R->getReg() &&
4779 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling9b0baeb2008-02-26 21:47:57 +00004780 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004781 } else {
4782 cerr << " #" << R->getReg();
4783 }
4784 } else if (const ExternalSymbolSDNode *ES =
4785 dyn_cast<ExternalSymbolSDNode>(this)) {
4786 cerr << "'" << ES->getSymbol() << "'";
4787 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4788 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +00004789 cerr << "<" << M->getValue() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790 else
Dan Gohman12a9c082008-02-06 22:27:42 +00004791 cerr << "<null>";
4792 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4793 if (M->MO.getValue())
4794 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4795 else
4796 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sandsc93fae32008-03-21 09:14:45 +00004797 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4798 cerr << N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004800 cerr << ":" << N->getVT().getMVTString();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004801 }
4802 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng034c4f82007-12-18 19:06:30 +00004803 const Value *SrcValue = LD->getSrcValue();
4804 int SrcOffset = LD->getSrcValueOffset();
4805 cerr << " <";
4806 if (SrcValue)
4807 cerr << SrcValue;
4808 else
4809 cerr << "null";
4810 cerr << ":" << SrcOffset << ">";
4811
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 bool doExt = true;
4813 switch (LD->getExtensionType()) {
4814 default: doExt = false; break;
4815 case ISD::EXTLOAD:
4816 cerr << " <anyext ";
4817 break;
4818 case ISD::SEXTLOAD:
4819 cerr << " <sext ";
4820 break;
4821 case ISD::ZEXTLOAD:
4822 cerr << " <zext ";
4823 break;
4824 }
4825 if (doExt)
Duncan Sands92c43912008-06-06 12:08:01 +00004826 cerr << LD->getMemoryVT().getMVTString() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827
4828 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00004829 if (*AM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 cerr << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00004831 if (LD->isVolatile())
4832 cerr << " <volatile>";
4833 cerr << " alignment=" << LD->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng7196a7b2007-12-18 07:02:08 +00004835 const Value *SrcValue = ST->getSrcValue();
4836 int SrcOffset = ST->getSrcValueOffset();
4837 cerr << " <";
4838 if (SrcValue)
4839 cerr << SrcValue;
4840 else
4841 cerr << "null";
4842 cerr << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00004843
4844 if (ST->isTruncatingStore())
4845 cerr << " <trunc "
Duncan Sands92c43912008-06-06 12:08:01 +00004846 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00004847
4848 const char *AM = getIndexedModeName(ST->getAddressingMode());
4849 if (*AM)
4850 cerr << " " << AM;
4851 if (ST->isVolatile())
4852 cerr << " <volatile>";
4853 cerr << " alignment=" << ST->getAlignment();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00004854 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4855 const Value *SrcValue = AT->getSrcValue();
4856 int SrcOffset = AT->getSrcValueOffset();
4857 cerr << " <";
4858 if (SrcValue)
4859 cerr << SrcValue;
4860 else
4861 cerr << "null";
4862 cerr << ":" << SrcOffset << ">";
4863 if (AT->isVolatile())
4864 cerr << " <volatile>";
4865 cerr << " alignment=" << AT->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004866 }
4867}
4868
4869static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
4870 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4871 if (N->getOperand(i).Val->hasOneUse())
4872 DumpNodes(N->getOperand(i).Val, indent+2, G);
4873 else
4874 cerr << "\n" << std::string(indent+2, ' ')
4875 << (void*)N->getOperand(i).Val << ": <multiple use>";
4876
4877
4878 cerr << "\n" << std::string(indent, ' ');
4879 N->dump(G);
4880}
4881
4882void SelectionDAG::dump() const {
4883 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
4884 std::vector<const SDNode*> Nodes;
4885 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4886 I != E; ++I)
4887 Nodes.push_back(I);
4888
4889 std::sort(Nodes.begin(), Nodes.end());
4890
4891 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4892 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
4893 DumpNodes(Nodes[i], 2, this);
4894 }
4895
4896 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4897
4898 cerr << "\n\n";
4899}
4900
4901const Type *ConstantPoolSDNode::getType() const {
4902 if (isMachineConstantPoolEntry())
4903 return Val.MachineCPVal->getType();
4904 return Val.ConstVal->getType();
4905}