blob: 83e773ed313d4454107272ed24255df3c442ae1b [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"
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +000015#include "llvm/GlobalAlias.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/GlobalVariable.h"
17#include "llvm/Intrinsics.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Assembly/Writer.h"
Dan Gohmane8b391e2008-04-12 04:36:06 +000020#include "llvm/CallingConv.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
22#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner53f5aee2007-10-15 17:47:20 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Support/MathExtras.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
30#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/ADT/SetVector.h"
33#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsa9810f32007-10-16 09:56:48 +000034#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/ADT/SmallVector.h"
36#include "llvm/ADT/StringExtras.h"
37#include <algorithm>
38#include <cmath>
39using namespace llvm;
40
41/// makeVTList - Return an instance of the SDVTList struct initialized with the
42/// specified members.
43static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
44 SDVTList Res = {VTs, NumVTs};
45 return Res;
46}
47
Chris Lattnerd037d482008-03-05 06:48:13 +000048static const fltSemantics *MVTToAPFloatSemantics(MVT::ValueType VT) {
49 switch (VT) {
50 default: assert(0 && "Unknown FP format");
51 case MVT::f32: return &APFloat::IEEEsingle;
52 case MVT::f64: return &APFloat::IEEEdouble;
53 case MVT::f80: return &APFloat::x87DoubleExtended;
54 case MVT::f128: return &APFloat::IEEEquad;
55 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
56 }
57}
58
Chris Lattner7bcb18f2008-02-03 06:49:24 +000059SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
60
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
Dale Johannesenc53301c2007-08-26 01:18:27 +000069bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen7f2c1d12007-08-25 22:10:57 +000070 return Value.bitwiseIsEqual(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071}
72
Dale Johannesenbbe2b702007-08-30 00:23:21 +000073bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
74 const APFloat& Val) {
Chris Lattnerd037d482008-03-05 06:48:13 +000075 assert(MVT::isFloatingPoint(VT) && "Can only convert between FP types");
76
Dale Johannesen90c25d12008-04-20 18:23:46 +000077 // PPC long double cannot be converted to any other type.
78 if (VT == MVT::ppcf128 ||
79 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerd037d482008-03-05 06:48:13 +000080 return false;
81
Dale Johannesenbbe2b702007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Chris Lattnerd037d482008-03-05 06:48:13 +000084 return Val2.convert(*MVTToAPFloatSemantics(VT),
85 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenbbe2b702007-08-30 00:23:21 +000086}
87
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088//===----------------------------------------------------------------------===//
89// ISD Namespace
90//===----------------------------------------------------------------------===//
91
92/// isBuildVectorAllOnes - Return true if the specified node is a
93/// BUILD_VECTOR where all of the elements are ~0 or undef.
94bool ISD::isBuildVectorAllOnes(const SDNode *N) {
95 // Look through a bit convert.
96 if (N->getOpcode() == ISD::BIT_CONVERT)
97 N = N->getOperand(0).Val;
98
99 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
100
101 unsigned i = 0, e = N->getNumOperands();
102
103 // Skip over all of the undef values.
104 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
105 ++i;
106
107 // Do not accept an all-undef vector.
108 if (i == e) return false;
109
110 // Do not accept build_vectors that aren't all constants or which have non-~0
111 // elements.
112 SDOperand NotZero = N->getOperand(i);
113 if (isa<ConstantSDNode>(NotZero)) {
114 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
115 return false;
116 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman161652c2008-02-29 01:47:35 +0000117 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
118 convertToAPInt().isAllOnesValue())
119 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 } else
121 return false;
122
123 // Okay, we have at least one ~0 value, check to see if the rest match or are
124 // undefs.
125 for (++i; i != e; ++i)
126 if (N->getOperand(i) != NotZero &&
127 N->getOperand(i).getOpcode() != ISD::UNDEF)
128 return false;
129 return true;
130}
131
132
133/// isBuildVectorAllZeros - Return true if the specified node is a
134/// BUILD_VECTOR where all of the elements are 0 or undef.
135bool ISD::isBuildVectorAllZeros(const SDNode *N) {
136 // Look through a bit convert.
137 if (N->getOpcode() == ISD::BIT_CONVERT)
138 N = N->getOperand(0).Val;
139
140 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
141
142 unsigned i = 0, e = N->getNumOperands();
143
144 // Skip over all of the undef values.
145 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
146 ++i;
147
148 // Do not accept an all-undef vector.
149 if (i == e) return false;
150
151 // Do not accept build_vectors that aren't all constants or which have non-~0
152 // elements.
153 SDOperand Zero = N->getOperand(i);
154 if (isa<ConstantSDNode>(Zero)) {
155 if (!cast<ConstantSDNode>(Zero)->isNullValue())
156 return false;
157 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000158 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 return false;
160 } else
161 return false;
162
163 // Okay, we have at least one ~0 value, check to see if the rest match or are
164 // undefs.
165 for (++i; i != e; ++i)
166 if (N->getOperand(i) != Zero &&
167 N->getOperand(i).getOpcode() != ISD::UNDEF)
168 return false;
169 return true;
170}
171
Evan Chengd1045a62008-02-18 23:04:32 +0000172/// isScalarToVector - Return true if the specified node is a
173/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
174/// element is not an undef.
175bool ISD::isScalarToVector(const SDNode *N) {
176 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
177 return true;
178
179 if (N->getOpcode() != ISD::BUILD_VECTOR)
180 return false;
181 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
182 return false;
183 unsigned NumElems = N->getNumOperands();
184 for (unsigned i = 1; i < NumElems; ++i) {
185 SDOperand V = N->getOperand(i);
186 if (V.getOpcode() != ISD::UNDEF)
187 return false;
188 }
189 return true;
190}
191
192
Evan Cheng13d1c292008-01-31 09:59:15 +0000193/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengee6db0f2008-02-04 23:10:38 +0000194/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Cheng13d1c292008-01-31 09:59:15 +0000195/// is 0).
196bool ISD::isDebugLabel(const SDNode *N) {
197 SDOperand Zero;
198 if (N->getOpcode() == ISD::LABEL)
199 Zero = N->getOperand(2);
200 else if (N->isTargetOpcode() &&
201 N->getTargetOpcode() == TargetInstrInfo::LABEL)
202 // Chain moved to last operand.
203 Zero = N->getOperand(1);
204 else
205 return false;
206 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
207}
208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
210/// when given the operation for (X op Y).
211ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
212 // To perform this operation, we just need to swap the L and G bits of the
213 // operation.
214 unsigned OldL = (Operation >> 2) & 1;
215 unsigned OldG = (Operation >> 1) & 1;
216 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
217 (OldL << 1) | // New G bit
218 (OldG << 2)); // New L bit.
219}
220
221/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
222/// 'op' is a valid SetCC operation.
223ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
224 unsigned Operation = Op;
225 if (isInteger)
226 Operation ^= 7; // Flip L, G, E bits, but not U.
227 else
228 Operation ^= 15; // Flip all of the condition bits.
229 if (Operation > ISD::SETTRUE2)
230 Operation &= ~8; // Don't let N and U bits get set.
231 return ISD::CondCode(Operation);
232}
233
234
235/// isSignedOp - For an integer comparison, return 1 if the comparison is a
236/// signed operation and 2 if the result is an unsigned comparison. Return zero
237/// if the operation does not depend on the sign of the input (setne and seteq).
238static int isSignedOp(ISD::CondCode Opcode) {
239 switch (Opcode) {
240 default: assert(0 && "Illegal integer setcc operation!");
241 case ISD::SETEQ:
242 case ISD::SETNE: return 0;
243 case ISD::SETLT:
244 case ISD::SETLE:
245 case ISD::SETGT:
246 case ISD::SETGE: return 1;
247 case ISD::SETULT:
248 case ISD::SETULE:
249 case ISD::SETUGT:
250 case ISD::SETUGE: return 2;
251 }
252}
253
254/// getSetCCOrOperation - Return the result of a logical OR between different
255/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
256/// returns SETCC_INVALID if it is not possible to represent the resultant
257/// comparison.
258ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
259 bool isInteger) {
260 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
261 // Cannot fold a signed integer setcc with an unsigned integer setcc.
262 return ISD::SETCC_INVALID;
263
264 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
265
266 // If the N and U bits get set then the resultant comparison DOES suddenly
267 // care about orderedness, and is true when ordered.
268 if (Op > ISD::SETTRUE2)
269 Op &= ~16; // Clear the U bit if the N bit is set.
270
271 // Canonicalize illegal integer setcc's.
272 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
273 Op = ISD::SETNE;
274
275 return ISD::CondCode(Op);
276}
277
278/// getSetCCAndOperation - Return the result of a logical AND between different
279/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
280/// function returns zero if it is not possible to represent the resultant
281/// comparison.
282ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
283 bool isInteger) {
284 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
285 // Cannot fold a signed setcc with an unsigned setcc.
286 return ISD::SETCC_INVALID;
287
288 // Combine all of the condition bits.
289 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
290
291 // Canonicalize illegal integer setcc's.
292 if (isInteger) {
293 switch (Result) {
294 default: break;
295 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmanf1e8e552008-05-14 18:17:09 +0000296 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
298 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
299 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
300 }
301 }
302
303 return Result;
304}
305
306const TargetMachine &SelectionDAG::getTarget() const {
307 return TLI.getTargetMachine();
308}
309
310//===----------------------------------------------------------------------===//
311// SDNode Profile Support
312//===----------------------------------------------------------------------===//
313
314/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
315///
316static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
317 ID.AddInteger(OpC);
318}
319
320/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
321/// solely with their pointer.
Dan Gohman089efff2008-05-13 00:00:25 +0000322static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 ID.AddPointer(VTList.VTs);
324}
325
326/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
327///
328static void AddNodeIDOperands(FoldingSetNodeID &ID,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000329 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 for (; NumOps; --NumOps, ++Ops) {
331 ID.AddPointer(Ops->Val);
332 ID.AddInteger(Ops->ResNo);
333 }
334}
335
336static void AddNodeIDNode(FoldingSetNodeID &ID,
337 unsigned short OpC, SDVTList VTList,
Dan Gohman1d47f5c2008-04-17 23:02:12 +0000338 SDOperandPtr OpList, unsigned N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 AddNodeIDOpcode(ID, OpC);
340 AddNodeIDValueTypes(ID, VTList);
341 AddNodeIDOperands(ID, OpList, N);
342}
343
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000344
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
346/// data.
347static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
348 AddNodeIDOpcode(ID, N->getOpcode());
349 // Add the return value info.
350 AddNodeIDValueTypes(ID, N->getVTList());
351 // Add the operand info.
352 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
353
354 // Handle SDNode leafs with special info.
355 switch (N->getOpcode()) {
356 default: break; // Normal nodes don't need extra info.
Duncan Sandsc93fae32008-03-21 09:14:45 +0000357 case ISD::ARG_FLAGS:
358 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
359 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 case ISD::TargetConstant:
361 case ISD::Constant:
Chris Lattnerf5e3e182008-02-20 06:28:01 +0000362 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 break;
364 case ISD::TargetConstantFP:
Dale Johannesendf8a8312007-08-31 04:03:46 +0000365 case ISD::ConstantFP: {
Ted Kremenekdc71c802008-02-11 17:24:50 +0000366 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 break;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000368 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 case ISD::TargetGlobalAddress:
370 case ISD::GlobalAddress:
371 case ISD::TargetGlobalTLSAddress:
372 case ISD::GlobalTLSAddress: {
373 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
374 ID.AddPointer(GA->getGlobal());
375 ID.AddInteger(GA->getOffset());
376 break;
377 }
378 case ISD::BasicBlock:
379 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
380 break;
381 case ISD::Register:
382 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
383 break;
Dan Gohman12a9c082008-02-06 22:27:42 +0000384 case ISD::SRCVALUE:
385 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
386 break;
387 case ISD::MEMOPERAND: {
Dan Gohman1fad9e62008-04-07 19:35:22 +0000388 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman12a9c082008-02-06 22:27:42 +0000389 ID.AddPointer(MO.getValue());
390 ID.AddInteger(MO.getFlags());
391 ID.AddInteger(MO.getOffset());
392 ID.AddInteger(MO.getSize());
393 ID.AddInteger(MO.getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 break;
395 }
396 case ISD::FrameIndex:
397 case ISD::TargetFrameIndex:
398 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
399 break;
400 case ISD::JumpTable:
401 case ISD::TargetJumpTable:
402 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
403 break;
404 case ISD::ConstantPool:
405 case ISD::TargetConstantPool: {
406 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
407 ID.AddInteger(CP->getAlignment());
408 ID.AddInteger(CP->getOffset());
409 if (CP->isMachineConstantPoolEntry())
410 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
411 else
412 ID.AddPointer(CP->getConstVal());
413 break;
414 }
415 case ISD::LOAD: {
416 LoadSDNode *LD = cast<LoadSDNode>(N);
417 ID.AddInteger(LD->getAddressingMode());
418 ID.AddInteger(LD->getExtensionType());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000419 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 ID.AddInteger(LD->getAlignment());
421 ID.AddInteger(LD->isVolatile());
422 break;
423 }
424 case ISD::STORE: {
425 StoreSDNode *ST = cast<StoreSDNode>(N);
426 ID.AddInteger(ST->getAddressingMode());
427 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000428 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 ID.AddInteger(ST->getAlignment());
430 ID.AddInteger(ST->isVolatile());
431 break;
432 }
433 }
434}
435
436//===----------------------------------------------------------------------===//
437// SelectionDAG Class
438//===----------------------------------------------------------------------===//
439
440/// RemoveDeadNodes - This method deletes all unreachable nodes in the
441/// SelectionDAG.
442void SelectionDAG::RemoveDeadNodes() {
443 // Create a dummy node (which is not added to allnodes), that adds a reference
444 // to the root node, preventing it from being deleted.
445 HandleSDNode Dummy(getRoot());
446
447 SmallVector<SDNode*, 128> DeadNodes;
448
449 // Add all obviously-dead nodes to the DeadNodes worklist.
450 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
451 if (I->use_empty())
452 DeadNodes.push_back(I);
453
454 // Process the worklist, deleting the nodes and adding their uses to the
455 // worklist.
456 while (!DeadNodes.empty()) {
457 SDNode *N = DeadNodes.back();
458 DeadNodes.pop_back();
459
460 // Take the node out of the appropriate CSE map.
461 RemoveNodeFromCSEMaps(N);
462
463 // Next, brutally remove the operand list. This is safe to do, as there are
464 // no cycles in the graph.
465 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000466 SDNode *Operand = I->getVal();
Roman Levenstein05650fd2008-04-07 10:06:32 +0000467 Operand->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468
469 // Now that we removed this operand, see if there are no uses of it left.
470 if (Operand->use_empty())
471 DeadNodes.push_back(Operand);
472 }
Roman Levenstein05650fd2008-04-07 10:06:32 +0000473 if (N->OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 delete[] N->OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +0000475 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 N->OperandList = 0;
477 N->NumOperands = 0;
478
479 // Finally, remove N itself.
480 AllNodes.erase(N);
481 }
482
483 // If the root changed (e.g. it was a dead load, update the root).
484 setRoot(Dummy.getValue());
485}
486
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000487void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 SmallVector<SDNode*, 16> DeadNodes;
489 DeadNodes.push_back(N);
490
491 // Process the worklist, deleting the nodes and adding their uses to the
492 // worklist.
493 while (!DeadNodes.empty()) {
494 SDNode *N = DeadNodes.back();
495 DeadNodes.pop_back();
496
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000497 if (UpdateListener)
498 UpdateListener->NodeDeleted(N);
499
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 // Take the node out of the appropriate CSE map.
501 RemoveNodeFromCSEMaps(N);
502
503 // Next, brutally remove the operand list. This is safe to do, as there are
504 // no cycles in the graph.
505 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000506 SDNode *Operand = I->getVal();
Roman Levenstein05650fd2008-04-07 10:06:32 +0000507 Operand->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508
509 // Now that we removed this operand, see if there are no uses of it left.
510 if (Operand->use_empty())
511 DeadNodes.push_back(Operand);
512 }
Roman Levenstein05650fd2008-04-07 10:06:32 +0000513 if (N->OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 delete[] N->OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +0000515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 N->OperandList = 0;
517 N->NumOperands = 0;
518
519 // Finally, remove N itself.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 AllNodes.erase(N);
521 }
522}
523
524void SelectionDAG::DeleteNode(SDNode *N) {
525 assert(N->use_empty() && "Cannot delete a node that is not dead!");
526
527 // First take this out of the appropriate CSE map.
528 RemoveNodeFromCSEMaps(N);
529
530 // Finally, remove uses due to operands of this node, remove from the
531 // AllNodes list, and delete the node.
532 DeleteNodeNotInCSEMaps(N);
533}
534
535void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
536
537 // Remove it from the AllNodes list.
538 AllNodes.remove(N);
539
540 // Drop all of the operands and decrement used nodes use counts.
541 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000542 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levenstein05650fd2008-04-07 10:06:32 +0000543 if (N->OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 delete[] N->OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +0000545 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 N->OperandList = 0;
547 N->NumOperands = 0;
548
549 delete N;
550}
551
552/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
553/// correspond to it. This is useful when we're about to delete or repurpose
554/// the node. We don't want future request for structurally identical nodes
555/// to return N anymore.
556void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
557 bool Erased = false;
558 switch (N->getOpcode()) {
559 case ISD::HANDLENODE: return; // noop.
560 case ISD::STRING:
561 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
562 break;
563 case ISD::CONDCODE:
564 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
565 "Cond code doesn't exist!");
566 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
567 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
568 break;
569 case ISD::ExternalSymbol:
570 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
571 break;
572 case ISD::TargetExternalSymbol:
573 Erased =
574 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
575 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000576 case ISD::VALUETYPE: {
577 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
578 if (MVT::isExtendedVT(VT)) {
579 Erased = ExtendedValueTypeNodes.erase(VT);
580 } else {
581 Erased = ValueTypeNodes[VT] != 0;
582 ValueTypeNodes[VT] = 0;
583 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000585 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 default:
587 // Remove it from the CSE Map.
588 Erased = CSEMap.RemoveNode(N);
589 break;
590 }
591#ifndef NDEBUG
592 // Verify that the node was actually in one of the CSE maps, unless it has a
593 // flag result (which cannot be CSE'd) or is one of the special cases that are
594 // not subject to CSE.
595 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
596 !N->isTargetOpcode()) {
597 N->dump(this);
598 cerr << "\n";
599 assert(0 && "Node is not in map!");
600 }
601#endif
602}
603
604/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
605/// has been taken out and modified in some way. If the specified node already
606/// exists in the CSE maps, do not modify the maps, but return the existing node
607/// instead. If it doesn't exist, add it and return null.
608///
609SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
610 assert(N->getNumOperands() && "This is a leaf node!");
611 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
612 return 0; // Never add these nodes.
613
614 // Check that remaining values produced are not flags.
615 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
616 if (N->getValueType(i) == MVT::Flag)
617 return 0; // Never CSE anything that produces a flag.
618
619 SDNode *New = CSEMap.GetOrInsertNode(N);
620 if (New != N) return New; // Node already existed.
621 return 0;
622}
623
624/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
625/// were replaced with those specified. If this node is never memoized,
626/// return null, otherwise return a pointer to the slot it would take. If a
627/// node already exists with these operands, the slot will be non-null.
628SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
629 void *&InsertPos) {
630 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
631 return 0; // Never add these nodes.
632
633 // Check that remaining values produced are not flags.
634 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
635 if (N->getValueType(i) == MVT::Flag)
636 return 0; // Never CSE anything that produces a flag.
637
638 SDOperand Ops[] = { Op };
639 FoldingSetNodeID ID;
640 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
641 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
642}
643
644/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
645/// were replaced with those specified. If this node is never memoized,
646/// return null, otherwise return a pointer to the slot it would take. If a
647/// node already exists with these operands, the slot will be non-null.
648SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
649 SDOperand Op1, SDOperand Op2,
650 void *&InsertPos) {
651 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
652 return 0; // Never add these nodes.
653
654 // Check that remaining values produced are not flags.
655 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
656 if (N->getValueType(i) == MVT::Flag)
657 return 0; // Never CSE anything that produces a flag.
658
659 SDOperand Ops[] = { Op1, Op2 };
660 FoldingSetNodeID ID;
661 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
662 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
663}
664
665
666/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
667/// were replaced with those specified. If this node is never memoized,
668/// return null, otherwise return a pointer to the slot it would take. If a
669/// node already exists with these operands, the slot will be non-null.
670SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000671 SDOperandPtr Ops,unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 void *&InsertPos) {
673 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
674 return 0; // Never add these nodes.
675
676 // Check that remaining values produced are not flags.
677 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
678 if (N->getValueType(i) == MVT::Flag)
679 return 0; // Never CSE anything that produces a flag.
680
681 FoldingSetNodeID ID;
682 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
683
684 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
685 ID.AddInteger(LD->getAddressingMode());
686 ID.AddInteger(LD->getExtensionType());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000687 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 ID.AddInteger(LD->getAlignment());
689 ID.AddInteger(LD->isVolatile());
690 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
691 ID.AddInteger(ST->getAddressingMode());
692 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000693 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 ID.AddInteger(ST->getAlignment());
695 ID.AddInteger(ST->isVolatile());
696 }
697
698 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
699}
700
701
702SelectionDAG::~SelectionDAG() {
703 while (!AllNodes.empty()) {
704 SDNode *N = AllNodes.begin();
705 N->SetNextInBucket(0);
Roman Levenstein05650fd2008-04-07 10:06:32 +0000706 if (N->OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 delete [] N->OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +0000708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 N->OperandList = 0;
710 N->NumOperands = 0;
711 AllNodes.pop_front();
712 }
713}
714
715SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
716 if (Op.getValueType() == VT) return Op;
Dan Gohman161652c2008-02-29 01:47:35 +0000717 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
718 MVT::getSizeInBits(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 return getNode(ISD::AND, Op.getValueType(), Op,
720 getConstant(Imm, Op.getValueType()));
721}
722
723SDOperand SelectionDAG::getString(const std::string &Val) {
724 StringSDNode *&N = StringNodes[Val];
725 if (!N) {
726 N = new StringSDNode(Val);
727 AllNodes.push_back(N);
728 }
729 return SDOperand(N, 0);
730}
731
732SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Dan Gohmandc458cf2008-02-08 22:59:30 +0000733 MVT::ValueType EltVT =
734 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
735
736 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
737}
738
739SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000741
742 MVT::ValueType EltVT =
743 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744
Dan Gohmandc458cf2008-02-08 22:59:30 +0000745 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
746 "APInt size does not match type size!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747
748 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
749 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000750 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenekdc71c802008-02-11 17:24:50 +0000751 ID.Add(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000753 SDNode *N = NULL;
754 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
755 if (!MVT::isVector(VT))
756 return SDOperand(N, 0);
757 if (!N) {
758 N = new ConstantSDNode(isT, Val, EltVT);
759 CSEMap.InsertNode(N, IP);
760 AllNodes.push_back(N);
761 }
762
763 SDOperand Result(N, 0);
764 if (MVT::isVector(VT)) {
765 SmallVector<SDOperand, 8> Ops;
766 Ops.assign(MVT::getVectorNumElements(VT), Result);
767 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
768 }
769 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770}
771
Chris Lattner5872a362008-01-17 07:00:52 +0000772SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
773 return getConstant(Val, TLI.getPointerTy(), isTarget);
774}
775
776
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000777SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 bool isTarget) {
779 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000780
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 MVT::ValueType EltVT =
782 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783
784 // Do the map lookup using the actual bit pattern for the floating point
785 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
786 // we don't have issues with SNANs.
787 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
788 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000789 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenekdc71c802008-02-11 17:24:50 +0000790 ID.Add(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 void *IP = 0;
792 SDNode *N = NULL;
793 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
794 if (!MVT::isVector(VT))
795 return SDOperand(N, 0);
796 if (!N) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000797 N = new ConstantFPSDNode(isTarget, V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 CSEMap.InsertNode(N, IP);
799 AllNodes.push_back(N);
800 }
801
802 SDOperand Result(N, 0);
803 if (MVT::isVector(VT)) {
804 SmallVector<SDOperand, 8> Ops;
805 Ops.assign(MVT::getVectorNumElements(VT), Result);
806 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
807 }
808 return Result;
809}
810
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000811SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
812 bool isTarget) {
813 MVT::ValueType EltVT =
814 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
815 if (EltVT==MVT::f32)
816 return getConstantFP(APFloat((float)Val), VT, isTarget);
817 else
818 return getConstantFP(APFloat(Val), VT, isTarget);
819}
820
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
822 MVT::ValueType VT, int Offset,
823 bool isTargetGA) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 unsigned Opc;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000825
826 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
827 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000828 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000829 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
830 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
831 }
832
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 if (GVar && GVar->isThreadLocal())
834 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
835 else
836 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000837
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000839 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 ID.AddPointer(GV);
841 ID.AddInteger(Offset);
842 void *IP = 0;
843 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
844 return SDOperand(E, 0);
845 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
846 CSEMap.InsertNode(N, IP);
847 AllNodes.push_back(N);
848 return SDOperand(N, 0);
849}
850
851SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
852 bool isTarget) {
853 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
854 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000855 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 ID.AddInteger(FI);
857 void *IP = 0;
858 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
859 return SDOperand(E, 0);
860 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
861 CSEMap.InsertNode(N, IP);
862 AllNodes.push_back(N);
863 return SDOperand(N, 0);
864}
865
866SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
867 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
868 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000869 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 ID.AddInteger(JTI);
871 void *IP = 0;
872 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
873 return SDOperand(E, 0);
874 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
875 CSEMap.InsertNode(N, IP);
876 AllNodes.push_back(N);
877 return SDOperand(N, 0);
878}
879
880SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
881 unsigned Alignment, int Offset,
882 bool isTarget) {
883 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
884 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000885 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 ID.AddInteger(Alignment);
887 ID.AddInteger(Offset);
888 ID.AddPointer(C);
889 void *IP = 0;
890 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
891 return SDOperand(E, 0);
892 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
893 CSEMap.InsertNode(N, IP);
894 AllNodes.push_back(N);
895 return SDOperand(N, 0);
896}
897
898
899SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
900 MVT::ValueType VT,
901 unsigned Alignment, int Offset,
902 bool isTarget) {
903 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
904 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000905 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 ID.AddInteger(Alignment);
907 ID.AddInteger(Offset);
908 C->AddSelectionDAGCSEId(ID);
909 void *IP = 0;
910 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
911 return SDOperand(E, 0);
912 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
913 CSEMap.InsertNode(N, IP);
914 AllNodes.push_back(N);
915 return SDOperand(N, 0);
916}
917
918
919SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
920 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000921 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 ID.AddPointer(MBB);
923 void *IP = 0;
924 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
925 return SDOperand(E, 0);
926 SDNode *N = new BasicBlockSDNode(MBB);
927 CSEMap.InsertNode(N, IP);
928 AllNodes.push_back(N);
929 return SDOperand(N, 0);
930}
931
Duncan Sandsc93fae32008-03-21 09:14:45 +0000932SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
933 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000934 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sandsc93fae32008-03-21 09:14:45 +0000935 ID.AddInteger(Flags.getRawBits());
936 void *IP = 0;
937 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
938 return SDOperand(E, 0);
939 SDNode *N = new ARG_FLAGSSDNode(Flags);
940 CSEMap.InsertNode(N, IP);
941 AllNodes.push_back(N);
942 return SDOperand(N, 0);
943}
944
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000946 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 ValueTypeNodes.resize(VT+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948
Duncan Sandsd7307a92007-10-17 13:49:58 +0000949 SDNode *&N = MVT::isExtendedVT(VT) ?
950 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
951
952 if (N) return SDOperand(N, 0);
953 N = new VTSDNode(VT);
954 AllNodes.push_back(N);
955 return SDOperand(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956}
957
958SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
959 SDNode *&N = ExternalSymbols[Sym];
960 if (N) return SDOperand(N, 0);
961 N = new ExternalSymbolSDNode(false, Sym, VT);
962 AllNodes.push_back(N);
963 return SDOperand(N, 0);
964}
965
966SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
967 MVT::ValueType VT) {
968 SDNode *&N = TargetExternalSymbols[Sym];
969 if (N) return SDOperand(N, 0);
970 N = new ExternalSymbolSDNode(true, Sym, VT);
971 AllNodes.push_back(N);
972 return SDOperand(N, 0);
973}
974
975SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
976 if ((unsigned)Cond >= CondCodeNodes.size())
977 CondCodeNodes.resize(Cond+1);
978
979 if (CondCodeNodes[Cond] == 0) {
980 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
981 AllNodes.push_back(CondCodeNodes[Cond]);
982 }
983 return SDOperand(CondCodeNodes[Cond], 0);
984}
985
986SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
987 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +0000988 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 ID.AddInteger(RegNo);
990 void *IP = 0;
991 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
992 return SDOperand(E, 0);
993 SDNode *N = new RegisterSDNode(RegNo, VT);
994 CSEMap.InsertNode(N, IP);
995 AllNodes.push_back(N);
996 return SDOperand(N, 0);
997}
998
Dan Gohman12a9c082008-02-06 22:27:42 +0000999SDOperand SelectionDAG::getSrcValue(const Value *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 assert((!V || isa<PointerType>(V->getType())) &&
1001 "SrcValue is not a pointer?");
1002
1003 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001004 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 ID.AddPointer(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007 void *IP = 0;
1008 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1009 return SDOperand(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001010
1011 SDNode *N = new SrcValueSDNode(V);
1012 CSEMap.InsertNode(N, IP);
1013 AllNodes.push_back(N);
1014 return SDOperand(N, 0);
1015}
1016
Dan Gohman1fad9e62008-04-07 19:35:22 +00001017SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman12a9c082008-02-06 22:27:42 +00001018 const Value *v = MO.getValue();
1019 assert((!v || isa<PointerType>(v->getType())) &&
1020 "SrcValue is not a pointer?");
1021
1022 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001023 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001024 ID.AddPointer(v);
1025 ID.AddInteger(MO.getFlags());
1026 ID.AddInteger(MO.getOffset());
1027 ID.AddInteger(MO.getSize());
1028 ID.AddInteger(MO.getAlignment());
1029
1030 void *IP = 0;
1031 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1032 return SDOperand(E, 0);
1033
1034 SDNode *N = new MemOperandSDNode(MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 CSEMap.InsertNode(N, IP);
1036 AllNodes.push_back(N);
1037 return SDOperand(N, 0);
1038}
1039
Chris Lattner53f5aee2007-10-15 17:47:20 +00001040/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1041/// specified value type.
1042SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1043 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1044 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1045 const Type *Ty = MVT::getTypeForValueType(VT);
1046 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1047 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1048 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1049}
1050
1051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1053 SDOperand N2, ISD::CondCode Cond) {
1054 // These setcc operations always fold.
1055 switch (Cond) {
1056 default: break;
1057 case ISD::SETFALSE:
1058 case ISD::SETFALSE2: return getConstant(0, VT);
1059 case ISD::SETTRUE:
1060 case ISD::SETTRUE2: return getConstant(1, VT);
1061
1062 case ISD::SETOEQ:
1063 case ISD::SETOGT:
1064 case ISD::SETOGE:
1065 case ISD::SETOLT:
1066 case ISD::SETOLE:
1067 case ISD::SETONE:
1068 case ISD::SETO:
1069 case ISD::SETUO:
1070 case ISD::SETUEQ:
1071 case ISD::SETUNE:
1072 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1073 break;
1074 }
1075
1076 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001077 const APInt &C2 = N2C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001079 const APInt &C1 = N1C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080
1081 switch (Cond) {
1082 default: assert(0 && "Unknown integer setcc!");
1083 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1084 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00001085 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1086 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1087 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1088 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1089 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1090 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1091 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1092 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 }
1094 }
1095 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001096 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001098 // No compile time operations on this type yet.
1099 if (N1C->getValueType(0) == MVT::ppcf128)
1100 return SDOperand();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001101
1102 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001104 default: break;
Dale Johannesen76844472007-08-31 17:03:33 +00001105 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1106 return getNode(ISD::UNDEF, VT);
1107 // fall through
1108 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1109 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1110 return getNode(ISD::UNDEF, VT);
1111 // fall through
1112 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001113 R==APFloat::cmpLessThan, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001114 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1115 return getNode(ISD::UNDEF, VT);
1116 // fall through
1117 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1118 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1119 return getNode(ISD::UNDEF, VT);
1120 // fall through
1121 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1122 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1123 return getNode(ISD::UNDEF, VT);
1124 // fall through
1125 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001126 R==APFloat::cmpEqual, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001127 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1128 return getNode(ISD::UNDEF, VT);
1129 // fall through
1130 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001131 R==APFloat::cmpEqual, VT);
1132 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1133 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1134 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1135 R==APFloat::cmpEqual, VT);
1136 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1137 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1138 R==APFloat::cmpLessThan, VT);
1139 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1140 R==APFloat::cmpUnordered, VT);
1141 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1142 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 }
1144 } else {
1145 // Ensure that the constant occurs on the RHS.
1146 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1147 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001148 }
1149
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 // Could not fold it.
1151 return SDOperand();
1152}
1153
Dan Gohman07961cd2008-02-25 21:11:39 +00001154/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1155/// use this predicate to simplify operations downstream.
1156bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1157 unsigned BitWidth = Op.getValueSizeInBits();
1158 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1159}
1160
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001161/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1162/// this predicate to simplify operations downstream. Mask is known to be zero
1163/// for bits that V cannot have.
Dan Gohman07961cd2008-02-25 21:11:39 +00001164bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001166 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1168 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1169 return (KnownZero & Mask) == Mask;
1170}
1171
1172/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1173/// known to be either zero or one and return them in the KnownZero/KnownOne
1174/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1175/// processing.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001176void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00001177 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178 unsigned Depth) const {
Dan Gohman229fa052008-02-13 00:35:47 +00001179 unsigned BitWidth = Mask.getBitWidth();
Dan Gohman56eaab32008-02-13 23:13:32 +00001180 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1181 "Mask size mismatches value type size!");
1182
Dan Gohman229fa052008-02-13 00:35:47 +00001183 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184 if (Depth == 6 || Mask == 0)
1185 return; // Limit search depth.
1186
Dan Gohman229fa052008-02-13 00:35:47 +00001187 APInt KnownZero2, KnownOne2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188
1189 switch (Op.getOpcode()) {
1190 case ISD::Constant:
1191 // We know all of the bits for a constant!
Dan Gohman229fa052008-02-13 00:35:47 +00001192 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 KnownZero = ~KnownOne & Mask;
1194 return;
1195 case ISD::AND:
1196 // If either the LHS or the RHS are Zero, the result is zero.
1197 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001198 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1199 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1201 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1202
1203 // Output known-1 bits are only known if set in both the LHS & RHS.
1204 KnownOne &= KnownOne2;
1205 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1206 KnownZero |= KnownZero2;
1207 return;
1208 case ISD::OR:
1209 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001210 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1211 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1213 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1214
1215 // Output known-0 bits are only known if clear in both the LHS & RHS.
1216 KnownZero &= KnownZero2;
1217 // Output known-1 are known to be set if set in either the LHS | RHS.
1218 KnownOne |= KnownOne2;
1219 return;
1220 case ISD::XOR: {
1221 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1222 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1223 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1224 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1225
1226 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohman229fa052008-02-13 00:35:47 +00001227 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1229 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1230 KnownZero = KnownZeroOut;
1231 return;
1232 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001233 case ISD::MUL: {
1234 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1235 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1236 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1237 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1238 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1239
1240 // If low bits are zero in either operand, output low known-0 bits.
1241 // Also compute a conserative estimate for high known-0 bits.
1242 // More trickiness is possible, but this is sufficient for the
1243 // interesting case of alignment computation.
1244 KnownOne.clear();
1245 unsigned TrailZ = KnownZero.countTrailingOnes() +
1246 KnownZero2.countTrailingOnes();
1247 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman4c451852008-05-07 00:35:55 +00001248 KnownZero2.countLeadingOnes(),
1249 BitWidth) - BitWidth;
Dan Gohmanbec16052008-04-28 17:02:21 +00001250
1251 TrailZ = std::min(TrailZ, BitWidth);
1252 LeadZ = std::min(LeadZ, BitWidth);
1253 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1254 APInt::getHighBitsSet(BitWidth, LeadZ);
1255 KnownZero &= Mask;
1256 return;
1257 }
1258 case ISD::UDIV: {
1259 // For the purposes of computing leading zeros we can conservatively
1260 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001261 // be less than the denominator.
Dan Gohmanbec16052008-04-28 17:02:21 +00001262 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1263 ComputeMaskedBits(Op.getOperand(0),
1264 AllOnes, KnownZero2, KnownOne2, Depth+1);
1265 unsigned LeadZ = KnownZero2.countLeadingOnes();
1266
1267 KnownOne2.clear();
1268 KnownZero2.clear();
1269 ComputeMaskedBits(Op.getOperand(1),
1270 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001271 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1272 if (RHSUnknownLeadingOnes != BitWidth)
1273 LeadZ = std::min(BitWidth,
1274 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001275
1276 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1277 return;
1278 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 case ISD::SELECT:
1280 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1281 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1282 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1283 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1284
1285 // Only known if known in both the LHS and RHS.
1286 KnownOne &= KnownOne2;
1287 KnownZero &= KnownZero2;
1288 return;
1289 case ISD::SELECT_CC:
1290 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1291 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1292 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1293 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1294
1295 // Only known if known in both the LHS and RHS.
1296 KnownOne &= KnownOne2;
1297 KnownZero &= KnownZero2;
1298 return;
1299 case ISD::SETCC:
1300 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohman229fa052008-02-13 00:35:47 +00001301 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1302 BitWidth > 1)
1303 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 return;
1305 case ISD::SHL:
1306 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1307 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman4b8d0002008-02-26 18:50:50 +00001308 unsigned ShAmt = SA->getValue();
1309
1310 // If the shift count is an invalid immediate, don't do anything.
1311 if (ShAmt >= BitWidth)
1312 return;
1313
1314 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 KnownZero, KnownOne, Depth+1);
1316 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman4b8d0002008-02-26 18:50:50 +00001317 KnownZero <<= ShAmt;
1318 KnownOne <<= ShAmt;
Dan Gohman229fa052008-02-13 00:35:47 +00001319 // low bits known zero.
Dan Gohman4b8d0002008-02-26 18:50:50 +00001320 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321 }
1322 return;
1323 case ISD::SRL:
1324 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1325 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326 unsigned ShAmt = SA->getValue();
1327
Dan Gohman4b8d0002008-02-26 18:50:50 +00001328 // If the shift count is an invalid immediate, don't do anything.
1329 if (ShAmt >= BitWidth)
1330 return;
1331
Dan Gohman229fa052008-02-13 00:35:47 +00001332 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333 KnownZero, KnownOne, Depth+1);
1334 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001335 KnownZero = KnownZero.lshr(ShAmt);
1336 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337
Dan Gohman4d81a742008-02-13 22:43:25 +00001338 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 KnownZero |= HighBits; // High bits known zero.
1340 }
1341 return;
1342 case ISD::SRA:
1343 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 unsigned ShAmt = SA->getValue();
1345
Dan Gohman4b8d0002008-02-26 18:50:50 +00001346 // If the shift count is an invalid immediate, don't do anything.
1347 if (ShAmt >= BitWidth)
1348 return;
1349
Dan Gohman229fa052008-02-13 00:35:47 +00001350 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 // If any of the demanded bits are produced by the sign extension, we also
1352 // demand the input sign bit.
Dan Gohman4d81a742008-02-13 22:43:25 +00001353 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1354 if (HighBits.getBoolValue())
Dan Gohman229fa052008-02-13 00:35:47 +00001355 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356
1357 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1358 Depth+1);
1359 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001360 KnownZero = KnownZero.lshr(ShAmt);
1361 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362
1363 // Handle the sign bits.
Dan Gohman229fa052008-02-13 00:35:47 +00001364 APInt SignBit = APInt::getSignBit(BitWidth);
1365 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366
Dan Gohman91507292008-02-20 16:30:17 +00001367 if (KnownZero.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368 KnownZero |= HighBits; // New bits are known zero.
Dan Gohman91507292008-02-20 16:30:17 +00001369 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 KnownOne |= HighBits; // New bits are known one.
1371 }
1372 }
1373 return;
1374 case ISD::SIGN_EXTEND_INREG: {
1375 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001376 unsigned EBits = MVT::getSizeInBits(EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377
1378 // Sign extension. Compute the demanded bits in the result that are not
1379 // present in the input.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001380 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381
Dan Gohmand0dfc772008-02-13 22:28:48 +00001382 APInt InSignBit = APInt::getSignBit(EBits);
1383 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384
1385 // If the sign extended bits are demanded, we know that the sign
1386 // bit is demanded.
Dan Gohman229fa052008-02-13 00:35:47 +00001387 InSignBit.zext(BitWidth);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001388 if (NewBits.getBoolValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389 InputDemandedBits |= InSignBit;
1390
1391 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1392 KnownZero, KnownOne, Depth+1);
1393 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1394
1395 // If the sign bit of the input is known set or clear, then we know the
1396 // top bits of the result.
Dan Gohman91507292008-02-20 16:30:17 +00001397 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 KnownZero |= NewBits;
1399 KnownOne &= ~NewBits;
Dan Gohman91507292008-02-20 16:30:17 +00001400 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 KnownOne |= NewBits;
1402 KnownZero &= ~NewBits;
1403 } else { // Input sign bit unknown
1404 KnownZero &= ~NewBits;
1405 KnownOne &= ~NewBits;
1406 }
1407 return;
1408 }
1409 case ISD::CTTZ:
1410 case ISD::CTLZ:
1411 case ISD::CTPOP: {
Dan Gohman229fa052008-02-13 00:35:47 +00001412 unsigned LowBits = Log2_32(BitWidth)+1;
1413 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1414 KnownOne = APInt(BitWidth, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 return;
1416 }
1417 case ISD::LOAD: {
1418 if (ISD::isZEXTLoad(Op.Val)) {
1419 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001420 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001421 unsigned MemBits = MVT::getSizeInBits(VT);
1422 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423 }
1424 return;
1425 }
1426 case ISD::ZERO_EXTEND: {
Dan Gohman229fa052008-02-13 00:35:47 +00001427 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1428 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001429 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1430 APInt InMask = Mask;
1431 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001432 KnownZero.trunc(InBits);
1433 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001434 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001435 KnownZero.zext(BitWidth);
1436 KnownOne.zext(BitWidth);
1437 KnownZero |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438 return;
1439 }
1440 case ISD::SIGN_EXTEND: {
1441 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohman229fa052008-02-13 00:35:47 +00001442 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman229fa052008-02-13 00:35:47 +00001443 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001444 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1445 APInt InMask = Mask;
1446 InMask.trunc(InBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447
1448 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohmand0dfc772008-02-13 22:28:48 +00001449 // bit is demanded. Temporarily set this bit in the mask for our callee.
1450 if (NewBits.getBoolValue())
1451 InMask |= InSignBit;
Dan Gohman229fa052008-02-13 00:35:47 +00001452
Dan Gohman229fa052008-02-13 00:35:47 +00001453 KnownZero.trunc(InBits);
1454 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001455 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1456
1457 // Note if the sign bit is known to be zero or one.
1458 bool SignBitKnownZero = KnownZero.isNegative();
1459 bool SignBitKnownOne = KnownOne.isNegative();
1460 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1461 "Sign bit can't be known to be both zero and one!");
1462
1463 // If the sign bit wasn't actually demanded by our caller, we don't
1464 // want it set in the KnownZero and KnownOne result values. Reset the
1465 // mask and reapply it to the result values.
1466 InMask = Mask;
1467 InMask.trunc(InBits);
1468 KnownZero &= InMask;
1469 KnownOne &= InMask;
1470
Dan Gohman229fa052008-02-13 00:35:47 +00001471 KnownZero.zext(BitWidth);
1472 KnownOne.zext(BitWidth);
1473
Dan Gohmand0dfc772008-02-13 22:28:48 +00001474 // If the sign bit is known zero or one, the top bits match.
1475 if (SignBitKnownZero)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001476 KnownZero |= NewBits;
Dan Gohmand0dfc772008-02-13 22:28:48 +00001477 else if (SignBitKnownOne)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 KnownOne |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479 return;
1480 }
1481 case ISD::ANY_EXTEND: {
Dan Gohman229fa052008-02-13 00:35:47 +00001482 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1483 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001484 APInt InMask = Mask;
1485 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001486 KnownZero.trunc(InBits);
1487 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001488 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001489 KnownZero.zext(BitWidth);
1490 KnownOne.zext(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 return;
1492 }
1493 case ISD::TRUNCATE: {
Dan Gohman229fa052008-02-13 00:35:47 +00001494 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1495 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001496 APInt InMask = Mask;
1497 InMask.zext(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001498 KnownZero.zext(InBits);
1499 KnownOne.zext(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001500 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001502 KnownZero.trunc(BitWidth);
1503 KnownOne.trunc(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 break;
1505 }
1506 case ISD::AssertZext: {
1507 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman229fa052008-02-13 00:35:47 +00001508 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1510 KnownOne, Depth+1);
1511 KnownZero |= (~InMask) & Mask;
1512 return;
1513 }
Chris Lattner13f06832007-12-22 21:26:52 +00001514 case ISD::FGETSIGN:
1515 // All bits are zero except the low bit.
Dan Gohman229fa052008-02-13 00:35:47 +00001516 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattner13f06832007-12-22 21:26:52 +00001517 return;
1518
Dan Gohmanbec16052008-04-28 17:02:21 +00001519 case ISD::SUB: {
1520 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1521 // We know that the top bits of C-X are clear if X contains less bits
1522 // than C (i.e. no wrap-around can happen). For example, 20-X is
1523 // positive if we can prove that X is >= 0 and < 16.
1524 if (CLHS->getAPIntValue().isNonNegative()) {
1525 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1526 // NLZ can't be BitWidth with no sign bit
1527 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1528 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1529 Depth+1);
1530
1531 // If all of the MaskV bits are known to be zero, then we know the
1532 // output top bits are zero, because we now know that the output is
1533 // from [0-C].
1534 if ((KnownZero2 & MaskV) == MaskV) {
1535 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1536 // Top bits known zero.
1537 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1538 }
1539 }
1540 }
1541 }
1542 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 case ISD::ADD: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001544 // Output known-0 bits are known if clear or set in both the low clear bits
1545 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1546 // low 3 bits clear.
Dan Gohmanbec16052008-04-28 17:02:21 +00001547 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1548 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1549 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1550 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1551
1552 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1553 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1554 KnownZeroOut = std::min(KnownZeroOut,
1555 KnownZero2.countTrailingOnes());
1556
1557 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 return;
1559 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001560 case ISD::SREM:
1561 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1562 APInt RA = Rem->getAPIntValue();
1563 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman5a154a12008-05-06 00:51:48 +00001564 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohmanbec16052008-04-28 17:02:21 +00001565 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1566 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001567
Dan Gohmanbec16052008-04-28 17:02:21 +00001568 // The sign of a remainder is equal to the sign of the first
1569 // operand (zero being positive).
1570 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1571 KnownZero2 |= ~LowBits;
1572 else if (KnownOne2[BitWidth-1])
1573 KnownOne2 |= ~LowBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574
Dan Gohmanbec16052008-04-28 17:02:21 +00001575 KnownZero |= KnownZero2 & Mask;
1576 KnownOne |= KnownOne2 & Mask;
1577
1578 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579 }
1580 }
1581 return;
Dan Gohmanbec16052008-04-28 17:02:21 +00001582 case ISD::UREM: {
1583 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1584 APInt RA = Rem->getAPIntValue();
Dan Gohman5a154a12008-05-06 00:51:48 +00001585 if (RA.isPowerOf2()) {
1586 APInt LowBits = (RA - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001587 APInt Mask2 = LowBits & Mask;
1588 KnownZero |= ~LowBits & Mask;
1589 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1590 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1591 break;
1592 }
1593 }
1594
1595 // Since the result is less than or equal to either operand, any leading
1596 // zero bits in either operand must also exist in the result.
1597 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1598 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1599 Depth+1);
1600 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1601 Depth+1);
1602
1603 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1604 KnownZero2.countLeadingOnes());
1605 KnownOne.clear();
1606 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1607 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 }
1609 default:
1610 // Allow the target to implement this method for its nodes.
1611 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1612 case ISD::INTRINSIC_WO_CHAIN:
1613 case ISD::INTRINSIC_W_CHAIN:
1614 case ISD::INTRINSIC_VOID:
1615 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1616 }
1617 return;
1618 }
1619}
1620
1621/// ComputeNumSignBits - Return the number of times the sign bit of the
1622/// register is replicated into the other bits. We know that at least 1 bit
1623/// is always equal to the sign bit (itself), but other cases can give us
1624/// information. For example, immediately after an "SRA X, 2", we know that
1625/// the top 3 bits are all equal to each other, so we return 3.
1626unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1627 MVT::ValueType VT = Op.getValueType();
1628 assert(MVT::isInteger(VT) && "Invalid VT!");
1629 unsigned VTBits = MVT::getSizeInBits(VT);
1630 unsigned Tmp, Tmp2;
1631
1632 if (Depth == 6)
1633 return 1; // Limit search depth.
1634
1635 switch (Op.getOpcode()) {
1636 default: break;
1637 case ISD::AssertSext:
1638 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1639 return VTBits-Tmp+1;
1640 case ISD::AssertZext:
1641 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1642 return VTBits-Tmp;
1643
1644 case ISD::Constant: {
Dan Gohman463db8c2008-03-03 23:35:36 +00001645 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1646 // If negative, return # leading ones.
1647 if (Val.isNegative())
1648 return Val.countLeadingOnes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649
Dan Gohman463db8c2008-03-03 23:35:36 +00001650 // Return # leading zeros.
1651 return Val.countLeadingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001652 }
1653
1654 case ISD::SIGN_EXTEND:
1655 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1656 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1657
1658 case ISD::SIGN_EXTEND_INREG:
1659 // Max of the input and what this extends.
1660 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1661 Tmp = VTBits-Tmp+1;
1662
1663 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1664 return std::max(Tmp, Tmp2);
1665
1666 case ISD::SRA:
1667 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1668 // SRA X, C -> adds C sign bits.
1669 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1670 Tmp += C->getValue();
1671 if (Tmp > VTBits) Tmp = VTBits;
1672 }
1673 return Tmp;
1674 case ISD::SHL:
1675 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1676 // shl destroys sign bits.
1677 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1678 if (C->getValue() >= VTBits || // Bad shift.
1679 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1680 return Tmp - C->getValue();
1681 }
1682 break;
1683 case ISD::AND:
1684 case ISD::OR:
1685 case ISD::XOR: // NOT is handled here.
1686 // Logical binary ops preserve the number of sign bits.
1687 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1688 if (Tmp == 1) return 1; // Early out.
1689 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1690 return std::min(Tmp, Tmp2);
1691
1692 case ISD::SELECT:
1693 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1694 if (Tmp == 1) return 1; // Early out.
1695 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1696 return std::min(Tmp, Tmp2);
1697
1698 case ISD::SETCC:
1699 // If setcc returns 0/-1, all bits are sign bits.
1700 if (TLI.getSetCCResultContents() ==
1701 TargetLowering::ZeroOrNegativeOneSetCCResult)
1702 return VTBits;
1703 break;
1704 case ISD::ROTL:
1705 case ISD::ROTR:
1706 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1707 unsigned RotAmt = C->getValue() & (VTBits-1);
1708
1709 // Handle rotate right by N like a rotate left by 32-N.
1710 if (Op.getOpcode() == ISD::ROTR)
1711 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1712
1713 // If we aren't rotating out all of the known-in sign bits, return the
1714 // number that are left. This handles rotl(sext(x), 1) for example.
1715 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1716 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1717 }
1718 break;
1719 case ISD::ADD:
1720 // Add can have at most one carry bit. Thus we know that the output
1721 // is, at worst, one more bit than the inputs.
1722 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1723 if (Tmp == 1) return 1; // Early out.
1724
1725 // Special case decrementing a value (ADD X, -1):
1726 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1727 if (CRHS->isAllOnesValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00001728 APInt KnownZero, KnownOne;
1729 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1731
1732 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1733 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00001734 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735 return VTBits;
1736
1737 // If we are subtracting one from a positive number, there is no carry
1738 // out of the result.
Dan Gohman63f4e462008-02-27 01:23:58 +00001739 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001740 return Tmp;
1741 }
1742
1743 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1744 if (Tmp2 == 1) return 1;
1745 return std::min(Tmp, Tmp2)-1;
1746 break;
1747
1748 case ISD::SUB:
1749 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1750 if (Tmp2 == 1) return 1;
1751
1752 // Handle NEG.
1753 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00001754 if (CLHS->isNullValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00001755 APInt KnownZero, KnownOne;
1756 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1758 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1759 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00001760 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 return VTBits;
1762
1763 // If the input is known to be positive (the sign bit is known clear),
1764 // the output of the NEG has the same number of sign bits as the input.
Dan Gohman63f4e462008-02-27 01:23:58 +00001765 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001766 return Tmp2;
1767
1768 // Otherwise, we treat this like a SUB.
1769 }
1770
1771 // Sub can have at most one carry bit. Thus we know that the output
1772 // is, at worst, one more bit than the inputs.
1773 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1774 if (Tmp == 1) return 1; // Early out.
1775 return std::min(Tmp, Tmp2)-1;
1776 break;
1777 case ISD::TRUNCATE:
1778 // FIXME: it's tricky to do anything useful for this, but it is an important
1779 // case for targets like X86.
1780 break;
1781 }
1782
1783 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1784 if (Op.getOpcode() == ISD::LOAD) {
1785 LoadSDNode *LD = cast<LoadSDNode>(Op);
1786 unsigned ExtType = LD->getExtensionType();
1787 switch (ExtType) {
1788 default: break;
1789 case ISD::SEXTLOAD: // '17' bits known
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001790 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791 return VTBits-Tmp+1;
1792 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001793 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001794 return VTBits-Tmp;
1795 }
1796 }
1797
1798 // Allow the target to implement this method for its nodes.
1799 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1800 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1801 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1802 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1803 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1804 if (NumBits > 1) return NumBits;
1805 }
1806
1807 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1808 // use this information.
Dan Gohman63f4e462008-02-27 01:23:58 +00001809 APInt KnownZero, KnownOne;
1810 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1812
Dan Gohman63f4e462008-02-27 01:23:58 +00001813 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001814 Mask = KnownZero;
Dan Gohman63f4e462008-02-27 01:23:58 +00001815 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001816 Mask = KnownOne;
1817 } else {
1818 // Nothing known.
1819 return 1;
1820 }
1821
1822 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1823 // the number of identical bits in the top of the input value.
Dan Gohman63f4e462008-02-27 01:23:58 +00001824 Mask = ~Mask;
1825 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826 // Return # leading zeros. We use 'min' here in case Val was zero before
1827 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman63f4e462008-02-27 01:23:58 +00001828 return std::min(VTBits, Mask.countLeadingZeros());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829}
1830
1831
Evan Cheng2e28d622008-02-02 04:07:54 +00001832bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1833 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1834 if (!GA) return false;
1835 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1836 if (!GV) return false;
1837 MachineModuleInfo *MMI = getMachineModuleInfo();
1838 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1839}
1840
1841
Evan Cheng411fc172008-05-13 08:35:03 +00001842/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1843/// element of the result of the vector shuffle.
1844SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned Idx) {
1845 MVT::ValueType VT = N->getValueType(0);
1846 SDOperand PermMask = N->getOperand(2);
1847 unsigned NumElems = PermMask.getNumOperands();
1848 SDOperand V = (Idx < NumElems) ? N->getOperand(0) : N->getOperand(1);
1849 Idx %= NumElems;
1850 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1851 return (Idx == 0)
1852 ? V.getOperand(0) : getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
1853 }
1854 if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
1855 SDOperand Elt = PermMask.getOperand(Idx);
1856 if (Elt.getOpcode() == ISD::UNDEF)
1857 return getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
1858 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Elt)->getValue());
1859 }
1860 return SDOperand();
1861}
1862
1863
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001864/// getNode - Gets or creates the specified node.
1865///
1866SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1867 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00001868 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 void *IP = 0;
1870 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1871 return SDOperand(E, 0);
1872 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1873 CSEMap.InsertNode(N, IP);
1874
1875 AllNodes.push_back(N);
1876 return SDOperand(N, 0);
1877}
1878
1879SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1880 SDOperand Operand) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 // Constant fold unary operations with an integer constant operand.
1882 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001883 const APInt &Val = C->getAPIntValue();
1884 unsigned BitWidth = MVT::getSizeInBits(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001885 switch (Opcode) {
1886 default: break;
Evan Chengeadaf442008-03-06 17:42:34 +00001887 case ISD::SIGN_EXTEND:
1888 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001889 case ISD::ANY_EXTEND:
Dan Gohman161652c2008-02-29 01:47:35 +00001890 case ISD::ZERO_EXTEND:
Evan Chengeadaf442008-03-06 17:42:34 +00001891 case ISD::TRUNCATE:
1892 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001893 case ISD::UINT_TO_FP:
1894 case ISD::SINT_TO_FP: {
1895 const uint64_t zero[] = {0, 0};
Dale Johannesenb89072e2007-10-16 23:38:29 +00001896 // No compile time operations on this type.
1897 if (VT==MVT::ppcf128)
1898 break;
Dan Gohman161652c2008-02-29 01:47:35 +00001899 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1900 (void)apf.convertFromAPInt(Val,
1901 Opcode==ISD::SINT_TO_FP,
1902 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001903 return getConstantFP(apf, VT);
1904 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001905 case ISD::BIT_CONVERT:
1906 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman161652c2008-02-29 01:47:35 +00001907 return getConstantFP(Val.bitsToFloat(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001908 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman161652c2008-02-29 01:47:35 +00001909 return getConstantFP(Val.bitsToDouble(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001910 break;
1911 case ISD::BSWAP:
Dan Gohman161652c2008-02-29 01:47:35 +00001912 return getConstant(Val.byteSwap(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001913 case ISD::CTPOP:
Dan Gohman161652c2008-02-29 01:47:35 +00001914 return getConstant(Val.countPopulation(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001915 case ISD::CTLZ:
Dan Gohman161652c2008-02-29 01:47:35 +00001916 return getConstant(Val.countLeadingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001917 case ISD::CTTZ:
Dan Gohman161652c2008-02-29 01:47:35 +00001918 return getConstant(Val.countTrailingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001919 }
1920 }
1921
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001922 // Constant fold unary operations with a floating point constant operand.
1923 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1924 APFloat V = C->getValueAPF(); // make copy
Chris Lattner5872a362008-01-17 07:00:52 +00001925 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00001926 switch (Opcode) {
1927 case ISD::FNEG:
1928 V.changeSign();
1929 return getConstantFP(V, VT);
1930 case ISD::FABS:
1931 V.clearSign();
1932 return getConstantFP(V, VT);
1933 case ISD::FP_ROUND:
1934 case ISD::FP_EXTEND:
1935 // This can return overflow, underflow, or inexact; we don't care.
1936 // FIXME need to be more flexible about rounding mode.
Chris Lattnerd037d482008-03-05 06:48:13 +00001937 (void)V.convert(*MVTToAPFloatSemantics(VT),
1938 APFloat::rmNearestTiesToEven);
Dale Johannesenb89072e2007-10-16 23:38:29 +00001939 return getConstantFP(V, VT);
1940 case ISD::FP_TO_SINT:
1941 case ISD::FP_TO_UINT: {
1942 integerPart x;
1943 assert(integerPartWidth >= 64);
1944 // FIXME need to be more flexible about rounding mode.
1945 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1946 Opcode==ISD::FP_TO_SINT,
1947 APFloat::rmTowardZero);
1948 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1949 break;
1950 return getConstant(x, VT);
1951 }
1952 case ISD::BIT_CONVERT:
1953 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1954 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1955 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1956 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001957 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00001958 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001959 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001960 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001961
1962 unsigned OpOpcode = Operand.Val->getOpcode();
1963 switch (Opcode) {
1964 case ISD::TokenFactor:
Dan Gohman849d7c62008-04-14 18:43:25 +00001965 case ISD::MERGE_VALUES:
1966 return Operand; // Factor or merge of one node? No need.
Chris Lattner5872a362008-01-17 07:00:52 +00001967 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001968 case ISD::FP_EXTEND:
1969 assert(MVT::isFloatingPoint(VT) &&
1970 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00001971 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnere6465dd2008-03-11 06:21:08 +00001972 if (Operand.getOpcode() == ISD::UNDEF)
1973 return getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001974 break;
Chris Lattnere6465dd2008-03-11 06:21:08 +00001975 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001976 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1977 "Invalid SIGN_EXTEND!");
1978 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001979 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1980 && "Invalid sext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1982 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1983 break;
1984 case ISD::ZERO_EXTEND:
1985 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1986 "Invalid ZERO_EXTEND!");
1987 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001988 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1989 && "Invalid zext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001990 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1991 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1992 break;
1993 case ISD::ANY_EXTEND:
1994 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1995 "Invalid ANY_EXTEND!");
1996 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001997 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1998 && "Invalid anyext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001999 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2000 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2001 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2002 break;
2003 case ISD::TRUNCATE:
2004 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
2005 "Invalid TRUNCATE!");
2006 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsa9810f32007-10-16 09:56:48 +00002007 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
2008 && "Invalid truncate node, src < dst!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009 if (OpOpcode == ISD::TRUNCATE)
2010 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2011 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2012 OpOpcode == ISD::ANY_EXTEND) {
2013 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsa9810f32007-10-16 09:56:48 +00002014 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
2015 < MVT::getSizeInBits(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsa9810f32007-10-16 09:56:48 +00002017 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
2018 > MVT::getSizeInBits(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2020 else
2021 return Operand.Val->getOperand(0);
2022 }
2023 break;
2024 case ISD::BIT_CONVERT:
2025 // Basic sanity checking.
2026 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
2027 && "Cannot BIT_CONVERT between types of different sizes!");
2028 if (VT == Operand.getValueType()) return Operand; // noop conversion.
2029 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2030 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
2031 if (OpOpcode == ISD::UNDEF)
2032 return getNode(ISD::UNDEF, VT);
2033 break;
2034 case ISD::SCALAR_TO_VECTOR:
2035 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
2036 MVT::getVectorElementType(VT) == Operand.getValueType() &&
2037 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattner9f0705c2008-03-08 23:43:36 +00002038 if (OpOpcode == ISD::UNDEF)
2039 return getNode(ISD::UNDEF, VT);
2040 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2041 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2042 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2043 Operand.getConstantOperandVal(1) == 0 &&
2044 Operand.getOperand(0).getValueType() == VT)
2045 return Operand.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046 break;
2047 case ISD::FNEG:
2048 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2049 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
2050 Operand.Val->getOperand(0));
2051 if (OpOpcode == ISD::FNEG) // --X -> X
2052 return Operand.Val->getOperand(0);
2053 break;
2054 case ISD::FABS:
2055 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2056 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2057 break;
2058 }
2059
2060 SDNode *N;
2061 SDVTList VTs = getVTList(VT);
2062 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
2063 FoldingSetNodeID ID;
2064 SDOperand Ops[1] = { Operand };
2065 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
2066 void *IP = 0;
2067 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2068 return SDOperand(E, 0);
2069 N = new UnarySDNode(Opcode, VTs, Operand);
2070 CSEMap.InsertNode(N, IP);
2071 } else {
2072 N = new UnarySDNode(Opcode, VTs, Operand);
2073 }
2074 AllNodes.push_back(N);
2075 return SDOperand(N, 0);
2076}
2077
2078
2079
2080SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2081 SDOperand N1, SDOperand N2) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002082 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2083 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002084 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002085 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 case ISD::TokenFactor:
2087 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2088 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002089 // Fold trivial token factors.
2090 if (N1.getOpcode() == ISD::EntryToken) return N2;
2091 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 break;
2093 case ISD::AND:
Chris Lattnercc126e32008-01-22 19:09:33 +00002094 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
2095 N1.getValueType() == VT && "Binary operator types must match!");
2096 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2097 // worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002098 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002099 return N2;
Chris Lattner8aa8a5e2008-01-26 01:05:42 +00002100 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2101 return N1;
Chris Lattnercc126e32008-01-22 19:09:33 +00002102 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103 case ISD::OR:
2104 case ISD::XOR:
Chris Lattnercc126e32008-01-22 19:09:33 +00002105 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
2106 N1.getValueType() == VT && "Binary operator types must match!");
2107 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2108 // worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002109 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002110 return N1;
2111 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002112 case ISD::UDIV:
2113 case ISD::UREM:
2114 case ISD::MULHU:
2115 case ISD::MULHS:
2116 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
2117 // fall through
2118 case ISD::ADD:
2119 case ISD::SUB:
2120 case ISD::MUL:
2121 case ISD::SDIV:
2122 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002123 case ISD::FADD:
2124 case ISD::FSUB:
2125 case ISD::FMUL:
2126 case ISD::FDIV:
2127 case ISD::FREM:
2128 assert(N1.getValueType() == N2.getValueType() &&
2129 N1.getValueType() == VT && "Binary operator types must match!");
2130 break;
2131 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2132 assert(N1.getValueType() == VT &&
2133 MVT::isFloatingPoint(N1.getValueType()) &&
2134 MVT::isFloatingPoint(N2.getValueType()) &&
2135 "Invalid FCOPYSIGN!");
2136 break;
2137 case ISD::SHL:
2138 case ISD::SRA:
2139 case ISD::SRL:
2140 case ISD::ROTL:
2141 case ISD::ROTR:
2142 assert(VT == N1.getValueType() &&
2143 "Shift operators return type must be the same as their first arg");
2144 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
2145 VT != MVT::i1 && "Shifts only work on integers");
2146 break;
2147 case ISD::FP_ROUND_INREG: {
2148 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2149 assert(VT == N1.getValueType() && "Not an inreg round!");
2150 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2151 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsa9810f32007-10-16 09:56:48 +00002152 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2153 "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002154 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002155 break;
2156 }
Chris Lattner5872a362008-01-17 07:00:52 +00002157 case ISD::FP_ROUND:
2158 assert(MVT::isFloatingPoint(VT) &&
2159 MVT::isFloatingPoint(N1.getValueType()) &&
2160 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2161 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002162 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00002163 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002164 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00002165 case ISD::AssertZext: {
2166 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2167 assert(VT == N1.getValueType() && "Not an inreg extend!");
2168 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2169 "Cannot *_EXTEND_INREG FP types");
2170 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2171 "Not extending!");
Duncan Sands539510b2008-02-10 10:08:52 +00002172 if (VT == EVT) return N1; // noop assertion.
Chris Lattnercc126e32008-01-22 19:09:33 +00002173 break;
2174 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175 case ISD::SIGN_EXTEND_INREG: {
2176 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2177 assert(VT == N1.getValueType() && "Not an inreg extend!");
2178 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2179 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsa9810f32007-10-16 09:56:48 +00002180 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2181 "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002182 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002183
Chris Lattnercc126e32008-01-22 19:09:33 +00002184 if (N1C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002185 APInt Val = N1C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002186 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman161652c2008-02-29 01:47:35 +00002187 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng7faa1d72008-03-06 08:20:51 +00002188 Val = Val.ashr(Val.getBitWidth()-FromBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189 return getConstant(Val, VT);
2190 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002191 break;
2192 }
2193 case ISD::EXTRACT_VECTOR_ELT:
2194 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2195
Chris Lattner9f0705c2008-03-08 23:43:36 +00002196 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2197 if (N1.getOpcode() == ISD::UNDEF)
2198 return getNode(ISD::UNDEF, VT);
2199
Chris Lattnercc126e32008-01-22 19:09:33 +00002200 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2201 // expanding copies of large vectors from registers.
2202 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2203 N1.getNumOperands() > 0) {
2204 unsigned Factor =
2205 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2206 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2207 N1.getOperand(N2C->getValue() / Factor),
2208 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2209 }
2210
2211 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2212 // expanding large vector constants.
2213 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2214 return N1.getOperand(N2C->getValue());
Chris Lattner9f0705c2008-03-08 23:43:36 +00002215
Chris Lattnercc126e32008-01-22 19:09:33 +00002216 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2217 // operations are lowered to scalars.
2218 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2219 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2220 if (IEC == N2C)
2221 return N1.getOperand(1);
2222 else
2223 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2224 }
2225 break;
2226 case ISD::EXTRACT_ELEMENT:
2227 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sandsc4d85172008-03-12 20:30:08 +00002228 assert(!MVT::isVector(N1.getValueType()) &&
2229 MVT::isInteger(N1.getValueType()) &&
2230 !MVT::isVector(VT) && MVT::isInteger(VT) &&
2231 "EXTRACT_ELEMENT only applies to integers!");
2232
Chris Lattnercc126e32008-01-22 19:09:33 +00002233 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2234 // 64-bit integers into 32-bit parts. Instead of building the extract of
2235 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2236 if (N1.getOpcode() == ISD::BUILD_PAIR)
2237 return N1.getOperand(N2C->getValue());
Duncan Sandsc4d85172008-03-12 20:30:08 +00002238
Chris Lattnercc126e32008-01-22 19:09:33 +00002239 // EXTRACT_ELEMENT of a constant int is also very common.
2240 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Dan Gohman452b4ce2008-03-24 16:38:05 +00002241 unsigned ElementSize = MVT::getSizeInBits(VT);
2242 unsigned Shift = ElementSize * N2C->getValue();
2243 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2244 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattnercc126e32008-01-22 19:09:33 +00002245 }
2246 break;
Duncan Sandsbd13a812008-02-20 17:38:09 +00002247 case ISD::EXTRACT_SUBVECTOR:
2248 if (N1.getValueType() == VT) // Trivial extraction.
2249 return N1;
2250 break;
Chris Lattnercc126e32008-01-22 19:09:33 +00002251 }
2252
2253 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 if (N2C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002255 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002256 switch (Opcode) {
2257 case ISD::ADD: return getConstant(C1 + C2, VT);
2258 case ISD::SUB: return getConstant(C1 - C2, VT);
2259 case ISD::MUL: return getConstant(C1 * C2, VT);
2260 case ISD::UDIV:
Dan Gohman161652c2008-02-29 01:47:35 +00002261 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002262 break;
2263 case ISD::UREM :
Dan Gohman161652c2008-02-29 01:47:35 +00002264 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002265 break;
2266 case ISD::SDIV :
Dan Gohman161652c2008-02-29 01:47:35 +00002267 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002268 break;
2269 case ISD::SREM :
Dan Gohman161652c2008-02-29 01:47:35 +00002270 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002271 break;
2272 case ISD::AND : return getConstant(C1 & C2, VT);
2273 case ISD::OR : return getConstant(C1 | C2, VT);
2274 case ISD::XOR : return getConstant(C1 ^ C2, VT);
2275 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00002276 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2277 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2278 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2279 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280 default: break;
2281 }
2282 } else { // Cannonicalize constant to RHS if commutative
2283 if (isCommutativeBinOp(Opcode)) {
2284 std::swap(N1C, N2C);
2285 std::swap(N1, N2);
2286 }
2287 }
2288 }
2289
Chris Lattnercc126e32008-01-22 19:09:33 +00002290 // Constant fold FP operations.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2292 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
2293 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002294 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2295 // Cannonicalize constant to RHS if commutative
2296 std::swap(N1CFP, N2CFP);
2297 std::swap(N1, N2);
2298 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002299 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2300 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002301 switch (Opcode) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002302 case ISD::FADD:
2303 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002304 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002305 return getConstantFP(V1, VT);
2306 break;
2307 case ISD::FSUB:
2308 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2309 if (s!=APFloat::opInvalidOp)
2310 return getConstantFP(V1, VT);
2311 break;
2312 case ISD::FMUL:
2313 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2314 if (s!=APFloat::opInvalidOp)
2315 return getConstantFP(V1, VT);
2316 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002318 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2319 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2320 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002321 break;
2322 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002323 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2324 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2325 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002326 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002327 case ISD::FCOPYSIGN:
2328 V1.copySign(V2);
2329 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002330 default: break;
2331 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002332 }
2333 }
2334
2335 // Canonicalize an UNDEF to the RHS, even over a constant.
2336 if (N1.getOpcode() == ISD::UNDEF) {
2337 if (isCommutativeBinOp(Opcode)) {
2338 std::swap(N1, N2);
2339 } else {
2340 switch (Opcode) {
2341 case ISD::FP_ROUND_INREG:
2342 case ISD::SIGN_EXTEND_INREG:
2343 case ISD::SUB:
2344 case ISD::FSUB:
2345 case ISD::FDIV:
2346 case ISD::FREM:
2347 case ISD::SRA:
2348 return N1; // fold op(undef, arg2) -> undef
2349 case ISD::UDIV:
2350 case ISD::SDIV:
2351 case ISD::UREM:
2352 case ISD::SREM:
2353 case ISD::SRL:
2354 case ISD::SHL:
2355 if (!MVT::isVector(VT))
2356 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2357 // For vectors, we can't easily build an all zero vector, just return
2358 // the LHS.
2359 return N2;
2360 }
2361 }
2362 }
2363
2364 // Fold a bunch of operators when the RHS is undef.
2365 if (N2.getOpcode() == ISD::UNDEF) {
2366 switch (Opcode) {
Evan Cheng5d00cb42008-03-25 20:08:07 +00002367 case ISD::XOR:
2368 if (N1.getOpcode() == ISD::UNDEF)
2369 // Handle undef ^ undef -> 0 special case. This is a common
2370 // idiom (misuse).
2371 return getConstant(0, VT);
2372 // fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002373 case ISD::ADD:
2374 case ISD::ADDC:
2375 case ISD::ADDE:
2376 case ISD::SUB:
2377 case ISD::FADD:
2378 case ISD::FSUB:
2379 case ISD::FMUL:
2380 case ISD::FDIV:
2381 case ISD::FREM:
2382 case ISD::UDIV:
2383 case ISD::SDIV:
2384 case ISD::UREM:
2385 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386 return N2; // fold op(arg1, undef) -> undef
2387 case ISD::MUL:
2388 case ISD::AND:
2389 case ISD::SRL:
2390 case ISD::SHL:
2391 if (!MVT::isVector(VT))
2392 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2393 // For vectors, we can't easily build an all zero vector, just return
2394 // the LHS.
2395 return N1;
2396 case ISD::OR:
2397 if (!MVT::isVector(VT))
2398 return getConstant(MVT::getIntVTBitMask(VT), VT);
2399 // For vectors, we can't easily build an all one vector, just return
2400 // the LHS.
2401 return N1;
2402 case ISD::SRA:
2403 return N1;
2404 }
2405 }
2406
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 // Memoize this node if possible.
2408 SDNode *N;
2409 SDVTList VTs = getVTList(VT);
2410 if (VT != MVT::Flag) {
2411 SDOperand Ops[] = { N1, N2 };
2412 FoldingSetNodeID ID;
2413 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2414 void *IP = 0;
2415 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2416 return SDOperand(E, 0);
2417 N = new BinarySDNode(Opcode, VTs, N1, N2);
2418 CSEMap.InsertNode(N, IP);
2419 } else {
2420 N = new BinarySDNode(Opcode, VTs, N1, N2);
2421 }
2422
2423 AllNodes.push_back(N);
2424 return SDOperand(N, 0);
2425}
2426
2427SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2428 SDOperand N1, SDOperand N2, SDOperand N3) {
2429 // Perform various simplifications.
2430 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2431 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2432 switch (Opcode) {
2433 case ISD::SETCC: {
2434 // Use FoldSetCC to simplify SETCC's.
2435 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2436 if (Simp.Val) return Simp;
2437 break;
2438 }
2439 case ISD::SELECT:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002440 if (N1C) {
2441 if (N1C->getValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 return N2; // select true, X, Y -> X
2443 else
2444 return N3; // select false, X, Y -> Y
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002445 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446
2447 if (N2 == N3) return N2; // select C, X, X -> X
2448 break;
2449 case ISD::BRCOND:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002450 if (N2C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 if (N2C->getValue()) // Unconditional branch
2452 return getNode(ISD::BR, MVT::Other, N1, N3);
2453 else
2454 return N1; // Never-taken branch
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002455 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 break;
2457 case ISD::VECTOR_SHUFFLE:
2458 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2459 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2460 N3.getOpcode() == ISD::BUILD_VECTOR &&
2461 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2462 "Illegal VECTOR_SHUFFLE node!");
2463 break;
2464 case ISD::BIT_CONVERT:
2465 // Fold bit_convert nodes from a type to themselves.
2466 if (N1.getValueType() == VT)
2467 return N1;
2468 break;
2469 }
2470
2471 // Memoize node if it doesn't produce a flag.
2472 SDNode *N;
2473 SDVTList VTs = getVTList(VT);
2474 if (VT != MVT::Flag) {
2475 SDOperand Ops[] = { N1, N2, N3 };
2476 FoldingSetNodeID ID;
2477 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2478 void *IP = 0;
2479 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2480 return SDOperand(E, 0);
2481 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2482 CSEMap.InsertNode(N, IP);
2483 } else {
2484 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2485 }
2486 AllNodes.push_back(N);
2487 return SDOperand(N, 0);
2488}
2489
2490SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2491 SDOperand N1, SDOperand N2, SDOperand N3,
2492 SDOperand N4) {
2493 SDOperand Ops[] = { N1, N2, N3, N4 };
2494 return getNode(Opcode, VT, Ops, 4);
2495}
2496
2497SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2498 SDOperand N1, SDOperand N2, SDOperand N3,
2499 SDOperand N4, SDOperand N5) {
2500 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2501 return getNode(Opcode, VT, Ops, 5);
2502}
2503
Dan Gohmane8b391e2008-04-12 04:36:06 +00002504/// getMemsetValue - Vectorized representation of the memset value
2505/// operand.
2506static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
2507 SelectionDAG &DAG) {
Evan Cheng8c590372008-05-15 08:39:06 +00002508 unsigned NumBits = MVT::isVector(VT) ?
2509 MVT::getSizeInBits(MVT::getVectorElementType(VT)) : MVT::getSizeInBits(VT);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002510 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Cheng8c590372008-05-15 08:39:06 +00002511 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002512 unsigned Shift = 8;
Evan Cheng8c590372008-05-15 08:39:06 +00002513 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002514 Val = (Val << Shift) | Val;
2515 Shift <<= 1;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002516 }
Evan Cheng8c590372008-05-15 08:39:06 +00002517 if (MVT::isInteger(VT))
2518 return DAG.getConstant(Val, VT);
2519 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002520 }
Evan Cheng8c590372008-05-15 08:39:06 +00002521
2522 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2523 unsigned Shift = 8;
2524 for (unsigned i = NumBits; i > 8; i >>= 1) {
2525 Value = DAG.getNode(ISD::OR, VT,
2526 DAG.getNode(ISD::SHL, VT, Value,
2527 DAG.getConstant(Shift, MVT::i8)), Value);
2528 Shift <<= 1;
2529 }
2530
2531 return Value;
Rafael Espindola80825902007-10-19 10:41:11 +00002532}
2533
Dan Gohmane8b391e2008-04-12 04:36:06 +00002534/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2535/// used when a memcpy is turned into a memset when the source is a constant
2536/// string ptr.
Evan Cheng8c590372008-05-15 08:39:06 +00002537static SDOperand getMemsetStringVal(MVT::ValueType VT, SelectionDAG &DAG,
Dan Gohmane8b391e2008-04-12 04:36:06 +00002538 const TargetLowering &TLI,
2539 std::string &Str, unsigned Offset) {
Evan Cheng8c590372008-05-15 08:39:06 +00002540 assert(!MVT::isVector(VT) && "Can't handle vector type here!");
2541 unsigned NumBits = MVT::getSizeInBits(VT);
2542 unsigned MSB = NumBits / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002543 uint64_t Val = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002544 if (TLI.isLittleEndian())
2545 Offset = Offset + MSB - 1;
2546 for (unsigned i = 0; i != MSB; ++i) {
2547 Val = (Val << 8) | (unsigned char)Str[Offset];
2548 Offset += TLI.isLittleEndian() ? -1 : 1;
2549 }
2550 return DAG.getConstant(Val, VT);
Rafael Espindola80825902007-10-19 10:41:11 +00002551}
2552
Dan Gohmane8b391e2008-04-12 04:36:06 +00002553/// getMemBasePlusOffset - Returns base and offset node for the
Evan Cheng8c590372008-05-15 08:39:06 +00002554///
Dan Gohmane8b391e2008-04-12 04:36:06 +00002555static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2556 SelectionDAG &DAG) {
2557 MVT::ValueType VT = Base.getValueType();
2558 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2559}
2560
Evan Cheng8c590372008-05-15 08:39:06 +00002561/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2562///
2563static bool isMemSrcFromString(SDOperand Src, std::string &Str,
2564 uint64_t &SrcOff) {
2565 unsigned SrcDelta = 0;
2566 GlobalAddressSDNode *G = NULL;
2567 if (Src.getOpcode() == ISD::GlobalAddress)
2568 G = cast<GlobalAddressSDNode>(Src);
2569 else if (Src.getOpcode() == ISD::ADD &&
2570 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2571 Src.getOperand(1).getOpcode() == ISD::Constant) {
2572 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2573 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2574 }
2575 if (!G)
2576 return false;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002577
Evan Cheng8c590372008-05-15 08:39:06 +00002578 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
2579 if (GV && GV->isConstant()) {
2580 Str = GV->getStringValue(false);
2581 if (!Str.empty()) {
2582 SrcOff += SrcDelta;
2583 return true;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002584 }
2585 }
2586
Evan Cheng8c590372008-05-15 08:39:06 +00002587 return false;
2588}
Dan Gohmane8b391e2008-04-12 04:36:06 +00002589
Evan Cheng8c590372008-05-15 08:39:06 +00002590/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2591/// to replace the memset / memcpy is below the threshold. It also returns the
2592/// types of the sequence of memory ops to perform memset / memcpy.
2593static
2594bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
2595 SDOperand Dst, SDOperand Src,
2596 unsigned Limit, uint64_t Size, unsigned &Align,
2597 SelectionDAG &DAG,
2598 const TargetLowering &TLI) {
2599 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
2600
2601 std::string Str;
2602 uint64_t SrcOff = 0;
2603 bool isSrcStr = isMemSrcFromString(Src, Str, SrcOff);
2604 bool isSrcConst = isa<ConstantSDNode>(Src);
2605 MVT::ValueType VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
2606 if (VT != MVT::iAny) {
2607 unsigned NewAlign = (unsigned)
2608 TLI.getTargetData()->getABITypeAlignment(MVT::getTypeForValueType(VT));
2609 // If source is a string constant, this will require an unaligned load.
2610 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2611 if (Dst.getOpcode() != ISD::FrameIndex) {
2612 // Can't change destination alignment. It requires a unaligned store.
2613 if (AllowUnalign)
2614 VT = MVT::iAny;
2615 } else {
2616 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2617 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2618 if (MFI->isFixedObjectIndex(FI)) {
2619 // Can't change destination alignment. It requires a unaligned store.
2620 if (AllowUnalign)
2621 VT = MVT::iAny;
2622 } else {
2623 // Give the stack frame object a larger alignment.
2624 MFI->setObjectAlignment(FI, NewAlign);
2625 Align = NewAlign;
2626 }
2627 }
2628 }
2629 }
2630
2631 if (VT == MVT::iAny) {
2632 if (AllowUnalign) {
2633 VT = MVT::i64;
2634 } else {
2635 switch (Align & 7) {
2636 case 0: VT = MVT::i64; break;
2637 case 4: VT = MVT::i32; break;
2638 case 2: VT = MVT::i16; break;
2639 default: VT = MVT::i8; break;
2640 }
2641 }
2642
2643 MVT::ValueType LVT = MVT::i64;
2644 while (!TLI.isTypeLegal(LVT))
2645 LVT = (MVT::ValueType)((unsigned)LVT - 1);
2646 assert(MVT::isInteger(LVT));
2647
2648 if (VT > LVT)
2649 VT = LVT;
2650 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002651
2652 unsigned NumMemOps = 0;
2653 while (Size != 0) {
2654 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2655 while (VTSize > Size) {
Evan Cheng8c590372008-05-15 08:39:06 +00002656 // For now, only use non-vector load / store's for the left-over pieces.
2657 if (MVT::isVector(VT)) {
2658 VT = MVT::i64;
2659 while (!TLI.isTypeLegal(VT))
2660 VT = (MVT::ValueType)((unsigned)VT - 1);
2661 VTSize = MVT::getSizeInBits(VT) / 8;
2662 } else {
2663 VT = (MVT::ValueType)((unsigned)VT - 1);
2664 VTSize >>= 1;
2665 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002666 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00002667
2668 if (++NumMemOps > Limit)
2669 return false;
2670 MemOps.push_back(VT);
2671 Size -= VTSize;
2672 }
2673
2674 return true;
2675}
2676
2677static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2678 SDOperand Chain, SDOperand Dst,
2679 SDOperand Src, uint64_t Size,
Evan Cheng8c590372008-05-15 08:39:06 +00002680 unsigned Align, bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00002681 const Value *DstSV, uint64_t DstSVOff,
2682 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00002683 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2684
2685 // Expand memcpy to a series of store ops if the size operand falls below
2686 // a certain threshold.
2687 std::vector<MVT::ValueType> MemOps;
2688 uint64_t Limit = -1;
2689 if (!AlwaysInline)
2690 Limit = TLI.getMaxStoresPerMemcpy();
Evan Cheng8c590372008-05-15 08:39:06 +00002691 unsigned DstAlign = Align; // Destination alignment can change.
2692 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
2693 DAG, TLI))
Dan Gohmane8b391e2008-04-12 04:36:06 +00002694 return SDOperand();
2695
Dan Gohmane8b391e2008-04-12 04:36:06 +00002696 std::string Str;
Dan Gohman65118f42008-04-28 17:15:20 +00002697 uint64_t SrcOff = 0, DstOff = 0;
Evan Cheng8c590372008-05-15 08:39:06 +00002698 bool CopyFromStr = isMemSrcFromString(Src, Str, SrcOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002699
Evan Cheng8c590372008-05-15 08:39:06 +00002700 SmallVector<SDOperand, 8> OutChains;
2701 unsigned NumMemOps = MemOps.size();
Dan Gohmane8b391e2008-04-12 04:36:06 +00002702 for (unsigned i = 0; i < NumMemOps; i++) {
2703 MVT::ValueType VT = MemOps[i];
2704 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2705 SDOperand Value, Store;
2706
Evan Cheng8c590372008-05-15 08:39:06 +00002707 if (CopyFromStr && !MVT::isVector(VT)) {
2708 // It's unlikely a store of a vector immediate can be done in a single
2709 // instruction. It would require a load from a constantpool first.
2710 // FIXME: Handle cases where store of vector immediate is done in a
2711 // single instruction.
Dan Gohmane8b391e2008-04-12 04:36:06 +00002712 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Cheng8c590372008-05-15 08:39:06 +00002713 Store = DAG.getStore(Chain, Value,
2714 getMemBasePlusOffset(Dst, DstOff, DAG),
2715 DstSV, DstSVOff + DstOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002716 } else {
2717 Value = DAG.getLoad(VT, Chain,
2718 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman65118f42008-04-28 17:15:20 +00002719 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Cheng8c590372008-05-15 08:39:06 +00002720 Store = DAG.getStore(Chain, Value,
2721 getMemBasePlusOffset(Dst, DstOff, DAG),
2722 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002723 }
2724 OutChains.push_back(Store);
2725 SrcOff += VTSize;
2726 DstOff += VTSize;
2727 }
2728
2729 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2730 &OutChains[0], OutChains.size());
2731}
2732
2733static SDOperand getMemsetStores(SelectionDAG &DAG,
2734 SDOperand Chain, SDOperand Dst,
2735 SDOperand Src, uint64_t Size,
2736 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002737 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002738 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2739
2740 // Expand memset to a series of load/store ops if the size operand
2741 // falls below a certain threshold.
2742 std::vector<MVT::ValueType> MemOps;
Evan Cheng8c590372008-05-15 08:39:06 +00002743 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
2744 Size, Align, DAG, TLI))
Dan Gohmane8b391e2008-04-12 04:36:06 +00002745 return SDOperand();
2746
2747 SmallVector<SDOperand, 8> OutChains;
Dan Gohman65118f42008-04-28 17:15:20 +00002748 uint64_t DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00002749
2750 unsigned NumMemOps = MemOps.size();
2751 for (unsigned i = 0; i < NumMemOps; i++) {
2752 MVT::ValueType VT = MemOps[i];
2753 unsigned VTSize = MVT::getSizeInBits(VT) / 8;
2754 SDOperand Value = getMemsetValue(Src, VT, DAG);
2755 SDOperand Store = DAG.getStore(Chain, Value,
2756 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman65118f42008-04-28 17:15:20 +00002757 DstSV, DstSVOff + DstOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002758 OutChains.push_back(Store);
2759 DstOff += VTSize;
2760 }
2761
2762 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2763 &OutChains[0], OutChains.size());
2764}
2765
2766SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola80825902007-10-19 10:41:11 +00002767 SDOperand Src, SDOperand Size,
Dan Gohmane8b391e2008-04-12 04:36:06 +00002768 unsigned Align, bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00002769 const Value *DstSV, uint64_t DstSVOff,
2770 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002771
2772 // Check to see if we should lower the memcpy to loads and stores first.
2773 // For cases within the target-specified limits, this is the best choice.
2774 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2775 if (ConstantSize) {
2776 // Memcpy with size zero? Just return the original chain.
2777 if (ConstantSize->isNullValue())
2778 return Chain;
2779
2780 SDOperand Result =
2781 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman65118f42008-04-28 17:15:20 +00002782 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002783 if (Result.Val)
2784 return Result;
2785 }
2786
2787 // Then check to see if we should lower the memcpy with target-specific
2788 // code. If the target chooses to do this, this is the next best.
2789 SDOperand Result =
2790 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2791 AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00002792 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002793 if (Result.Val)
2794 return Result;
2795
2796 // If we really need inline code and the target declined to provide it,
2797 // use a (potentially long) sequence of loads and stores.
2798 if (AlwaysInline) {
2799 assert(ConstantSize && "AlwaysInline requires a constant size!");
2800 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2801 ConstantSize->getValue(), Align, true,
Dan Gohman65118f42008-04-28 17:15:20 +00002802 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002803 }
2804
2805 // Emit a library call.
2806 TargetLowering::ArgListTy Args;
2807 TargetLowering::ArgListEntry Entry;
2808 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2809 Entry.Node = Dst; Args.push_back(Entry);
2810 Entry.Node = Src; Args.push_back(Entry);
2811 Entry.Node = Size; Args.push_back(Entry);
2812 std::pair<SDOperand,SDOperand> CallResult =
2813 TLI.LowerCallTo(Chain, Type::VoidTy,
2814 false, false, false, CallingConv::C, false,
2815 getExternalSymbol("memcpy", TLI.getPointerTy()),
2816 Args, *this);
2817 return CallResult.second;
2818}
2819
2820SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2821 SDOperand Src, SDOperand Size,
2822 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002823 const Value *DstSV, uint64_t DstSVOff,
2824 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002825
2826 // TODO: Optimize small memmove cases with simple loads and stores,
2827 // ensuring that all loads precede all stores. This can cause severe
2828 // register pressure, so targets should be careful with the size limit.
2829
2830 // Then check to see if we should lower the memmove with target-specific
2831 // code. If the target chooses to do this, this is the next best.
2832 SDOperand Result =
2833 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002834 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002835 if (Result.Val)
2836 return Result;
2837
2838 // Emit a library call.
2839 TargetLowering::ArgListTy Args;
2840 TargetLowering::ArgListEntry Entry;
2841 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2842 Entry.Node = Dst; Args.push_back(Entry);
2843 Entry.Node = Src; Args.push_back(Entry);
2844 Entry.Node = Size; Args.push_back(Entry);
2845 std::pair<SDOperand,SDOperand> CallResult =
2846 TLI.LowerCallTo(Chain, Type::VoidTy,
2847 false, false, false, CallingConv::C, false,
2848 getExternalSymbol("memmove", TLI.getPointerTy()),
2849 Args, *this);
2850 return CallResult.second;
2851}
2852
2853SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2854 SDOperand Src, SDOperand Size,
2855 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002856 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00002857
2858 // Check to see if we should lower the memset to stores first.
2859 // For cases within the target-specified limits, this is the best choice.
2860 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2861 if (ConstantSize) {
2862 // Memset with size zero? Just return the original chain.
2863 if (ConstantSize->isNullValue())
2864 return Chain;
2865
2866 SDOperand Result =
2867 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002868 DstSV, DstSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002869 if (Result.Val)
2870 return Result;
2871 }
2872
2873 // Then check to see if we should lower the memset with target-specific
2874 // code. If the target chooses to do this, this is the next best.
2875 SDOperand Result =
2876 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman65118f42008-04-28 17:15:20 +00002877 DstSV, DstSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00002878 if (Result.Val)
2879 return Result;
2880
2881 // Emit a library call.
2882 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2883 TargetLowering::ArgListTy Args;
2884 TargetLowering::ArgListEntry Entry;
2885 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2886 Args.push_back(Entry);
2887 // Extend or truncate the argument to be an i32 value for the call.
2888 if (Src.getValueType() > MVT::i32)
2889 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2890 else
2891 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2892 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2893 Args.push_back(Entry);
2894 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2895 Args.push_back(Entry);
2896 std::pair<SDOperand,SDOperand> CallResult =
2897 TLI.LowerCallTo(Chain, Type::VoidTy,
2898 false, false, false, CallingConv::C, false,
2899 getExternalSymbol("memset", TLI.getPointerTy()),
2900 Args, *this);
2901 return CallResult.second;
Rafael Espindola80825902007-10-19 10:41:11 +00002902}
2903
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002904SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002905 SDOperand Ptr, SDOperand Cmp,
2906 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002907 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002908 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2909 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002910 FoldingSetNodeID ID;
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002911 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002912 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2913 ID.AddInteger((unsigned int)VT);
2914 void* IP = 0;
2915 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2916 return SDOperand(E, 0);
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002917 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002918 CSEMap.InsertNode(N, IP);
2919 AllNodes.push_back(N);
2920 return SDOperand(N, 0);
2921}
2922
2923SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002924 SDOperand Ptr, SDOperand Val,
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002925 MVT::ValueType VT) {
Mon P Wang078a62d2008-05-05 19:05:59 +00002926 assert(( Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_LSS
2927 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
2928 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
2929 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
2930 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002931 && "Invalid Atomic Op");
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002932 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002933 FoldingSetNodeID ID;
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002934 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002935 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2936 ID.AddInteger((unsigned int)VT);
2937 void* IP = 0;
2938 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2939 return SDOperand(E, 0);
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002940 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002941 CSEMap.InsertNode(N, IP);
2942 AllNodes.push_back(N);
2943 return SDOperand(N, 0);
2944}
2945
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00002946SDOperand
Duncan Sandsb2589712008-03-28 09:45:24 +00002947SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
2948 MVT::ValueType VT, SDOperand Chain,
2949 SDOperand Ptr, SDOperand Offset,
2950 const Value *SV, int SVOffset, MVT::ValueType EVT,
2951 bool isVolatile, unsigned Alignment) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2953 const Type *Ty = 0;
2954 if (VT != MVT::iPTR) {
2955 Ty = MVT::getTypeForValueType(VT);
2956 } else if (SV) {
2957 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2958 assert(PT && "Value for load must be a pointer");
2959 Ty = PT->getElementType();
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00002960 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 assert(Ty && "Could not get type information for load");
2962 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2963 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00002965 if (VT == EVT) {
2966 ExtType = ISD::NON_EXTLOAD;
2967 } else if (ExtType == ISD::NON_EXTLOAD) {
2968 assert(VT == EVT && "Non-extending load from different memory type!");
2969 } else {
2970 // Extending load.
2971 if (MVT::isVector(VT))
2972 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2973 else
2974 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2975 "Should only be an extending load, not truncating!");
2976 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2977 "Cannot sign/zero extend a FP/Vector load!");
2978 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2979 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980 }
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00002981
2982 bool Indexed = AM != ISD::UNINDEXED;
2983 assert(Indexed || Offset.getOpcode() == ISD::UNDEF &&
2984 "Unindexed load with an offset!");
2985
2986 SDVTList VTs = Indexed ?
2987 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
2988 SDOperand Ops[] = { Chain, Ptr, Offset };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002989 FoldingSetNodeID ID;
2990 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00002991 ID.AddInteger(AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 ID.AddInteger(ExtType);
Chris Lattner4a22a672007-09-13 06:09:48 +00002993 ID.AddInteger((unsigned int)EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002994 ID.AddInteger(Alignment);
2995 ID.AddInteger(isVolatile);
2996 void *IP = 0;
2997 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2998 return SDOperand(E, 0);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00002999 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3000 Alignment, isVolatile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001 CSEMap.InsertNode(N, IP);
3002 AllNodes.push_back(N);
3003 return SDOperand(N, 0);
3004}
3005
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003006SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
3007 SDOperand Chain, SDOperand Ptr,
3008 const Value *SV, int SVOffset,
3009 bool isVolatile, unsigned Alignment) {
3010 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandsb2589712008-03-28 09:45:24 +00003011 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3012 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003013}
3014
3015SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
3016 SDOperand Chain, SDOperand Ptr,
3017 const Value *SV,
3018 int SVOffset, MVT::ValueType EVT,
3019 bool isVolatile, unsigned Alignment) {
3020 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandsb2589712008-03-28 09:45:24 +00003021 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3022 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands9a2f4dc2008-03-27 20:23:40 +00003023}
3024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003025SDOperand
3026SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3027 SDOperand Offset, ISD::MemIndexedMode AM) {
3028 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3029 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3030 "Load is already a indexed load!");
Duncan Sandsb2589712008-03-28 09:45:24 +00003031 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3032 LD->getChain(), Base, Offset, LD->getSrcValue(),
3033 LD->getSrcValueOffset(), LD->getMemoryVT(),
3034 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003035}
3036
3037SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
3038 SDOperand Ptr, const Value *SV, int SVOffset,
3039 bool isVolatile, unsigned Alignment) {
3040 MVT::ValueType VT = Val.getValueType();
3041
3042 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3043 const Type *Ty = 0;
3044 if (VT != MVT::iPTR) {
3045 Ty = MVT::getTypeForValueType(VT);
3046 } else if (SV) {
3047 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3048 assert(PT && "Value for store must be a pointer");
3049 Ty = PT->getElementType();
3050 }
3051 assert(Ty && "Could not get type information for store");
3052 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3053 }
3054 SDVTList VTs = getVTList(MVT::Other);
3055 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3056 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
3057 FoldingSetNodeID ID;
3058 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3059 ID.AddInteger(ISD::UNINDEXED);
3060 ID.AddInteger(false);
Chris Lattner4a22a672007-09-13 06:09:48 +00003061 ID.AddInteger((unsigned int)VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003062 ID.AddInteger(Alignment);
3063 ID.AddInteger(isVolatile);
3064 void *IP = 0;
3065 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3066 return SDOperand(E, 0);
3067 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3068 VT, SV, SVOffset, Alignment, isVolatile);
3069 CSEMap.InsertNode(N, IP);
3070 AllNodes.push_back(N);
3071 return SDOperand(N, 0);
3072}
3073
3074SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
3075 SDOperand Ptr, const Value *SV,
3076 int SVOffset, MVT::ValueType SVT,
3077 bool isVolatile, unsigned Alignment) {
3078 MVT::ValueType VT = Val.getValueType();
Duncan Sands06fcf652007-10-30 12:40:58 +00003079
3080 if (VT == SVT)
3081 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003082
Duncan Sandsa9810f32007-10-16 09:56:48 +00003083 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
3084 "Not a truncation?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
3086 "Can't do FP-INT conversion!");
3087
3088 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3089 const Type *Ty = 0;
3090 if (VT != MVT::iPTR) {
3091 Ty = MVT::getTypeForValueType(VT);
3092 } else if (SV) {
3093 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3094 assert(PT && "Value for store must be a pointer");
3095 Ty = PT->getElementType();
3096 }
3097 assert(Ty && "Could not get type information for store");
3098 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3099 }
3100 SDVTList VTs = getVTList(MVT::Other);
3101 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3102 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
3103 FoldingSetNodeID ID;
3104 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3105 ID.AddInteger(ISD::UNINDEXED);
Duncan Sands06fcf652007-10-30 12:40:58 +00003106 ID.AddInteger(1);
Chris Lattner4a22a672007-09-13 06:09:48 +00003107 ID.AddInteger((unsigned int)SVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108 ID.AddInteger(Alignment);
3109 ID.AddInteger(isVolatile);
3110 void *IP = 0;
3111 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3112 return SDOperand(E, 0);
Duncan Sands06fcf652007-10-30 12:40:58 +00003113 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003114 SVT, SV, SVOffset, Alignment, isVolatile);
3115 CSEMap.InsertNode(N, IP);
3116 AllNodes.push_back(N);
3117 return SDOperand(N, 0);
3118}
3119
3120SDOperand
3121SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3122 SDOperand Offset, ISD::MemIndexedMode AM) {
3123 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3124 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3125 "Store is already a indexed store!");
3126 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3127 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3128 FoldingSetNodeID ID;
3129 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3130 ID.AddInteger(AM);
3131 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003132 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133 ID.AddInteger(ST->getAlignment());
3134 ID.AddInteger(ST->isVolatile());
3135 void *IP = 0;
3136 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3137 return SDOperand(E, 0);
3138 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003139 ST->isTruncatingStore(), ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003140 ST->getSrcValue(), ST->getSrcValueOffset(),
3141 ST->getAlignment(), ST->isVolatile());
3142 CSEMap.InsertNode(N, IP);
3143 AllNodes.push_back(N);
3144 return SDOperand(N, 0);
3145}
3146
3147SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
3148 SDOperand Chain, SDOperand Ptr,
3149 SDOperand SV) {
3150 SDOperand Ops[] = { Chain, Ptr, SV };
3151 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
3152}
3153
3154SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003155 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156 switch (NumOps) {
3157 case 0: return getNode(Opcode, VT);
3158 case 1: return getNode(Opcode, VT, Ops[0]);
3159 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3160 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
3161 default: break;
3162 }
3163
3164 switch (Opcode) {
3165 default: break;
3166 case ISD::SELECT_CC: {
3167 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
3168 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3169 "LHS and RHS of condition must have same type!");
3170 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3171 "True and False arms of SelectCC must have same type!");
3172 assert(Ops[2].getValueType() == VT &&
3173 "select_cc node must be of same type as true and false value!");
3174 break;
3175 }
3176 case ISD::BR_CC: {
3177 assert(NumOps == 5 && "BR_CC takes 5 operands!");
3178 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3179 "LHS/RHS of comparison should match types!");
3180 break;
3181 }
3182 }
3183
3184 // Memoize nodes.
3185 SDNode *N;
3186 SDVTList VTs = getVTList(VT);
3187 if (VT != MVT::Flag) {
3188 FoldingSetNodeID ID;
3189 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
3190 void *IP = 0;
3191 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3192 return SDOperand(E, 0);
3193 N = new SDNode(Opcode, VTs, Ops, NumOps);
3194 CSEMap.InsertNode(N, IP);
3195 } else {
3196 N = new SDNode(Opcode, VTs, Ops, NumOps);
3197 }
3198 AllNodes.push_back(N);
3199 return SDOperand(N, 0);
3200}
3201
3202SDOperand SelectionDAG::getNode(unsigned Opcode,
3203 std::vector<MVT::ValueType> &ResultTys,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003204 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3206 Ops, NumOps);
3207}
3208
3209SDOperand SelectionDAG::getNode(unsigned Opcode,
3210 const MVT::ValueType *VTs, unsigned NumVTs,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003211 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003212 if (NumVTs == 1)
3213 return getNode(Opcode, VTs[0], Ops, NumOps);
3214 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3215}
3216
3217SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003218 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 if (VTList.NumVTs == 1)
3220 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
3221
3222 switch (Opcode) {
3223 // FIXME: figure out how to safely handle things like
3224 // int foo(int x) { return 1 << (x & 255); }
3225 // int bar() { return foo(256); }
3226#if 0
3227 case ISD::SRA_PARTS:
3228 case ISD::SRL_PARTS:
3229 case ISD::SHL_PARTS:
3230 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3231 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
3232 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3233 else if (N3.getOpcode() == ISD::AND)
3234 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3235 // If the and is only masking out bits that cannot effect the shift,
3236 // eliminate the and.
3237 unsigned NumBits = MVT::getSizeInBits(VT)*2;
3238 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3239 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3240 }
3241 break;
3242#endif
3243 }
3244
3245 // Memoize the node unless it returns a flag.
3246 SDNode *N;
3247 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3248 FoldingSetNodeID ID;
3249 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3250 void *IP = 0;
3251 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3252 return SDOperand(E, 0);
3253 if (NumOps == 1)
3254 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3255 else if (NumOps == 2)
3256 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3257 else if (NumOps == 3)
3258 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3259 else
3260 N = new SDNode(Opcode, VTList, Ops, NumOps);
3261 CSEMap.InsertNode(N, IP);
3262 } else {
3263 if (NumOps == 1)
3264 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3265 else if (NumOps == 2)
3266 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3267 else if (NumOps == 3)
3268 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3269 else
3270 N = new SDNode(Opcode, VTList, Ops, NumOps);
3271 }
3272 AllNodes.push_back(N);
3273 return SDOperand(N, 0);
3274}
3275
Dan Gohman798d1272007-10-08 15:49:58 +00003276SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003277 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman798d1272007-10-08 15:49:58 +00003278}
3279
3280SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3281 SDOperand N1) {
3282 SDOperand Ops[] = { N1 };
3283 return getNode(Opcode, VTList, Ops, 1);
3284}
3285
3286SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3287 SDOperand N1, SDOperand N2) {
3288 SDOperand Ops[] = { N1, N2 };
3289 return getNode(Opcode, VTList, Ops, 2);
3290}
3291
3292SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3293 SDOperand N1, SDOperand N2, SDOperand N3) {
3294 SDOperand Ops[] = { N1, N2, N3 };
3295 return getNode(Opcode, VTList, Ops, 3);
3296}
3297
3298SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3299 SDOperand N1, SDOperand N2, SDOperand N3,
3300 SDOperand N4) {
3301 SDOperand Ops[] = { N1, N2, N3, N4 };
3302 return getNode(Opcode, VTList, Ops, 4);
3303}
3304
3305SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3306 SDOperand N1, SDOperand N2, SDOperand N3,
3307 SDOperand N4, SDOperand N5) {
3308 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3309 return getNode(Opcode, VTList, Ops, 5);
3310}
3311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003312SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00003313 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314}
3315
3316SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
3317 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3318 E = VTList.end(); I != E; ++I) {
3319 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
3320 return makeVTList(&(*I)[0], 2);
3321 }
3322 std::vector<MVT::ValueType> V;
3323 V.push_back(VT1);
3324 V.push_back(VT2);
3325 VTList.push_front(V);
3326 return makeVTList(&(*VTList.begin())[0], 2);
3327}
3328SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
3329 MVT::ValueType VT3) {
3330 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3331 E = VTList.end(); I != E; ++I) {
3332 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3333 (*I)[2] == VT3)
3334 return makeVTList(&(*I)[0], 3);
3335 }
3336 std::vector<MVT::ValueType> V;
3337 V.push_back(VT1);
3338 V.push_back(VT2);
3339 V.push_back(VT3);
3340 VTList.push_front(V);
3341 return makeVTList(&(*VTList.begin())[0], 3);
3342}
3343
3344SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
3345 switch (NumVTs) {
3346 case 0: assert(0 && "Cannot have nodes without results!");
3347 case 1: return getVTList(VTs[0]);
3348 case 2: return getVTList(VTs[0], VTs[1]);
3349 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3350 default: break;
3351 }
3352
3353 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
3354 E = VTList.end(); I != E; ++I) {
3355 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3356
3357 bool NoMatch = false;
3358 for (unsigned i = 2; i != NumVTs; ++i)
3359 if (VTs[i] != (*I)[i]) {
3360 NoMatch = true;
3361 break;
3362 }
3363 if (!NoMatch)
3364 return makeVTList(&*I->begin(), NumVTs);
3365 }
3366
3367 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
3368 return makeVTList(&*VTList.begin()->begin(), NumVTs);
3369}
3370
3371
3372/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3373/// specified operands. If the resultant node already exists in the DAG,
3374/// this does not modify the specified node, instead it returns the node that
3375/// already exists. If the resultant node does not exist in the DAG, the
3376/// input node is returned. As a degenerate case, if you specify the same
3377/// input operands as the node already has, the input node is returned.
3378SDOperand SelectionDAG::
3379UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3380 SDNode *N = InN.Val;
3381 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3382
3383 // Check to see if there is no change.
3384 if (Op == N->getOperand(0)) return InN;
3385
3386 // See if the modified node already exists.
3387 void *InsertPos = 0;
3388 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3389 return SDOperand(Existing, InN.ResNo);
3390
3391 // Nope it doesn't. Remove the node from it's current place in the maps.
3392 if (InsertPos)
3393 RemoveNodeFromCSEMaps(N);
3394
3395 // Now we update the operands.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003396 N->OperandList[0].getVal()->removeUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003397 N->OperandList[0] = Op;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003398 N->OperandList[0].setUser(N);
3399 Op.Val->addUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003400
3401 // If this gets put into a CSE map, add it.
3402 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3403 return InN;
3404}
3405
3406SDOperand SelectionDAG::
3407UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3408 SDNode *N = InN.Val;
3409 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3410
3411 // Check to see if there is no change.
3412 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3413 return InN; // No operands changed, just return the input node.
3414
3415 // See if the modified node already exists.
3416 void *InsertPos = 0;
3417 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3418 return SDOperand(Existing, InN.ResNo);
3419
3420 // Nope it doesn't. Remove the node from it's current place in the maps.
3421 if (InsertPos)
3422 RemoveNodeFromCSEMaps(N);
3423
3424 // Now we update the operands.
3425 if (N->OperandList[0] != Op1) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003426 N->OperandList[0].getVal()->removeUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003427 N->OperandList[0] = Op1;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003428 N->OperandList[0].setUser(N);
3429 Op1.Val->addUser(0, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430 }
3431 if (N->OperandList[1] != Op2) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003432 N->OperandList[1].getVal()->removeUser(1, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003433 N->OperandList[1] = Op2;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003434 N->OperandList[1].setUser(N);
3435 Op2.Val->addUser(1, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003436 }
3437
3438 // If this gets put into a CSE map, add it.
3439 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3440 return InN;
3441}
3442
3443SDOperand SelectionDAG::
3444UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
3445 SDOperand Ops[] = { Op1, Op2, Op3 };
3446 return UpdateNodeOperands(N, Ops, 3);
3447}
3448
3449SDOperand SelectionDAG::
3450UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3451 SDOperand Op3, SDOperand Op4) {
3452 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3453 return UpdateNodeOperands(N, Ops, 4);
3454}
3455
3456SDOperand SelectionDAG::
3457UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3458 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
3459 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3460 return UpdateNodeOperands(N, Ops, 5);
3461}
3462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003463SDOperand SelectionDAG::
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003464UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003465 SDNode *N = InN.Val;
3466 assert(N->getNumOperands() == NumOps &&
3467 "Update with wrong number of operands");
3468
3469 // Check to see if there is no change.
3470 bool AnyChange = false;
3471 for (unsigned i = 0; i != NumOps; ++i) {
3472 if (Ops[i] != N->getOperand(i)) {
3473 AnyChange = true;
3474 break;
3475 }
3476 }
3477
3478 // No operands changed, just return the input node.
3479 if (!AnyChange) return InN;
3480
3481 // See if the modified node already exists.
3482 void *InsertPos = 0;
3483 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
3484 return SDOperand(Existing, InN.ResNo);
3485
Dan Gohman14a027d2008-05-02 00:05:03 +00003486 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003487 if (InsertPos)
3488 RemoveNodeFromCSEMaps(N);
3489
3490 // Now we update the operands.
3491 for (unsigned i = 0; i != NumOps; ++i) {
3492 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003493 N->OperandList[i].getVal()->removeUser(i, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003494 N->OperandList[i] = Ops[i];
Roman Levenstein05650fd2008-04-07 10:06:32 +00003495 N->OperandList[i].setUser(N);
3496 Ops[i].Val->addUser(i, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003497 }
3498 }
3499
3500 // If this gets put into a CSE map, add it.
3501 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
3502 return InN;
3503}
3504
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003505/// MorphNodeTo - This frees the operands of the current node, resets the
3506/// opcode, types, and operands to the specified value. This should only be
3507/// used by the SelectionDAG class.
3508void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman1d47f5c2008-04-17 23:02:12 +00003509 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510 NodeType = Opc;
3511 ValueList = L.VTs;
3512 NumValues = L.NumVTs;
3513
3514 // Clear the operands list, updating used nodes to remove this from their
3515 // use list.
3516 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003517 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003518
3519 // If NumOps is larger than the # of operands we currently have, reallocate
3520 // the operand list.
3521 if (NumOps > NumOperands) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003522 if (OperandsNeedDelete) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003523 delete [] OperandList;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003524 }
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003525 OperandList = new SDUse[NumOps];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003526 OperandsNeedDelete = true;
3527 }
3528
3529 // Assign the new operands.
3530 NumOperands = NumOps;
3531
3532 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3533 OperandList[i] = Ops[i];
Roman Levenstein05650fd2008-04-07 10:06:32 +00003534 OperandList[i].setUser(this);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003535 SDNode *N = OperandList[i].getVal();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003536 N->addUser(i, this);
3537 ++N->UsesSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003538 }
3539}
3540
3541/// SelectNodeTo - These are used for target selectors to *mutate* the
3542/// specified node to have the specified return type, Target opcode, and
3543/// operands. Note that target opcodes are stored as
3544/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
3545///
3546/// Note that SelectNodeTo returns the resultant node. If there is already a
3547/// node of the specified opcode and operands, it returns that node instead of
3548/// the current one.
3549SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3550 MVT::ValueType VT) {
3551 SDVTList VTs = getVTList(VT);
3552 FoldingSetNodeID ID;
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003553 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003554 void *IP = 0;
3555 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3556 return ON;
3557
3558 RemoveNodeFromCSEMaps(N);
3559
Dan Gohman1d47f5c2008-04-17 23:02:12 +00003560 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, SDOperandPtr(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003561
3562 CSEMap.InsertNode(N, IP);
3563 return N;
3564}
3565
3566SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3567 MVT::ValueType VT, SDOperand Op1) {
3568 // If an identical node already exists, use it.
3569 SDVTList VTs = getVTList(VT);
3570 SDOperand Ops[] = { Op1 };
3571
3572 FoldingSetNodeID ID;
3573 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3574 void *IP = 0;
3575 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3576 return ON;
3577
3578 RemoveNodeFromCSEMaps(N);
3579 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3580 CSEMap.InsertNode(N, IP);
3581 return N;
3582}
3583
3584SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3585 MVT::ValueType VT, SDOperand Op1,
3586 SDOperand Op2) {
3587 // If an identical node already exists, use it.
3588 SDVTList VTs = getVTList(VT);
3589 SDOperand Ops[] = { Op1, Op2 };
3590
3591 FoldingSetNodeID ID;
3592 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3593 void *IP = 0;
3594 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3595 return ON;
3596
3597 RemoveNodeFromCSEMaps(N);
3598
3599 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3600
3601 CSEMap.InsertNode(N, IP); // Memoize the new node.
3602 return N;
3603}
3604
3605SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3606 MVT::ValueType VT, SDOperand Op1,
3607 SDOperand Op2, SDOperand Op3) {
3608 // If an identical node already exists, use it.
3609 SDVTList VTs = getVTList(VT);
3610 SDOperand Ops[] = { Op1, Op2, Op3 };
3611 FoldingSetNodeID ID;
3612 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3613 void *IP = 0;
3614 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3615 return ON;
3616
3617 RemoveNodeFromCSEMaps(N);
3618
3619 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3620
3621 CSEMap.InsertNode(N, IP); // Memoize the new node.
3622 return N;
3623}
3624
3625SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003626 MVT::ValueType VT, SDOperandPtr Ops,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003627 unsigned NumOps) {
3628 // If an identical node already exists, use it.
3629 SDVTList VTs = getVTList(VT);
3630 FoldingSetNodeID ID;
3631 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3632 void *IP = 0;
3633 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3634 return ON;
3635
3636 RemoveNodeFromCSEMaps(N);
3637 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3638
3639 CSEMap.InsertNode(N, IP); // Memoize the new node.
3640 return N;
3641}
3642
3643SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3644 MVT::ValueType VT1, MVT::ValueType VT2,
3645 SDOperand Op1, SDOperand Op2) {
3646 SDVTList VTs = getVTList(VT1, VT2);
3647 FoldingSetNodeID ID;
3648 SDOperand Ops[] = { Op1, Op2 };
3649 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3650 void *IP = 0;
3651 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3652 return ON;
3653
3654 RemoveNodeFromCSEMaps(N);
3655 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3656 CSEMap.InsertNode(N, IP); // Memoize the new node.
3657 return N;
3658}
3659
3660SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3661 MVT::ValueType VT1, MVT::ValueType VT2,
3662 SDOperand Op1, SDOperand Op2,
3663 SDOperand Op3) {
3664 // If an identical node already exists, use it.
3665 SDVTList VTs = getVTList(VT1, VT2);
3666 SDOperand Ops[] = { Op1, Op2, Op3 };
3667 FoldingSetNodeID ID;
3668 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3669 void *IP = 0;
3670 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3671 return ON;
3672
3673 RemoveNodeFromCSEMaps(N);
3674
3675 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3676 CSEMap.InsertNode(N, IP); // Memoize the new node.
3677 return N;
3678}
3679
3680
3681/// getTargetNode - These are used for target selectors to create a new node
3682/// with specified return type(s), target opcode, and operands.
3683///
3684/// Note that getTargetNode returns the resultant node. If there is already a
3685/// node of the specified opcode and operands, it returns that node instead of
3686/// the current one.
3687SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3688 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3689}
3690SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3691 SDOperand Op1) {
3692 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3693}
3694SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3695 SDOperand Op1, SDOperand Op2) {
3696 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3697}
3698SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3699 SDOperand Op1, SDOperand Op2,
3700 SDOperand Op3) {
3701 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3702}
3703SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003704 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003705 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3706}
3707SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003708 MVT::ValueType VT2) {
3709 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3710 SDOperand Op;
3711 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3712}
3713SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003714 MVT::ValueType VT2, SDOperand Op1) {
3715 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3716 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3717}
3718SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3719 MVT::ValueType VT2, SDOperand Op1,
3720 SDOperand Op2) {
3721 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3722 SDOperand Ops[] = { Op1, Op2 };
3723 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3724}
3725SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3726 MVT::ValueType VT2, SDOperand Op1,
3727 SDOperand Op2, SDOperand Op3) {
3728 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3729 SDOperand Ops[] = { Op1, Op2, Op3 };
3730 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3731}
3732SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3733 MVT::ValueType VT2,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003734 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3736 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3737}
3738SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3739 MVT::ValueType VT2, MVT::ValueType VT3,
3740 SDOperand Op1, SDOperand Op2) {
3741 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3742 SDOperand Ops[] = { Op1, Op2 };
3743 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3744}
3745SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3746 MVT::ValueType VT2, MVT::ValueType VT3,
3747 SDOperand Op1, SDOperand Op2,
3748 SDOperand Op3) {
3749 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3750 SDOperand Ops[] = { Op1, Op2, Op3 };
3751 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3752}
3753SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3754 MVT::ValueType VT2, MVT::ValueType VT3,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003755 SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003756 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3757 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3758}
Evan Chenge1d067e2007-09-12 23:39:49 +00003759SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3760 MVT::ValueType VT2, MVT::ValueType VT3,
3761 MVT::ValueType VT4,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003762 SDOperandPtr Ops, unsigned NumOps) {
Evan Chenge1d067e2007-09-12 23:39:49 +00003763 std::vector<MVT::ValueType> VTList;
3764 VTList.push_back(VT1);
3765 VTList.push_back(VT2);
3766 VTList.push_back(VT3);
3767 VTList.push_back(VT4);
3768 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3769 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3770}
Evan Chenge3940912007-10-05 01:10:49 +00003771SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3772 std::vector<MVT::ValueType> &ResultTys,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003773 SDOperandPtr Ops, unsigned NumOps) {
Evan Chenge3940912007-10-05 01:10:49 +00003774 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3775 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3776 Ops, NumOps).Val;
3777}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778
Evan Chengd1113582008-03-22 01:55:50 +00003779/// getNodeIfExists - Get the specified node if it's already available, or
3780/// else return NULL.
3781SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003782 SDOperandPtr Ops, unsigned NumOps) {
Evan Chengd1113582008-03-22 01:55:50 +00003783 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3784 FoldingSetNodeID ID;
3785 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3786 void *IP = 0;
3787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3788 return E;
3789 }
3790 return NULL;
3791}
3792
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003793
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003794/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3795/// This can cause recursive merging of nodes in the DAG.
3796///
Chris Lattnerdca329f2008-02-03 03:35:22 +00003797/// This version assumes From has a single result value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003798///
Chris Lattnerdca329f2008-02-03 03:35:22 +00003799void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003800 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00003801 SDNode *From = FromN.Val;
Chris Lattnerdca329f2008-02-03 03:35:22 +00003802 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003803 "Cannot replace with this method!");
Chris Lattnerdca329f2008-02-03 03:35:22 +00003804 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levenstein05650fd2008-04-07 10:06:32 +00003805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003806 while (!From->use_empty()) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003807 SDNode::use_iterator UI = From->use_begin();
3808 SDNode *U = UI->getUser();
3809
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 // This node is about to morph, remove its old self from the CSE maps.
3811 RemoveNodeFromCSEMaps(U);
Roman Levenstein05650fd2008-04-07 10:06:32 +00003812 int operandNum = 0;
3813 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3814 I != E; ++I, ++operandNum)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003815 if (I->getVal() == From) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003816 From->removeUser(operandNum, U);
Chris Lattnerdca329f2008-02-03 03:35:22 +00003817 *I = To;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003818 I->setUser(U);
3819 To.Val->addUser(operandNum, U);
3820 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821
3822 // Now that we have modified U, add it back to the CSE maps. If it already
3823 // exists there, recursively merge the results together.
3824 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003825 ReplaceAllUsesWith(U, Existing, UpdateListener);
3826 // U is now dead. Inform the listener if it exists and delete it.
3827 if (UpdateListener)
3828 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003829 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003830 } else {
3831 // If the node doesn't already exist, we updated it. Inform a listener if
3832 // it exists.
3833 if (UpdateListener)
3834 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003835 }
3836 }
3837}
3838
3839/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3840/// This can cause recursive merging of nodes in the DAG.
3841///
3842/// This version assumes From/To have matching types and numbers of result
3843/// values.
3844///
3845void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003846 DAGUpdateListener *UpdateListener) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003847 assert(From != To && "Cannot replace uses of with self");
3848 assert(From->getNumValues() == To->getNumValues() &&
3849 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattnerdca329f2008-02-03 03:35:22 +00003850 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003851 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3852 UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853
3854 while (!From->use_empty()) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003855 SDNode::use_iterator UI = From->use_begin();
3856 SDNode *U = UI->getUser();
3857
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003858 // This node is about to morph, remove its old self from the CSE maps.
3859 RemoveNodeFromCSEMaps(U);
Roman Levenstein05650fd2008-04-07 10:06:32 +00003860 int operandNum = 0;
3861 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3862 I != E; ++I, ++operandNum)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003863 if (I->getVal() == From) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003864 From->removeUser(operandNum, U);
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003865 I->getVal() = To;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003866 To->addUser(operandNum, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867 }
Roman Levenstein05650fd2008-04-07 10:06:32 +00003868
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003869 // Now that we have modified U, add it back to the CSE maps. If it already
3870 // exists there, recursively merge the results together.
3871 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003872 ReplaceAllUsesWith(U, Existing, UpdateListener);
3873 // U is now dead. Inform the listener if it exists and delete it.
3874 if (UpdateListener)
3875 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003876 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003877 } else {
3878 // If the node doesn't already exist, we updated it. Inform a listener if
3879 // it exists.
3880 if (UpdateListener)
3881 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003882 }
3883 }
3884}
3885
3886/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3887/// This can cause recursive merging of nodes in the DAG.
3888///
3889/// This version can replace From with any result values. To must match the
3890/// number and types of values returned by From.
3891void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003892 SDOperandPtr To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003893 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00003894 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003895 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003896
3897 while (!From->use_empty()) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00003898 SDNode::use_iterator UI = From->use_begin();
3899 SDNode *U = UI->getUser();
3900
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901 // This node is about to morph, remove its old self from the CSE maps.
3902 RemoveNodeFromCSEMaps(U);
Roman Levenstein05650fd2008-04-07 10:06:32 +00003903 int operandNum = 0;
3904 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3905 I != E; ++I, ++operandNum)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00003906 if (I->getVal() == From) {
3907 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levenstein05650fd2008-04-07 10:06:32 +00003908 From->removeUser(operandNum, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003909 *I = ToOp;
Roman Levenstein05650fd2008-04-07 10:06:32 +00003910 I->setUser(U);
3911 ToOp.Val->addUser(operandNum, U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003912 }
Roman Levenstein05650fd2008-04-07 10:06:32 +00003913
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003914 // Now that we have modified U, add it back to the CSE maps. If it already
3915 // exists there, recursively merge the results together.
3916 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003917 ReplaceAllUsesWith(U, Existing, UpdateListener);
3918 // U is now dead. Inform the listener if it exists and delete it.
3919 if (UpdateListener)
3920 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003922 } else {
3923 // If the node doesn't already exist, we updated it. Inform a listener if
3924 // it exists.
3925 if (UpdateListener)
3926 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003927 }
3928 }
3929}
3930
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003931namespace {
3932 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3933 /// any deleted nodes from the set passed into its constructor and recursively
3934 /// notifies another update listener if specified.
3935 class ChainedSetUpdaterListener :
3936 public SelectionDAG::DAGUpdateListener {
3937 SmallSetVector<SDNode*, 16> &Set;
3938 SelectionDAG::DAGUpdateListener *Chain;
3939 public:
3940 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3941 SelectionDAG::DAGUpdateListener *chain)
3942 : Set(set), Chain(chain) {}
Roman Levenstein05650fd2008-04-07 10:06:32 +00003943
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003944 virtual void NodeDeleted(SDNode *N) {
3945 Set.remove(N);
3946 if (Chain) Chain->NodeDeleted(N);
3947 }
3948 virtual void NodeUpdated(SDNode *N) {
3949 if (Chain) Chain->NodeUpdated(N);
3950 }
3951 };
3952}
3953
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3955/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003956/// handled the same way as for ReplaceAllUsesWith.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003958 DAGUpdateListener *UpdateListener){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003959 assert(From != To && "Cannot replace a value with itself");
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003960
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003961 // Handle the simple, trivial, case efficiently.
Chris Lattnerdca329f2008-02-03 03:35:22 +00003962 if (From.Val->getNumValues() == 1) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003963 ReplaceAllUsesWith(From, To, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 return;
3965 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003966
3967 if (From.use_empty()) return;
3968
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003969 // Get all of the users of From.Val. We want these in a nice,
3970 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levenstein05650fd2008-04-07 10:06:32 +00003971 SmallSetVector<SDNode*, 16> Users;
3972 for (SDNode::use_iterator UI = From.Val->use_begin(),
3973 E = From.Val->use_end(); UI != E; ++UI) {
3974 SDNode *User = UI->getUser();
3975 if (!Users.count(User))
3976 Users.insert(User);
3977 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003979 // When one of the recursive merges deletes nodes from the graph, we need to
3980 // make sure that UpdateListener is notified *and* that the node is removed
3981 // from Users if present. CSUL does this.
3982 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner8a258202007-10-15 06:10:22 +00003983
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003984 while (!Users.empty()) {
3985 // We know that this user uses some value of From. If it is the right
3986 // value, update it.
3987 SDNode *User = Users.back();
3988 Users.pop_back();
3989
Chris Lattner8a258202007-10-15 06:10:22 +00003990 // Scan for an operand that matches From.
Roman Levenstein05650fd2008-04-07 10:06:32 +00003991 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner8a258202007-10-15 06:10:22 +00003992 for (; Op != E; ++Op)
3993 if (*Op == From) break;
3994
3995 // If there are no matches, the user must use some other result of From.
3996 if (Op == E) continue;
3997
3998 // Okay, we know this user needs to be updated. Remove its old self
3999 // from the CSE maps.
4000 RemoveNodeFromCSEMaps(User);
4001
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004002 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner8a258202007-10-15 06:10:22 +00004003 for (; Op != E; ++Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004004 if (*Op == From) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00004005 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif5bd3d9b2008-04-11 09:34:57 +00004006 *Op = To;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004007 Op->setUser(User);
4008 To.Val->addUser(Op-User->op_begin(), User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004009 }
4010 }
Chris Lattner8a258202007-10-15 06:10:22 +00004011
4012 // Now that we have modified User, add it back to the CSE maps. If it
4013 // already exists there, recursively merge the results together.
4014 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004015 if (!Existing) {
4016 if (UpdateListener) UpdateListener->NodeUpdated(User);
4017 continue; // Continue on to next user.
4018 }
Chris Lattner8a258202007-10-15 06:10:22 +00004019
4020 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004021 // to replace the dead one with the existing one. This can cause
Chris Lattner8a258202007-10-15 06:10:22 +00004022 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004023 // can cause deletion of nodes that used the old value. To handle this, we
4024 // use CSUL to remove them from the Users set.
4025 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner8a258202007-10-15 06:10:22 +00004026
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004027 // User is now dead. Notify a listener if present.
4028 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner8a258202007-10-15 06:10:22 +00004029 DeleteNodeNotInCSEMaps(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030 }
4031}
4032
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004033/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4034/// their allnodes order. It returns the maximum id.
4035unsigned SelectionDAG::AssignNodeIds() {
4036 unsigned Id = 0;
4037 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4038 SDNode *N = I;
4039 N->setNodeId(Id++);
4040 }
4041 return Id;
4042}
4043
4044/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
4045/// based on their topological order. It returns the maximum id and a vector
4046/// of the SDNodes* in assigned order by reference.
4047unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
4048 unsigned DAGSize = AllNodes.size();
4049 std::vector<unsigned> InDegree(DAGSize);
4050 std::vector<SDNode*> Sources;
4051
4052 // Use a two pass approach to avoid using a std::map which is slow.
4053 unsigned Id = 0;
4054 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4055 SDNode *N = I;
4056 N->setNodeId(Id++);
4057 unsigned Degree = N->use_size();
4058 InDegree[N->getNodeId()] = Degree;
4059 if (Degree == 0)
4060 Sources.push_back(N);
4061 }
4062
4063 TopOrder.clear();
4064 while (!Sources.empty()) {
4065 SDNode *N = Sources.back();
4066 Sources.pop_back();
4067 TopOrder.push_back(N);
4068 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004069 SDNode *P = I->getVal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 unsigned Degree = --InDegree[P->getNodeId()];
4071 if (Degree == 0)
4072 Sources.push_back(P);
4073 }
4074 }
4075
4076 // Second pass, assign the actual topological order as node ids.
4077 Id = 0;
4078 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4079 TI != TE; ++TI)
4080 (*TI)->setNodeId(Id++);
4081
4082 return Id;
4083}
4084
4085
4086
4087//===----------------------------------------------------------------------===//
4088// SDNode Class
4089//===----------------------------------------------------------------------===//
4090
4091// Out-of-line virtual method to give class a home.
4092void SDNode::ANCHOR() {}
4093void UnarySDNode::ANCHOR() {}
4094void BinarySDNode::ANCHOR() {}
4095void TernarySDNode::ANCHOR() {}
4096void HandleSDNode::ANCHOR() {}
4097void StringSDNode::ANCHOR() {}
4098void ConstantSDNode::ANCHOR() {}
4099void ConstantFPSDNode::ANCHOR() {}
4100void GlobalAddressSDNode::ANCHOR() {}
4101void FrameIndexSDNode::ANCHOR() {}
4102void JumpTableSDNode::ANCHOR() {}
4103void ConstantPoolSDNode::ANCHOR() {}
4104void BasicBlockSDNode::ANCHOR() {}
4105void SrcValueSDNode::ANCHOR() {}
Dan Gohman12a9c082008-02-06 22:27:42 +00004106void MemOperandSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004107void RegisterSDNode::ANCHOR() {}
4108void ExternalSymbolSDNode::ANCHOR() {}
4109void CondCodeSDNode::ANCHOR() {}
Duncan Sandsc93fae32008-03-21 09:14:45 +00004110void ARG_FLAGSSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111void VTSDNode::ANCHOR() {}
4112void LoadSDNode::ANCHOR() {}
4113void StoreSDNode::ANCHOR() {}
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004114void AtomicSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004115
4116HandleSDNode::~HandleSDNode() {
4117 SDVTList VTs = { 0, 0 };
Dan Gohman1d47f5c2008-04-17 23:02:12 +00004118 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119}
4120
4121GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
4122 MVT::ValueType VT, int o)
4123 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman53491e92007-07-23 20:24:29 +00004124 cast<GlobalVariable>(GA)->isThreadLocal() ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125 // Thread Local
4126 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4127 // Non Thread Local
4128 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4129 getSDVTList(VT)), Offset(o) {
4130 TheGlobal = const_cast<GlobalValue*>(GA);
4131}
4132
Dan Gohman1fad9e62008-04-07 19:35:22 +00004133/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman12a9c082008-02-06 22:27:42 +00004134/// reference performed by this load or store.
Dan Gohman1fad9e62008-04-07 19:35:22 +00004135MachineMemOperand LSBaseSDNode::getMemOperand() const {
Dan Gohman12a9c082008-02-06 22:27:42 +00004136 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
4137 int Flags =
Dan Gohman1fad9e62008-04-07 19:35:22 +00004138 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4139 MachineMemOperand::MOStore;
4140 if (IsVolatile) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman12a9c082008-02-06 22:27:42 +00004141
4142 // Check if the load references a frame index, and does not have
4143 // an SV attached.
4144 const FrameIndexSDNode *FI =
4145 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4146 if (!getSrcValue() && FI)
Dan Gohman1fad9e62008-04-07 19:35:22 +00004147 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4148 FI->getIndex(), Size, Alignment);
Dan Gohman12a9c082008-02-06 22:27:42 +00004149 else
Dan Gohman1fad9e62008-04-07 19:35:22 +00004150 return MachineMemOperand(getSrcValue(), Flags,
4151 getSrcValueOffset(), Size, Alignment);
Dan Gohman12a9c082008-02-06 22:27:42 +00004152}
4153
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154/// Profile - Gather unique data for the node.
4155///
4156void SDNode::Profile(FoldingSetNodeID &ID) {
4157 AddNodeIDNode(ID, this);
4158}
4159
4160/// getValueTypeList - Return a pointer to the specified value type.
4161///
Dan Gohman8cdf7892008-02-08 03:26:46 +00004162const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00004163 if (MVT::isExtendedVT(VT)) {
4164 static std::set<MVT::ValueType> EVTs;
Dan Gohman8cdf7892008-02-08 03:26:46 +00004165 return &(*EVTs.insert(VT).first);
Duncan Sandsa9810f32007-10-16 09:56:48 +00004166 } else {
4167 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
4168 VTs[VT] = VT;
4169 return &VTs[VT];
4170 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171}
Duncan Sandsa9810f32007-10-16 09:56:48 +00004172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004173/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4174/// indicated value. This method ignores uses of other values defined by this
4175/// operation.
4176bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
4177 assert(Value < getNumValues() && "Bad value!");
4178
4179 // If there is only one value, this is easy.
4180 if (getNumValues() == 1)
4181 return use_size() == NUses;
Evan Cheng0af04f72007-08-02 05:29:38 +00004182 if (use_size() < NUses) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004183
4184 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4185
4186 SmallPtrSet<SDNode*, 32> UsersHandled;
4187
Roman Levenstein05650fd2008-04-07 10:06:32 +00004188 // TODO: Only iterate over uses of a given value of the node
4189 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4190 if (*UI == TheValue) {
4191 if (NUses == 0)
4192 return false;
4193 --NUses;
4194 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004195 }
4196
4197 // Found exactly the right number of uses?
4198 return NUses == 0;
4199}
4200
4201
Evan Cheng0af04f72007-08-02 05:29:38 +00004202/// hasAnyUseOfValue - Return true if there are any use of the indicated
4203/// value. This method ignores uses of other values defined by this operation.
4204bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4205 assert(Value < getNumValues() && "Bad value!");
4206
Dan Gohman301f4052008-01-29 13:02:09 +00004207 if (use_empty()) return false;
Evan Cheng0af04f72007-08-02 05:29:38 +00004208
4209 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4210
4211 SmallPtrSet<SDNode*, 32> UsersHandled;
4212
Roman Levenstein05650fd2008-04-07 10:06:32 +00004213 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4214 SDNode *User = UI->getUser();
Evan Cheng0af04f72007-08-02 05:29:38 +00004215 if (User->getNumOperands() == 1 ||
4216 UsersHandled.insert(User)) // First time we've seen this?
4217 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4218 if (User->getOperand(i) == TheValue) {
4219 return true;
4220 }
4221 }
4222
4223 return false;
4224}
4225
4226
Evan Chengd9387682008-03-04 00:41:45 +00004227/// isOnlyUseOf - Return true if this node is the only use of N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228///
Evan Chengd9387682008-03-04 00:41:45 +00004229bool SDNode::isOnlyUseOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230 bool Seen = false;
4231 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00004232 SDNode *User = I->getUser();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 if (User == this)
4234 Seen = true;
4235 else
4236 return false;
4237 }
4238
4239 return Seen;
4240}
4241
4242/// isOperand - Return true if this node is an operand of N.
4243///
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004244bool SDOperand::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4246 if (*this == N->getOperand(i))
4247 return true;
4248 return false;
4249}
4250
Evan Chengd9387682008-03-04 00:41:45 +00004251bool SDNode::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004253 if (this == N->OperandList[i].getVal())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254 return true;
4255 return false;
4256}
4257
Chris Lattner10d94f92008-01-16 05:49:24 +00004258/// reachesChainWithoutSideEffects - Return true if this operand (which must
4259/// be a chain) reaches the specified operand without crossing any
4260/// side-effecting instructions. In practice, this looks through token
4261/// factors and non-volatile loads. In order to remain efficient, this only
4262/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004263bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner10d94f92008-01-16 05:49:24 +00004264 unsigned Depth) const {
4265 if (*this == Dest) return true;
4266
4267 // Don't search too deeply, we just want to be able to see through
4268 // TokenFactor's etc.
4269 if (Depth == 0) return false;
4270
4271 // If this is a token factor, all inputs to the TF happen in parallel. If any
4272 // of the operands of the TF reach dest, then we can do the xform.
4273 if (getOpcode() == ISD::TokenFactor) {
4274 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4275 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4276 return true;
4277 return false;
4278 }
4279
4280 // Loads don't have side effects, look through them.
4281 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4282 if (!Ld->isVolatile())
4283 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4284 }
4285 return false;
4286}
4287
4288
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
4290 SmallPtrSet<SDNode *, 32> &Visited) {
4291 if (found || !Visited.insert(N))
4292 return;
4293
4294 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4295 SDNode *Op = N->getOperand(i).Val;
4296 if (Op == P) {
4297 found = true;
4298 return;
4299 }
4300 findPredecessor(Op, P, found, Visited);
4301 }
4302}
4303
Evan Chengd9387682008-03-04 00:41:45 +00004304/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305/// is either an operand of N or it can be reached by recursively traversing
4306/// up the operands.
4307/// NOTE: this is an expensive method. Use it carefully.
Evan Chengd9387682008-03-04 00:41:45 +00004308bool SDNode::isPredecessorOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004309 SmallPtrSet<SDNode *, 32> Visited;
4310 bool found = false;
4311 findPredecessor(N, this, found, Visited);
4312 return found;
4313}
4314
4315uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4316 assert(Num < NumOperands && "Invalid child # of SDNode!");
4317 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4318}
4319
4320std::string SDNode::getOperationName(const SelectionDAG *G) const {
4321 switch (getOpcode()) {
4322 default:
4323 if (getOpcode() < ISD::BUILTIN_OP_END)
4324 return "<<Unknown DAG Node>>";
4325 else {
4326 if (G) {
4327 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
4328 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner0c2a4f32008-01-07 03:13:06 +00004329 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004330
4331 TargetLowering &TLI = G->getTargetLoweringInfo();
4332 const char *Name =
4333 TLI.getTargetNodeName(getOpcode());
4334 if (Name) return Name;
4335 }
4336
4337 return "<<Unknown Target Node>>";
4338 }
4339
Evan Chengd1d68072008-03-08 00:58:38 +00004340 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth785610d2008-02-16 01:24:58 +00004341 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthe44f3902008-02-21 06:45:13 +00004342 case ISD::ATOMIC_LCS: return "AtomicLCS";
4343 case ISD::ATOMIC_LAS: return "AtomicLAS";
Mon P Wang078a62d2008-05-05 19:05:59 +00004344 case ISD::ATOMIC_LSS: return "AtomicLSS";
4345 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4346 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4347 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
4348 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4349 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4350 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4351 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4352 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004353 case ISD::PCMARKER: return "PCMarker";
4354 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
4355 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman12a9c082008-02-06 22:27:42 +00004356 case ISD::MEMOPERAND: return "MemOperand";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 case ISD::EntryToken: return "EntryToken";
4358 case ISD::TokenFactor: return "TokenFactor";
4359 case ISD::AssertSext: return "AssertSext";
4360 case ISD::AssertZext: return "AssertZext";
4361
4362 case ISD::STRING: return "String";
4363 case ISD::BasicBlock: return "BasicBlock";
Duncan Sandsc93fae32008-03-21 09:14:45 +00004364 case ISD::ARG_FLAGS: return "ArgFlags";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365 case ISD::VALUETYPE: return "ValueType";
4366 case ISD::Register: return "Register";
4367
4368 case ISD::Constant: return "Constant";
4369 case ISD::ConstantFP: return "ConstantFP";
4370 case ISD::GlobalAddress: return "GlobalAddress";
4371 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
4372 case ISD::FrameIndex: return "FrameIndex";
4373 case ISD::JumpTable: return "JumpTable";
4374 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
4375 case ISD::RETURNADDR: return "RETURNADDR";
4376 case ISD::FRAMEADDR: return "FRAMEADDR";
4377 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
4378 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4379 case ISD::EHSELECTION: return "EHSELECTION";
4380 case ISD::EH_RETURN: return "EH_RETURN";
4381 case ISD::ConstantPool: return "ConstantPool";
4382 case ISD::ExternalSymbol: return "ExternalSymbol";
4383 case ISD::INTRINSIC_WO_CHAIN: {
4384 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4385 return Intrinsic::getName((Intrinsic::ID)IID);
4386 }
4387 case ISD::INTRINSIC_VOID:
4388 case ISD::INTRINSIC_W_CHAIN: {
4389 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
4390 return Intrinsic::getName((Intrinsic::ID)IID);
4391 }
4392
4393 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
4394 case ISD::TargetConstant: return "TargetConstant";
4395 case ISD::TargetConstantFP:return "TargetConstantFP";
4396 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
4397 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
4398 case ISD::TargetFrameIndex: return "TargetFrameIndex";
4399 case ISD::TargetJumpTable: return "TargetJumpTable";
4400 case ISD::TargetConstantPool: return "TargetConstantPool";
4401 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
4402
4403 case ISD::CopyToReg: return "CopyToReg";
4404 case ISD::CopyFromReg: return "CopyFromReg";
4405 case ISD::UNDEF: return "undef";
4406 case ISD::MERGE_VALUES: return "merge_values";
4407 case ISD::INLINEASM: return "inlineasm";
4408 case ISD::LABEL: return "label";
Evan Cheng2e28d622008-02-02 04:07:54 +00004409 case ISD::DECLARE: return "declare";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 case ISD::HANDLENODE: return "handlenode";
4411 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
4412 case ISD::CALL: return "call";
4413
4414 // Unary operators
4415 case ISD::FABS: return "fabs";
4416 case ISD::FNEG: return "fneg";
4417 case ISD::FSQRT: return "fsqrt";
4418 case ISD::FSIN: return "fsin";
4419 case ISD::FCOS: return "fcos";
4420 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00004421 case ISD::FPOW: return "fpow";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004422
4423 // Binary operators
4424 case ISD::ADD: return "add";
4425 case ISD::SUB: return "sub";
4426 case ISD::MUL: return "mul";
4427 case ISD::MULHU: return "mulhu";
4428 case ISD::MULHS: return "mulhs";
4429 case ISD::SDIV: return "sdiv";
4430 case ISD::UDIV: return "udiv";
4431 case ISD::SREM: return "srem";
4432 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00004433 case ISD::SMUL_LOHI: return "smul_lohi";
4434 case ISD::UMUL_LOHI: return "umul_lohi";
4435 case ISD::SDIVREM: return "sdivrem";
4436 case ISD::UDIVREM: return "divrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437 case ISD::AND: return "and";
4438 case ISD::OR: return "or";
4439 case ISD::XOR: return "xor";
4440 case ISD::SHL: return "shl";
4441 case ISD::SRA: return "sra";
4442 case ISD::SRL: return "srl";
4443 case ISD::ROTL: return "rotl";
4444 case ISD::ROTR: return "rotr";
4445 case ISD::FADD: return "fadd";
4446 case ISD::FSUB: return "fsub";
4447 case ISD::FMUL: return "fmul";
4448 case ISD::FDIV: return "fdiv";
4449 case ISD::FREM: return "frem";
4450 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00004451 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452
4453 case ISD::SETCC: return "setcc";
Nate Begeman9a1ce152008-05-12 19:40:03 +00004454 case ISD::VSETCC: return "vsetcc";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 case ISD::SELECT: return "select";
4456 case ISD::SELECT_CC: return "select_cc";
4457 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
4458 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
4459 case ISD::CONCAT_VECTORS: return "concat_vectors";
4460 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
4461 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
4462 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
4463 case ISD::CARRY_FALSE: return "carry_false";
4464 case ISD::ADDC: return "addc";
4465 case ISD::ADDE: return "adde";
4466 case ISD::SUBC: return "subc";
4467 case ISD::SUBE: return "sube";
4468 case ISD::SHL_PARTS: return "shl_parts";
4469 case ISD::SRA_PARTS: return "sra_parts";
4470 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lambb768c2e2007-07-26 07:34:40 +00004471
4472 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4473 case ISD::INSERT_SUBREG: return "insert_subreg";
4474
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 // Conversion operators.
4476 case ISD::SIGN_EXTEND: return "sign_extend";
4477 case ISD::ZERO_EXTEND: return "zero_extend";
4478 case ISD::ANY_EXTEND: return "any_extend";
4479 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
4480 case ISD::TRUNCATE: return "truncate";
4481 case ISD::FP_ROUND: return "fp_round";
Dan Gohman819574c2008-01-31 00:41:03 +00004482 case ISD::FLT_ROUNDS_: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004483 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
4484 case ISD::FP_EXTEND: return "fp_extend";
4485
4486 case ISD::SINT_TO_FP: return "sint_to_fp";
4487 case ISD::UINT_TO_FP: return "uint_to_fp";
4488 case ISD::FP_TO_SINT: return "fp_to_sint";
4489 case ISD::FP_TO_UINT: return "fp_to_uint";
4490 case ISD::BIT_CONVERT: return "bit_convert";
4491
4492 // Control flow instructions
4493 case ISD::BR: return "br";
4494 case ISD::BRIND: return "brind";
4495 case ISD::BR_JT: return "br_jt";
4496 case ISD::BRCOND: return "brcond";
4497 case ISD::BR_CC: return "br_cc";
4498 case ISD::RET: return "ret";
4499 case ISD::CALLSEQ_START: return "callseq_start";
4500 case ISD::CALLSEQ_END: return "callseq_end";
4501
4502 // Other operators
4503 case ISD::LOAD: return "load";
4504 case ISD::STORE: return "store";
4505 case ISD::VAARG: return "vaarg";
4506 case ISD::VACOPY: return "vacopy";
4507 case ISD::VAEND: return "vaend";
4508 case ISD::VASTART: return "vastart";
4509 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
4510 case ISD::EXTRACT_ELEMENT: return "extract_element";
4511 case ISD::BUILD_PAIR: return "build_pair";
4512 case ISD::STACKSAVE: return "stacksave";
4513 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00004514 case ISD::TRAP: return "trap";
4515
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004516 // Bit manipulation
4517 case ISD::BSWAP: return "bswap";
4518 case ISD::CTPOP: return "ctpop";
4519 case ISD::CTTZ: return "cttz";
4520 case ISD::CTLZ: return "ctlz";
4521
4522 // Debug info
4523 case ISD::LOCATION: return "location";
4524 case ISD::DEBUG_LOC: return "debug_loc";
4525
Duncan Sands38947cd2007-07-27 12:58:54 +00004526 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00004527 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00004528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 case ISD::CONDCODE:
4530 switch (cast<CondCodeSDNode>(this)->get()) {
4531 default: assert(0 && "Unknown setcc condition!");
4532 case ISD::SETOEQ: return "setoeq";
4533 case ISD::SETOGT: return "setogt";
4534 case ISD::SETOGE: return "setoge";
4535 case ISD::SETOLT: return "setolt";
4536 case ISD::SETOLE: return "setole";
4537 case ISD::SETONE: return "setone";
4538
4539 case ISD::SETO: return "seto";
4540 case ISD::SETUO: return "setuo";
4541 case ISD::SETUEQ: return "setue";
4542 case ISD::SETUGT: return "setugt";
4543 case ISD::SETUGE: return "setuge";
4544 case ISD::SETULT: return "setult";
4545 case ISD::SETULE: return "setule";
4546 case ISD::SETUNE: return "setune";
4547
4548 case ISD::SETEQ: return "seteq";
4549 case ISD::SETGT: return "setgt";
4550 case ISD::SETGE: return "setge";
4551 case ISD::SETLT: return "setlt";
4552 case ISD::SETLE: return "setle";
4553 case ISD::SETNE: return "setne";
4554 }
4555 }
4556}
4557
4558const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
4559 switch (AM) {
4560 default:
4561 return "";
4562 case ISD::PRE_INC:
4563 return "<pre-inc>";
4564 case ISD::PRE_DEC:
4565 return "<pre-dec>";
4566 case ISD::POST_INC:
4567 return "<post-inc>";
4568 case ISD::POST_DEC:
4569 return "<post-dec>";
4570 }
4571}
4572
Duncan Sandsc93fae32008-03-21 09:14:45 +00004573std::string ISD::ArgFlagsTy::getArgFlagsString() {
4574 std::string S = "< ";
4575
4576 if (isZExt())
4577 S += "zext ";
4578 if (isSExt())
4579 S += "sext ";
4580 if (isInReg())
4581 S += "inreg ";
4582 if (isSRet())
4583 S += "sret ";
4584 if (isByVal())
4585 S += "byval ";
4586 if (isNest())
4587 S += "nest ";
4588 if (getByValAlign())
4589 S += "byval-align:" + utostr(getByValAlign()) + " ";
4590 if (getOrigAlign())
4591 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4592 if (getByValSize())
4593 S += "byval-size:" + utostr(getByValSize()) + " ";
4594 return S + ">";
4595}
4596
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004597void SDNode::dump() const { dump(0); }
4598void SDNode::dump(const SelectionDAG *G) const {
4599 cerr << (void*)this << ": ";
4600
4601 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
4602 if (i) cerr << ",";
4603 if (getValueType(i) == MVT::Other)
4604 cerr << "ch";
4605 else
4606 cerr << MVT::getValueTypeString(getValueType(i));
4607 }
4608 cerr << " = " << getOperationName(G);
4609
4610 cerr << " ";
4611 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
4612 if (i) cerr << ", ";
4613 cerr << (void*)getOperand(i).Val;
4614 if (unsigned RN = getOperand(i).ResNo)
4615 cerr << ":" << RN;
4616 }
4617
Evan Chengaad43a02007-12-11 02:08:35 +00004618 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4619 SDNode *Mask = getOperand(2).Val;
4620 cerr << "<";
4621 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4622 if (i) cerr << ",";
4623 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4624 cerr << "u";
4625 else
4626 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4627 }
4628 cerr << ">";
4629 }
4630
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
4632 cerr << "<" << CSDN->getValue() << ">";
4633 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00004634 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4635 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4636 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4637 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4638 else {
4639 cerr << "<APFloat(";
4640 CSDN->getValueAPF().convertToAPInt().dump();
4641 cerr << ")>";
4642 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 } else if (const GlobalAddressSDNode *GADN =
4644 dyn_cast<GlobalAddressSDNode>(this)) {
4645 int offset = GADN->getOffset();
4646 cerr << "<";
4647 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
4648 if (offset > 0)
4649 cerr << " + " << offset;
4650 else
4651 cerr << " " << offset;
4652 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
4653 cerr << "<" << FIDN->getIndex() << ">";
4654 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
4655 cerr << "<" << JTDN->getIndex() << ">";
4656 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
4657 int offset = CP->getOffset();
4658 if (CP->isMachineConstantPoolEntry())
4659 cerr << "<" << *CP->getMachineCPVal() << ">";
4660 else
4661 cerr << "<" << *CP->getConstVal() << ">";
4662 if (offset > 0)
4663 cerr << " + " << offset;
4664 else
4665 cerr << " " << offset;
4666 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
4667 cerr << "<";
4668 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4669 if (LBB)
4670 cerr << LBB->getName() << " ";
4671 cerr << (const void*)BBDN->getBasicBlock() << ">";
4672 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman1e57df32008-02-10 18:45:23 +00004673 if (G && R->getReg() &&
4674 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling9b0baeb2008-02-26 21:47:57 +00004675 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676 } else {
4677 cerr << " #" << R->getReg();
4678 }
4679 } else if (const ExternalSymbolSDNode *ES =
4680 dyn_cast<ExternalSymbolSDNode>(this)) {
4681 cerr << "'" << ES->getSymbol() << "'";
4682 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4683 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +00004684 cerr << "<" << M->getValue() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685 else
Dan Gohman12a9c082008-02-06 22:27:42 +00004686 cerr << "<null>";
4687 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4688 if (M->MO.getValue())
4689 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4690 else
4691 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sandsc93fae32008-03-21 09:14:45 +00004692 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4693 cerr << N->getArgFlags().getArgFlagsString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004694 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
4695 cerr << ":" << MVT::getValueTypeString(N->getVT());
4696 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng034c4f82007-12-18 19:06:30 +00004697 const Value *SrcValue = LD->getSrcValue();
4698 int SrcOffset = LD->getSrcValueOffset();
4699 cerr << " <";
4700 if (SrcValue)
4701 cerr << SrcValue;
4702 else
4703 cerr << "null";
4704 cerr << ":" << SrcOffset << ">";
4705
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706 bool doExt = true;
4707 switch (LD->getExtensionType()) {
4708 default: doExt = false; break;
4709 case ISD::EXTLOAD:
4710 cerr << " <anyext ";
4711 break;
4712 case ISD::SEXTLOAD:
4713 cerr << " <sext ";
4714 break;
4715 case ISD::ZEXTLOAD:
4716 cerr << " <zext ";
4717 break;
4718 }
4719 if (doExt)
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004720 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721
4722 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00004723 if (*AM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724 cerr << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00004725 if (LD->isVolatile())
4726 cerr << " <volatile>";
4727 cerr << " alignment=" << LD->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng7196a7b2007-12-18 07:02:08 +00004729 const Value *SrcValue = ST->getSrcValue();
4730 int SrcOffset = ST->getSrcValueOffset();
4731 cerr << " <";
4732 if (SrcValue)
4733 cerr << SrcValue;
4734 else
4735 cerr << "null";
4736 cerr << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00004737
4738 if (ST->isTruncatingStore())
4739 cerr << " <trunc "
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004740 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00004741
4742 const char *AM = getIndexedModeName(ST->getAddressingMode());
4743 if (*AM)
4744 cerr << " " << AM;
4745 if (ST->isVolatile())
4746 cerr << " <volatile>";
4747 cerr << " alignment=" << ST->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748 }
4749}
4750
4751static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
4752 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4753 if (N->getOperand(i).Val->hasOneUse())
4754 DumpNodes(N->getOperand(i).Val, indent+2, G);
4755 else
4756 cerr << "\n" << std::string(indent+2, ' ')
4757 << (void*)N->getOperand(i).Val << ": <multiple use>";
4758
4759
4760 cerr << "\n" << std::string(indent, ' ');
4761 N->dump(G);
4762}
4763
4764void SelectionDAG::dump() const {
4765 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
4766 std::vector<const SDNode*> Nodes;
4767 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4768 I != E; ++I)
4769 Nodes.push_back(I);
4770
4771 std::sort(Nodes.begin(), Nodes.end());
4772
4773 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4774 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
4775 DumpNodes(Nodes[i], 2, this);
4776 }
4777
4778 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4779
4780 cerr << "\n\n";
4781}
4782
4783const Type *ConstantPoolSDNode::getType() const {
4784 if (isMachineConstantPoolEntry())
4785 return Val.MachineCPVal->getType();
4786 return Val.ConstVal->getType();
4787}