blob: 6e15318a16a977e4428fc754ac5d7e621440b203 [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//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/Constants.h"
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/GlobalVariable.h"
18#include "llvm/Intrinsics.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Assembly/Writer.h"
21#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
77 // Anything can be extended to ppc long double.
78 if (VT == MVT::ppcf128)
79 return true;
80
81 // PPC long double cannot be shrunk to anything though.
82 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
83 return false;
84
Dale Johannesenbbe2b702007-08-30 00:23:21 +000085 // convert modifies in place, so make a copy.
86 APFloat Val2 = APFloat(Val);
Chris Lattnerd037d482008-03-05 06:48:13 +000087 return Val2.convert(*MVTToAPFloatSemantics(VT),
88 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenbbe2b702007-08-30 00:23:21 +000089}
90
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091//===----------------------------------------------------------------------===//
92// ISD Namespace
93//===----------------------------------------------------------------------===//
94
95/// isBuildVectorAllOnes - Return true if the specified node is a
96/// BUILD_VECTOR where all of the elements are ~0 or undef.
97bool ISD::isBuildVectorAllOnes(const SDNode *N) {
98 // Look through a bit convert.
99 if (N->getOpcode() == ISD::BIT_CONVERT)
100 N = N->getOperand(0).Val;
101
102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
103
104 unsigned i = 0, e = N->getNumOperands();
105
106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
109
110 // Do not accept an all-undef vector.
111 if (i == e) return false;
112
113 // Do not accept build_vectors that aren't all constants or which have non-~0
114 // elements.
115 SDOperand NotZero = N->getOperand(i);
116 if (isa<ConstantSDNode>(NotZero)) {
117 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
118 return false;
119 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman161652c2008-02-29 01:47:35 +0000120 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
121 convertToAPInt().isAllOnesValue())
122 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 } else
124 return false;
125
126 // Okay, we have at least one ~0 value, check to see if the rest match or are
127 // undefs.
128 for (++i; i != e; ++i)
129 if (N->getOperand(i) != NotZero &&
130 N->getOperand(i).getOpcode() != ISD::UNDEF)
131 return false;
132 return true;
133}
134
135
136/// isBuildVectorAllZeros - Return true if the specified node is a
137/// BUILD_VECTOR where all of the elements are 0 or undef.
138bool ISD::isBuildVectorAllZeros(const SDNode *N) {
139 // Look through a bit convert.
140 if (N->getOpcode() == ISD::BIT_CONVERT)
141 N = N->getOperand(0).Val;
142
143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
144
145 unsigned i = 0, e = N->getNumOperands();
146
147 // Skip over all of the undef values.
148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
149 ++i;
150
151 // Do not accept an all-undef vector.
152 if (i == e) return false;
153
154 // Do not accept build_vectors that aren't all constants or which have non-~0
155 // elements.
156 SDOperand Zero = N->getOperand(i);
157 if (isa<ConstantSDNode>(Zero)) {
158 if (!cast<ConstantSDNode>(Zero)->isNullValue())
159 return false;
160 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 return false;
163 } else
164 return false;
165
166 // Okay, we have at least one ~0 value, check to see if the rest match or are
167 // undefs.
168 for (++i; i != e; ++i)
169 if (N->getOperand(i) != Zero &&
170 N->getOperand(i).getOpcode() != ISD::UNDEF)
171 return false;
172 return true;
173}
174
Evan Chengd1045a62008-02-18 23:04:32 +0000175/// isScalarToVector - Return true if the specified node is a
176/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
177/// element is not an undef.
178bool ISD::isScalarToVector(const SDNode *N) {
179 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
180 return true;
181
182 if (N->getOpcode() != ISD::BUILD_VECTOR)
183 return false;
184 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
185 return false;
186 unsigned NumElems = N->getNumOperands();
187 for (unsigned i = 1; i < NumElems; ++i) {
188 SDOperand V = N->getOperand(i);
189 if (V.getOpcode() != ISD::UNDEF)
190 return false;
191 }
192 return true;
193}
194
195
Evan Cheng13d1c292008-01-31 09:59:15 +0000196/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengee6db0f2008-02-04 23:10:38 +0000197/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Cheng13d1c292008-01-31 09:59:15 +0000198/// is 0).
199bool ISD::isDebugLabel(const SDNode *N) {
200 SDOperand Zero;
201 if (N->getOpcode() == ISD::LABEL)
202 Zero = N->getOperand(2);
203 else if (N->isTargetOpcode() &&
204 N->getTargetOpcode() == TargetInstrInfo::LABEL)
205 // Chain moved to last operand.
206 Zero = N->getOperand(1);
207 else
208 return false;
209 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
210}
211
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
213/// when given the operation for (X op Y).
214ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
215 // To perform this operation, we just need to swap the L and G bits of the
216 // operation.
217 unsigned OldL = (Operation >> 2) & 1;
218 unsigned OldG = (Operation >> 1) & 1;
219 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
220 (OldL << 1) | // New G bit
221 (OldG << 2)); // New L bit.
222}
223
224/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
225/// 'op' is a valid SetCC operation.
226ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
227 unsigned Operation = Op;
228 if (isInteger)
229 Operation ^= 7; // Flip L, G, E bits, but not U.
230 else
231 Operation ^= 15; // Flip all of the condition bits.
232 if (Operation > ISD::SETTRUE2)
233 Operation &= ~8; // Don't let N and U bits get set.
234 return ISD::CondCode(Operation);
235}
236
237
238/// isSignedOp - For an integer comparison, return 1 if the comparison is a
239/// signed operation and 2 if the result is an unsigned comparison. Return zero
240/// if the operation does not depend on the sign of the input (setne and seteq).
241static int isSignedOp(ISD::CondCode Opcode) {
242 switch (Opcode) {
243 default: assert(0 && "Illegal integer setcc operation!");
244 case ISD::SETEQ:
245 case ISD::SETNE: return 0;
246 case ISD::SETLT:
247 case ISD::SETLE:
248 case ISD::SETGT:
249 case ISD::SETGE: return 1;
250 case ISD::SETULT:
251 case ISD::SETULE:
252 case ISD::SETUGT:
253 case ISD::SETUGE: return 2;
254 }
255}
256
257/// getSetCCOrOperation - Return the result of a logical OR between different
258/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
259/// returns SETCC_INVALID if it is not possible to represent the resultant
260/// comparison.
261ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
262 bool isInteger) {
263 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
264 // Cannot fold a signed integer setcc with an unsigned integer setcc.
265 return ISD::SETCC_INVALID;
266
267 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
268
269 // If the N and U bits get set then the resultant comparison DOES suddenly
270 // care about orderedness, and is true when ordered.
271 if (Op > ISD::SETTRUE2)
272 Op &= ~16; // Clear the U bit if the N bit is set.
273
274 // Canonicalize illegal integer setcc's.
275 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
276 Op = ISD::SETNE;
277
278 return ISD::CondCode(Op);
279}
280
281/// getSetCCAndOperation - Return the result of a logical AND between different
282/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
283/// function returns zero if it is not possible to represent the resultant
284/// comparison.
285ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
286 bool isInteger) {
287 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
288 // Cannot fold a signed setcc with an unsigned setcc.
289 return ISD::SETCC_INVALID;
290
291 // Combine all of the condition bits.
292 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
293
294 // Canonicalize illegal integer setcc's.
295 if (isInteger) {
296 switch (Result) {
297 default: break;
298 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
299 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
300 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
301 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
302 }
303 }
304
305 return Result;
306}
307
308const TargetMachine &SelectionDAG::getTarget() const {
309 return TLI.getTargetMachine();
310}
311
312//===----------------------------------------------------------------------===//
313// SDNode Profile Support
314//===----------------------------------------------------------------------===//
315
316/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
317///
318static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
319 ID.AddInteger(OpC);
320}
321
322/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
323/// solely with their pointer.
324void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
325 ID.AddPointer(VTList.VTs);
326}
327
328/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
329///
330static void AddNodeIDOperands(FoldingSetNodeID &ID,
331 const SDOperand *Ops, unsigned NumOps) {
332 for (; NumOps; --NumOps, ++Ops) {
333 ID.AddPointer(Ops->Val);
334 ID.AddInteger(Ops->ResNo);
335 }
336}
337
338static void AddNodeIDNode(FoldingSetNodeID &ID,
339 unsigned short OpC, SDVTList VTList,
340 const SDOperand *OpList, unsigned N) {
341 AddNodeIDOpcode(ID, OpC);
342 AddNodeIDValueTypes(ID, VTList);
343 AddNodeIDOperands(ID, OpList, N);
344}
345
346/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
347/// data.
348static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
349 AddNodeIDOpcode(ID, N->getOpcode());
350 // Add the return value info.
351 AddNodeIDValueTypes(ID, N->getVTList());
352 // Add the operand info.
353 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
354
355 // Handle SDNode leafs with special info.
356 switch (N->getOpcode()) {
357 default: break; // Normal nodes don't need extra info.
358 case ISD::TargetConstant:
359 case ISD::Constant:
Chris Lattnerf5e3e182008-02-20 06:28:01 +0000360 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 break;
362 case ISD::TargetConstantFP:
Dale Johannesendf8a8312007-08-31 04:03:46 +0000363 case ISD::ConstantFP: {
Ted Kremenekdc71c802008-02-11 17:24:50 +0000364 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 break;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 case ISD::TargetGlobalAddress:
368 case ISD::GlobalAddress:
369 case ISD::TargetGlobalTLSAddress:
370 case ISD::GlobalTLSAddress: {
371 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
372 ID.AddPointer(GA->getGlobal());
373 ID.AddInteger(GA->getOffset());
374 break;
375 }
376 case ISD::BasicBlock:
377 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
378 break;
379 case ISD::Register:
380 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
381 break;
Dan Gohman12a9c082008-02-06 22:27:42 +0000382 case ISD::SRCVALUE:
383 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
384 break;
385 case ISD::MEMOPERAND: {
386 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
387 ID.AddPointer(MO.getValue());
388 ID.AddInteger(MO.getFlags());
389 ID.AddInteger(MO.getOffset());
390 ID.AddInteger(MO.getSize());
391 ID.AddInteger(MO.getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 break;
393 }
394 case ISD::FrameIndex:
395 case ISD::TargetFrameIndex:
396 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
397 break;
398 case ISD::JumpTable:
399 case ISD::TargetJumpTable:
400 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
401 break;
402 case ISD::ConstantPool:
403 case ISD::TargetConstantPool: {
404 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
405 ID.AddInteger(CP->getAlignment());
406 ID.AddInteger(CP->getOffset());
407 if (CP->isMachineConstantPoolEntry())
408 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
409 else
410 ID.AddPointer(CP->getConstVal());
411 break;
412 }
413 case ISD::LOAD: {
414 LoadSDNode *LD = cast<LoadSDNode>(N);
415 ID.AddInteger(LD->getAddressingMode());
416 ID.AddInteger(LD->getExtensionType());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000417 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 ID.AddInteger(LD->getAlignment());
419 ID.AddInteger(LD->isVolatile());
420 break;
421 }
422 case ISD::STORE: {
423 StoreSDNode *ST = cast<StoreSDNode>(N);
424 ID.AddInteger(ST->getAddressingMode());
425 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000426 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 ID.AddInteger(ST->getAlignment());
428 ID.AddInteger(ST->isVolatile());
429 break;
430 }
431 }
432}
433
434//===----------------------------------------------------------------------===//
435// SelectionDAG Class
436//===----------------------------------------------------------------------===//
437
438/// RemoveDeadNodes - This method deletes all unreachable nodes in the
439/// SelectionDAG.
440void SelectionDAG::RemoveDeadNodes() {
441 // Create a dummy node (which is not added to allnodes), that adds a reference
442 // to the root node, preventing it from being deleted.
443 HandleSDNode Dummy(getRoot());
444
445 SmallVector<SDNode*, 128> DeadNodes;
446
447 // Add all obviously-dead nodes to the DeadNodes worklist.
448 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
449 if (I->use_empty())
450 DeadNodes.push_back(I);
451
452 // Process the worklist, deleting the nodes and adding their uses to the
453 // worklist.
454 while (!DeadNodes.empty()) {
455 SDNode *N = DeadNodes.back();
456 DeadNodes.pop_back();
457
458 // Take the node out of the appropriate CSE map.
459 RemoveNodeFromCSEMaps(N);
460
461 // Next, brutally remove the operand list. This is safe to do, as there are
462 // no cycles in the graph.
463 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
464 SDNode *Operand = I->Val;
465 Operand->removeUser(N);
466
467 // Now that we removed this operand, see if there are no uses of it left.
468 if (Operand->use_empty())
469 DeadNodes.push_back(Operand);
470 }
471 if (N->OperandsNeedDelete)
472 delete[] N->OperandList;
473 N->OperandList = 0;
474 N->NumOperands = 0;
475
476 // Finally, remove N itself.
477 AllNodes.erase(N);
478 }
479
480 // If the root changed (e.g. it was a dead load, update the root).
481 setRoot(Dummy.getValue());
482}
483
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000484void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 SmallVector<SDNode*, 16> DeadNodes;
486 DeadNodes.push_back(N);
487
488 // Process the worklist, deleting the nodes and adding their uses to the
489 // worklist.
490 while (!DeadNodes.empty()) {
491 SDNode *N = DeadNodes.back();
492 DeadNodes.pop_back();
493
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000494 if (UpdateListener)
495 UpdateListener->NodeDeleted(N);
496
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 // Take the node out of the appropriate CSE map.
498 RemoveNodeFromCSEMaps(N);
499
500 // Next, brutally remove the operand list. This is safe to do, as there are
501 // no cycles in the graph.
502 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
503 SDNode *Operand = I->Val;
504 Operand->removeUser(N);
505
506 // Now that we removed this operand, see if there are no uses of it left.
507 if (Operand->use_empty())
508 DeadNodes.push_back(Operand);
509 }
510 if (N->OperandsNeedDelete)
511 delete[] N->OperandList;
512 N->OperandList = 0;
513 N->NumOperands = 0;
514
515 // Finally, remove N itself.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 AllNodes.erase(N);
517 }
518}
519
520void SelectionDAG::DeleteNode(SDNode *N) {
521 assert(N->use_empty() && "Cannot delete a node that is not dead!");
522
523 // First take this out of the appropriate CSE map.
524 RemoveNodeFromCSEMaps(N);
525
526 // Finally, remove uses due to operands of this node, remove from the
527 // AllNodes list, and delete the node.
528 DeleteNodeNotInCSEMaps(N);
529}
530
531void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
532
533 // Remove it from the AllNodes list.
534 AllNodes.remove(N);
535
536 // Drop all of the operands and decrement used nodes use counts.
537 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
538 I->Val->removeUser(N);
539 if (N->OperandsNeedDelete)
540 delete[] N->OperandList;
541 N->OperandList = 0;
542 N->NumOperands = 0;
543
544 delete N;
545}
546
547/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
548/// correspond to it. This is useful when we're about to delete or repurpose
549/// the node. We don't want future request for structurally identical nodes
550/// to return N anymore.
551void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
552 bool Erased = false;
553 switch (N->getOpcode()) {
554 case ISD::HANDLENODE: return; // noop.
555 case ISD::STRING:
556 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
557 break;
558 case ISD::CONDCODE:
559 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
560 "Cond code doesn't exist!");
561 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
562 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
563 break;
564 case ISD::ExternalSymbol:
565 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
566 break;
567 case ISD::TargetExternalSymbol:
568 Erased =
569 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
570 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000571 case ISD::VALUETYPE: {
572 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
573 if (MVT::isExtendedVT(VT)) {
574 Erased = ExtendedValueTypeNodes.erase(VT);
575 } else {
576 Erased = ValueTypeNodes[VT] != 0;
577 ValueTypeNodes[VT] = 0;
578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000580 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 default:
582 // Remove it from the CSE Map.
583 Erased = CSEMap.RemoveNode(N);
584 break;
585 }
586#ifndef NDEBUG
587 // Verify that the node was actually in one of the CSE maps, unless it has a
588 // flag result (which cannot be CSE'd) or is one of the special cases that are
589 // not subject to CSE.
590 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
591 !N->isTargetOpcode()) {
592 N->dump(this);
593 cerr << "\n";
594 assert(0 && "Node is not in map!");
595 }
596#endif
597}
598
599/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
600/// has been taken out and modified in some way. If the specified node already
601/// exists in the CSE maps, do not modify the maps, but return the existing node
602/// instead. If it doesn't exist, add it and return null.
603///
604SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
605 assert(N->getNumOperands() && "This is a leaf node!");
606 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
607 return 0; // Never add these nodes.
608
609 // Check that remaining values produced are not flags.
610 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
611 if (N->getValueType(i) == MVT::Flag)
612 return 0; // Never CSE anything that produces a flag.
613
614 SDNode *New = CSEMap.GetOrInsertNode(N);
615 if (New != N) return New; // Node already existed.
616 return 0;
617}
618
619/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
620/// were replaced with those specified. If this node is never memoized,
621/// return null, otherwise return a pointer to the slot it would take. If a
622/// node already exists with these operands, the slot will be non-null.
623SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
624 void *&InsertPos) {
625 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
626 return 0; // Never add these nodes.
627
628 // Check that remaining values produced are not flags.
629 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
630 if (N->getValueType(i) == MVT::Flag)
631 return 0; // Never CSE anything that produces a flag.
632
633 SDOperand Ops[] = { Op };
634 FoldingSetNodeID ID;
635 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
636 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
637}
638
639/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
640/// were replaced with those specified. If this node is never memoized,
641/// return null, otherwise return a pointer to the slot it would take. If a
642/// node already exists with these operands, the slot will be non-null.
643SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
644 SDOperand Op1, SDOperand Op2,
645 void *&InsertPos) {
646 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
647 return 0; // Never add these nodes.
648
649 // Check that remaining values produced are not flags.
650 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
651 if (N->getValueType(i) == MVT::Flag)
652 return 0; // Never CSE anything that produces a flag.
653
654 SDOperand Ops[] = { Op1, Op2 };
655 FoldingSetNodeID ID;
656 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
657 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
658}
659
660
661/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
662/// were replaced with those specified. If this node is never memoized,
663/// return null, otherwise return a pointer to the slot it would take. If a
664/// node already exists with these operands, the slot will be non-null.
665SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
666 const SDOperand *Ops,unsigned NumOps,
667 void *&InsertPos) {
668 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
669 return 0; // Never add these nodes.
670
671 // Check that remaining values produced are not flags.
672 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
673 if (N->getValueType(i) == MVT::Flag)
674 return 0; // Never CSE anything that produces a flag.
675
676 FoldingSetNodeID ID;
677 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
678
679 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
680 ID.AddInteger(LD->getAddressingMode());
681 ID.AddInteger(LD->getExtensionType());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000682 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 ID.AddInteger(LD->getAlignment());
684 ID.AddInteger(LD->isVolatile());
685 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
686 ID.AddInteger(ST->getAddressingMode());
687 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000688 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 ID.AddInteger(ST->getAlignment());
690 ID.AddInteger(ST->isVolatile());
691 }
692
693 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
694}
695
696
697SelectionDAG::~SelectionDAG() {
698 while (!AllNodes.empty()) {
699 SDNode *N = AllNodes.begin();
700 N->SetNextInBucket(0);
701 if (N->OperandsNeedDelete)
702 delete [] N->OperandList;
703 N->OperandList = 0;
704 N->NumOperands = 0;
705 AllNodes.pop_front();
706 }
707}
708
709SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
710 if (Op.getValueType() == VT) return Op;
Dan Gohman161652c2008-02-29 01:47:35 +0000711 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
712 MVT::getSizeInBits(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 return getNode(ISD::AND, Op.getValueType(), Op,
714 getConstant(Imm, Op.getValueType()));
715}
716
717SDOperand SelectionDAG::getString(const std::string &Val) {
718 StringSDNode *&N = StringNodes[Val];
719 if (!N) {
720 N = new StringSDNode(Val);
721 AllNodes.push_back(N);
722 }
723 return SDOperand(N, 0);
724}
725
726SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Dan Gohmandc458cf2008-02-08 22:59:30 +0000727 MVT::ValueType EltVT =
728 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
729
730 return getConstant(APInt(MVT::getSizeInBits(EltVT), Val), VT, isT);
731}
732
733SDOperand SelectionDAG::getConstant(const APInt &Val, MVT::ValueType VT, bool isT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000735
736 MVT::ValueType EltVT =
737 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738
Dan Gohmandc458cf2008-02-08 22:59:30 +0000739 assert(Val.getBitWidth() == MVT::getSizeInBits(EltVT) &&
740 "APInt size does not match type size!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741
742 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
743 FoldingSetNodeID ID;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000744 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenekdc71c802008-02-11 17:24:50 +0000745 ID.Add(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000747 SDNode *N = NULL;
748 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
749 if (!MVT::isVector(VT))
750 return SDOperand(N, 0);
751 if (!N) {
752 N = new ConstantSDNode(isT, Val, EltVT);
753 CSEMap.InsertNode(N, IP);
754 AllNodes.push_back(N);
755 }
756
757 SDOperand Result(N, 0);
758 if (MVT::isVector(VT)) {
759 SmallVector<SDOperand, 8> Ops;
760 Ops.assign(MVT::getVectorNumElements(VT), Result);
761 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
762 }
763 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764}
765
Chris Lattner5872a362008-01-17 07:00:52 +0000766SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
767 return getConstant(Val, TLI.getPointerTy(), isTarget);
768}
769
770
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000771SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 bool isTarget) {
773 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000774
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775 MVT::ValueType EltVT =
776 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000777
778 // Do the map lookup using the actual bit pattern for the floating point
779 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
780 // we don't have issues with SNANs.
781 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
782 FoldingSetNodeID ID;
783 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenekdc71c802008-02-11 17:24:50 +0000784 ID.Add(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 void *IP = 0;
786 SDNode *N = NULL;
787 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
788 if (!MVT::isVector(VT))
789 return SDOperand(N, 0);
790 if (!N) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000791 N = new ConstantFPSDNode(isTarget, V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 CSEMap.InsertNode(N, IP);
793 AllNodes.push_back(N);
794 }
795
796 SDOperand Result(N, 0);
797 if (MVT::isVector(VT)) {
798 SmallVector<SDOperand, 8> Ops;
799 Ops.assign(MVT::getVectorNumElements(VT), Result);
800 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
801 }
802 return Result;
803}
804
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000805SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
806 bool isTarget) {
807 MVT::ValueType EltVT =
808 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
809 if (EltVT==MVT::f32)
810 return getConstantFP(APFloat((float)Val), VT, isTarget);
811 else
812 return getConstantFP(APFloat(Val), VT, isTarget);
813}
814
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
816 MVT::ValueType VT, int Offset,
817 bool isTargetGA) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 unsigned Opc;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000819
820 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
821 if (!GVar) {
822 // If GV is an alias - use aliasee for determing thread-localness
823 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
824 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
825 }
826
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 if (GVar && GVar->isThreadLocal())
828 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
829 else
830 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000831
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 FoldingSetNodeID ID;
833 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
834 ID.AddPointer(GV);
835 ID.AddInteger(Offset);
836 void *IP = 0;
837 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
838 return SDOperand(E, 0);
839 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
840 CSEMap.InsertNode(N, IP);
841 AllNodes.push_back(N);
842 return SDOperand(N, 0);
843}
844
845SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
846 bool isTarget) {
847 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
848 FoldingSetNodeID ID;
849 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
850 ID.AddInteger(FI);
851 void *IP = 0;
852 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
853 return SDOperand(E, 0);
854 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
855 CSEMap.InsertNode(N, IP);
856 AllNodes.push_back(N);
857 return SDOperand(N, 0);
858}
859
860SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
861 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
862 FoldingSetNodeID ID;
863 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
864 ID.AddInteger(JTI);
865 void *IP = 0;
866 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
867 return SDOperand(E, 0);
868 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
869 CSEMap.InsertNode(N, IP);
870 AllNodes.push_back(N);
871 return SDOperand(N, 0);
872}
873
874SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
875 unsigned Alignment, int Offset,
876 bool isTarget) {
877 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
878 FoldingSetNodeID ID;
879 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
880 ID.AddInteger(Alignment);
881 ID.AddInteger(Offset);
882 ID.AddPointer(C);
883 void *IP = 0;
884 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
885 return SDOperand(E, 0);
886 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
887 CSEMap.InsertNode(N, IP);
888 AllNodes.push_back(N);
889 return SDOperand(N, 0);
890}
891
892
893SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
894 MVT::ValueType VT,
895 unsigned Alignment, int Offset,
896 bool isTarget) {
897 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
898 FoldingSetNodeID ID;
899 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
900 ID.AddInteger(Alignment);
901 ID.AddInteger(Offset);
902 C->AddSelectionDAGCSEId(ID);
903 void *IP = 0;
904 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
905 return SDOperand(E, 0);
906 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
907 CSEMap.InsertNode(N, IP);
908 AllNodes.push_back(N);
909 return SDOperand(N, 0);
910}
911
912
913SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
914 FoldingSetNodeID ID;
915 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
916 ID.AddPointer(MBB);
917 void *IP = 0;
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new BasicBlockSDNode(MBB);
921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
924}
925
926SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000927 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 ValueTypeNodes.resize(VT+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929
Duncan Sandsd7307a92007-10-17 13:49:58 +0000930 SDNode *&N = MVT::isExtendedVT(VT) ?
931 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
932
933 if (N) return SDOperand(N, 0);
934 N = new VTSDNode(VT);
935 AllNodes.push_back(N);
936 return SDOperand(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937}
938
939SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
940 SDNode *&N = ExternalSymbols[Sym];
941 if (N) return SDOperand(N, 0);
942 N = new ExternalSymbolSDNode(false, Sym, VT);
943 AllNodes.push_back(N);
944 return SDOperand(N, 0);
945}
946
947SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
948 MVT::ValueType VT) {
949 SDNode *&N = TargetExternalSymbols[Sym];
950 if (N) return SDOperand(N, 0);
951 N = new ExternalSymbolSDNode(true, Sym, VT);
952 AllNodes.push_back(N);
953 return SDOperand(N, 0);
954}
955
956SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
957 if ((unsigned)Cond >= CondCodeNodes.size())
958 CondCodeNodes.resize(Cond+1);
959
960 if (CondCodeNodes[Cond] == 0) {
961 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
962 AllNodes.push_back(CondCodeNodes[Cond]);
963 }
964 return SDOperand(CondCodeNodes[Cond], 0);
965}
966
967SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
968 FoldingSetNodeID ID;
969 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
970 ID.AddInteger(RegNo);
971 void *IP = 0;
972 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
973 return SDOperand(E, 0);
974 SDNode *N = new RegisterSDNode(RegNo, VT);
975 CSEMap.InsertNode(N, IP);
976 AllNodes.push_back(N);
977 return SDOperand(N, 0);
978}
979
Dan Gohman12a9c082008-02-06 22:27:42 +0000980SDOperand SelectionDAG::getSrcValue(const Value *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 assert((!V || isa<PointerType>(V->getType())) &&
982 "SrcValue is not a pointer?");
983
984 FoldingSetNodeID ID;
985 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
986 ID.AddPointer(V);
Dan Gohman12a9c082008-02-06 22:27:42 +0000987
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 void *IP = 0;
989 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
990 return SDOperand(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +0000991
992 SDNode *N = new SrcValueSDNode(V);
993 CSEMap.InsertNode(N, IP);
994 AllNodes.push_back(N);
995 return SDOperand(N, 0);
996}
997
998SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
999 const Value *v = MO.getValue();
1000 assert((!v || isa<PointerType>(v->getType())) &&
1001 "SrcValue is not a pointer?");
1002
1003 FoldingSetNodeID ID;
1004 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
1005 ID.AddPointer(v);
1006 ID.AddInteger(MO.getFlags());
1007 ID.AddInteger(MO.getOffset());
1008 ID.AddInteger(MO.getSize());
1009 ID.AddInteger(MO.getAlignment());
1010
1011 void *IP = 0;
1012 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1013 return SDOperand(E, 0);
1014
1015 SDNode *N = new MemOperandSDNode(MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016 CSEMap.InsertNode(N, IP);
1017 AllNodes.push_back(N);
1018 return SDOperand(N, 0);
1019}
1020
Chris Lattner53f5aee2007-10-15 17:47:20 +00001021/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1022/// specified value type.
1023SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
1024 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1025 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
1026 const Type *Ty = MVT::getTypeForValueType(VT);
1027 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1028 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1029 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1030}
1031
1032
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1034 SDOperand N2, ISD::CondCode Cond) {
1035 // These setcc operations always fold.
1036 switch (Cond) {
1037 default: break;
1038 case ISD::SETFALSE:
1039 case ISD::SETFALSE2: return getConstant(0, VT);
1040 case ISD::SETTRUE:
1041 case ISD::SETTRUE2: return getConstant(1, VT);
1042
1043 case ISD::SETOEQ:
1044 case ISD::SETOGT:
1045 case ISD::SETOGE:
1046 case ISD::SETOLT:
1047 case ISD::SETOLE:
1048 case ISD::SETONE:
1049 case ISD::SETO:
1050 case ISD::SETUO:
1051 case ISD::SETUEQ:
1052 case ISD::SETUNE:
1053 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1054 break;
1055 }
1056
1057 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001058 const APInt &C2 = N2C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001060 const APInt &C1 = N1C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061
1062 switch (Cond) {
1063 default: assert(0 && "Unknown integer setcc!");
1064 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1065 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00001066 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1067 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1068 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1069 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1070 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1071 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1072 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1073 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 }
1075 }
1076 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001077 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001079 // No compile time operations on this type yet.
1080 if (N1C->getValueType(0) == MVT::ppcf128)
1081 return SDOperand();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001082
1083 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001085 default: break;
Dale Johannesen76844472007-08-31 17:03:33 +00001086 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1087 return getNode(ISD::UNDEF, VT);
1088 // fall through
1089 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1090 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1091 return getNode(ISD::UNDEF, VT);
1092 // fall through
1093 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001094 R==APFloat::cmpLessThan, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001095 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1096 return getNode(ISD::UNDEF, VT);
1097 // fall through
1098 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1099 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1100 return getNode(ISD::UNDEF, VT);
1101 // fall through
1102 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1103 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1104 return getNode(ISD::UNDEF, VT);
1105 // fall through
1106 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001107 R==APFloat::cmpEqual, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001108 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1109 return getNode(ISD::UNDEF, VT);
1110 // fall through
1111 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001112 R==APFloat::cmpEqual, VT);
1113 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1114 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1115 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1116 R==APFloat::cmpEqual, VT);
1117 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1118 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1119 R==APFloat::cmpLessThan, VT);
1120 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1121 R==APFloat::cmpUnordered, VT);
1122 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1123 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 }
1125 } else {
1126 // Ensure that the constant occurs on the RHS.
1127 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1128 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001129 }
1130
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 // Could not fold it.
1132 return SDOperand();
1133}
1134
Dan Gohman07961cd2008-02-25 21:11:39 +00001135/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1136/// use this predicate to simplify operations downstream.
1137bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1138 unsigned BitWidth = Op.getValueSizeInBits();
1139 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1140}
1141
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1143/// this predicate to simplify operations downstream. Mask is known to be zero
1144/// for bits that V cannot have.
Dan Gohman07961cd2008-02-25 21:11:39 +00001145bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001146 unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001147 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1149 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1150 return (KnownZero & Mask) == Mask;
1151}
1152
1153/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1154/// known to be either zero or one and return them in the KnownZero/KnownOne
1155/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1156/// processing.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001157void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00001158 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159 unsigned Depth) const {
Dan Gohman229fa052008-02-13 00:35:47 +00001160 unsigned BitWidth = Mask.getBitWidth();
Dan Gohman56eaab32008-02-13 23:13:32 +00001161 assert(BitWidth == MVT::getSizeInBits(Op.getValueType()) &&
1162 "Mask size mismatches value type size!");
1163
Dan Gohman229fa052008-02-13 00:35:47 +00001164 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 if (Depth == 6 || Mask == 0)
1166 return; // Limit search depth.
1167
Dan Gohman229fa052008-02-13 00:35:47 +00001168 APInt KnownZero2, KnownOne2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169
1170 switch (Op.getOpcode()) {
1171 case ISD::Constant:
1172 // We know all of the bits for a constant!
Dan Gohman229fa052008-02-13 00:35:47 +00001173 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 KnownZero = ~KnownOne & Mask;
1175 return;
1176 case ISD::AND:
1177 // If either the LHS or the RHS are Zero, the result is zero.
1178 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001179 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1180 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001181 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1182 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1183
1184 // Output known-1 bits are only known if set in both the LHS & RHS.
1185 KnownOne &= KnownOne2;
1186 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1187 KnownZero |= KnownZero2;
1188 return;
1189 case ISD::OR:
1190 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001191 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1192 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1194 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1195
1196 // Output known-0 bits are only known if clear in both the LHS & RHS.
1197 KnownZero &= KnownZero2;
1198 // Output known-1 are known to be set if set in either the LHS | RHS.
1199 KnownOne |= KnownOne2;
1200 return;
1201 case ISD::XOR: {
1202 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1203 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1204 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1205 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1206
1207 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohman229fa052008-02-13 00:35:47 +00001208 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001209 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1210 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1211 KnownZero = KnownZeroOut;
1212 return;
1213 }
1214 case ISD::SELECT:
1215 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1216 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1217 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1218 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1219
1220 // Only known if known in both the LHS and RHS.
1221 KnownOne &= KnownOne2;
1222 KnownZero &= KnownZero2;
1223 return;
1224 case ISD::SELECT_CC:
1225 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1226 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1227 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1228 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1229
1230 // Only known if known in both the LHS and RHS.
1231 KnownOne &= KnownOne2;
1232 KnownZero &= KnownZero2;
1233 return;
1234 case ISD::SETCC:
1235 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohman229fa052008-02-13 00:35:47 +00001236 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1237 BitWidth > 1)
1238 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239 return;
1240 case ISD::SHL:
1241 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1242 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman4b8d0002008-02-26 18:50:50 +00001243 unsigned ShAmt = SA->getValue();
1244
1245 // If the shift count is an invalid immediate, don't do anything.
1246 if (ShAmt >= BitWidth)
1247 return;
1248
1249 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001250 KnownZero, KnownOne, Depth+1);
1251 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman4b8d0002008-02-26 18:50:50 +00001252 KnownZero <<= ShAmt;
1253 KnownOne <<= ShAmt;
Dan Gohman229fa052008-02-13 00:35:47 +00001254 // low bits known zero.
Dan Gohman4b8d0002008-02-26 18:50:50 +00001255 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001256 }
1257 return;
1258 case ISD::SRL:
1259 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1260 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 unsigned ShAmt = SA->getValue();
1262
Dan Gohman4b8d0002008-02-26 18:50:50 +00001263 // If the shift count is an invalid immediate, don't do anything.
1264 if (ShAmt >= BitWidth)
1265 return;
1266
Dan Gohman229fa052008-02-13 00:35:47 +00001267 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 KnownZero, KnownOne, Depth+1);
1269 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001270 KnownZero = KnownZero.lshr(ShAmt);
1271 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272
Dan Gohman4d81a742008-02-13 22:43:25 +00001273 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 KnownZero |= HighBits; // High bits known zero.
1275 }
1276 return;
1277 case ISD::SRA:
1278 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 unsigned ShAmt = SA->getValue();
1280
Dan Gohman4b8d0002008-02-26 18:50:50 +00001281 // If the shift count is an invalid immediate, don't do anything.
1282 if (ShAmt >= BitWidth)
1283 return;
1284
Dan Gohman229fa052008-02-13 00:35:47 +00001285 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 // If any of the demanded bits are produced by the sign extension, we also
1287 // demand the input sign bit.
Dan Gohman4d81a742008-02-13 22:43:25 +00001288 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1289 if (HighBits.getBoolValue())
Dan Gohman229fa052008-02-13 00:35:47 +00001290 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291
1292 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1293 Depth+1);
1294 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001295 KnownZero = KnownZero.lshr(ShAmt);
1296 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001297
1298 // Handle the sign bits.
Dan Gohman229fa052008-02-13 00:35:47 +00001299 APInt SignBit = APInt::getSignBit(BitWidth);
1300 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301
Dan Gohman91507292008-02-20 16:30:17 +00001302 if (KnownZero.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001303 KnownZero |= HighBits; // New bits are known zero.
Dan Gohman91507292008-02-20 16:30:17 +00001304 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 KnownOne |= HighBits; // New bits are known one.
1306 }
1307 }
1308 return;
1309 case ISD::SIGN_EXTEND_INREG: {
1310 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001311 unsigned EBits = MVT::getSizeInBits(EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312
1313 // Sign extension. Compute the demanded bits in the result that are not
1314 // present in the input.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001315 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316
Dan Gohmand0dfc772008-02-13 22:28:48 +00001317 APInt InSignBit = APInt::getSignBit(EBits);
1318 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319
1320 // If the sign extended bits are demanded, we know that the sign
1321 // bit is demanded.
Dan Gohman229fa052008-02-13 00:35:47 +00001322 InSignBit.zext(BitWidth);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001323 if (NewBits.getBoolValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 InputDemandedBits |= InSignBit;
1325
1326 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1327 KnownZero, KnownOne, Depth+1);
1328 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1329
1330 // If the sign bit of the input is known set or clear, then we know the
1331 // top bits of the result.
Dan Gohman91507292008-02-20 16:30:17 +00001332 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333 KnownZero |= NewBits;
1334 KnownOne &= ~NewBits;
Dan Gohman91507292008-02-20 16:30:17 +00001335 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 KnownOne |= NewBits;
1337 KnownZero &= ~NewBits;
1338 } else { // Input sign bit unknown
1339 KnownZero &= ~NewBits;
1340 KnownOne &= ~NewBits;
1341 }
1342 return;
1343 }
1344 case ISD::CTTZ:
1345 case ISD::CTLZ:
1346 case ISD::CTPOP: {
Dan Gohman229fa052008-02-13 00:35:47 +00001347 unsigned LowBits = Log2_32(BitWidth)+1;
1348 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
1349 KnownOne = APInt(BitWidth, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 return;
1351 }
1352 case ISD::LOAD: {
1353 if (ISD::isZEXTLoad(Op.Val)) {
1354 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001355 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001356 unsigned MemBits = MVT::getSizeInBits(VT);
1357 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 }
1359 return;
1360 }
1361 case ISD::ZERO_EXTEND: {
Dan Gohman229fa052008-02-13 00:35:47 +00001362 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1363 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001364 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1365 APInt InMask = Mask;
1366 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001367 KnownZero.trunc(InBits);
1368 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001369 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001370 KnownZero.zext(BitWidth);
1371 KnownOne.zext(BitWidth);
1372 KnownZero |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 return;
1374 }
1375 case ISD::SIGN_EXTEND: {
1376 MVT::ValueType InVT = Op.getOperand(0).getValueType();
Dan Gohman229fa052008-02-13 00:35:47 +00001377 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohman229fa052008-02-13 00:35:47 +00001378 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001379 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1380 APInt InMask = Mask;
1381 InMask.trunc(InBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382
1383 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohmand0dfc772008-02-13 22:28:48 +00001384 // bit is demanded. Temporarily set this bit in the mask for our callee.
1385 if (NewBits.getBoolValue())
1386 InMask |= InSignBit;
Dan Gohman229fa052008-02-13 00:35:47 +00001387
Dan Gohman229fa052008-02-13 00:35:47 +00001388 KnownZero.trunc(InBits);
1389 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001390 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1391
1392 // Note if the sign bit is known to be zero or one.
1393 bool SignBitKnownZero = KnownZero.isNegative();
1394 bool SignBitKnownOne = KnownOne.isNegative();
1395 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1396 "Sign bit can't be known to be both zero and one!");
1397
1398 // If the sign bit wasn't actually demanded by our caller, we don't
1399 // want it set in the KnownZero and KnownOne result values. Reset the
1400 // mask and reapply it to the result values.
1401 InMask = Mask;
1402 InMask.trunc(InBits);
1403 KnownZero &= InMask;
1404 KnownOne &= InMask;
1405
Dan Gohman229fa052008-02-13 00:35:47 +00001406 KnownZero.zext(BitWidth);
1407 KnownOne.zext(BitWidth);
1408
Dan Gohmand0dfc772008-02-13 22:28:48 +00001409 // If the sign bit is known zero or one, the top bits match.
1410 if (SignBitKnownZero)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001411 KnownZero |= NewBits;
Dan Gohmand0dfc772008-02-13 22:28:48 +00001412 else if (SignBitKnownOne)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 KnownOne |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001414 return;
1415 }
1416 case ISD::ANY_EXTEND: {
Dan Gohman229fa052008-02-13 00:35:47 +00001417 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1418 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001419 APInt InMask = Mask;
1420 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001421 KnownZero.trunc(InBits);
1422 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001423 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001424 KnownZero.zext(BitWidth);
1425 KnownOne.zext(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426 return;
1427 }
1428 case ISD::TRUNCATE: {
Dan Gohman229fa052008-02-13 00:35:47 +00001429 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1430 unsigned InBits = MVT::getSizeInBits(InVT);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001431 APInt InMask = Mask;
1432 InMask.zext(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001433 KnownZero.zext(InBits);
1434 KnownOne.zext(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001435 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001437 KnownZero.trunc(BitWidth);
1438 KnownOne.trunc(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001439 break;
1440 }
1441 case ISD::AssertZext: {
1442 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman229fa052008-02-13 00:35:47 +00001443 APInt InMask = APInt::getLowBitsSet(BitWidth, MVT::getSizeInBits(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1445 KnownOne, Depth+1);
1446 KnownZero |= (~InMask) & Mask;
1447 return;
1448 }
Chris Lattner13f06832007-12-22 21:26:52 +00001449 case ISD::FGETSIGN:
1450 // All bits are zero except the low bit.
Dan Gohman229fa052008-02-13 00:35:47 +00001451 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattner13f06832007-12-22 21:26:52 +00001452 return;
1453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 case ISD::ADD: {
1455 // If either the LHS or the RHS are Zero, the result is zero.
1456 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1457 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1458 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1459 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1460
1461 // Output known-0 bits are known if clear or set in both the low clear bits
1462 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1463 // low 3 bits clear.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001464 unsigned KnownZeroOut = std::min(KnownZero.countTrailingOnes(),
1465 KnownZero2.countTrailingOnes());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466
Dan Gohman229fa052008-02-13 00:35:47 +00001467 KnownZero = APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1468 KnownOne = APInt(BitWidth, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 return;
1470 }
1471 case ISD::SUB: {
1472 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1473 if (!CLHS) return;
1474
1475 // We know that the top bits of C-X are clear if X contains less bits
1476 // than C (i.e. no wrap-around can happen). For example, 20-X is
1477 // positive if we can prove that X is >= 0 and < 16.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001478 if (CLHS->getAPIntValue().isNonNegative()) {
Dan Gohman229fa052008-02-13 00:35:47 +00001479 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1480 // NLZ can't be BitWidth with no sign bit
Chris Lattner69946fd2008-02-14 18:48:56 +00001481 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1483
1484 // If all of the MaskV bits are known to be zero, then we know the output
1485 // top bits are zero, because we now know that the output is from [0-C].
1486 if ((KnownZero & MaskV) == MaskV) {
Dan Gohman229fa052008-02-13 00:35:47 +00001487 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1488 // Top bits known zero.
1489 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1490 KnownOne = APInt(BitWidth, 0); // No one bits known.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 } else {
Dan Gohman229fa052008-02-13 00:35:47 +00001492 KnownZero = KnownOne = APInt(BitWidth, 0); // Otherwise, nothing known.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 }
1494 }
1495 return;
1496 }
1497 default:
1498 // Allow the target to implement this method for its nodes.
1499 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1500 case ISD::INTRINSIC_WO_CHAIN:
1501 case ISD::INTRINSIC_W_CHAIN:
1502 case ISD::INTRINSIC_VOID:
1503 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1504 }
1505 return;
1506 }
1507}
1508
1509/// ComputeNumSignBits - Return the number of times the sign bit of the
1510/// register is replicated into the other bits. We know that at least 1 bit
1511/// is always equal to the sign bit (itself), but other cases can give us
1512/// information. For example, immediately after an "SRA X, 2", we know that
1513/// the top 3 bits are all equal to each other, so we return 3.
1514unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1515 MVT::ValueType VT = Op.getValueType();
1516 assert(MVT::isInteger(VT) && "Invalid VT!");
1517 unsigned VTBits = MVT::getSizeInBits(VT);
1518 unsigned Tmp, Tmp2;
1519
1520 if (Depth == 6)
1521 return 1; // Limit search depth.
1522
1523 switch (Op.getOpcode()) {
1524 default: break;
1525 case ISD::AssertSext:
1526 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1527 return VTBits-Tmp+1;
1528 case ISD::AssertZext:
1529 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1530 return VTBits-Tmp;
1531
1532 case ISD::Constant: {
Dan Gohman463db8c2008-03-03 23:35:36 +00001533 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1534 // If negative, return # leading ones.
1535 if (Val.isNegative())
1536 return Val.countLeadingOnes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001537
Dan Gohman463db8c2008-03-03 23:35:36 +00001538 // Return # leading zeros.
1539 return Val.countLeadingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540 }
1541
1542 case ISD::SIGN_EXTEND:
1543 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1544 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1545
1546 case ISD::SIGN_EXTEND_INREG:
1547 // Max of the input and what this extends.
1548 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1549 Tmp = VTBits-Tmp+1;
1550
1551 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1552 return std::max(Tmp, Tmp2);
1553
1554 case ISD::SRA:
1555 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1556 // SRA X, C -> adds C sign bits.
1557 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1558 Tmp += C->getValue();
1559 if (Tmp > VTBits) Tmp = VTBits;
1560 }
1561 return Tmp;
1562 case ISD::SHL:
1563 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1564 // shl destroys sign bits.
1565 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1566 if (C->getValue() >= VTBits || // Bad shift.
1567 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1568 return Tmp - C->getValue();
1569 }
1570 break;
1571 case ISD::AND:
1572 case ISD::OR:
1573 case ISD::XOR: // NOT is handled here.
1574 // Logical binary ops preserve the number of sign bits.
1575 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1576 if (Tmp == 1) return 1; // Early out.
1577 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1578 return std::min(Tmp, Tmp2);
1579
1580 case ISD::SELECT:
1581 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1582 if (Tmp == 1) return 1; // Early out.
1583 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1584 return std::min(Tmp, Tmp2);
1585
1586 case ISD::SETCC:
1587 // If setcc returns 0/-1, all bits are sign bits.
1588 if (TLI.getSetCCResultContents() ==
1589 TargetLowering::ZeroOrNegativeOneSetCCResult)
1590 return VTBits;
1591 break;
1592 case ISD::ROTL:
1593 case ISD::ROTR:
1594 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1595 unsigned RotAmt = C->getValue() & (VTBits-1);
1596
1597 // Handle rotate right by N like a rotate left by 32-N.
1598 if (Op.getOpcode() == ISD::ROTR)
1599 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1600
1601 // If we aren't rotating out all of the known-in sign bits, return the
1602 // number that are left. This handles rotl(sext(x), 1) for example.
1603 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1604 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1605 }
1606 break;
1607 case ISD::ADD:
1608 // Add can have at most one carry bit. Thus we know that the output
1609 // is, at worst, one more bit than the inputs.
1610 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1611 if (Tmp == 1) return 1; // Early out.
1612
1613 // Special case decrementing a value (ADD X, -1):
1614 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1615 if (CRHS->isAllOnesValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00001616 APInt KnownZero, KnownOne;
1617 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001618 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1619
1620 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1621 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00001622 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001623 return VTBits;
1624
1625 // If we are subtracting one from a positive number, there is no carry
1626 // out of the result.
Dan Gohman63f4e462008-02-27 01:23:58 +00001627 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001628 return Tmp;
1629 }
1630
1631 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1632 if (Tmp2 == 1) return 1;
1633 return std::min(Tmp, Tmp2)-1;
1634 break;
1635
1636 case ISD::SUB:
1637 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1638 if (Tmp2 == 1) return 1;
1639
1640 // Handle NEG.
1641 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1642 if (CLHS->getValue() == 0) {
Dan Gohman63f4e462008-02-27 01:23:58 +00001643 APInt KnownZero, KnownOne;
1644 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001645 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1646 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1647 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00001648 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 return VTBits;
1650
1651 // If the input is known to be positive (the sign bit is known clear),
1652 // the output of the NEG has the same number of sign bits as the input.
Dan Gohman63f4e462008-02-27 01:23:58 +00001653 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001654 return Tmp2;
1655
1656 // Otherwise, we treat this like a SUB.
1657 }
1658
1659 // Sub can have at most one carry bit. Thus we know that the output
1660 // is, at worst, one more bit than the inputs.
1661 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1662 if (Tmp == 1) return 1; // Early out.
1663 return std::min(Tmp, Tmp2)-1;
1664 break;
1665 case ISD::TRUNCATE:
1666 // FIXME: it's tricky to do anything useful for this, but it is an important
1667 // case for targets like X86.
1668 break;
1669 }
1670
1671 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1672 if (Op.getOpcode() == ISD::LOAD) {
1673 LoadSDNode *LD = cast<LoadSDNode>(Op);
1674 unsigned ExtType = LD->getExtensionType();
1675 switch (ExtType) {
1676 default: break;
1677 case ISD::SEXTLOAD: // '17' bits known
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001678 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001679 return VTBits-Tmp+1;
1680 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001681 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 return VTBits-Tmp;
1683 }
1684 }
1685
1686 // Allow the target to implement this method for its nodes.
1687 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1688 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1689 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1690 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1691 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1692 if (NumBits > 1) return NumBits;
1693 }
1694
1695 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1696 // use this information.
Dan Gohman63f4e462008-02-27 01:23:58 +00001697 APInt KnownZero, KnownOne;
1698 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1700
Dan Gohman63f4e462008-02-27 01:23:58 +00001701 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001702 Mask = KnownZero;
Dan Gohman63f4e462008-02-27 01:23:58 +00001703 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001704 Mask = KnownOne;
1705 } else {
1706 // Nothing known.
1707 return 1;
1708 }
1709
1710 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1711 // the number of identical bits in the top of the input value.
Dan Gohman63f4e462008-02-27 01:23:58 +00001712 Mask = ~Mask;
1713 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001714 // Return # leading zeros. We use 'min' here in case Val was zero before
1715 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman63f4e462008-02-27 01:23:58 +00001716 return std::min(VTBits, Mask.countLeadingZeros());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717}
1718
1719
Evan Cheng2e28d622008-02-02 04:07:54 +00001720bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1721 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1722 if (!GA) return false;
1723 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1724 if (!GV) return false;
1725 MachineModuleInfo *MMI = getMachineModuleInfo();
1726 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1727}
1728
1729
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730/// getNode - Gets or creates the specified node.
1731///
1732SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1733 FoldingSetNodeID ID;
1734 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1735 void *IP = 0;
1736 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1737 return SDOperand(E, 0);
1738 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1739 CSEMap.InsertNode(N, IP);
1740
1741 AllNodes.push_back(N);
1742 return SDOperand(N, 0);
1743}
1744
1745SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1746 SDOperand Operand) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001747 // Constant fold unary operations with an integer constant operand.
1748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman161652c2008-02-29 01:47:35 +00001749 const APInt &Val = C->getAPIntValue();
1750 unsigned BitWidth = MVT::getSizeInBits(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 switch (Opcode) {
1752 default: break;
Evan Chengeadaf442008-03-06 17:42:34 +00001753 case ISD::SIGN_EXTEND:
1754 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 case ISD::ANY_EXTEND:
Dan Gohman161652c2008-02-29 01:47:35 +00001756 case ISD::ZERO_EXTEND:
Evan Chengeadaf442008-03-06 17:42:34 +00001757 case ISD::TRUNCATE:
1758 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001759 case ISD::UINT_TO_FP:
1760 case ISD::SINT_TO_FP: {
1761 const uint64_t zero[] = {0, 0};
Dale Johannesenb89072e2007-10-16 23:38:29 +00001762 // No compile time operations on this type.
1763 if (VT==MVT::ppcf128)
1764 break;
Dan Gohman161652c2008-02-29 01:47:35 +00001765 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1766 (void)apf.convertFromAPInt(Val,
1767 Opcode==ISD::SINT_TO_FP,
1768 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001769 return getConstantFP(apf, VT);
1770 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001771 case ISD::BIT_CONVERT:
1772 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman161652c2008-02-29 01:47:35 +00001773 return getConstantFP(Val.bitsToFloat(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman161652c2008-02-29 01:47:35 +00001775 return getConstantFP(Val.bitsToDouble(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 break;
1777 case ISD::BSWAP:
Dan Gohman161652c2008-02-29 01:47:35 +00001778 return getConstant(Val.byteSwap(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779 case ISD::CTPOP:
Dan Gohman161652c2008-02-29 01:47:35 +00001780 return getConstant(Val.countPopulation(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001781 case ISD::CTLZ:
Dan Gohman161652c2008-02-29 01:47:35 +00001782 return getConstant(Val.countLeadingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001783 case ISD::CTTZ:
Dan Gohman161652c2008-02-29 01:47:35 +00001784 return getConstant(Val.countTrailingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001785 }
1786 }
1787
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001788 // Constant fold unary operations with a floating point constant operand.
1789 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1790 APFloat V = C->getValueAPF(); // make copy
Chris Lattner5872a362008-01-17 07:00:52 +00001791 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00001792 switch (Opcode) {
1793 case ISD::FNEG:
1794 V.changeSign();
1795 return getConstantFP(V, VT);
1796 case ISD::FABS:
1797 V.clearSign();
1798 return getConstantFP(V, VT);
1799 case ISD::FP_ROUND:
1800 case ISD::FP_EXTEND:
1801 // This can return overflow, underflow, or inexact; we don't care.
1802 // FIXME need to be more flexible about rounding mode.
Chris Lattnerd037d482008-03-05 06:48:13 +00001803 (void)V.convert(*MVTToAPFloatSemantics(VT),
1804 APFloat::rmNearestTiesToEven);
Dale Johannesenb89072e2007-10-16 23:38:29 +00001805 return getConstantFP(V, VT);
1806 case ISD::FP_TO_SINT:
1807 case ISD::FP_TO_UINT: {
1808 integerPart x;
1809 assert(integerPartWidth >= 64);
1810 // FIXME need to be more flexible about rounding mode.
1811 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1812 Opcode==ISD::FP_TO_SINT,
1813 APFloat::rmTowardZero);
1814 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1815 break;
1816 return getConstant(x, VT);
1817 }
1818 case ISD::BIT_CONVERT:
1819 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1820 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1821 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1822 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001823 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00001824 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001825 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001826 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001827
1828 unsigned OpOpcode = Operand.Val->getOpcode();
1829 switch (Opcode) {
1830 case ISD::TokenFactor:
1831 return Operand; // Factor of one node? No factor.
Chris Lattner5872a362008-01-17 07:00:52 +00001832 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 case ISD::FP_EXTEND:
1834 assert(MVT::isFloatingPoint(VT) &&
1835 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00001836 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnere6465dd2008-03-11 06:21:08 +00001837 if (Operand.getOpcode() == ISD::UNDEF)
1838 return getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001839 break;
Chris Lattnere6465dd2008-03-11 06:21:08 +00001840 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1842 "Invalid SIGN_EXTEND!");
1843 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001844 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1845 && "Invalid sext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001846 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1847 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1848 break;
1849 case ISD::ZERO_EXTEND:
1850 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1851 "Invalid ZERO_EXTEND!");
1852 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001853 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1854 && "Invalid zext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001855 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1856 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1857 break;
1858 case ISD::ANY_EXTEND:
1859 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1860 "Invalid ANY_EXTEND!");
1861 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001862 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1863 && "Invalid anyext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001864 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1865 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1866 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1867 break;
1868 case ISD::TRUNCATE:
1869 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1870 "Invalid TRUNCATE!");
1871 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsa9810f32007-10-16 09:56:48 +00001872 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1873 && "Invalid truncate node, src < dst!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001874 if (OpOpcode == ISD::TRUNCATE)
1875 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1876 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1877 OpOpcode == ISD::ANY_EXTEND) {
1878 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsa9810f32007-10-16 09:56:48 +00001879 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1880 < MVT::getSizeInBits(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsa9810f32007-10-16 09:56:48 +00001882 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1883 > MVT::getSizeInBits(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001884 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1885 else
1886 return Operand.Val->getOperand(0);
1887 }
1888 break;
1889 case ISD::BIT_CONVERT:
1890 // Basic sanity checking.
1891 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1892 && "Cannot BIT_CONVERT between types of different sizes!");
1893 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1894 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1895 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1896 if (OpOpcode == ISD::UNDEF)
1897 return getNode(ISD::UNDEF, VT);
1898 break;
1899 case ISD::SCALAR_TO_VECTOR:
1900 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1901 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1902 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattner9f0705c2008-03-08 23:43:36 +00001903 if (OpOpcode == ISD::UNDEF)
1904 return getNode(ISD::UNDEF, VT);
1905 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
1906 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
1907 isa<ConstantSDNode>(Operand.getOperand(1)) &&
1908 Operand.getConstantOperandVal(1) == 0 &&
1909 Operand.getOperand(0).getValueType() == VT)
1910 return Operand.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001911 break;
1912 case ISD::FNEG:
1913 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1914 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1915 Operand.Val->getOperand(0));
1916 if (OpOpcode == ISD::FNEG) // --X -> X
1917 return Operand.Val->getOperand(0);
1918 break;
1919 case ISD::FABS:
1920 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1921 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1922 break;
1923 }
1924
1925 SDNode *N;
1926 SDVTList VTs = getVTList(VT);
1927 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1928 FoldingSetNodeID ID;
1929 SDOperand Ops[1] = { Operand };
1930 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1931 void *IP = 0;
1932 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1933 return SDOperand(E, 0);
1934 N = new UnarySDNode(Opcode, VTs, Operand);
1935 CSEMap.InsertNode(N, IP);
1936 } else {
1937 N = new UnarySDNode(Opcode, VTs, Operand);
1938 }
1939 AllNodes.push_back(N);
1940 return SDOperand(N, 0);
1941}
1942
1943
1944
1945SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1946 SDOperand N1, SDOperand N2) {
Chris Lattnercc126e32008-01-22 19:09:33 +00001947 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1948 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001949 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00001950 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001951 case ISD::TokenFactor:
1952 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1953 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00001954 // Fold trivial token factors.
1955 if (N1.getOpcode() == ISD::EntryToken) return N2;
1956 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001957 break;
1958 case ISD::AND:
Chris Lattnercc126e32008-01-22 19:09:33 +00001959 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1960 N1.getValueType() == VT && "Binary operator types must match!");
1961 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1962 // worth handling here.
1963 if (N2C && N2C->getValue() == 0)
1964 return N2;
Chris Lattner8aa8a5e2008-01-26 01:05:42 +00001965 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1966 return N1;
Chris Lattnercc126e32008-01-22 19:09:33 +00001967 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001968 case ISD::OR:
1969 case ISD::XOR:
Chris Lattnercc126e32008-01-22 19:09:33 +00001970 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1971 N1.getValueType() == VT && "Binary operator types must match!");
1972 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1973 // worth handling here.
1974 if (N2C && N2C->getValue() == 0)
1975 return N1;
1976 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977 case ISD::UDIV:
1978 case ISD::UREM:
1979 case ISD::MULHU:
1980 case ISD::MULHS:
1981 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1982 // fall through
1983 case ISD::ADD:
1984 case ISD::SUB:
1985 case ISD::MUL:
1986 case ISD::SDIV:
1987 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988 case ISD::FADD:
1989 case ISD::FSUB:
1990 case ISD::FMUL:
1991 case ISD::FDIV:
1992 case ISD::FREM:
1993 assert(N1.getValueType() == N2.getValueType() &&
1994 N1.getValueType() == VT && "Binary operator types must match!");
1995 break;
1996 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1997 assert(N1.getValueType() == VT &&
1998 MVT::isFloatingPoint(N1.getValueType()) &&
1999 MVT::isFloatingPoint(N2.getValueType()) &&
2000 "Invalid FCOPYSIGN!");
2001 break;
2002 case ISD::SHL:
2003 case ISD::SRA:
2004 case ISD::SRL:
2005 case ISD::ROTL:
2006 case ISD::ROTR:
2007 assert(VT == N1.getValueType() &&
2008 "Shift operators return type must be the same as their first arg");
2009 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
2010 VT != MVT::i1 && "Shifts only work on integers");
2011 break;
2012 case ISD::FP_ROUND_INREG: {
2013 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2014 assert(VT == N1.getValueType() && "Not an inreg round!");
2015 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
2016 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsa9810f32007-10-16 09:56:48 +00002017 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2018 "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002019 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002020 break;
2021 }
Chris Lattner5872a362008-01-17 07:00:52 +00002022 case ISD::FP_ROUND:
2023 assert(MVT::isFloatingPoint(VT) &&
2024 MVT::isFloatingPoint(N1.getValueType()) &&
2025 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2026 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002027 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00002028 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002029 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00002030 case ISD::AssertZext: {
2031 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2032 assert(VT == N1.getValueType() && "Not an inreg extend!");
2033 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2034 "Cannot *_EXTEND_INREG FP types");
2035 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2036 "Not extending!");
Duncan Sands539510b2008-02-10 10:08:52 +00002037 if (VT == EVT) return N1; // noop assertion.
Chris Lattnercc126e32008-01-22 19:09:33 +00002038 break;
2039 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002040 case ISD::SIGN_EXTEND_INREG: {
2041 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2042 assert(VT == N1.getValueType() && "Not an inreg extend!");
2043 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2044 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsa9810f32007-10-16 09:56:48 +00002045 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2046 "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002047 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048
Chris Lattnercc126e32008-01-22 19:09:33 +00002049 if (N1C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002050 APInt Val = N1C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
Dan Gohman161652c2008-02-29 01:47:35 +00002052 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng7faa1d72008-03-06 08:20:51 +00002053 Val = Val.ashr(Val.getBitWidth()-FromBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054 return getConstant(Val, VT);
2055 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002056 break;
2057 }
2058 case ISD::EXTRACT_VECTOR_ELT:
2059 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2060
Chris Lattner9f0705c2008-03-08 23:43:36 +00002061 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2062 if (N1.getOpcode() == ISD::UNDEF)
2063 return getNode(ISD::UNDEF, VT);
2064
Chris Lattnercc126e32008-01-22 19:09:33 +00002065 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2066 // expanding copies of large vectors from registers.
2067 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2068 N1.getNumOperands() > 0) {
2069 unsigned Factor =
2070 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2071 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2072 N1.getOperand(N2C->getValue() / Factor),
2073 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2074 }
2075
2076 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2077 // expanding large vector constants.
2078 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2079 return N1.getOperand(N2C->getValue());
Chris Lattner9f0705c2008-03-08 23:43:36 +00002080
Chris Lattnercc126e32008-01-22 19:09:33 +00002081 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2082 // operations are lowered to scalars.
2083 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2084 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2085 if (IEC == N2C)
2086 return N1.getOperand(1);
2087 else
2088 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2089 }
2090 break;
2091 case ISD::EXTRACT_ELEMENT:
2092 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093
Chris Lattnercc126e32008-01-22 19:09:33 +00002094 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2095 // 64-bit integers into 32-bit parts. Instead of building the extract of
2096 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2097 if (N1.getOpcode() == ISD::BUILD_PAIR)
2098 return N1.getOperand(N2C->getValue());
2099
2100 // EXTRACT_ELEMENT of a constant int is also very common.
2101 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2102 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2103 return getConstant(C->getValue() >> Shift, VT);
2104 }
2105 break;
Duncan Sandsbd13a812008-02-20 17:38:09 +00002106 case ISD::EXTRACT_SUBVECTOR:
2107 if (N1.getValueType() == VT) // Trivial extraction.
2108 return N1;
2109 break;
Chris Lattnercc126e32008-01-22 19:09:33 +00002110 }
2111
2112 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002113 if (N2C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002114 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002115 switch (Opcode) {
2116 case ISD::ADD: return getConstant(C1 + C2, VT);
2117 case ISD::SUB: return getConstant(C1 - C2, VT);
2118 case ISD::MUL: return getConstant(C1 * C2, VT);
2119 case ISD::UDIV:
Dan Gohman161652c2008-02-29 01:47:35 +00002120 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 break;
2122 case ISD::UREM :
Dan Gohman161652c2008-02-29 01:47:35 +00002123 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002124 break;
2125 case ISD::SDIV :
Dan Gohman161652c2008-02-29 01:47:35 +00002126 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002127 break;
2128 case ISD::SREM :
Dan Gohman161652c2008-02-29 01:47:35 +00002129 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002130 break;
2131 case ISD::AND : return getConstant(C1 & C2, VT);
2132 case ISD::OR : return getConstant(C1 | C2, VT);
2133 case ISD::XOR : return getConstant(C1 ^ C2, VT);
2134 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00002135 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2136 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2137 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2138 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002139 default: break;
2140 }
2141 } else { // Cannonicalize constant to RHS if commutative
2142 if (isCommutativeBinOp(Opcode)) {
2143 std::swap(N1C, N2C);
2144 std::swap(N1, N2);
2145 }
2146 }
2147 }
2148
Chris Lattnercc126e32008-01-22 19:09:33 +00002149 // Constant fold FP operations.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2151 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
2152 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002153 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2154 // Cannonicalize constant to RHS if commutative
2155 std::swap(N1CFP, N2CFP);
2156 std::swap(N1, N2);
2157 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002158 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2159 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002160 switch (Opcode) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002161 case ISD::FADD:
2162 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002163 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002164 return getConstantFP(V1, VT);
2165 break;
2166 case ISD::FSUB:
2167 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2168 if (s!=APFloat::opInvalidOp)
2169 return getConstantFP(V1, VT);
2170 break;
2171 case ISD::FMUL:
2172 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2173 if (s!=APFloat::opInvalidOp)
2174 return getConstantFP(V1, VT);
2175 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002176 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002177 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2178 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2179 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002180 break;
2181 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002182 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2183 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2184 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002186 case ISD::FCOPYSIGN:
2187 V1.copySign(V2);
2188 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189 default: break;
2190 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 }
2192 }
2193
2194 // Canonicalize an UNDEF to the RHS, even over a constant.
2195 if (N1.getOpcode() == ISD::UNDEF) {
2196 if (isCommutativeBinOp(Opcode)) {
2197 std::swap(N1, N2);
2198 } else {
2199 switch (Opcode) {
2200 case ISD::FP_ROUND_INREG:
2201 case ISD::SIGN_EXTEND_INREG:
2202 case ISD::SUB:
2203 case ISD::FSUB:
2204 case ISD::FDIV:
2205 case ISD::FREM:
2206 case ISD::SRA:
2207 return N1; // fold op(undef, arg2) -> undef
2208 case ISD::UDIV:
2209 case ISD::SDIV:
2210 case ISD::UREM:
2211 case ISD::SREM:
2212 case ISD::SRL:
2213 case ISD::SHL:
2214 if (!MVT::isVector(VT))
2215 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2216 // For vectors, we can't easily build an all zero vector, just return
2217 // the LHS.
2218 return N2;
2219 }
2220 }
2221 }
2222
2223 // Fold a bunch of operators when the RHS is undef.
2224 if (N2.getOpcode() == ISD::UNDEF) {
2225 switch (Opcode) {
2226 case ISD::ADD:
2227 case ISD::ADDC:
2228 case ISD::ADDE:
2229 case ISD::SUB:
2230 case ISD::FADD:
2231 case ISD::FSUB:
2232 case ISD::FMUL:
2233 case ISD::FDIV:
2234 case ISD::FREM:
2235 case ISD::UDIV:
2236 case ISD::SDIV:
2237 case ISD::UREM:
2238 case ISD::SREM:
2239 case ISD::XOR:
2240 return N2; // fold op(arg1, undef) -> undef
2241 case ISD::MUL:
2242 case ISD::AND:
2243 case ISD::SRL:
2244 case ISD::SHL:
2245 if (!MVT::isVector(VT))
2246 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2247 // For vectors, we can't easily build an all zero vector, just return
2248 // the LHS.
2249 return N1;
2250 case ISD::OR:
2251 if (!MVT::isVector(VT))
2252 return getConstant(MVT::getIntVTBitMask(VT), VT);
2253 // For vectors, we can't easily build an all one vector, just return
2254 // the LHS.
2255 return N1;
2256 case ISD::SRA:
2257 return N1;
2258 }
2259 }
2260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002261 // Memoize this node if possible.
2262 SDNode *N;
2263 SDVTList VTs = getVTList(VT);
2264 if (VT != MVT::Flag) {
2265 SDOperand Ops[] = { N1, N2 };
2266 FoldingSetNodeID ID;
2267 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2268 void *IP = 0;
2269 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2270 return SDOperand(E, 0);
2271 N = new BinarySDNode(Opcode, VTs, N1, N2);
2272 CSEMap.InsertNode(N, IP);
2273 } else {
2274 N = new BinarySDNode(Opcode, VTs, N1, N2);
2275 }
2276
2277 AllNodes.push_back(N);
2278 return SDOperand(N, 0);
2279}
2280
2281SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2282 SDOperand N1, SDOperand N2, SDOperand N3) {
2283 // Perform various simplifications.
2284 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2285 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2286 switch (Opcode) {
2287 case ISD::SETCC: {
2288 // Use FoldSetCC to simplify SETCC's.
2289 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2290 if (Simp.Val) return Simp;
2291 break;
2292 }
2293 case ISD::SELECT:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002294 if (N1C) {
2295 if (N1C->getValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002296 return N2; // select true, X, Y -> X
2297 else
2298 return N3; // select false, X, Y -> Y
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002299 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002300
2301 if (N2 == N3) return N2; // select C, X, X -> X
2302 break;
2303 case ISD::BRCOND:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002304 if (N2C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002305 if (N2C->getValue()) // Unconditional branch
2306 return getNode(ISD::BR, MVT::Other, N1, N3);
2307 else
2308 return N1; // Never-taken branch
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002309 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310 break;
2311 case ISD::VECTOR_SHUFFLE:
2312 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2313 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2314 N3.getOpcode() == ISD::BUILD_VECTOR &&
2315 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2316 "Illegal VECTOR_SHUFFLE node!");
2317 break;
2318 case ISD::BIT_CONVERT:
2319 // Fold bit_convert nodes from a type to themselves.
2320 if (N1.getValueType() == VT)
2321 return N1;
2322 break;
2323 }
2324
2325 // Memoize node if it doesn't produce a flag.
2326 SDNode *N;
2327 SDVTList VTs = getVTList(VT);
2328 if (VT != MVT::Flag) {
2329 SDOperand Ops[] = { N1, N2, N3 };
2330 FoldingSetNodeID ID;
2331 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2332 void *IP = 0;
2333 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2334 return SDOperand(E, 0);
2335 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2336 CSEMap.InsertNode(N, IP);
2337 } else {
2338 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2339 }
2340 AllNodes.push_back(N);
2341 return SDOperand(N, 0);
2342}
2343
2344SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2345 SDOperand N1, SDOperand N2, SDOperand N3,
2346 SDOperand N4) {
2347 SDOperand Ops[] = { N1, N2, N3, N4 };
2348 return getNode(Opcode, VT, Ops, 4);
2349}
2350
2351SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2352 SDOperand N1, SDOperand N2, SDOperand N3,
2353 SDOperand N4, SDOperand N5) {
2354 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2355 return getNode(Opcode, VT, Ops, 5);
2356}
2357
Rafael Espindola80825902007-10-19 10:41:11 +00002358SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2359 SDOperand Src, SDOperand Size,
2360 SDOperand Align,
2361 SDOperand AlwaysInline) {
2362 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2363 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2364}
2365
2366SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2367 SDOperand Src, SDOperand Size,
2368 SDOperand Align,
2369 SDOperand AlwaysInline) {
2370 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2371 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2372}
2373
2374SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2375 SDOperand Src, SDOperand Size,
2376 SDOperand Align,
2377 SDOperand AlwaysInline) {
2378 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2379 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2380}
2381
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002382SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002383 SDOperand Ptr, SDOperand Cmp,
2384 SDOperand Swp, MVT::ValueType VT) {
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002385 assert(Opcode == ISD::ATOMIC_LCS && "Invalid Atomic Op");
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002386 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2387 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002388 FoldingSetNodeID ID;
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002389 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002390 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
2391 ID.AddInteger((unsigned int)VT);
2392 void* IP = 0;
2393 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2394 return SDOperand(E, 0);
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002395 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, VT);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002396 CSEMap.InsertNode(N, IP);
2397 AllNodes.push_back(N);
2398 return SDOperand(N, 0);
2399}
2400
2401SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002402 SDOperand Ptr, SDOperand Val,
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002403 MVT::ValueType VT) {
2404 assert((Opcode == ISD::ATOMIC_LAS || Opcode == ISD::ATOMIC_SWAP)
2405 && "Invalid Atomic Op");
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002406 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002407 FoldingSetNodeID ID;
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002408 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002409 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2410 ID.AddInteger((unsigned int)VT);
2411 void* IP = 0;
2412 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2413 return SDOperand(E, 0);
Andrew Lenharth2205e3b2008-02-21 16:11:38 +00002414 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, VT);
Andrew Lenharthe44f3902008-02-21 06:45:13 +00002415 CSEMap.InsertNode(N, IP);
2416 AllNodes.push_back(N);
2417 return SDOperand(N, 0);
2418}
2419
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002420SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2421 SDOperand Chain, SDOperand Ptr,
2422 const Value *SV, int SVOffset,
2423 bool isVolatile, unsigned Alignment) {
2424 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2425 const Type *Ty = 0;
2426 if (VT != MVT::iPTR) {
2427 Ty = MVT::getTypeForValueType(VT);
2428 } else if (SV) {
2429 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2430 assert(PT && "Value for load must be a pointer");
2431 Ty = PT->getElementType();
2432 }
2433 assert(Ty && "Could not get type information for load");
2434 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2435 }
2436 SDVTList VTs = getVTList(VT, MVT::Other);
2437 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2438 SDOperand Ops[] = { Chain, Ptr, Undef };
2439 FoldingSetNodeID ID;
2440 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2441 ID.AddInteger(ISD::UNINDEXED);
2442 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner4a22a672007-09-13 06:09:48 +00002443 ID.AddInteger((unsigned int)VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 ID.AddInteger(Alignment);
2445 ID.AddInteger(isVolatile);
2446 void *IP = 0;
2447 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2448 return SDOperand(E, 0);
2449 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2450 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2451 isVolatile);
2452 CSEMap.InsertNode(N, IP);
2453 AllNodes.push_back(N);
2454 return SDOperand(N, 0);
2455}
2456
2457SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2458 SDOperand Chain, SDOperand Ptr,
2459 const Value *SV,
2460 int SVOffset, MVT::ValueType EVT,
2461 bool isVolatile, unsigned Alignment) {
2462 // If they are asking for an extending load from/to the same thing, return a
2463 // normal load.
2464 if (VT == EVT)
Duncan Sands9b614742007-10-19 13:05:40 +00002465 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466
2467 if (MVT::isVector(VT))
2468 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2469 else
Duncan Sandsa9810f32007-10-16 09:56:48 +00002470 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2471 "Should only be an extending load, not truncating!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2473 "Cannot sign/zero extend a FP/Vector load!");
2474 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2475 "Cannot convert from FP to Int or Int -> FP!");
2476
2477 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2478 const Type *Ty = 0;
2479 if (VT != MVT::iPTR) {
2480 Ty = MVT::getTypeForValueType(VT);
2481 } else if (SV) {
2482 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2483 assert(PT && "Value for load must be a pointer");
2484 Ty = PT->getElementType();
2485 }
2486 assert(Ty && "Could not get type information for load");
2487 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2488 }
2489 SDVTList VTs = getVTList(VT, MVT::Other);
2490 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2491 SDOperand Ops[] = { Chain, Ptr, Undef };
2492 FoldingSetNodeID ID;
2493 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2494 ID.AddInteger(ISD::UNINDEXED);
2495 ID.AddInteger(ExtType);
Chris Lattner4a22a672007-09-13 06:09:48 +00002496 ID.AddInteger((unsigned int)EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002497 ID.AddInteger(Alignment);
2498 ID.AddInteger(isVolatile);
2499 void *IP = 0;
2500 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2501 return SDOperand(E, 0);
2502 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2503 SV, SVOffset, Alignment, isVolatile);
2504 CSEMap.InsertNode(N, IP);
2505 AllNodes.push_back(N);
2506 return SDOperand(N, 0);
2507}
2508
2509SDOperand
2510SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2511 SDOperand Offset, ISD::MemIndexedMode AM) {
2512 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2513 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2514 "Load is already a indexed load!");
2515 MVT::ValueType VT = OrigLoad.getValueType();
2516 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2517 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2518 FoldingSetNodeID ID;
2519 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2520 ID.AddInteger(AM);
2521 ID.AddInteger(LD->getExtensionType());
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002522 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 ID.AddInteger(LD->getAlignment());
2524 ID.AddInteger(LD->isVolatile());
2525 void *IP = 0;
2526 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2527 return SDOperand(E, 0);
2528 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002529 LD->getExtensionType(), LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 LD->getSrcValue(), LD->getSrcValueOffset(),
2531 LD->getAlignment(), LD->isVolatile());
2532 CSEMap.InsertNode(N, IP);
2533 AllNodes.push_back(N);
2534 return SDOperand(N, 0);
2535}
2536
2537SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2538 SDOperand Ptr, const Value *SV, int SVOffset,
2539 bool isVolatile, unsigned Alignment) {
2540 MVT::ValueType VT = Val.getValueType();
2541
2542 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2543 const Type *Ty = 0;
2544 if (VT != MVT::iPTR) {
2545 Ty = MVT::getTypeForValueType(VT);
2546 } else if (SV) {
2547 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2548 assert(PT && "Value for store must be a pointer");
2549 Ty = PT->getElementType();
2550 }
2551 assert(Ty && "Could not get type information for store");
2552 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2553 }
2554 SDVTList VTs = getVTList(MVT::Other);
2555 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2556 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2557 FoldingSetNodeID ID;
2558 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2559 ID.AddInteger(ISD::UNINDEXED);
2560 ID.AddInteger(false);
Chris Lattner4a22a672007-09-13 06:09:48 +00002561 ID.AddInteger((unsigned int)VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 ID.AddInteger(Alignment);
2563 ID.AddInteger(isVolatile);
2564 void *IP = 0;
2565 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2566 return SDOperand(E, 0);
2567 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2568 VT, SV, SVOffset, Alignment, isVolatile);
2569 CSEMap.InsertNode(N, IP);
2570 AllNodes.push_back(N);
2571 return SDOperand(N, 0);
2572}
2573
2574SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2575 SDOperand Ptr, const Value *SV,
2576 int SVOffset, MVT::ValueType SVT,
2577 bool isVolatile, unsigned Alignment) {
2578 MVT::ValueType VT = Val.getValueType();
Duncan Sands06fcf652007-10-30 12:40:58 +00002579
2580 if (VT == SVT)
2581 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582
Duncan Sandsa9810f32007-10-16 09:56:48 +00002583 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2584 "Not a truncation?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2586 "Can't do FP-INT conversion!");
2587
2588 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2589 const Type *Ty = 0;
2590 if (VT != MVT::iPTR) {
2591 Ty = MVT::getTypeForValueType(VT);
2592 } else if (SV) {
2593 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2594 assert(PT && "Value for store must be a pointer");
2595 Ty = PT->getElementType();
2596 }
2597 assert(Ty && "Could not get type information for store");
2598 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2599 }
2600 SDVTList VTs = getVTList(MVT::Other);
2601 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2602 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2603 FoldingSetNodeID ID;
2604 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2605 ID.AddInteger(ISD::UNINDEXED);
Duncan Sands06fcf652007-10-30 12:40:58 +00002606 ID.AddInteger(1);
Chris Lattner4a22a672007-09-13 06:09:48 +00002607 ID.AddInteger((unsigned int)SVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608 ID.AddInteger(Alignment);
2609 ID.AddInteger(isVolatile);
2610 void *IP = 0;
2611 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2612 return SDOperand(E, 0);
Duncan Sands06fcf652007-10-30 12:40:58 +00002613 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002614 SVT, SV, SVOffset, Alignment, isVolatile);
2615 CSEMap.InsertNode(N, IP);
2616 AllNodes.push_back(N);
2617 return SDOperand(N, 0);
2618}
2619
2620SDOperand
2621SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2622 SDOperand Offset, ISD::MemIndexedMode AM) {
2623 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2624 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2625 "Store is already a indexed store!");
2626 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2627 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2628 FoldingSetNodeID ID;
2629 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2630 ID.AddInteger(AM);
2631 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002632 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633 ID.AddInteger(ST->getAlignment());
2634 ID.AddInteger(ST->isVolatile());
2635 void *IP = 0;
2636 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2637 return SDOperand(E, 0);
2638 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002639 ST->isTruncatingStore(), ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640 ST->getSrcValue(), ST->getSrcValueOffset(),
2641 ST->getAlignment(), ST->isVolatile());
2642 CSEMap.InsertNode(N, IP);
2643 AllNodes.push_back(N);
2644 return SDOperand(N, 0);
2645}
2646
2647SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2648 SDOperand Chain, SDOperand Ptr,
2649 SDOperand SV) {
2650 SDOperand Ops[] = { Chain, Ptr, SV };
2651 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2652}
2653
2654SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2655 const SDOperand *Ops, unsigned NumOps) {
2656 switch (NumOps) {
2657 case 0: return getNode(Opcode, VT);
2658 case 1: return getNode(Opcode, VT, Ops[0]);
2659 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2660 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2661 default: break;
2662 }
2663
2664 switch (Opcode) {
2665 default: break;
2666 case ISD::SELECT_CC: {
2667 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2668 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2669 "LHS and RHS of condition must have same type!");
2670 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2671 "True and False arms of SelectCC must have same type!");
2672 assert(Ops[2].getValueType() == VT &&
2673 "select_cc node must be of same type as true and false value!");
2674 break;
2675 }
2676 case ISD::BR_CC: {
2677 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2678 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2679 "LHS/RHS of comparison should match types!");
2680 break;
2681 }
2682 }
2683
2684 // Memoize nodes.
2685 SDNode *N;
2686 SDVTList VTs = getVTList(VT);
2687 if (VT != MVT::Flag) {
2688 FoldingSetNodeID ID;
2689 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2690 void *IP = 0;
2691 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2692 return SDOperand(E, 0);
2693 N = new SDNode(Opcode, VTs, Ops, NumOps);
2694 CSEMap.InsertNode(N, IP);
2695 } else {
2696 N = new SDNode(Opcode, VTs, Ops, NumOps);
2697 }
2698 AllNodes.push_back(N);
2699 return SDOperand(N, 0);
2700}
2701
2702SDOperand SelectionDAG::getNode(unsigned Opcode,
2703 std::vector<MVT::ValueType> &ResultTys,
2704 const SDOperand *Ops, unsigned NumOps) {
2705 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2706 Ops, NumOps);
2707}
2708
2709SDOperand SelectionDAG::getNode(unsigned Opcode,
2710 const MVT::ValueType *VTs, unsigned NumVTs,
2711 const SDOperand *Ops, unsigned NumOps) {
2712 if (NumVTs == 1)
2713 return getNode(Opcode, VTs[0], Ops, NumOps);
2714 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2715}
2716
2717SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2718 const SDOperand *Ops, unsigned NumOps) {
2719 if (VTList.NumVTs == 1)
2720 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2721
2722 switch (Opcode) {
2723 // FIXME: figure out how to safely handle things like
2724 // int foo(int x) { return 1 << (x & 255); }
2725 // int bar() { return foo(256); }
2726#if 0
2727 case ISD::SRA_PARTS:
2728 case ISD::SRL_PARTS:
2729 case ISD::SHL_PARTS:
2730 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2731 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2732 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2733 else if (N3.getOpcode() == ISD::AND)
2734 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2735 // If the and is only masking out bits that cannot effect the shift,
2736 // eliminate the and.
2737 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2738 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2739 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2740 }
2741 break;
2742#endif
2743 }
2744
2745 // Memoize the node unless it returns a flag.
2746 SDNode *N;
2747 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2748 FoldingSetNodeID ID;
2749 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2750 void *IP = 0;
2751 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2752 return SDOperand(E, 0);
2753 if (NumOps == 1)
2754 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2755 else if (NumOps == 2)
2756 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2757 else if (NumOps == 3)
2758 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2759 else
2760 N = new SDNode(Opcode, VTList, Ops, NumOps);
2761 CSEMap.InsertNode(N, IP);
2762 } else {
2763 if (NumOps == 1)
2764 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2765 else if (NumOps == 2)
2766 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2767 else if (NumOps == 3)
2768 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2769 else
2770 N = new SDNode(Opcode, VTList, Ops, NumOps);
2771 }
2772 AllNodes.push_back(N);
2773 return SDOperand(N, 0);
2774}
2775
Dan Gohman798d1272007-10-08 15:49:58 +00002776SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2777 return getNode(Opcode, VTList, 0, 0);
2778}
2779
2780SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2781 SDOperand N1) {
2782 SDOperand Ops[] = { N1 };
2783 return getNode(Opcode, VTList, Ops, 1);
2784}
2785
2786SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2787 SDOperand N1, SDOperand N2) {
2788 SDOperand Ops[] = { N1, N2 };
2789 return getNode(Opcode, VTList, Ops, 2);
2790}
2791
2792SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2793 SDOperand N1, SDOperand N2, SDOperand N3) {
2794 SDOperand Ops[] = { N1, N2, N3 };
2795 return getNode(Opcode, VTList, Ops, 3);
2796}
2797
2798SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2799 SDOperand N1, SDOperand N2, SDOperand N3,
2800 SDOperand N4) {
2801 SDOperand Ops[] = { N1, N2, N3, N4 };
2802 return getNode(Opcode, VTList, Ops, 4);
2803}
2804
2805SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2806 SDOperand N1, SDOperand N2, SDOperand N3,
2807 SDOperand N4, SDOperand N5) {
2808 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2809 return getNode(Opcode, VTList, Ops, 5);
2810}
2811
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00002813 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814}
2815
2816SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2817 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2818 E = VTList.end(); I != E; ++I) {
2819 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2820 return makeVTList(&(*I)[0], 2);
2821 }
2822 std::vector<MVT::ValueType> V;
2823 V.push_back(VT1);
2824 V.push_back(VT2);
2825 VTList.push_front(V);
2826 return makeVTList(&(*VTList.begin())[0], 2);
2827}
2828SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2829 MVT::ValueType VT3) {
2830 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2831 E = VTList.end(); I != E; ++I) {
2832 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2833 (*I)[2] == VT3)
2834 return makeVTList(&(*I)[0], 3);
2835 }
2836 std::vector<MVT::ValueType> V;
2837 V.push_back(VT1);
2838 V.push_back(VT2);
2839 V.push_back(VT3);
2840 VTList.push_front(V);
2841 return makeVTList(&(*VTList.begin())[0], 3);
2842}
2843
2844SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2845 switch (NumVTs) {
2846 case 0: assert(0 && "Cannot have nodes without results!");
2847 case 1: return getVTList(VTs[0]);
2848 case 2: return getVTList(VTs[0], VTs[1]);
2849 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2850 default: break;
2851 }
2852
2853 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2854 E = VTList.end(); I != E; ++I) {
2855 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2856
2857 bool NoMatch = false;
2858 for (unsigned i = 2; i != NumVTs; ++i)
2859 if (VTs[i] != (*I)[i]) {
2860 NoMatch = true;
2861 break;
2862 }
2863 if (!NoMatch)
2864 return makeVTList(&*I->begin(), NumVTs);
2865 }
2866
2867 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2868 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2869}
2870
2871
2872/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2873/// specified operands. If the resultant node already exists in the DAG,
2874/// this does not modify the specified node, instead it returns the node that
2875/// already exists. If the resultant node does not exist in the DAG, the
2876/// input node is returned. As a degenerate case, if you specify the same
2877/// input operands as the node already has, the input node is returned.
2878SDOperand SelectionDAG::
2879UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2880 SDNode *N = InN.Val;
2881 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2882
2883 // Check to see if there is no change.
2884 if (Op == N->getOperand(0)) return InN;
2885
2886 // See if the modified node already exists.
2887 void *InsertPos = 0;
2888 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2889 return SDOperand(Existing, InN.ResNo);
2890
2891 // Nope it doesn't. Remove the node from it's current place in the maps.
2892 if (InsertPos)
2893 RemoveNodeFromCSEMaps(N);
2894
2895 // Now we update the operands.
2896 N->OperandList[0].Val->removeUser(N);
2897 Op.Val->addUser(N);
2898 N->OperandList[0] = Op;
2899
2900 // If this gets put into a CSE map, add it.
2901 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2902 return InN;
2903}
2904
2905SDOperand SelectionDAG::
2906UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2907 SDNode *N = InN.Val;
2908 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2909
2910 // Check to see if there is no change.
2911 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2912 return InN; // No operands changed, just return the input node.
2913
2914 // See if the modified node already exists.
2915 void *InsertPos = 0;
2916 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2917 return SDOperand(Existing, InN.ResNo);
2918
2919 // Nope it doesn't. Remove the node from it's current place in the maps.
2920 if (InsertPos)
2921 RemoveNodeFromCSEMaps(N);
2922
2923 // Now we update the operands.
2924 if (N->OperandList[0] != Op1) {
2925 N->OperandList[0].Val->removeUser(N);
2926 Op1.Val->addUser(N);
2927 N->OperandList[0] = Op1;
2928 }
2929 if (N->OperandList[1] != Op2) {
2930 N->OperandList[1].Val->removeUser(N);
2931 Op2.Val->addUser(N);
2932 N->OperandList[1] = Op2;
2933 }
2934
2935 // If this gets put into a CSE map, add it.
2936 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2937 return InN;
2938}
2939
2940SDOperand SelectionDAG::
2941UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2942 SDOperand Ops[] = { Op1, Op2, Op3 };
2943 return UpdateNodeOperands(N, Ops, 3);
2944}
2945
2946SDOperand SelectionDAG::
2947UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2948 SDOperand Op3, SDOperand Op4) {
2949 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2950 return UpdateNodeOperands(N, Ops, 4);
2951}
2952
2953SDOperand SelectionDAG::
2954UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2955 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2956 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2957 return UpdateNodeOperands(N, Ops, 5);
2958}
2959
2960
2961SDOperand SelectionDAG::
2962UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2963 SDNode *N = InN.Val;
2964 assert(N->getNumOperands() == NumOps &&
2965 "Update with wrong number of operands");
2966
2967 // Check to see if there is no change.
2968 bool AnyChange = false;
2969 for (unsigned i = 0; i != NumOps; ++i) {
2970 if (Ops[i] != N->getOperand(i)) {
2971 AnyChange = true;
2972 break;
2973 }
2974 }
2975
2976 // No operands changed, just return the input node.
2977 if (!AnyChange) return InN;
2978
2979 // See if the modified node already exists.
2980 void *InsertPos = 0;
2981 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2982 return SDOperand(Existing, InN.ResNo);
2983
2984 // Nope it doesn't. Remove the node from it's current place in the maps.
2985 if (InsertPos)
2986 RemoveNodeFromCSEMaps(N);
2987
2988 // Now we update the operands.
2989 for (unsigned i = 0; i != NumOps; ++i) {
2990 if (N->OperandList[i] != Ops[i]) {
2991 N->OperandList[i].Val->removeUser(N);
2992 Ops[i].Val->addUser(N);
2993 N->OperandList[i] = Ops[i];
2994 }
2995 }
2996
2997 // If this gets put into a CSE map, add it.
2998 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2999 return InN;
3000}
3001
3002
3003/// MorphNodeTo - This frees the operands of the current node, resets the
3004/// opcode, types, and operands to the specified value. This should only be
3005/// used by the SelectionDAG class.
3006void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
3007 const SDOperand *Ops, unsigned NumOps) {
3008 NodeType = Opc;
3009 ValueList = L.VTs;
3010 NumValues = L.NumVTs;
3011
3012 // Clear the operands list, updating used nodes to remove this from their
3013 // use list.
3014 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3015 I->Val->removeUser(this);
3016
3017 // If NumOps is larger than the # of operands we currently have, reallocate
3018 // the operand list.
3019 if (NumOps > NumOperands) {
3020 if (OperandsNeedDelete)
3021 delete [] OperandList;
3022 OperandList = new SDOperand[NumOps];
3023 OperandsNeedDelete = true;
3024 }
3025
3026 // Assign the new operands.
3027 NumOperands = NumOps;
3028
3029 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3030 OperandList[i] = Ops[i];
3031 SDNode *N = OperandList[i].Val;
3032 N->Uses.push_back(this);
3033 }
3034}
3035
3036/// SelectNodeTo - These are used for target selectors to *mutate* the
3037/// specified node to have the specified return type, Target opcode, and
3038/// operands. Note that target opcodes are stored as
3039/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
3040///
3041/// Note that SelectNodeTo returns the resultant node. If there is already a
3042/// node of the specified opcode and operands, it returns that node instead of
3043/// the current one.
3044SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3045 MVT::ValueType VT) {
3046 SDVTList VTs = getVTList(VT);
3047 FoldingSetNodeID ID;
3048 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
3049 void *IP = 0;
3050 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3051 return ON;
3052
3053 RemoveNodeFromCSEMaps(N);
3054
3055 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
3056
3057 CSEMap.InsertNode(N, IP);
3058 return N;
3059}
3060
3061SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3062 MVT::ValueType VT, SDOperand Op1) {
3063 // If an identical node already exists, use it.
3064 SDVTList VTs = getVTList(VT);
3065 SDOperand Ops[] = { Op1 };
3066
3067 FoldingSetNodeID ID;
3068 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3069 void *IP = 0;
3070 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3071 return ON;
3072
3073 RemoveNodeFromCSEMaps(N);
3074 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3075 CSEMap.InsertNode(N, IP);
3076 return N;
3077}
3078
3079SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3080 MVT::ValueType VT, SDOperand Op1,
3081 SDOperand Op2) {
3082 // If an identical node already exists, use it.
3083 SDVTList VTs = getVTList(VT);
3084 SDOperand Ops[] = { Op1, Op2 };
3085
3086 FoldingSetNodeID ID;
3087 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3088 void *IP = 0;
3089 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3090 return ON;
3091
3092 RemoveNodeFromCSEMaps(N);
3093
3094 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3095
3096 CSEMap.InsertNode(N, IP); // Memoize the new node.
3097 return N;
3098}
3099
3100SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3101 MVT::ValueType VT, SDOperand Op1,
3102 SDOperand Op2, SDOperand Op3) {
3103 // If an identical node already exists, use it.
3104 SDVTList VTs = getVTList(VT);
3105 SDOperand Ops[] = { Op1, Op2, Op3 };
3106 FoldingSetNodeID ID;
3107 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3108 void *IP = 0;
3109 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3110 return ON;
3111
3112 RemoveNodeFromCSEMaps(N);
3113
3114 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3115
3116 CSEMap.InsertNode(N, IP); // Memoize the new node.
3117 return N;
3118}
3119
3120SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3121 MVT::ValueType VT, const SDOperand *Ops,
3122 unsigned NumOps) {
3123 // If an identical node already exists, use it.
3124 SDVTList VTs = getVTList(VT);
3125 FoldingSetNodeID ID;
3126 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3127 void *IP = 0;
3128 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3129 return ON;
3130
3131 RemoveNodeFromCSEMaps(N);
3132 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3133
3134 CSEMap.InsertNode(N, IP); // Memoize the new node.
3135 return N;
3136}
3137
3138SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3139 MVT::ValueType VT1, MVT::ValueType VT2,
3140 SDOperand Op1, SDOperand Op2) {
3141 SDVTList VTs = getVTList(VT1, VT2);
3142 FoldingSetNodeID ID;
3143 SDOperand Ops[] = { Op1, Op2 };
3144 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3145 void *IP = 0;
3146 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3147 return ON;
3148
3149 RemoveNodeFromCSEMaps(N);
3150 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3151 CSEMap.InsertNode(N, IP); // Memoize the new node.
3152 return N;
3153}
3154
3155SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3156 MVT::ValueType VT1, MVT::ValueType VT2,
3157 SDOperand Op1, SDOperand Op2,
3158 SDOperand Op3) {
3159 // If an identical node already exists, use it.
3160 SDVTList VTs = getVTList(VT1, VT2);
3161 SDOperand Ops[] = { Op1, Op2, Op3 };
3162 FoldingSetNodeID ID;
3163 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3164 void *IP = 0;
3165 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3166 return ON;
3167
3168 RemoveNodeFromCSEMaps(N);
3169
3170 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3171 CSEMap.InsertNode(N, IP); // Memoize the new node.
3172 return N;
3173}
3174
3175
3176/// getTargetNode - These are used for target selectors to create a new node
3177/// with specified return type(s), target opcode, and operands.
3178///
3179/// Note that getTargetNode returns the resultant node. If there is already a
3180/// node of the specified opcode and operands, it returns that node instead of
3181/// the current one.
3182SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3183 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3184}
3185SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3186 SDOperand Op1) {
3187 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3188}
3189SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3190 SDOperand Op1, SDOperand Op2) {
3191 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3192}
3193SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3194 SDOperand Op1, SDOperand Op2,
3195 SDOperand Op3) {
3196 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3197}
3198SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3199 const SDOperand *Ops, unsigned NumOps) {
3200 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3201}
3202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003203 MVT::ValueType VT2) {
3204 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3205 SDOperand Op;
3206 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3207}
3208SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003209 MVT::ValueType VT2, SDOperand Op1) {
3210 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3211 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3212}
3213SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3214 MVT::ValueType VT2, SDOperand Op1,
3215 SDOperand Op2) {
3216 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3217 SDOperand Ops[] = { Op1, Op2 };
3218 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3219}
3220SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3221 MVT::ValueType VT2, SDOperand Op1,
3222 SDOperand Op2, SDOperand Op3) {
3223 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3224 SDOperand Ops[] = { Op1, Op2, Op3 };
3225 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3226}
3227SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3228 MVT::ValueType VT2,
3229 const SDOperand *Ops, unsigned NumOps) {
3230 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3231 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3232}
3233SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3234 MVT::ValueType VT2, MVT::ValueType VT3,
3235 SDOperand Op1, SDOperand Op2) {
3236 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3237 SDOperand Ops[] = { Op1, Op2 };
3238 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3239}
3240SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3241 MVT::ValueType VT2, MVT::ValueType VT3,
3242 SDOperand Op1, SDOperand Op2,
3243 SDOperand Op3) {
3244 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3245 SDOperand Ops[] = { Op1, Op2, Op3 };
3246 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3247}
3248SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3249 MVT::ValueType VT2, MVT::ValueType VT3,
3250 const SDOperand *Ops, unsigned NumOps) {
3251 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3252 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3253}
Evan Chenge1d067e2007-09-12 23:39:49 +00003254SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3255 MVT::ValueType VT2, MVT::ValueType VT3,
3256 MVT::ValueType VT4,
3257 const SDOperand *Ops, unsigned NumOps) {
3258 std::vector<MVT::ValueType> VTList;
3259 VTList.push_back(VT1);
3260 VTList.push_back(VT2);
3261 VTList.push_back(VT3);
3262 VTList.push_back(VT4);
3263 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3264 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3265}
Evan Chenge3940912007-10-05 01:10:49 +00003266SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3267 std::vector<MVT::ValueType> &ResultTys,
3268 const SDOperand *Ops, unsigned NumOps) {
3269 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3270 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3271 Ops, NumOps).Val;
3272}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003274
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003275/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3276/// This can cause recursive merging of nodes in the DAG.
3277///
Chris Lattnerdca329f2008-02-03 03:35:22 +00003278/// This version assumes From has a single result value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003279///
Chris Lattnerdca329f2008-02-03 03:35:22 +00003280void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003281 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00003282 SDNode *From = FromN.Val;
Chris Lattnerdca329f2008-02-03 03:35:22 +00003283 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003284 "Cannot replace with this method!");
Chris Lattnerdca329f2008-02-03 03:35:22 +00003285 assert(From != To.Val && "Cannot replace uses of with self");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286
3287 while (!From->use_empty()) {
3288 // Process users until they are all gone.
3289 SDNode *U = *From->use_begin();
3290
3291 // This node is about to morph, remove its old self from the CSE maps.
3292 RemoveNodeFromCSEMaps(U);
3293
3294 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3295 I != E; ++I)
3296 if (I->Val == From) {
3297 From->removeUser(U);
Chris Lattnerdca329f2008-02-03 03:35:22 +00003298 *I = To;
3299 To.Val->addUser(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 }
3301
3302 // Now that we have modified U, add it back to the CSE maps. If it already
3303 // exists there, recursively merge the results together.
3304 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003305 ReplaceAllUsesWith(U, Existing, UpdateListener);
3306 // U is now dead. Inform the listener if it exists and delete it.
3307 if (UpdateListener)
3308 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003310 } else {
3311 // If the node doesn't already exist, we updated it. Inform a listener if
3312 // it exists.
3313 if (UpdateListener)
3314 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003315 }
3316 }
3317}
3318
3319/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3320/// This can cause recursive merging of nodes in the DAG.
3321///
3322/// This version assumes From/To have matching types and numbers of result
3323/// values.
3324///
3325void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003326 DAGUpdateListener *UpdateListener) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003327 assert(From != To && "Cannot replace uses of with self");
3328 assert(From->getNumValues() == To->getNumValues() &&
3329 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattnerdca329f2008-02-03 03:35:22 +00003330 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003331 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3332 UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003333
3334 while (!From->use_empty()) {
3335 // Process users until they are all gone.
3336 SDNode *U = *From->use_begin();
3337
3338 // This node is about to morph, remove its old self from the CSE maps.
3339 RemoveNodeFromCSEMaps(U);
3340
3341 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3342 I != E; ++I)
3343 if (I->Val == From) {
3344 From->removeUser(U);
3345 I->Val = To;
3346 To->addUser(U);
3347 }
3348
3349 // Now that we have modified U, add it back to the CSE maps. If it already
3350 // exists there, recursively merge the results together.
3351 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003352 ReplaceAllUsesWith(U, Existing, UpdateListener);
3353 // U is now dead. Inform the listener if it exists and delete it.
3354 if (UpdateListener)
3355 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003356 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003357 } else {
3358 // If the node doesn't already exist, we updated it. Inform a listener if
3359 // it exists.
3360 if (UpdateListener)
3361 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003362 }
3363 }
3364}
3365
3366/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3367/// This can cause recursive merging of nodes in the DAG.
3368///
3369/// This version can replace From with any result values. To must match the
3370/// number and types of values returned by From.
3371void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3372 const SDOperand *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003373 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00003374 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003375 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376
3377 while (!From->use_empty()) {
3378 // Process users until they are all gone.
3379 SDNode *U = *From->use_begin();
3380
3381 // This node is about to morph, remove its old self from the CSE maps.
3382 RemoveNodeFromCSEMaps(U);
3383
3384 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3385 I != E; ++I)
3386 if (I->Val == From) {
3387 const SDOperand &ToOp = To[I->ResNo];
3388 From->removeUser(U);
3389 *I = ToOp;
3390 ToOp.Val->addUser(U);
3391 }
3392
3393 // Now that we have modified U, add it back to the CSE maps. If it already
3394 // exists there, recursively merge the results together.
3395 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003396 ReplaceAllUsesWith(U, Existing, UpdateListener);
3397 // U is now dead. Inform the listener if it exists and delete it.
3398 if (UpdateListener)
3399 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003400 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003401 } else {
3402 // If the node doesn't already exist, we updated it. Inform a listener if
3403 // it exists.
3404 if (UpdateListener)
3405 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003406 }
3407 }
3408}
3409
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003410namespace {
3411 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3412 /// any deleted nodes from the set passed into its constructor and recursively
3413 /// notifies another update listener if specified.
3414 class ChainedSetUpdaterListener :
3415 public SelectionDAG::DAGUpdateListener {
3416 SmallSetVector<SDNode*, 16> &Set;
3417 SelectionDAG::DAGUpdateListener *Chain;
3418 public:
3419 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3420 SelectionDAG::DAGUpdateListener *chain)
3421 : Set(set), Chain(chain) {}
3422
3423 virtual void NodeDeleted(SDNode *N) {
3424 Set.remove(N);
3425 if (Chain) Chain->NodeDeleted(N);
3426 }
3427 virtual void NodeUpdated(SDNode *N) {
3428 if (Chain) Chain->NodeUpdated(N);
3429 }
3430 };
3431}
3432
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003433/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3434/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003435/// handled the same way as for ReplaceAllUsesWith.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003436void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003437 DAGUpdateListener *UpdateListener){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003438 assert(From != To && "Cannot replace a value with itself");
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003439
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003440 // Handle the simple, trivial, case efficiently.
Chris Lattnerdca329f2008-02-03 03:35:22 +00003441 if (From.Val->getNumValues() == 1) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003442 ReplaceAllUsesWith(From, To, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003443 return;
3444 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003445
3446 if (From.use_empty()) return;
3447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003448 // Get all of the users of From.Val. We want these in a nice,
3449 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3450 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3451
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003452 // When one of the recursive merges deletes nodes from the graph, we need to
3453 // make sure that UpdateListener is notified *and* that the node is removed
3454 // from Users if present. CSUL does this.
3455 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner8a258202007-10-15 06:10:22 +00003456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003457 while (!Users.empty()) {
3458 // We know that this user uses some value of From. If it is the right
3459 // value, update it.
3460 SDNode *User = Users.back();
3461 Users.pop_back();
3462
Chris Lattner8a258202007-10-15 06:10:22 +00003463 // Scan for an operand that matches From.
3464 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3465 for (; Op != E; ++Op)
3466 if (*Op == From) break;
3467
3468 // If there are no matches, the user must use some other result of From.
3469 if (Op == E) continue;
3470
3471 // Okay, we know this user needs to be updated. Remove its old self
3472 // from the CSE maps.
3473 RemoveNodeFromCSEMaps(User);
3474
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003475 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner8a258202007-10-15 06:10:22 +00003476 for (; Op != E; ++Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003477 if (*Op == From) {
Chris Lattner8a258202007-10-15 06:10:22 +00003478 From.Val->removeUser(User);
3479 *Op = To;
3480 To.Val->addUser(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003481 }
3482 }
Chris Lattner8a258202007-10-15 06:10:22 +00003483
3484 // Now that we have modified User, add it back to the CSE maps. If it
3485 // already exists there, recursively merge the results together.
3486 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003487 if (!Existing) {
3488 if (UpdateListener) UpdateListener->NodeUpdated(User);
3489 continue; // Continue on to next user.
3490 }
Chris Lattner8a258202007-10-15 06:10:22 +00003491
3492 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003493 // to replace the dead one with the existing one. This can cause
Chris Lattner8a258202007-10-15 06:10:22 +00003494 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003495 // can cause deletion of nodes that used the old value. To handle this, we
3496 // use CSUL to remove them from the Users set.
3497 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner8a258202007-10-15 06:10:22 +00003498
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003499 // User is now dead. Notify a listener if present.
3500 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner8a258202007-10-15 06:10:22 +00003501 DeleteNodeNotInCSEMaps(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003502 }
3503}
3504
3505
3506/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3507/// their allnodes order. It returns the maximum id.
3508unsigned SelectionDAG::AssignNodeIds() {
3509 unsigned Id = 0;
3510 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3511 SDNode *N = I;
3512 N->setNodeId(Id++);
3513 }
3514 return Id;
3515}
3516
3517/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3518/// based on their topological order. It returns the maximum id and a vector
3519/// of the SDNodes* in assigned order by reference.
3520unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3521 unsigned DAGSize = AllNodes.size();
3522 std::vector<unsigned> InDegree(DAGSize);
3523 std::vector<SDNode*> Sources;
3524
3525 // Use a two pass approach to avoid using a std::map which is slow.
3526 unsigned Id = 0;
3527 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3528 SDNode *N = I;
3529 N->setNodeId(Id++);
3530 unsigned Degree = N->use_size();
3531 InDegree[N->getNodeId()] = Degree;
3532 if (Degree == 0)
3533 Sources.push_back(N);
3534 }
3535
3536 TopOrder.clear();
3537 while (!Sources.empty()) {
3538 SDNode *N = Sources.back();
3539 Sources.pop_back();
3540 TopOrder.push_back(N);
3541 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3542 SDNode *P = I->Val;
3543 unsigned Degree = --InDegree[P->getNodeId()];
3544 if (Degree == 0)
3545 Sources.push_back(P);
3546 }
3547 }
3548
3549 // Second pass, assign the actual topological order as node ids.
3550 Id = 0;
3551 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3552 TI != TE; ++TI)
3553 (*TI)->setNodeId(Id++);
3554
3555 return Id;
3556}
3557
3558
3559
3560//===----------------------------------------------------------------------===//
3561// SDNode Class
3562//===----------------------------------------------------------------------===//
3563
3564// Out-of-line virtual method to give class a home.
3565void SDNode::ANCHOR() {}
3566void UnarySDNode::ANCHOR() {}
3567void BinarySDNode::ANCHOR() {}
3568void TernarySDNode::ANCHOR() {}
3569void HandleSDNode::ANCHOR() {}
3570void StringSDNode::ANCHOR() {}
3571void ConstantSDNode::ANCHOR() {}
3572void ConstantFPSDNode::ANCHOR() {}
3573void GlobalAddressSDNode::ANCHOR() {}
3574void FrameIndexSDNode::ANCHOR() {}
3575void JumpTableSDNode::ANCHOR() {}
3576void ConstantPoolSDNode::ANCHOR() {}
3577void BasicBlockSDNode::ANCHOR() {}
3578void SrcValueSDNode::ANCHOR() {}
Dan Gohman12a9c082008-02-06 22:27:42 +00003579void MemOperandSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003580void RegisterSDNode::ANCHOR() {}
3581void ExternalSymbolSDNode::ANCHOR() {}
3582void CondCodeSDNode::ANCHOR() {}
3583void VTSDNode::ANCHOR() {}
3584void LoadSDNode::ANCHOR() {}
3585void StoreSDNode::ANCHOR() {}
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003586void AtomicSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003587
3588HandleSDNode::~HandleSDNode() {
3589 SDVTList VTs = { 0, 0 };
3590 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3591}
3592
3593GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3594 MVT::ValueType VT, int o)
3595 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman53491e92007-07-23 20:24:29 +00003596 cast<GlobalVariable>(GA)->isThreadLocal() ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003597 // Thread Local
3598 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3599 // Non Thread Local
3600 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3601 getSDVTList(VT)), Offset(o) {
3602 TheGlobal = const_cast<GlobalValue*>(GA);
3603}
3604
Dan Gohman12a9c082008-02-06 22:27:42 +00003605/// getMemOperand - Return a MemOperand object describing the memory
3606/// reference performed by this load or store.
3607MemOperand LSBaseSDNode::getMemOperand() const {
3608 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3609 int Flags =
3610 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3611 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3612
3613 // Check if the load references a frame index, and does not have
3614 // an SV attached.
3615 const FrameIndexSDNode *FI =
3616 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3617 if (!getSrcValue() && FI)
Dan Gohmanfb020b62008-02-07 18:41:25 +00003618 return MemOperand(PseudoSourceValue::getFixedStack(), Flags,
Dan Gohman12a9c082008-02-06 22:27:42 +00003619 FI->getIndex(), Size, Alignment);
3620 else
3621 return MemOperand(getSrcValue(), Flags,
3622 getSrcValueOffset(), Size, Alignment);
3623}
3624
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003625/// Profile - Gather unique data for the node.
3626///
3627void SDNode::Profile(FoldingSetNodeID &ID) {
3628 AddNodeIDNode(ID, this);
3629}
3630
3631/// getValueTypeList - Return a pointer to the specified value type.
3632///
Dan Gohman8cdf7892008-02-08 03:26:46 +00003633const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00003634 if (MVT::isExtendedVT(VT)) {
3635 static std::set<MVT::ValueType> EVTs;
Dan Gohman8cdf7892008-02-08 03:26:46 +00003636 return &(*EVTs.insert(VT).first);
Duncan Sandsa9810f32007-10-16 09:56:48 +00003637 } else {
3638 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3639 VTs[VT] = VT;
3640 return &VTs[VT];
3641 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003642}
Duncan Sandsa9810f32007-10-16 09:56:48 +00003643
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3645/// indicated value. This method ignores uses of other values defined by this
3646/// operation.
3647bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3648 assert(Value < getNumValues() && "Bad value!");
3649
3650 // If there is only one value, this is easy.
3651 if (getNumValues() == 1)
3652 return use_size() == NUses;
Evan Cheng0af04f72007-08-02 05:29:38 +00003653 if (use_size() < NUses) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003654
3655 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3656
3657 SmallPtrSet<SDNode*, 32> UsersHandled;
3658
3659 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3660 SDNode *User = *UI;
3661 if (User->getNumOperands() == 1 ||
3662 UsersHandled.insert(User)) // First time we've seen this?
3663 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3664 if (User->getOperand(i) == TheValue) {
3665 if (NUses == 0)
3666 return false; // too many uses
3667 --NUses;
3668 }
3669 }
3670
3671 // Found exactly the right number of uses?
3672 return NUses == 0;
3673}
3674
3675
Evan Cheng0af04f72007-08-02 05:29:38 +00003676/// hasAnyUseOfValue - Return true if there are any use of the indicated
3677/// value. This method ignores uses of other values defined by this operation.
3678bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3679 assert(Value < getNumValues() && "Bad value!");
3680
Dan Gohman301f4052008-01-29 13:02:09 +00003681 if (use_empty()) return false;
Evan Cheng0af04f72007-08-02 05:29:38 +00003682
3683 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3684
3685 SmallPtrSet<SDNode*, 32> UsersHandled;
3686
3687 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3688 SDNode *User = *UI;
3689 if (User->getNumOperands() == 1 ||
3690 UsersHandled.insert(User)) // First time we've seen this?
3691 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3692 if (User->getOperand(i) == TheValue) {
3693 return true;
3694 }
3695 }
3696
3697 return false;
3698}
3699
3700
Evan Chengd9387682008-03-04 00:41:45 +00003701/// isOnlyUseOf - Return true if this node is the only use of N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003702///
Evan Chengd9387682008-03-04 00:41:45 +00003703bool SDNode::isOnlyUseOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003704 bool Seen = false;
3705 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3706 SDNode *User = *I;
3707 if (User == this)
3708 Seen = true;
3709 else
3710 return false;
3711 }
3712
3713 return Seen;
3714}
3715
3716/// isOperand - Return true if this node is an operand of N.
3717///
Evan Chengd9387682008-03-04 00:41:45 +00003718bool SDOperand::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003719 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3720 if (*this == N->getOperand(i))
3721 return true;
3722 return false;
3723}
3724
Evan Chengd9387682008-03-04 00:41:45 +00003725bool SDNode::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003726 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3727 if (this == N->OperandList[i].Val)
3728 return true;
3729 return false;
3730}
3731
Chris Lattner10d94f92008-01-16 05:49:24 +00003732/// reachesChainWithoutSideEffects - Return true if this operand (which must
3733/// be a chain) reaches the specified operand without crossing any
3734/// side-effecting instructions. In practice, this looks through token
3735/// factors and non-volatile loads. In order to remain efficient, this only
3736/// looks a couple of nodes in, it does not do an exhaustive search.
3737bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3738 unsigned Depth) const {
3739 if (*this == Dest) return true;
3740
3741 // Don't search too deeply, we just want to be able to see through
3742 // TokenFactor's etc.
3743 if (Depth == 0) return false;
3744
3745 // If this is a token factor, all inputs to the TF happen in parallel. If any
3746 // of the operands of the TF reach dest, then we can do the xform.
3747 if (getOpcode() == ISD::TokenFactor) {
3748 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3749 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3750 return true;
3751 return false;
3752 }
3753
3754 // Loads don't have side effects, look through them.
3755 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3756 if (!Ld->isVolatile())
3757 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3758 }
3759 return false;
3760}
3761
3762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003763static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3764 SmallPtrSet<SDNode *, 32> &Visited) {
3765 if (found || !Visited.insert(N))
3766 return;
3767
3768 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3769 SDNode *Op = N->getOperand(i).Val;
3770 if (Op == P) {
3771 found = true;
3772 return;
3773 }
3774 findPredecessor(Op, P, found, Visited);
3775 }
3776}
3777
Evan Chengd9387682008-03-04 00:41:45 +00003778/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003779/// is either an operand of N or it can be reached by recursively traversing
3780/// up the operands.
3781/// NOTE: this is an expensive method. Use it carefully.
Evan Chengd9387682008-03-04 00:41:45 +00003782bool SDNode::isPredecessorOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003783 SmallPtrSet<SDNode *, 32> Visited;
3784 bool found = false;
3785 findPredecessor(N, this, found, Visited);
3786 return found;
3787}
3788
3789uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3790 assert(Num < NumOperands && "Invalid child # of SDNode!");
3791 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3792}
3793
3794std::string SDNode::getOperationName(const SelectionDAG *G) const {
3795 switch (getOpcode()) {
3796 default:
3797 if (getOpcode() < ISD::BUILTIN_OP_END)
3798 return "<<Unknown DAG Node>>";
3799 else {
3800 if (G) {
3801 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3802 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner0c2a4f32008-01-07 03:13:06 +00003803 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003804
3805 TargetLowering &TLI = G->getTargetLoweringInfo();
3806 const char *Name =
3807 TLI.getTargetNodeName(getOpcode());
3808 if (Name) return Name;
3809 }
3810
3811 return "<<Unknown Target Node>>";
3812 }
3813
Evan Chengd1d68072008-03-08 00:58:38 +00003814 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth785610d2008-02-16 01:24:58 +00003815 case ISD::MEMBARRIER: return "MemBarrier";
Andrew Lenharthe44f3902008-02-21 06:45:13 +00003816 case ISD::ATOMIC_LCS: return "AtomicLCS";
3817 case ISD::ATOMIC_LAS: return "AtomicLAS";
3818 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003819 case ISD::PCMARKER: return "PCMarker";
3820 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3821 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman12a9c082008-02-06 22:27:42 +00003822 case ISD::MEMOPERAND: return "MemOperand";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003823 case ISD::EntryToken: return "EntryToken";
3824 case ISD::TokenFactor: return "TokenFactor";
3825 case ISD::AssertSext: return "AssertSext";
3826 case ISD::AssertZext: return "AssertZext";
3827
3828 case ISD::STRING: return "String";
3829 case ISD::BasicBlock: return "BasicBlock";
3830 case ISD::VALUETYPE: return "ValueType";
3831 case ISD::Register: return "Register";
3832
3833 case ISD::Constant: return "Constant";
3834 case ISD::ConstantFP: return "ConstantFP";
3835 case ISD::GlobalAddress: return "GlobalAddress";
3836 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3837 case ISD::FrameIndex: return "FrameIndex";
3838 case ISD::JumpTable: return "JumpTable";
3839 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3840 case ISD::RETURNADDR: return "RETURNADDR";
3841 case ISD::FRAMEADDR: return "FRAMEADDR";
3842 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3843 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3844 case ISD::EHSELECTION: return "EHSELECTION";
3845 case ISD::EH_RETURN: return "EH_RETURN";
3846 case ISD::ConstantPool: return "ConstantPool";
3847 case ISD::ExternalSymbol: return "ExternalSymbol";
3848 case ISD::INTRINSIC_WO_CHAIN: {
3849 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3850 return Intrinsic::getName((Intrinsic::ID)IID);
3851 }
3852 case ISD::INTRINSIC_VOID:
3853 case ISD::INTRINSIC_W_CHAIN: {
3854 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3855 return Intrinsic::getName((Intrinsic::ID)IID);
3856 }
3857
3858 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3859 case ISD::TargetConstant: return "TargetConstant";
3860 case ISD::TargetConstantFP:return "TargetConstantFP";
3861 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3862 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3863 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3864 case ISD::TargetJumpTable: return "TargetJumpTable";
3865 case ISD::TargetConstantPool: return "TargetConstantPool";
3866 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3867
3868 case ISD::CopyToReg: return "CopyToReg";
3869 case ISD::CopyFromReg: return "CopyFromReg";
3870 case ISD::UNDEF: return "undef";
3871 case ISD::MERGE_VALUES: return "merge_values";
3872 case ISD::INLINEASM: return "inlineasm";
3873 case ISD::LABEL: return "label";
Evan Cheng2e28d622008-02-02 04:07:54 +00003874 case ISD::DECLARE: return "declare";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 case ISD::HANDLENODE: return "handlenode";
3876 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3877 case ISD::CALL: return "call";
3878
3879 // Unary operators
3880 case ISD::FABS: return "fabs";
3881 case ISD::FNEG: return "fneg";
3882 case ISD::FSQRT: return "fsqrt";
3883 case ISD::FSIN: return "fsin";
3884 case ISD::FCOS: return "fcos";
3885 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00003886 case ISD::FPOW: return "fpow";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887
3888 // Binary operators
3889 case ISD::ADD: return "add";
3890 case ISD::SUB: return "sub";
3891 case ISD::MUL: return "mul";
3892 case ISD::MULHU: return "mulhu";
3893 case ISD::MULHS: return "mulhs";
3894 case ISD::SDIV: return "sdiv";
3895 case ISD::UDIV: return "udiv";
3896 case ISD::SREM: return "srem";
3897 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00003898 case ISD::SMUL_LOHI: return "smul_lohi";
3899 case ISD::UMUL_LOHI: return "umul_lohi";
3900 case ISD::SDIVREM: return "sdivrem";
3901 case ISD::UDIVREM: return "divrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003902 case ISD::AND: return "and";
3903 case ISD::OR: return "or";
3904 case ISD::XOR: return "xor";
3905 case ISD::SHL: return "shl";
3906 case ISD::SRA: return "sra";
3907 case ISD::SRL: return "srl";
3908 case ISD::ROTL: return "rotl";
3909 case ISD::ROTR: return "rotr";
3910 case ISD::FADD: return "fadd";
3911 case ISD::FSUB: return "fsub";
3912 case ISD::FMUL: return "fmul";
3913 case ISD::FDIV: return "fdiv";
3914 case ISD::FREM: return "frem";
3915 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00003916 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917
3918 case ISD::SETCC: return "setcc";
3919 case ISD::SELECT: return "select";
3920 case ISD::SELECT_CC: return "select_cc";
3921 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3922 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3923 case ISD::CONCAT_VECTORS: return "concat_vectors";
3924 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3925 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3926 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3927 case ISD::CARRY_FALSE: return "carry_false";
3928 case ISD::ADDC: return "addc";
3929 case ISD::ADDE: return "adde";
3930 case ISD::SUBC: return "subc";
3931 case ISD::SUBE: return "sube";
3932 case ISD::SHL_PARTS: return "shl_parts";
3933 case ISD::SRA_PARTS: return "sra_parts";
3934 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lambb768c2e2007-07-26 07:34:40 +00003935
3936 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3937 case ISD::INSERT_SUBREG: return "insert_subreg";
3938
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939 // Conversion operators.
3940 case ISD::SIGN_EXTEND: return "sign_extend";
3941 case ISD::ZERO_EXTEND: return "zero_extend";
3942 case ISD::ANY_EXTEND: return "any_extend";
3943 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3944 case ISD::TRUNCATE: return "truncate";
3945 case ISD::FP_ROUND: return "fp_round";
Dan Gohman819574c2008-01-31 00:41:03 +00003946 case ISD::FLT_ROUNDS_: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003947 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3948 case ISD::FP_EXTEND: return "fp_extend";
3949
3950 case ISD::SINT_TO_FP: return "sint_to_fp";
3951 case ISD::UINT_TO_FP: return "uint_to_fp";
3952 case ISD::FP_TO_SINT: return "fp_to_sint";
3953 case ISD::FP_TO_UINT: return "fp_to_uint";
3954 case ISD::BIT_CONVERT: return "bit_convert";
3955
3956 // Control flow instructions
3957 case ISD::BR: return "br";
3958 case ISD::BRIND: return "brind";
3959 case ISD::BR_JT: return "br_jt";
3960 case ISD::BRCOND: return "brcond";
3961 case ISD::BR_CC: return "br_cc";
3962 case ISD::RET: return "ret";
3963 case ISD::CALLSEQ_START: return "callseq_start";
3964 case ISD::CALLSEQ_END: return "callseq_end";
3965
3966 // Other operators
3967 case ISD::LOAD: return "load";
3968 case ISD::STORE: return "store";
3969 case ISD::VAARG: return "vaarg";
3970 case ISD::VACOPY: return "vacopy";
3971 case ISD::VAEND: return "vaend";
3972 case ISD::VASTART: return "vastart";
3973 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3974 case ISD::EXTRACT_ELEMENT: return "extract_element";
3975 case ISD::BUILD_PAIR: return "build_pair";
3976 case ISD::STACKSAVE: return "stacksave";
3977 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003978 case ISD::TRAP: return "trap";
3979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 // Block memory operations.
3981 case ISD::MEMSET: return "memset";
3982 case ISD::MEMCPY: return "memcpy";
3983 case ISD::MEMMOVE: return "memmove";
3984
3985 // Bit manipulation
3986 case ISD::BSWAP: return "bswap";
3987 case ISD::CTPOP: return "ctpop";
3988 case ISD::CTTZ: return "cttz";
3989 case ISD::CTLZ: return "ctlz";
3990
3991 // Debug info
3992 case ISD::LOCATION: return "location";
3993 case ISD::DEBUG_LOC: return "debug_loc";
3994
Duncan Sands38947cd2007-07-27 12:58:54 +00003995 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00003996 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00003997
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003998 case ISD::CONDCODE:
3999 switch (cast<CondCodeSDNode>(this)->get()) {
4000 default: assert(0 && "Unknown setcc condition!");
4001 case ISD::SETOEQ: return "setoeq";
4002 case ISD::SETOGT: return "setogt";
4003 case ISD::SETOGE: return "setoge";
4004 case ISD::SETOLT: return "setolt";
4005 case ISD::SETOLE: return "setole";
4006 case ISD::SETONE: return "setone";
4007
4008 case ISD::SETO: return "seto";
4009 case ISD::SETUO: return "setuo";
4010 case ISD::SETUEQ: return "setue";
4011 case ISD::SETUGT: return "setugt";
4012 case ISD::SETUGE: return "setuge";
4013 case ISD::SETULT: return "setult";
4014 case ISD::SETULE: return "setule";
4015 case ISD::SETUNE: return "setune";
4016
4017 case ISD::SETEQ: return "seteq";
4018 case ISD::SETGT: return "setgt";
4019 case ISD::SETGE: return "setge";
4020 case ISD::SETLT: return "setlt";
4021 case ISD::SETLE: return "setle";
4022 case ISD::SETNE: return "setne";
4023 }
4024 }
4025}
4026
4027const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
4028 switch (AM) {
4029 default:
4030 return "";
4031 case ISD::PRE_INC:
4032 return "<pre-inc>";
4033 case ISD::PRE_DEC:
4034 return "<pre-dec>";
4035 case ISD::POST_INC:
4036 return "<post-inc>";
4037 case ISD::POST_DEC:
4038 return "<post-dec>";
4039 }
4040}
4041
4042void SDNode::dump() const { dump(0); }
4043void SDNode::dump(const SelectionDAG *G) const {
4044 cerr << (void*)this << ": ";
4045
4046 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
4047 if (i) cerr << ",";
4048 if (getValueType(i) == MVT::Other)
4049 cerr << "ch";
4050 else
4051 cerr << MVT::getValueTypeString(getValueType(i));
4052 }
4053 cerr << " = " << getOperationName(G);
4054
4055 cerr << " ";
4056 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
4057 if (i) cerr << ", ";
4058 cerr << (void*)getOperand(i).Val;
4059 if (unsigned RN = getOperand(i).ResNo)
4060 cerr << ":" << RN;
4061 }
4062
Evan Chengaad43a02007-12-11 02:08:35 +00004063 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4064 SDNode *Mask = getOperand(2).Val;
4065 cerr << "<";
4066 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4067 if (i) cerr << ",";
4068 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4069 cerr << "u";
4070 else
4071 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4072 }
4073 cerr << ">";
4074 }
4075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004076 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
4077 cerr << "<" << CSDN->getValue() << ">";
4078 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00004079 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4080 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4081 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4082 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4083 else {
4084 cerr << "<APFloat(";
4085 CSDN->getValueAPF().convertToAPInt().dump();
4086 cerr << ")>";
4087 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004088 } else if (const GlobalAddressSDNode *GADN =
4089 dyn_cast<GlobalAddressSDNode>(this)) {
4090 int offset = GADN->getOffset();
4091 cerr << "<";
4092 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
4093 if (offset > 0)
4094 cerr << " + " << offset;
4095 else
4096 cerr << " " << offset;
4097 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
4098 cerr << "<" << FIDN->getIndex() << ">";
4099 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
4100 cerr << "<" << JTDN->getIndex() << ">";
4101 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
4102 int offset = CP->getOffset();
4103 if (CP->isMachineConstantPoolEntry())
4104 cerr << "<" << *CP->getMachineCPVal() << ">";
4105 else
4106 cerr << "<" << *CP->getConstVal() << ">";
4107 if (offset > 0)
4108 cerr << " + " << offset;
4109 else
4110 cerr << " " << offset;
4111 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
4112 cerr << "<";
4113 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4114 if (LBB)
4115 cerr << LBB->getName() << " ";
4116 cerr << (const void*)BBDN->getBasicBlock() << ">";
4117 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman1e57df32008-02-10 18:45:23 +00004118 if (G && R->getReg() &&
4119 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling9b0baeb2008-02-26 21:47:57 +00004120 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004121 } else {
4122 cerr << " #" << R->getReg();
4123 }
4124 } else if (const ExternalSymbolSDNode *ES =
4125 dyn_cast<ExternalSymbolSDNode>(this)) {
4126 cerr << "'" << ES->getSymbol() << "'";
4127 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4128 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +00004129 cerr << "<" << M->getValue() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004130 else
Dan Gohman12a9c082008-02-06 22:27:42 +00004131 cerr << "<null>";
4132 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4133 if (M->MO.getValue())
4134 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4135 else
4136 cerr << "<null:" << M->MO.getOffset() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004137 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
4138 cerr << ":" << MVT::getValueTypeString(N->getVT());
4139 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng034c4f82007-12-18 19:06:30 +00004140 const Value *SrcValue = LD->getSrcValue();
4141 int SrcOffset = LD->getSrcValueOffset();
4142 cerr << " <";
4143 if (SrcValue)
4144 cerr << SrcValue;
4145 else
4146 cerr << "null";
4147 cerr << ":" << SrcOffset << ">";
4148
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149 bool doExt = true;
4150 switch (LD->getExtensionType()) {
4151 default: doExt = false; break;
4152 case ISD::EXTLOAD:
4153 cerr << " <anyext ";
4154 break;
4155 case ISD::SEXTLOAD:
4156 cerr << " <sext ";
4157 break;
4158 case ISD::ZEXTLOAD:
4159 cerr << " <zext ";
4160 break;
4161 }
4162 if (doExt)
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004163 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164
4165 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00004166 if (*AM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 cerr << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00004168 if (LD->isVolatile())
4169 cerr << " <volatile>";
4170 cerr << " alignment=" << LD->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng7196a7b2007-12-18 07:02:08 +00004172 const Value *SrcValue = ST->getSrcValue();
4173 int SrcOffset = ST->getSrcValueOffset();
4174 cerr << " <";
4175 if (SrcValue)
4176 cerr << SrcValue;
4177 else
4178 cerr << "null";
4179 cerr << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00004180
4181 if (ST->isTruncatingStore())
4182 cerr << " <trunc "
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004183 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00004184
4185 const char *AM = getIndexedModeName(ST->getAddressingMode());
4186 if (*AM)
4187 cerr << " " << AM;
4188 if (ST->isVolatile())
4189 cerr << " <volatile>";
4190 cerr << " alignment=" << ST->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004191 }
4192}
4193
4194static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
4195 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4196 if (N->getOperand(i).Val->hasOneUse())
4197 DumpNodes(N->getOperand(i).Val, indent+2, G);
4198 else
4199 cerr << "\n" << std::string(indent+2, ' ')
4200 << (void*)N->getOperand(i).Val << ": <multiple use>";
4201
4202
4203 cerr << "\n" << std::string(indent, ' ');
4204 N->dump(G);
4205}
4206
4207void SelectionDAG::dump() const {
4208 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
4209 std::vector<const SDNode*> Nodes;
4210 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4211 I != E; ++I)
4212 Nodes.push_back(I);
4213
4214 std::sort(Nodes.begin(), Nodes.end());
4215
4216 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4217 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
4218 DumpNodes(Nodes[i], 2, this);
4219 }
4220
4221 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4222
4223 cerr << "\n\n";
4224}
4225
4226const Type *ConstantPoolSDNode::getType() const {
4227 if (isMachineConstantPoolEntry())
4228 return Val.MachineCPVal->getType();
4229 return Val.ConstVal->getType();
4230}