blob: efbaa5a7f6dca6a95f6cbb72463d90192e6444e9 [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"
16#include "llvm/GlobalVariable.h"
17#include "llvm/Intrinsics.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Assembly/Writer.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner53f5aee2007-10-15 17:47:20 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Support/MathExtras.h"
24#include "llvm/Target/MRegisterInfo.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/ADT/SetVector.h"
30#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsa9810f32007-10-16 09:56:48 +000031#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/SmallVector.h"
33#include "llvm/ADT/StringExtras.h"
34#include <algorithm>
35#include <cmath>
36using namespace llvm;
37
38/// makeVTList - Return an instance of the SDVTList struct initialized with the
39/// specified members.
40static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
41 SDVTList Res = {VTs, NumVTs};
42 return Res;
43}
44
45//===----------------------------------------------------------------------===//
46// ConstantFPSDNode Class
47//===----------------------------------------------------------------------===//
48
49/// isExactlyValue - We don't rely on operator== working on double values, as
50/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
51/// As such, this method can be used to do an exact bit-for-bit comparison of
52/// two floating point values.
Dale Johannesenc53301c2007-08-26 01:18:27 +000053bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen7f2c1d12007-08-25 22:10:57 +000054 return Value.bitwiseIsEqual(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055}
56
Dale Johannesenbbe2b702007-08-30 00:23:21 +000057bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
58 const APFloat& Val) {
59 // convert modifies in place, so make a copy.
60 APFloat Val2 = APFloat(Val);
61 switch (VT) {
62 default:
63 return false; // These can't be represented as floating point!
64
65 // FIXME rounding mode needs to be more flexible
66 case MVT::f32:
67 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
68 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
69 APFloat::opOK;
70 case MVT::f64:
71 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
72 &Val2.getSemantics() == &APFloat::IEEEdouble ||
73 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
74 APFloat::opOK;
75 // TODO: Figure out how to test if we can use a shorter type instead!
76 case MVT::f80:
77 case MVT::f128:
78 case MVT::ppcf128:
79 return true;
80 }
81}
82
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083//===----------------------------------------------------------------------===//
84// ISD Namespace
85//===----------------------------------------------------------------------===//
86
87/// isBuildVectorAllOnes - Return true if the specified node is a
88/// BUILD_VECTOR where all of the elements are ~0 or undef.
89bool ISD::isBuildVectorAllOnes(const SDNode *N) {
90 // Look through a bit convert.
91 if (N->getOpcode() == ISD::BIT_CONVERT)
92 N = N->getOperand(0).Val;
93
94 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
95
96 unsigned i = 0, e = N->getNumOperands();
97
98 // Skip over all of the undef values.
99 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
100 ++i;
101
102 // Do not accept an all-undef vector.
103 if (i == e) return false;
104
105 // Do not accept build_vectors that aren't all constants or which have non-~0
106 // elements.
107 SDOperand NotZero = N->getOperand(i);
108 if (isa<ConstantSDNode>(NotZero)) {
109 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
110 return false;
111 } else if (isa<ConstantFPSDNode>(NotZero)) {
112 MVT::ValueType VT = NotZero.getValueType();
113 if (VT== MVT::f64) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000114 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
115 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 return false;
117 } else {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000118 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
119 getValueAPF().convertToAPInt().getZExtValue() !=
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 (uint32_t)-1)
121 return false;
122 }
123 } 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
175/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
176/// when given the operation for (X op Y).
177ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
178 // To perform this operation, we just need to swap the L and G bits of the
179 // operation.
180 unsigned OldL = (Operation >> 2) & 1;
181 unsigned OldG = (Operation >> 1) & 1;
182 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
183 (OldL << 1) | // New G bit
184 (OldG << 2)); // New L bit.
185}
186
187/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
188/// 'op' is a valid SetCC operation.
189ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
190 unsigned Operation = Op;
191 if (isInteger)
192 Operation ^= 7; // Flip L, G, E bits, but not U.
193 else
194 Operation ^= 15; // Flip all of the condition bits.
195 if (Operation > ISD::SETTRUE2)
196 Operation &= ~8; // Don't let N and U bits get set.
197 return ISD::CondCode(Operation);
198}
199
200
201/// isSignedOp - For an integer comparison, return 1 if the comparison is a
202/// signed operation and 2 if the result is an unsigned comparison. Return zero
203/// if the operation does not depend on the sign of the input (setne and seteq).
204static int isSignedOp(ISD::CondCode Opcode) {
205 switch (Opcode) {
206 default: assert(0 && "Illegal integer setcc operation!");
207 case ISD::SETEQ:
208 case ISD::SETNE: return 0;
209 case ISD::SETLT:
210 case ISD::SETLE:
211 case ISD::SETGT:
212 case ISD::SETGE: return 1;
213 case ISD::SETULT:
214 case ISD::SETULE:
215 case ISD::SETUGT:
216 case ISD::SETUGE: return 2;
217 }
218}
219
220/// getSetCCOrOperation - Return the result of a logical OR between different
221/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
222/// returns SETCC_INVALID if it is not possible to represent the resultant
223/// comparison.
224ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
225 bool isInteger) {
226 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
227 // Cannot fold a signed integer setcc with an unsigned integer setcc.
228 return ISD::SETCC_INVALID;
229
230 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
231
232 // If the N and U bits get set then the resultant comparison DOES suddenly
233 // care about orderedness, and is true when ordered.
234 if (Op > ISD::SETTRUE2)
235 Op &= ~16; // Clear the U bit if the N bit is set.
236
237 // Canonicalize illegal integer setcc's.
238 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
239 Op = ISD::SETNE;
240
241 return ISD::CondCode(Op);
242}
243
244/// getSetCCAndOperation - Return the result of a logical AND between different
245/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
246/// function returns zero if it is not possible to represent the resultant
247/// comparison.
248ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
249 bool isInteger) {
250 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
251 // Cannot fold a signed setcc with an unsigned setcc.
252 return ISD::SETCC_INVALID;
253
254 // Combine all of the condition bits.
255 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
256
257 // Canonicalize illegal integer setcc's.
258 if (isInteger) {
259 switch (Result) {
260 default: break;
261 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
262 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
263 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
264 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
265 }
266 }
267
268 return Result;
269}
270
271const TargetMachine &SelectionDAG::getTarget() const {
272 return TLI.getTargetMachine();
273}
274
275//===----------------------------------------------------------------------===//
276// SDNode Profile Support
277//===----------------------------------------------------------------------===//
278
279/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
280///
281static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
282 ID.AddInteger(OpC);
283}
284
285/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
286/// solely with their pointer.
287void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
288 ID.AddPointer(VTList.VTs);
289}
290
291/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
292///
293static void AddNodeIDOperands(FoldingSetNodeID &ID,
294 const SDOperand *Ops, unsigned NumOps) {
295 for (; NumOps; --NumOps, ++Ops) {
296 ID.AddPointer(Ops->Val);
297 ID.AddInteger(Ops->ResNo);
298 }
299}
300
301static void AddNodeIDNode(FoldingSetNodeID &ID,
302 unsigned short OpC, SDVTList VTList,
303 const SDOperand *OpList, unsigned N) {
304 AddNodeIDOpcode(ID, OpC);
305 AddNodeIDValueTypes(ID, VTList);
306 AddNodeIDOperands(ID, OpList, N);
307}
308
309/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
310/// data.
311static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
312 AddNodeIDOpcode(ID, N->getOpcode());
313 // Add the return value info.
314 AddNodeIDValueTypes(ID, N->getVTList());
315 // Add the operand info.
316 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
317
318 // Handle SDNode leafs with special info.
319 switch (N->getOpcode()) {
320 default: break; // Normal nodes don't need extra info.
321 case ISD::TargetConstant:
322 case ISD::Constant:
323 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
324 break;
325 case ISD::TargetConstantFP:
Dale Johannesendf8a8312007-08-31 04:03:46 +0000326 case ISD::ConstantFP: {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000327 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 break;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000329 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 case ISD::TargetGlobalAddress:
331 case ISD::GlobalAddress:
332 case ISD::TargetGlobalTLSAddress:
333 case ISD::GlobalTLSAddress: {
334 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
335 ID.AddPointer(GA->getGlobal());
336 ID.AddInteger(GA->getOffset());
337 break;
338 }
339 case ISD::BasicBlock:
340 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
341 break;
342 case ISD::Register:
343 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
344 break;
345 case ISD::SRCVALUE: {
346 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
347 ID.AddPointer(SV->getValue());
348 ID.AddInteger(SV->getOffset());
349 break;
350 }
351 case ISD::FrameIndex:
352 case ISD::TargetFrameIndex:
353 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
354 break;
355 case ISD::JumpTable:
356 case ISD::TargetJumpTable:
357 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
358 break;
359 case ISD::ConstantPool:
360 case ISD::TargetConstantPool: {
361 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
362 ID.AddInteger(CP->getAlignment());
363 ID.AddInteger(CP->getOffset());
364 if (CP->isMachineConstantPoolEntry())
365 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
366 else
367 ID.AddPointer(CP->getConstVal());
368 break;
369 }
370 case ISD::LOAD: {
371 LoadSDNode *LD = cast<LoadSDNode>(N);
372 ID.AddInteger(LD->getAddressingMode());
373 ID.AddInteger(LD->getExtensionType());
Chris Lattner4a22a672007-09-13 06:09:48 +0000374 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 ID.AddInteger(LD->getAlignment());
376 ID.AddInteger(LD->isVolatile());
377 break;
378 }
379 case ISD::STORE: {
380 StoreSDNode *ST = cast<StoreSDNode>(N);
381 ID.AddInteger(ST->getAddressingMode());
382 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner4a22a672007-09-13 06:09:48 +0000383 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 ID.AddInteger(ST->getAlignment());
385 ID.AddInteger(ST->isVolatile());
386 break;
387 }
388 }
389}
390
391//===----------------------------------------------------------------------===//
392// SelectionDAG Class
393//===----------------------------------------------------------------------===//
394
395/// RemoveDeadNodes - This method deletes all unreachable nodes in the
396/// SelectionDAG.
397void SelectionDAG::RemoveDeadNodes() {
398 // Create a dummy node (which is not added to allnodes), that adds a reference
399 // to the root node, preventing it from being deleted.
400 HandleSDNode Dummy(getRoot());
401
402 SmallVector<SDNode*, 128> DeadNodes;
403
404 // Add all obviously-dead nodes to the DeadNodes worklist.
405 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
406 if (I->use_empty())
407 DeadNodes.push_back(I);
408
409 // Process the worklist, deleting the nodes and adding their uses to the
410 // worklist.
411 while (!DeadNodes.empty()) {
412 SDNode *N = DeadNodes.back();
413 DeadNodes.pop_back();
414
415 // Take the node out of the appropriate CSE map.
416 RemoveNodeFromCSEMaps(N);
417
418 // Next, brutally remove the operand list. This is safe to do, as there are
419 // no cycles in the graph.
420 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
421 SDNode *Operand = I->Val;
422 Operand->removeUser(N);
423
424 // Now that we removed this operand, see if there are no uses of it left.
425 if (Operand->use_empty())
426 DeadNodes.push_back(Operand);
427 }
428 if (N->OperandsNeedDelete)
429 delete[] N->OperandList;
430 N->OperandList = 0;
431 N->NumOperands = 0;
432
433 // Finally, remove N itself.
434 AllNodes.erase(N);
435 }
436
437 // If the root changed (e.g. it was a dead load, update the root).
438 setRoot(Dummy.getValue());
439}
440
441void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
442 SmallVector<SDNode*, 16> DeadNodes;
443 DeadNodes.push_back(N);
444
445 // Process the worklist, deleting the nodes and adding their uses to the
446 // worklist.
447 while (!DeadNodes.empty()) {
448 SDNode *N = DeadNodes.back();
449 DeadNodes.pop_back();
450
451 // Take the node out of the appropriate CSE map.
452 RemoveNodeFromCSEMaps(N);
453
454 // Next, brutally remove the operand list. This is safe to do, as there are
455 // no cycles in the graph.
456 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
457 SDNode *Operand = I->Val;
458 Operand->removeUser(N);
459
460 // Now that we removed this operand, see if there are no uses of it left.
461 if (Operand->use_empty())
462 DeadNodes.push_back(Operand);
463 }
464 if (N->OperandsNeedDelete)
465 delete[] N->OperandList;
466 N->OperandList = 0;
467 N->NumOperands = 0;
468
469 // Finally, remove N itself.
470 Deleted.push_back(N);
471 AllNodes.erase(N);
472 }
473}
474
475void SelectionDAG::DeleteNode(SDNode *N) {
476 assert(N->use_empty() && "Cannot delete a node that is not dead!");
477
478 // First take this out of the appropriate CSE map.
479 RemoveNodeFromCSEMaps(N);
480
481 // Finally, remove uses due to operands of this node, remove from the
482 // AllNodes list, and delete the node.
483 DeleteNodeNotInCSEMaps(N);
484}
485
486void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
487
488 // Remove it from the AllNodes list.
489 AllNodes.remove(N);
490
491 // Drop all of the operands and decrement used nodes use counts.
492 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
493 I->Val->removeUser(N);
494 if (N->OperandsNeedDelete)
495 delete[] N->OperandList;
496 N->OperandList = 0;
497 N->NumOperands = 0;
498
499 delete N;
500}
501
502/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
503/// correspond to it. This is useful when we're about to delete or repurpose
504/// the node. We don't want future request for structurally identical nodes
505/// to return N anymore.
506void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
507 bool Erased = false;
508 switch (N->getOpcode()) {
509 case ISD::HANDLENODE: return; // noop.
510 case ISD::STRING:
511 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
512 break;
513 case ISD::CONDCODE:
514 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
515 "Cond code doesn't exist!");
516 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
517 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
518 break;
519 case ISD::ExternalSymbol:
520 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
521 break;
522 case ISD::TargetExternalSymbol:
523 Erased =
524 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
525 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000526 case ISD::VALUETYPE: {
527 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
528 if (MVT::isExtendedVT(VT)) {
529 Erased = ExtendedValueTypeNodes.erase(VT);
530 } else {
531 Erased = ValueTypeNodes[VT] != 0;
532 ValueTypeNodes[VT] = 0;
533 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000535 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 default:
537 // Remove it from the CSE Map.
538 Erased = CSEMap.RemoveNode(N);
539 break;
540 }
541#ifndef NDEBUG
542 // Verify that the node was actually in one of the CSE maps, unless it has a
543 // flag result (which cannot be CSE'd) or is one of the special cases that are
544 // not subject to CSE.
545 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
546 !N->isTargetOpcode()) {
547 N->dump(this);
548 cerr << "\n";
549 assert(0 && "Node is not in map!");
550 }
551#endif
552}
553
554/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
555/// has been taken out and modified in some way. If the specified node already
556/// exists in the CSE maps, do not modify the maps, but return the existing node
557/// instead. If it doesn't exist, add it and return null.
558///
559SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
560 assert(N->getNumOperands() && "This is a leaf node!");
561 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
562 return 0; // Never add these nodes.
563
564 // Check that remaining values produced are not flags.
565 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
566 if (N->getValueType(i) == MVT::Flag)
567 return 0; // Never CSE anything that produces a flag.
568
569 SDNode *New = CSEMap.GetOrInsertNode(N);
570 if (New != N) return New; // Node already existed.
571 return 0;
572}
573
574/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
575/// were replaced with those specified. If this node is never memoized,
576/// return null, otherwise return a pointer to the slot it would take. If a
577/// node already exists with these operands, the slot will be non-null.
578SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
579 void *&InsertPos) {
580 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
581 return 0; // Never add these nodes.
582
583 // Check that remaining values produced are not flags.
584 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
585 if (N->getValueType(i) == MVT::Flag)
586 return 0; // Never CSE anything that produces a flag.
587
588 SDOperand Ops[] = { Op };
589 FoldingSetNodeID ID;
590 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
591 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
592}
593
594/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
595/// were replaced with those specified. If this node is never memoized,
596/// return null, otherwise return a pointer to the slot it would take. If a
597/// node already exists with these operands, the slot will be non-null.
598SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
599 SDOperand Op1, SDOperand Op2,
600 void *&InsertPos) {
601 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
602 return 0; // Never add these nodes.
603
604 // Check that remaining values produced are not flags.
605 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
606 if (N->getValueType(i) == MVT::Flag)
607 return 0; // Never CSE anything that produces a flag.
608
609 SDOperand Ops[] = { Op1, Op2 };
610 FoldingSetNodeID ID;
611 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
612 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
613}
614
615
616/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
617/// were replaced with those specified. If this node is never memoized,
618/// return null, otherwise return a pointer to the slot it would take. If a
619/// node already exists with these operands, the slot will be non-null.
620SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
621 const SDOperand *Ops,unsigned NumOps,
622 void *&InsertPos) {
623 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
624 return 0; // Never add these nodes.
625
626 // Check that remaining values produced are not flags.
627 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
628 if (N->getValueType(i) == MVT::Flag)
629 return 0; // Never CSE anything that produces a flag.
630
631 FoldingSetNodeID ID;
632 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
633
634 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
635 ID.AddInteger(LD->getAddressingMode());
636 ID.AddInteger(LD->getExtensionType());
Chris Lattner4a22a672007-09-13 06:09:48 +0000637 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 ID.AddInteger(LD->getAlignment());
639 ID.AddInteger(LD->isVolatile());
640 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
641 ID.AddInteger(ST->getAddressingMode());
642 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner4a22a672007-09-13 06:09:48 +0000643 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 ID.AddInteger(ST->getAlignment());
645 ID.AddInteger(ST->isVolatile());
646 }
647
648 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
649}
650
651
652SelectionDAG::~SelectionDAG() {
653 while (!AllNodes.empty()) {
654 SDNode *N = AllNodes.begin();
655 N->SetNextInBucket(0);
656 if (N->OperandsNeedDelete)
657 delete [] N->OperandList;
658 N->OperandList = 0;
659 N->NumOperands = 0;
660 AllNodes.pop_front();
661 }
662}
663
664SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
665 if (Op.getValueType() == VT) return Op;
666 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
667 return getNode(ISD::AND, Op.getValueType(), Op,
668 getConstant(Imm, Op.getValueType()));
669}
670
671SDOperand SelectionDAG::getString(const std::string &Val) {
672 StringSDNode *&N = StringNodes[Val];
673 if (!N) {
674 N = new StringSDNode(Val);
675 AllNodes.push_back(N);
676 }
677 return SDOperand(N, 0);
678}
679
680SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
681 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000682
683 MVT::ValueType EltVT =
684 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685
686 // Mask out any bits that are not valid for this constant.
Dan Gohman5b9d6412007-12-12 22:21:26 +0000687 Val &= MVT::getIntVTBitMask(EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688
689 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
690 FoldingSetNodeID ID;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000691 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 ID.AddInteger(Val);
693 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000694 SDNode *N = NULL;
695 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
696 if (!MVT::isVector(VT))
697 return SDOperand(N, 0);
698 if (!N) {
699 N = new ConstantSDNode(isT, Val, EltVT);
700 CSEMap.InsertNode(N, IP);
701 AllNodes.push_back(N);
702 }
703
704 SDOperand Result(N, 0);
705 if (MVT::isVector(VT)) {
706 SmallVector<SDOperand, 8> Ops;
707 Ops.assign(MVT::getVectorNumElements(VT), Result);
708 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
709 }
710 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711}
712
Chris Lattner5872a362008-01-17 07:00:52 +0000713SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
714 return getConstant(Val, TLI.getPointerTy(), isTarget);
715}
716
717
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000718SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 bool isTarget) {
720 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000721
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 MVT::ValueType EltVT =
723 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724
725 // Do the map lookup using the actual bit pattern for the floating point
726 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
727 // we don't have issues with SNANs.
728 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
729 FoldingSetNodeID ID;
730 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000731 ID.AddAPFloat(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732 void *IP = 0;
733 SDNode *N = NULL;
734 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
735 if (!MVT::isVector(VT))
736 return SDOperand(N, 0);
737 if (!N) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000738 N = new ConstantFPSDNode(isTarget, V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 CSEMap.InsertNode(N, IP);
740 AllNodes.push_back(N);
741 }
742
743 SDOperand Result(N, 0);
744 if (MVT::isVector(VT)) {
745 SmallVector<SDOperand, 8> Ops;
746 Ops.assign(MVT::getVectorNumElements(VT), Result);
747 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
748 }
749 return Result;
750}
751
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000752SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
753 bool isTarget) {
754 MVT::ValueType EltVT =
755 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
756 if (EltVT==MVT::f32)
757 return getConstantFP(APFloat((float)Val), VT, isTarget);
758 else
759 return getConstantFP(APFloat(Val), VT, isTarget);
760}
761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
763 MVT::ValueType VT, int Offset,
764 bool isTargetGA) {
765 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
766 unsigned Opc;
767 if (GVar && GVar->isThreadLocal())
768 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
769 else
770 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
771 FoldingSetNodeID ID;
772 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
773 ID.AddPointer(GV);
774 ID.AddInteger(Offset);
775 void *IP = 0;
776 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
777 return SDOperand(E, 0);
778 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
779 CSEMap.InsertNode(N, IP);
780 AllNodes.push_back(N);
781 return SDOperand(N, 0);
782}
783
784SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
785 bool isTarget) {
786 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
787 FoldingSetNodeID ID;
788 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
789 ID.AddInteger(FI);
790 void *IP = 0;
791 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
792 return SDOperand(E, 0);
793 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
794 CSEMap.InsertNode(N, IP);
795 AllNodes.push_back(N);
796 return SDOperand(N, 0);
797}
798
799SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
800 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
801 FoldingSetNodeID ID;
802 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
803 ID.AddInteger(JTI);
804 void *IP = 0;
805 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
806 return SDOperand(E, 0);
807 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
808 CSEMap.InsertNode(N, IP);
809 AllNodes.push_back(N);
810 return SDOperand(N, 0);
811}
812
813SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
814 unsigned Alignment, int Offset,
815 bool isTarget) {
816 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
817 FoldingSetNodeID ID;
818 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
819 ID.AddInteger(Alignment);
820 ID.AddInteger(Offset);
821 ID.AddPointer(C);
822 void *IP = 0;
823 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
824 return SDOperand(E, 0);
825 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
826 CSEMap.InsertNode(N, IP);
827 AllNodes.push_back(N);
828 return SDOperand(N, 0);
829}
830
831
832SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
833 MVT::ValueType VT,
834 unsigned Alignment, int Offset,
835 bool isTarget) {
836 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
837 FoldingSetNodeID ID;
838 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
839 ID.AddInteger(Alignment);
840 ID.AddInteger(Offset);
841 C->AddSelectionDAGCSEId(ID);
842 void *IP = 0;
843 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
844 return SDOperand(E, 0);
845 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
846 CSEMap.InsertNode(N, IP);
847 AllNodes.push_back(N);
848 return SDOperand(N, 0);
849}
850
851
852SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
853 FoldingSetNodeID ID;
854 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
855 ID.AddPointer(MBB);
856 void *IP = 0;
857 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
858 return SDOperand(E, 0);
859 SDNode *N = new BasicBlockSDNode(MBB);
860 CSEMap.InsertNode(N, IP);
861 AllNodes.push_back(N);
862 return SDOperand(N, 0);
863}
864
865SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000866 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 ValueTypeNodes.resize(VT+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868
Duncan Sandsd7307a92007-10-17 13:49:58 +0000869 SDNode *&N = MVT::isExtendedVT(VT) ?
870 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
871
872 if (N) return SDOperand(N, 0);
873 N = new VTSDNode(VT);
874 AllNodes.push_back(N);
875 return SDOperand(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876}
877
878SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
879 SDNode *&N = ExternalSymbols[Sym];
880 if (N) return SDOperand(N, 0);
881 N = new ExternalSymbolSDNode(false, Sym, VT);
882 AllNodes.push_back(N);
883 return SDOperand(N, 0);
884}
885
886SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
887 MVT::ValueType VT) {
888 SDNode *&N = TargetExternalSymbols[Sym];
889 if (N) return SDOperand(N, 0);
890 N = new ExternalSymbolSDNode(true, Sym, VT);
891 AllNodes.push_back(N);
892 return SDOperand(N, 0);
893}
894
895SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
896 if ((unsigned)Cond >= CondCodeNodes.size())
897 CondCodeNodes.resize(Cond+1);
898
899 if (CondCodeNodes[Cond] == 0) {
900 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
901 AllNodes.push_back(CondCodeNodes[Cond]);
902 }
903 return SDOperand(CondCodeNodes[Cond], 0);
904}
905
906SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
907 FoldingSetNodeID ID;
908 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
909 ID.AddInteger(RegNo);
910 void *IP = 0;
911 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
912 return SDOperand(E, 0);
913 SDNode *N = new RegisterSDNode(RegNo, VT);
914 CSEMap.InsertNode(N, IP);
915 AllNodes.push_back(N);
916 return SDOperand(N, 0);
917}
918
919SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
920 assert((!V || isa<PointerType>(V->getType())) &&
921 "SrcValue is not a pointer?");
922
923 FoldingSetNodeID ID;
924 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
925 ID.AddPointer(V);
926 ID.AddInteger(Offset);
927 void *IP = 0;
928 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
929 return SDOperand(E, 0);
930 SDNode *N = new SrcValueSDNode(V, Offset);
931 CSEMap.InsertNode(N, IP);
932 AllNodes.push_back(N);
933 return SDOperand(N, 0);
934}
935
Chris Lattner53f5aee2007-10-15 17:47:20 +0000936/// CreateStackTemporary - Create a stack temporary, suitable for holding the
937/// specified value type.
938SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
939 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
940 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
941 const Type *Ty = MVT::getTypeForValueType(VT);
942 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
943 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
944 return getFrameIndex(FrameIdx, TLI.getPointerTy());
945}
946
947
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
949 SDOperand N2, ISD::CondCode Cond) {
950 // These setcc operations always fold.
951 switch (Cond) {
952 default: break;
953 case ISD::SETFALSE:
954 case ISD::SETFALSE2: return getConstant(0, VT);
955 case ISD::SETTRUE:
956 case ISD::SETTRUE2: return getConstant(1, VT);
957
958 case ISD::SETOEQ:
959 case ISD::SETOGT:
960 case ISD::SETOGE:
961 case ISD::SETOLT:
962 case ISD::SETOLE:
963 case ISD::SETONE:
964 case ISD::SETO:
965 case ISD::SETUO:
966 case ISD::SETUEQ:
967 case ISD::SETUNE:
968 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
969 break;
970 }
971
972 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
973 uint64_t C2 = N2C->getValue();
974 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
975 uint64_t C1 = N1C->getValue();
976
977 // Sign extend the operands if required
978 if (ISD::isSignedIntSetCC(Cond)) {
979 C1 = N1C->getSignExtended();
980 C2 = N2C->getSignExtended();
981 }
982
983 switch (Cond) {
984 default: assert(0 && "Unknown integer setcc!");
985 case ISD::SETEQ: return getConstant(C1 == C2, VT);
986 case ISD::SETNE: return getConstant(C1 != C2, VT);
987 case ISD::SETULT: return getConstant(C1 < C2, VT);
988 case ISD::SETUGT: return getConstant(C1 > C2, VT);
989 case ISD::SETULE: return getConstant(C1 <= C2, VT);
990 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
991 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
992 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
993 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
994 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
995 }
996 }
997 }
998 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
999 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001000 // No compile time operations on this type yet.
1001 if (N1C->getValueType(0) == MVT::ppcf128)
1002 return SDOperand();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001003
1004 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001006 default: break;
Dale Johannesen76844472007-08-31 17:03:33 +00001007 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1008 return getNode(ISD::UNDEF, VT);
1009 // fall through
1010 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1011 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1012 return getNode(ISD::UNDEF, VT);
1013 // fall through
1014 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001015 R==APFloat::cmpLessThan, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001016 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1017 return getNode(ISD::UNDEF, VT);
1018 // fall through
1019 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1020 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1021 return getNode(ISD::UNDEF, VT);
1022 // fall through
1023 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1024 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1025 return getNode(ISD::UNDEF, VT);
1026 // fall through
1027 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001028 R==APFloat::cmpEqual, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001029 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1030 return getNode(ISD::UNDEF, VT);
1031 // fall through
1032 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001033 R==APFloat::cmpEqual, VT);
1034 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1035 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1036 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1037 R==APFloat::cmpEqual, VT);
1038 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1039 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1040 R==APFloat::cmpLessThan, VT);
1041 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1042 R==APFloat::cmpUnordered, VT);
1043 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1044 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 }
1046 } else {
1047 // Ensure that the constant occurs on the RHS.
1048 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1049 }
1050
1051 // Could not fold it.
1052 return SDOperand();
1053}
1054
1055/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1056/// this predicate to simplify operations downstream. Mask is known to be zero
1057/// for bits that V cannot have.
1058bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1059 unsigned Depth) const {
1060 // The masks are not wide enough to represent this type! Should use APInt.
1061 if (Op.getValueType() == MVT::i128)
1062 return false;
1063
1064 uint64_t KnownZero, KnownOne;
1065 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1066 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1067 return (KnownZero & Mask) == Mask;
1068}
1069
1070/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1071/// known to be either zero or one and return them in the KnownZero/KnownOne
1072/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1073/// processing.
1074void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1075 uint64_t &KnownZero, uint64_t &KnownOne,
1076 unsigned Depth) const {
1077 KnownZero = KnownOne = 0; // Don't know anything.
1078 if (Depth == 6 || Mask == 0)
1079 return; // Limit search depth.
1080
1081 // The masks are not wide enough to represent this type! Should use APInt.
1082 if (Op.getValueType() == MVT::i128)
1083 return;
1084
1085 uint64_t KnownZero2, KnownOne2;
1086
1087 switch (Op.getOpcode()) {
1088 case ISD::Constant:
1089 // We know all of the bits for a constant!
1090 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1091 KnownZero = ~KnownOne & Mask;
1092 return;
1093 case ISD::AND:
1094 // If either the LHS or the RHS are Zero, the result is zero.
1095 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1096 Mask &= ~KnownZero;
1097 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1098 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1099 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1100
1101 // Output known-1 bits are only known if set in both the LHS & RHS.
1102 KnownOne &= KnownOne2;
1103 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1104 KnownZero |= KnownZero2;
1105 return;
1106 case ISD::OR:
1107 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1108 Mask &= ~KnownOne;
1109 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1110 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1111 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1112
1113 // Output known-0 bits are only known if clear in both the LHS & RHS.
1114 KnownZero &= KnownZero2;
1115 // Output known-1 are known to be set if set in either the LHS | RHS.
1116 KnownOne |= KnownOne2;
1117 return;
1118 case ISD::XOR: {
1119 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1120 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1121 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1122 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1123
1124 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1125 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1126 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1127 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1128 KnownZero = KnownZeroOut;
1129 return;
1130 }
1131 case ISD::SELECT:
1132 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1133 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1134 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1135 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1136
1137 // Only known if known in both the LHS and RHS.
1138 KnownOne &= KnownOne2;
1139 KnownZero &= KnownZero2;
1140 return;
1141 case ISD::SELECT_CC:
1142 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1143 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1144 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1145 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1146
1147 // Only known if known in both the LHS and RHS.
1148 KnownOne &= KnownOne2;
1149 KnownZero &= KnownZero2;
1150 return;
1151 case ISD::SETCC:
1152 // If we know the result of a setcc has the top bits zero, use this info.
1153 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1154 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1155 return;
1156 case ISD::SHL:
1157 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1158 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1159 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1160 KnownZero, KnownOne, Depth+1);
1161 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1162 KnownZero <<= SA->getValue();
1163 KnownOne <<= SA->getValue();
1164 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1165 }
1166 return;
1167 case ISD::SRL:
1168 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1169 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1170 MVT::ValueType VT = Op.getValueType();
1171 unsigned ShAmt = SA->getValue();
1172
1173 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1174 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1175 KnownZero, KnownOne, Depth+1);
1176 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1177 KnownZero &= TypeMask;
1178 KnownOne &= TypeMask;
1179 KnownZero >>= ShAmt;
1180 KnownOne >>= ShAmt;
1181
1182 uint64_t HighBits = (1ULL << ShAmt)-1;
1183 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1184 KnownZero |= HighBits; // High bits known zero.
1185 }
1186 return;
1187 case ISD::SRA:
1188 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1189 MVT::ValueType VT = Op.getValueType();
1190 unsigned ShAmt = SA->getValue();
1191
1192 // Compute the new bits that are at the top now.
1193 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1194
1195 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1196 // If any of the demanded bits are produced by the sign extension, we also
1197 // demand the input sign bit.
1198 uint64_t HighBits = (1ULL << ShAmt)-1;
1199 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1200 if (HighBits & Mask)
1201 InDemandedMask |= MVT::getIntVTSignBit(VT);
1202
1203 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1204 Depth+1);
1205 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1206 KnownZero &= TypeMask;
1207 KnownOne &= TypeMask;
1208 KnownZero >>= ShAmt;
1209 KnownOne >>= ShAmt;
1210
1211 // Handle the sign bits.
1212 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1213 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1214
1215 if (KnownZero & SignBit) {
1216 KnownZero |= HighBits; // New bits are known zero.
1217 } else if (KnownOne & SignBit) {
1218 KnownOne |= HighBits; // New bits are known one.
1219 }
1220 }
1221 return;
1222 case ISD::SIGN_EXTEND_INREG: {
1223 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1224
1225 // Sign extension. Compute the demanded bits in the result that are not
1226 // present in the input.
1227 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1228
1229 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1230 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1231
1232 // If the sign extended bits are demanded, we know that the sign
1233 // bit is demanded.
1234 if (NewBits)
1235 InputDemandedBits |= InSignBit;
1236
1237 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1238 KnownZero, KnownOne, Depth+1);
1239 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1240
1241 // If the sign bit of the input is known set or clear, then we know the
1242 // top bits of the result.
1243 if (KnownZero & InSignBit) { // Input sign bit known clear
1244 KnownZero |= NewBits;
1245 KnownOne &= ~NewBits;
1246 } else if (KnownOne & InSignBit) { // Input sign bit known set
1247 KnownOne |= NewBits;
1248 KnownZero &= ~NewBits;
1249 } else { // Input sign bit unknown
1250 KnownZero &= ~NewBits;
1251 KnownOne &= ~NewBits;
1252 }
1253 return;
1254 }
1255 case ISD::CTTZ:
1256 case ISD::CTLZ:
1257 case ISD::CTPOP: {
1258 MVT::ValueType VT = Op.getValueType();
1259 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1260 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1261 KnownOne = 0;
1262 return;
1263 }
1264 case ISD::LOAD: {
1265 if (ISD::isZEXTLoad(Op.Val)) {
1266 LoadSDNode *LD = cast<LoadSDNode>(Op);
1267 MVT::ValueType VT = LD->getLoadedVT();
1268 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1269 }
1270 return;
1271 }
1272 case ISD::ZERO_EXTEND: {
1273 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1274 uint64_t NewBits = (~InMask) & Mask;
1275 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1276 KnownOne, Depth+1);
1277 KnownZero |= NewBits & Mask;
1278 KnownOne &= ~NewBits;
1279 return;
1280 }
1281 case ISD::SIGN_EXTEND: {
1282 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1283 unsigned InBits = MVT::getSizeInBits(InVT);
1284 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1285 uint64_t InSignBit = 1ULL << (InBits-1);
1286 uint64_t NewBits = (~InMask) & Mask;
1287 uint64_t InDemandedBits = Mask & InMask;
1288
1289 // If any of the sign extended bits are demanded, we know that the sign
1290 // bit is demanded.
1291 if (NewBits & Mask)
1292 InDemandedBits |= InSignBit;
1293
1294 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1295 KnownOne, Depth+1);
1296 // If the sign bit is known zero or one, the top bits match.
1297 if (KnownZero & InSignBit) {
1298 KnownZero |= NewBits;
1299 KnownOne &= ~NewBits;
1300 } else if (KnownOne & InSignBit) {
1301 KnownOne |= NewBits;
1302 KnownZero &= ~NewBits;
1303 } else { // Otherwise, top bits aren't known.
1304 KnownOne &= ~NewBits;
1305 KnownZero &= ~NewBits;
1306 }
1307 return;
1308 }
1309 case ISD::ANY_EXTEND: {
1310 MVT::ValueType VT = Op.getOperand(0).getValueType();
1311 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1312 KnownZero, KnownOne, Depth+1);
1313 return;
1314 }
1315 case ISD::TRUNCATE: {
1316 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1317 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1318 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1319 KnownZero &= OutMask;
1320 KnownOne &= OutMask;
1321 break;
1322 }
1323 case ISD::AssertZext: {
1324 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1325 uint64_t InMask = MVT::getIntVTBitMask(VT);
1326 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1327 KnownOne, Depth+1);
1328 KnownZero |= (~InMask) & Mask;
1329 return;
1330 }
Chris Lattner13f06832007-12-22 21:26:52 +00001331 case ISD::FGETSIGN:
1332 // All bits are zero except the low bit.
1333 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1334 return;
1335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 case ISD::ADD: {
1337 // If either the LHS or the RHS are Zero, the result is zero.
1338 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1339 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1340 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1341 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1342
1343 // Output known-0 bits are known if clear or set in both the low clear bits
1344 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1345 // low 3 bits clear.
1346 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1347 CountTrailingZeros_64(~KnownZero2));
1348
1349 KnownZero = (1ULL << KnownZeroOut) - 1;
1350 KnownOne = 0;
1351 return;
1352 }
1353 case ISD::SUB: {
1354 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1355 if (!CLHS) return;
1356
1357 // We know that the top bits of C-X are clear if X contains less bits
1358 // than C (i.e. no wrap-around can happen). For example, 20-X is
1359 // positive if we can prove that X is >= 0 and < 16.
1360 MVT::ValueType VT = CLHS->getValueType(0);
1361 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1362 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1363 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1364 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1365 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1366
1367 // If all of the MaskV bits are known to be zero, then we know the output
1368 // top bits are zero, because we now know that the output is from [0-C].
1369 if ((KnownZero & MaskV) == MaskV) {
1370 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1371 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1372 KnownOne = 0; // No one bits known.
1373 } else {
1374 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1375 }
1376 }
1377 return;
1378 }
1379 default:
1380 // Allow the target to implement this method for its nodes.
1381 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1382 case ISD::INTRINSIC_WO_CHAIN:
1383 case ISD::INTRINSIC_W_CHAIN:
1384 case ISD::INTRINSIC_VOID:
1385 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1386 }
1387 return;
1388 }
1389}
1390
1391/// ComputeNumSignBits - Return the number of times the sign bit of the
1392/// register is replicated into the other bits. We know that at least 1 bit
1393/// is always equal to the sign bit (itself), but other cases can give us
1394/// information. For example, immediately after an "SRA X, 2", we know that
1395/// the top 3 bits are all equal to each other, so we return 3.
1396unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1397 MVT::ValueType VT = Op.getValueType();
1398 assert(MVT::isInteger(VT) && "Invalid VT!");
1399 unsigned VTBits = MVT::getSizeInBits(VT);
1400 unsigned Tmp, Tmp2;
1401
1402 if (Depth == 6)
1403 return 1; // Limit search depth.
1404
1405 switch (Op.getOpcode()) {
1406 default: break;
1407 case ISD::AssertSext:
1408 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1409 return VTBits-Tmp+1;
1410 case ISD::AssertZext:
1411 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1412 return VTBits-Tmp;
1413
1414 case ISD::Constant: {
1415 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1416 // If negative, invert the bits, then look at it.
1417 if (Val & MVT::getIntVTSignBit(VT))
1418 Val = ~Val;
1419
1420 // Shift the bits so they are the leading bits in the int64_t.
1421 Val <<= 64-VTBits;
1422
1423 // Return # leading zeros. We use 'min' here in case Val was zero before
1424 // shifting. We don't want to return '64' as for an i32 "0".
1425 return std::min(VTBits, CountLeadingZeros_64(Val));
1426 }
1427
1428 case ISD::SIGN_EXTEND:
1429 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1430 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1431
1432 case ISD::SIGN_EXTEND_INREG:
1433 // Max of the input and what this extends.
1434 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1435 Tmp = VTBits-Tmp+1;
1436
1437 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1438 return std::max(Tmp, Tmp2);
1439
1440 case ISD::SRA:
1441 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1442 // SRA X, C -> adds C sign bits.
1443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1444 Tmp += C->getValue();
1445 if (Tmp > VTBits) Tmp = VTBits;
1446 }
1447 return Tmp;
1448 case ISD::SHL:
1449 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1450 // shl destroys sign bits.
1451 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1452 if (C->getValue() >= VTBits || // Bad shift.
1453 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1454 return Tmp - C->getValue();
1455 }
1456 break;
1457 case ISD::AND:
1458 case ISD::OR:
1459 case ISD::XOR: // NOT is handled here.
1460 // Logical binary ops preserve the number of sign bits.
1461 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1462 if (Tmp == 1) return 1; // Early out.
1463 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1464 return std::min(Tmp, Tmp2);
1465
1466 case ISD::SELECT:
1467 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1468 if (Tmp == 1) return 1; // Early out.
1469 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1470 return std::min(Tmp, Tmp2);
1471
1472 case ISD::SETCC:
1473 // If setcc returns 0/-1, all bits are sign bits.
1474 if (TLI.getSetCCResultContents() ==
1475 TargetLowering::ZeroOrNegativeOneSetCCResult)
1476 return VTBits;
1477 break;
1478 case ISD::ROTL:
1479 case ISD::ROTR:
1480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1481 unsigned RotAmt = C->getValue() & (VTBits-1);
1482
1483 // Handle rotate right by N like a rotate left by 32-N.
1484 if (Op.getOpcode() == ISD::ROTR)
1485 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1486
1487 // If we aren't rotating out all of the known-in sign bits, return the
1488 // number that are left. This handles rotl(sext(x), 1) for example.
1489 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1490 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1491 }
1492 break;
1493 case ISD::ADD:
1494 // Add can have at most one carry bit. Thus we know that the output
1495 // is, at worst, one more bit than the inputs.
1496 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1497 if (Tmp == 1) return 1; // Early out.
1498
1499 // Special case decrementing a value (ADD X, -1):
1500 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1501 if (CRHS->isAllOnesValue()) {
1502 uint64_t KnownZero, KnownOne;
1503 uint64_t Mask = MVT::getIntVTBitMask(VT);
1504 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1505
1506 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1507 // sign bits set.
1508 if ((KnownZero|1) == Mask)
1509 return VTBits;
1510
1511 // If we are subtracting one from a positive number, there is no carry
1512 // out of the result.
1513 if (KnownZero & MVT::getIntVTSignBit(VT))
1514 return Tmp;
1515 }
1516
1517 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1518 if (Tmp2 == 1) return 1;
1519 return std::min(Tmp, Tmp2)-1;
1520 break;
1521
1522 case ISD::SUB:
1523 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1524 if (Tmp2 == 1) return 1;
1525
1526 // Handle NEG.
1527 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1528 if (CLHS->getValue() == 0) {
1529 uint64_t KnownZero, KnownOne;
1530 uint64_t Mask = MVT::getIntVTBitMask(VT);
1531 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1532 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1533 // sign bits set.
1534 if ((KnownZero|1) == Mask)
1535 return VTBits;
1536
1537 // If the input is known to be positive (the sign bit is known clear),
1538 // the output of the NEG has the same number of sign bits as the input.
1539 if (KnownZero & MVT::getIntVTSignBit(VT))
1540 return Tmp2;
1541
1542 // Otherwise, we treat this like a SUB.
1543 }
1544
1545 // Sub can have at most one carry bit. Thus we know that the output
1546 // is, at worst, one more bit than the inputs.
1547 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1548 if (Tmp == 1) return 1; // Early out.
1549 return std::min(Tmp, Tmp2)-1;
1550 break;
1551 case ISD::TRUNCATE:
1552 // FIXME: it's tricky to do anything useful for this, but it is an important
1553 // case for targets like X86.
1554 break;
1555 }
1556
1557 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1558 if (Op.getOpcode() == ISD::LOAD) {
1559 LoadSDNode *LD = cast<LoadSDNode>(Op);
1560 unsigned ExtType = LD->getExtensionType();
1561 switch (ExtType) {
1562 default: break;
1563 case ISD::SEXTLOAD: // '17' bits known
1564 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1565 return VTBits-Tmp+1;
1566 case ISD::ZEXTLOAD: // '16' bits known
1567 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1568 return VTBits-Tmp;
1569 }
1570 }
1571
1572 // Allow the target to implement this method for its nodes.
1573 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1574 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1575 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1576 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1577 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1578 if (NumBits > 1) return NumBits;
1579 }
1580
1581 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1582 // use this information.
1583 uint64_t KnownZero, KnownOne;
1584 uint64_t Mask = MVT::getIntVTBitMask(VT);
1585 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1586
1587 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1588 if (KnownZero & SignBit) { // SignBit is 0
1589 Mask = KnownZero;
1590 } else if (KnownOne & SignBit) { // SignBit is 1;
1591 Mask = KnownOne;
1592 } else {
1593 // Nothing known.
1594 return 1;
1595 }
1596
1597 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1598 // the number of identical bits in the top of the input value.
1599 Mask ^= ~0ULL;
1600 Mask <<= 64-VTBits;
1601 // Return # leading zeros. We use 'min' here in case Val was zero before
1602 // shifting. We don't want to return '64' as for an i32 "0".
1603 return std::min(VTBits, CountLeadingZeros_64(Mask));
1604}
1605
1606
1607/// getNode - Gets or creates the specified node.
1608///
1609SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1610 FoldingSetNodeID ID;
1611 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1612 void *IP = 0;
1613 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1614 return SDOperand(E, 0);
1615 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1616 CSEMap.InsertNode(N, IP);
1617
1618 AllNodes.push_back(N);
1619 return SDOperand(N, 0);
1620}
1621
1622SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1623 SDOperand Operand) {
1624 unsigned Tmp1;
1625 // Constant fold unary operations with an integer constant operand.
1626 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1627 uint64_t Val = C->getValue();
1628 switch (Opcode) {
1629 default: break;
1630 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1631 case ISD::ANY_EXTEND:
1632 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1633 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001634 case ISD::UINT_TO_FP:
1635 case ISD::SINT_TO_FP: {
1636 const uint64_t zero[] = {0, 0};
Dale Johannesenb89072e2007-10-16 23:38:29 +00001637 // No compile time operations on this type.
1638 if (VT==MVT::ppcf128)
1639 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00001640 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Booth4bdd45a2007-10-07 11:45:55 +00001641 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesena6f79742007-09-21 22:09:37 +00001642 MVT::getSizeInBits(Operand.getValueType()),
1643 Opcode==ISD::SINT_TO_FP,
Dale Johannesen87fa68f2007-09-30 18:19:03 +00001644 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001645 return getConstantFP(apf, VT);
1646 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001647 case ISD::BIT_CONVERT:
1648 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1649 return getConstantFP(BitsToFloat(Val), VT);
1650 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1651 return getConstantFP(BitsToDouble(Val), VT);
1652 break;
1653 case ISD::BSWAP:
1654 switch(VT) {
1655 default: assert(0 && "Invalid bswap!"); break;
1656 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1657 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1658 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1659 }
1660 break;
1661 case ISD::CTPOP:
1662 switch(VT) {
1663 default: assert(0 && "Invalid ctpop!"); break;
1664 case MVT::i1: return getConstant(Val != 0, VT);
1665 case MVT::i8:
1666 Tmp1 = (unsigned)Val & 0xFF;
1667 return getConstant(CountPopulation_32(Tmp1), VT);
1668 case MVT::i16:
1669 Tmp1 = (unsigned)Val & 0xFFFF;
1670 return getConstant(CountPopulation_32(Tmp1), VT);
1671 case MVT::i32:
1672 return getConstant(CountPopulation_32((unsigned)Val), VT);
1673 case MVT::i64:
1674 return getConstant(CountPopulation_64(Val), VT);
1675 }
1676 case ISD::CTLZ:
1677 switch(VT) {
1678 default: assert(0 && "Invalid ctlz!"); break;
1679 case MVT::i1: return getConstant(Val == 0, VT);
1680 case MVT::i8:
1681 Tmp1 = (unsigned)Val & 0xFF;
1682 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1683 case MVT::i16:
1684 Tmp1 = (unsigned)Val & 0xFFFF;
1685 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1686 case MVT::i32:
1687 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1688 case MVT::i64:
1689 return getConstant(CountLeadingZeros_64(Val), VT);
1690 }
1691 case ISD::CTTZ:
1692 switch(VT) {
1693 default: assert(0 && "Invalid cttz!"); break;
1694 case MVT::i1: return getConstant(Val == 0, VT);
1695 case MVT::i8:
1696 Tmp1 = (unsigned)Val | 0x100;
1697 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1698 case MVT::i16:
1699 Tmp1 = (unsigned)Val | 0x10000;
1700 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1701 case MVT::i32:
1702 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1703 case MVT::i64:
1704 return getConstant(CountTrailingZeros_64(Val), VT);
1705 }
1706 }
1707 }
1708
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001709 // Constant fold unary operations with a floating point constant operand.
1710 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1711 APFloat V = C->getValueAPF(); // make copy
Chris Lattner5872a362008-01-17 07:00:52 +00001712 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00001713 switch (Opcode) {
1714 case ISD::FNEG:
1715 V.changeSign();
1716 return getConstantFP(V, VT);
1717 case ISD::FABS:
1718 V.clearSign();
1719 return getConstantFP(V, VT);
1720 case ISD::FP_ROUND:
1721 case ISD::FP_EXTEND:
1722 // This can return overflow, underflow, or inexact; we don't care.
1723 // FIXME need to be more flexible about rounding mode.
1724 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1725 VT==MVT::f64 ? APFloat::IEEEdouble :
1726 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1727 VT==MVT::f128 ? APFloat::IEEEquad :
1728 APFloat::Bogus,
1729 APFloat::rmNearestTiesToEven);
1730 return getConstantFP(V, VT);
1731 case ISD::FP_TO_SINT:
1732 case ISD::FP_TO_UINT: {
1733 integerPart x;
1734 assert(integerPartWidth >= 64);
1735 // FIXME need to be more flexible about rounding mode.
1736 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1737 Opcode==ISD::FP_TO_SINT,
1738 APFloat::rmTowardZero);
1739 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1740 break;
1741 return getConstant(x, VT);
1742 }
1743 case ISD::BIT_CONVERT:
1744 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1745 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1746 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1747 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001748 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00001749 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001750 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001751 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752
1753 unsigned OpOpcode = Operand.Val->getOpcode();
1754 switch (Opcode) {
1755 case ISD::TokenFactor:
1756 return Operand; // Factor of one node? No factor.
Chris Lattner5872a362008-01-17 07:00:52 +00001757 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758 case ISD::FP_EXTEND:
1759 assert(MVT::isFloatingPoint(VT) &&
1760 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00001761 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001762 break;
Chris Lattner5872a362008-01-17 07:00:52 +00001763 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1765 "Invalid SIGN_EXTEND!");
1766 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001767 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1768 && "Invalid sext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001769 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1770 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1771 break;
1772 case ISD::ZERO_EXTEND:
1773 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1774 "Invalid ZERO_EXTEND!");
1775 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001776 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1777 && "Invalid zext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001778 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1779 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1780 break;
1781 case ISD::ANY_EXTEND:
1782 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1783 "Invalid ANY_EXTEND!");
1784 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001785 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1786 && "Invalid anyext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001787 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1788 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1789 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1790 break;
1791 case ISD::TRUNCATE:
1792 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1793 "Invalid TRUNCATE!");
1794 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsa9810f32007-10-16 09:56:48 +00001795 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1796 && "Invalid truncate node, src < dst!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797 if (OpOpcode == ISD::TRUNCATE)
1798 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1799 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1800 OpOpcode == ISD::ANY_EXTEND) {
1801 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsa9810f32007-10-16 09:56:48 +00001802 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1803 < MVT::getSizeInBits(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001804 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsa9810f32007-10-16 09:56:48 +00001805 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1806 > MVT::getSizeInBits(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001807 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1808 else
1809 return Operand.Val->getOperand(0);
1810 }
1811 break;
1812 case ISD::BIT_CONVERT:
1813 // Basic sanity checking.
1814 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1815 && "Cannot BIT_CONVERT between types of different sizes!");
1816 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1817 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1818 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1819 if (OpOpcode == ISD::UNDEF)
1820 return getNode(ISD::UNDEF, VT);
1821 break;
1822 case ISD::SCALAR_TO_VECTOR:
1823 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1824 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1825 "Illegal SCALAR_TO_VECTOR node!");
1826 break;
1827 case ISD::FNEG:
1828 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1829 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1830 Operand.Val->getOperand(0));
1831 if (OpOpcode == ISD::FNEG) // --X -> X
1832 return Operand.Val->getOperand(0);
1833 break;
1834 case ISD::FABS:
1835 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1836 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1837 break;
1838 }
1839
1840 SDNode *N;
1841 SDVTList VTs = getVTList(VT);
1842 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1843 FoldingSetNodeID ID;
1844 SDOperand Ops[1] = { Operand };
1845 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1846 void *IP = 0;
1847 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1848 return SDOperand(E, 0);
1849 N = new UnarySDNode(Opcode, VTs, Operand);
1850 CSEMap.InsertNode(N, IP);
1851 } else {
1852 N = new UnarySDNode(Opcode, VTs, Operand);
1853 }
1854 AllNodes.push_back(N);
1855 return SDOperand(N, 0);
1856}
1857
1858
1859
1860SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1861 SDOperand N1, SDOperand N2) {
Chris Lattnercc126e32008-01-22 19:09:33 +00001862 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1863 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001864 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00001865 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001866 case ISD::TokenFactor:
1867 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1868 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00001869 // Fold trivial token factors.
1870 if (N1.getOpcode() == ISD::EntryToken) return N2;
1871 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001872 break;
1873 case ISD::AND:
Chris Lattnercc126e32008-01-22 19:09:33 +00001874 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1875 N1.getValueType() == VT && "Binary operator types must match!");
1876 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1877 // worth handling here.
1878 if (N2C && N2C->getValue() == 0)
1879 return N2;
1880 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 case ISD::OR:
1882 case ISD::XOR:
Chris Lattnercc126e32008-01-22 19:09:33 +00001883 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1884 N1.getValueType() == VT && "Binary operator types must match!");
1885 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1886 // worth handling here.
1887 if (N2C && N2C->getValue() == 0)
1888 return N1;
1889 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 case ISD::UDIV:
1891 case ISD::UREM:
1892 case ISD::MULHU:
1893 case ISD::MULHS:
1894 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1895 // fall through
1896 case ISD::ADD:
1897 case ISD::SUB:
1898 case ISD::MUL:
1899 case ISD::SDIV:
1900 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001901 case ISD::FADD:
1902 case ISD::FSUB:
1903 case ISD::FMUL:
1904 case ISD::FDIV:
1905 case ISD::FREM:
1906 assert(N1.getValueType() == N2.getValueType() &&
1907 N1.getValueType() == VT && "Binary operator types must match!");
1908 break;
1909 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1910 assert(N1.getValueType() == VT &&
1911 MVT::isFloatingPoint(N1.getValueType()) &&
1912 MVT::isFloatingPoint(N2.getValueType()) &&
1913 "Invalid FCOPYSIGN!");
1914 break;
1915 case ISD::SHL:
1916 case ISD::SRA:
1917 case ISD::SRL:
1918 case ISD::ROTL:
1919 case ISD::ROTR:
1920 assert(VT == N1.getValueType() &&
1921 "Shift operators return type must be the same as their first arg");
1922 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1923 VT != MVT::i1 && "Shifts only work on integers");
1924 break;
1925 case ISD::FP_ROUND_INREG: {
1926 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1927 assert(VT == N1.getValueType() && "Not an inreg round!");
1928 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1929 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsa9810f32007-10-16 09:56:48 +00001930 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1931 "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00001932 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001933 break;
1934 }
Chris Lattner5872a362008-01-17 07:00:52 +00001935 case ISD::FP_ROUND:
1936 assert(MVT::isFloatingPoint(VT) &&
1937 MVT::isFloatingPoint(N1.getValueType()) &&
1938 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
1939 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00001940 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00001941 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00001943 case ISD::AssertZext: {
1944 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1945 assert(VT == N1.getValueType() && "Not an inreg extend!");
1946 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1947 "Cannot *_EXTEND_INREG FP types");
1948 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1949 "Not extending!");
1950 break;
1951 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001952 case ISD::SIGN_EXTEND_INREG: {
1953 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1954 assert(VT == N1.getValueType() && "Not an inreg extend!");
1955 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1956 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsa9810f32007-10-16 09:56:48 +00001957 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1958 "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00001959 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001960
Chris Lattnercc126e32008-01-22 19:09:33 +00001961 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001962 int64_t Val = N1C->getValue();
1963 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1964 Val <<= 64-FromBits;
1965 Val >>= 64-FromBits;
1966 return getConstant(Val, VT);
1967 }
Chris Lattnercc126e32008-01-22 19:09:33 +00001968 break;
1969 }
1970 case ISD::EXTRACT_VECTOR_ELT:
1971 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
1972
1973 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
1974 // expanding copies of large vectors from registers.
1975 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
1976 N1.getNumOperands() > 0) {
1977 unsigned Factor =
1978 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
1979 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
1980 N1.getOperand(N2C->getValue() / Factor),
1981 getConstant(N2C->getValue() % Factor, N2.getValueType()));
1982 }
1983
1984 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
1985 // expanding large vector constants.
1986 if (N1.getOpcode() == ISD::BUILD_VECTOR)
1987 return N1.getOperand(N2C->getValue());
1988
1989 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
1990 // operations are lowered to scalars.
1991 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
1992 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
1993 if (IEC == N2C)
1994 return N1.getOperand(1);
1995 else
1996 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
1997 }
1998 break;
1999 case ISD::EXTRACT_ELEMENT:
2000 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001
Chris Lattnercc126e32008-01-22 19:09:33 +00002002 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2003 // 64-bit integers into 32-bit parts. Instead of building the extract of
2004 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2005 if (N1.getOpcode() == ISD::BUILD_PAIR)
2006 return N1.getOperand(N2C->getValue());
2007
2008 // EXTRACT_ELEMENT of a constant int is also very common.
2009 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2010 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2011 return getConstant(C->getValue() >> Shift, VT);
2012 }
2013 break;
2014 }
2015
2016 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002017 if (N2C) {
2018 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
2019 switch (Opcode) {
2020 case ISD::ADD: return getConstant(C1 + C2, VT);
2021 case ISD::SUB: return getConstant(C1 - C2, VT);
2022 case ISD::MUL: return getConstant(C1 * C2, VT);
2023 case ISD::UDIV:
2024 if (C2) return getConstant(C1 / C2, VT);
2025 break;
2026 case ISD::UREM :
2027 if (C2) return getConstant(C1 % C2, VT);
2028 break;
2029 case ISD::SDIV :
2030 if (C2) return getConstant(N1C->getSignExtended() /
2031 N2C->getSignExtended(), VT);
2032 break;
2033 case ISD::SREM :
2034 if (C2) return getConstant(N1C->getSignExtended() %
2035 N2C->getSignExtended(), VT);
2036 break;
2037 case ISD::AND : return getConstant(C1 & C2, VT);
2038 case ISD::OR : return getConstant(C1 | C2, VT);
2039 case ISD::XOR : return getConstant(C1 ^ C2, VT);
2040 case ISD::SHL : return getConstant(C1 << C2, VT);
2041 case ISD::SRL : return getConstant(C1 >> C2, VT);
2042 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
2043 case ISD::ROTL :
2044 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
2045 VT);
2046 case ISD::ROTR :
2047 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
2048 VT);
2049 default: break;
2050 }
2051 } else { // Cannonicalize constant to RHS if commutative
2052 if (isCommutativeBinOp(Opcode)) {
2053 std::swap(N1C, N2C);
2054 std::swap(N1, N2);
2055 }
2056 }
2057 }
2058
Chris Lattnercc126e32008-01-22 19:09:33 +00002059 // Constant fold FP operations.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2061 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
2062 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002063 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2064 // Cannonicalize constant to RHS if commutative
2065 std::swap(N1CFP, N2CFP);
2066 std::swap(N1, N2);
2067 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002068 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2069 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002070 switch (Opcode) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002071 case ISD::FADD:
2072 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002073 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002074 return getConstantFP(V1, VT);
2075 break;
2076 case ISD::FSUB:
2077 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2078 if (s!=APFloat::opInvalidOp)
2079 return getConstantFP(V1, VT);
2080 break;
2081 case ISD::FMUL:
2082 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2083 if (s!=APFloat::opInvalidOp)
2084 return getConstantFP(V1, VT);
2085 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002087 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2088 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2089 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002090 break;
2091 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002092 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2093 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2094 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002095 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002096 case ISD::FCOPYSIGN:
2097 V1.copySign(V2);
2098 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 default: break;
2100 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 }
2102 }
2103
2104 // Canonicalize an UNDEF to the RHS, even over a constant.
2105 if (N1.getOpcode() == ISD::UNDEF) {
2106 if (isCommutativeBinOp(Opcode)) {
2107 std::swap(N1, N2);
2108 } else {
2109 switch (Opcode) {
2110 case ISD::FP_ROUND_INREG:
2111 case ISD::SIGN_EXTEND_INREG:
2112 case ISD::SUB:
2113 case ISD::FSUB:
2114 case ISD::FDIV:
2115 case ISD::FREM:
2116 case ISD::SRA:
2117 return N1; // fold op(undef, arg2) -> undef
2118 case ISD::UDIV:
2119 case ISD::SDIV:
2120 case ISD::UREM:
2121 case ISD::SREM:
2122 case ISD::SRL:
2123 case ISD::SHL:
2124 if (!MVT::isVector(VT))
2125 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2126 // For vectors, we can't easily build an all zero vector, just return
2127 // the LHS.
2128 return N2;
2129 }
2130 }
2131 }
2132
2133 // Fold a bunch of operators when the RHS is undef.
2134 if (N2.getOpcode() == ISD::UNDEF) {
2135 switch (Opcode) {
2136 case ISD::ADD:
2137 case ISD::ADDC:
2138 case ISD::ADDE:
2139 case ISD::SUB:
2140 case ISD::FADD:
2141 case ISD::FSUB:
2142 case ISD::FMUL:
2143 case ISD::FDIV:
2144 case ISD::FREM:
2145 case ISD::UDIV:
2146 case ISD::SDIV:
2147 case ISD::UREM:
2148 case ISD::SREM:
2149 case ISD::XOR:
2150 return N2; // fold op(arg1, undef) -> undef
2151 case ISD::MUL:
2152 case ISD::AND:
2153 case ISD::SRL:
2154 case ISD::SHL:
2155 if (!MVT::isVector(VT))
2156 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2157 // For vectors, we can't easily build an all zero vector, just return
2158 // the LHS.
2159 return N1;
2160 case ISD::OR:
2161 if (!MVT::isVector(VT))
2162 return getConstant(MVT::getIntVTBitMask(VT), VT);
2163 // For vectors, we can't easily build an all one vector, just return
2164 // the LHS.
2165 return N1;
2166 case ISD::SRA:
2167 return N1;
2168 }
2169 }
2170
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171 // Memoize this node if possible.
2172 SDNode *N;
2173 SDVTList VTs = getVTList(VT);
2174 if (VT != MVT::Flag) {
2175 SDOperand Ops[] = { N1, N2 };
2176 FoldingSetNodeID ID;
2177 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2178 void *IP = 0;
2179 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2180 return SDOperand(E, 0);
2181 N = new BinarySDNode(Opcode, VTs, N1, N2);
2182 CSEMap.InsertNode(N, IP);
2183 } else {
2184 N = new BinarySDNode(Opcode, VTs, N1, N2);
2185 }
2186
2187 AllNodes.push_back(N);
2188 return SDOperand(N, 0);
2189}
2190
2191SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2192 SDOperand N1, SDOperand N2, SDOperand N3) {
2193 // Perform various simplifications.
2194 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2195 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2196 switch (Opcode) {
2197 case ISD::SETCC: {
2198 // Use FoldSetCC to simplify SETCC's.
2199 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2200 if (Simp.Val) return Simp;
2201 break;
2202 }
2203 case ISD::SELECT:
2204 if (N1C)
2205 if (N1C->getValue())
2206 return N2; // select true, X, Y -> X
2207 else
2208 return N3; // select false, X, Y -> Y
2209
2210 if (N2 == N3) return N2; // select C, X, X -> X
2211 break;
2212 case ISD::BRCOND:
2213 if (N2C)
2214 if (N2C->getValue()) // Unconditional branch
2215 return getNode(ISD::BR, MVT::Other, N1, N3);
2216 else
2217 return N1; // Never-taken branch
2218 break;
2219 case ISD::VECTOR_SHUFFLE:
2220 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2221 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2222 N3.getOpcode() == ISD::BUILD_VECTOR &&
2223 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2224 "Illegal VECTOR_SHUFFLE node!");
2225 break;
2226 case ISD::BIT_CONVERT:
2227 // Fold bit_convert nodes from a type to themselves.
2228 if (N1.getValueType() == VT)
2229 return N1;
2230 break;
2231 }
2232
2233 // Memoize node if it doesn't produce a flag.
2234 SDNode *N;
2235 SDVTList VTs = getVTList(VT);
2236 if (VT != MVT::Flag) {
2237 SDOperand Ops[] = { N1, N2, N3 };
2238 FoldingSetNodeID ID;
2239 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2240 void *IP = 0;
2241 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2242 return SDOperand(E, 0);
2243 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2244 CSEMap.InsertNode(N, IP);
2245 } else {
2246 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2247 }
2248 AllNodes.push_back(N);
2249 return SDOperand(N, 0);
2250}
2251
2252SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2253 SDOperand N1, SDOperand N2, SDOperand N3,
2254 SDOperand N4) {
2255 SDOperand Ops[] = { N1, N2, N3, N4 };
2256 return getNode(Opcode, VT, Ops, 4);
2257}
2258
2259SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2260 SDOperand N1, SDOperand N2, SDOperand N3,
2261 SDOperand N4, SDOperand N5) {
2262 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2263 return getNode(Opcode, VT, Ops, 5);
2264}
2265
Rafael Espindola80825902007-10-19 10:41:11 +00002266SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2267 SDOperand Src, SDOperand Size,
2268 SDOperand Align,
2269 SDOperand AlwaysInline) {
2270 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2271 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2272}
2273
2274SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2275 SDOperand Src, SDOperand Size,
2276 SDOperand Align,
2277 SDOperand AlwaysInline) {
2278 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2279 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2280}
2281
2282SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2283 SDOperand Src, SDOperand Size,
2284 SDOperand Align,
2285 SDOperand AlwaysInline) {
2286 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2287 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2288}
2289
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002290SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2291 SDOperand Chain, SDOperand Ptr,
2292 const Value *SV, int SVOffset,
2293 bool isVolatile, unsigned Alignment) {
2294 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2295 const Type *Ty = 0;
2296 if (VT != MVT::iPTR) {
2297 Ty = MVT::getTypeForValueType(VT);
2298 } else if (SV) {
2299 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2300 assert(PT && "Value for load must be a pointer");
2301 Ty = PT->getElementType();
2302 }
2303 assert(Ty && "Could not get type information for load");
2304 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2305 }
2306 SDVTList VTs = getVTList(VT, MVT::Other);
2307 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2308 SDOperand Ops[] = { Chain, Ptr, Undef };
2309 FoldingSetNodeID ID;
2310 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2311 ID.AddInteger(ISD::UNINDEXED);
2312 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner4a22a672007-09-13 06:09:48 +00002313 ID.AddInteger((unsigned int)VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002314 ID.AddInteger(Alignment);
2315 ID.AddInteger(isVolatile);
2316 void *IP = 0;
2317 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2318 return SDOperand(E, 0);
2319 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2320 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2321 isVolatile);
2322 CSEMap.InsertNode(N, IP);
2323 AllNodes.push_back(N);
2324 return SDOperand(N, 0);
2325}
2326
2327SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2328 SDOperand Chain, SDOperand Ptr,
2329 const Value *SV,
2330 int SVOffset, MVT::ValueType EVT,
2331 bool isVolatile, unsigned Alignment) {
2332 // If they are asking for an extending load from/to the same thing, return a
2333 // normal load.
2334 if (VT == EVT)
Duncan Sands9b614742007-10-19 13:05:40 +00002335 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002336
2337 if (MVT::isVector(VT))
2338 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2339 else
Duncan Sandsa9810f32007-10-16 09:56:48 +00002340 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2341 "Should only be an extending load, not truncating!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002342 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2343 "Cannot sign/zero extend a FP/Vector load!");
2344 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2345 "Cannot convert from FP to Int or Int -> FP!");
2346
2347 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2348 const Type *Ty = 0;
2349 if (VT != MVT::iPTR) {
2350 Ty = MVT::getTypeForValueType(VT);
2351 } else if (SV) {
2352 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2353 assert(PT && "Value for load must be a pointer");
2354 Ty = PT->getElementType();
2355 }
2356 assert(Ty && "Could not get type information for load");
2357 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2358 }
2359 SDVTList VTs = getVTList(VT, MVT::Other);
2360 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2361 SDOperand Ops[] = { Chain, Ptr, Undef };
2362 FoldingSetNodeID ID;
2363 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2364 ID.AddInteger(ISD::UNINDEXED);
2365 ID.AddInteger(ExtType);
Chris Lattner4a22a672007-09-13 06:09:48 +00002366 ID.AddInteger((unsigned int)EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367 ID.AddInteger(Alignment);
2368 ID.AddInteger(isVolatile);
2369 void *IP = 0;
2370 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2371 return SDOperand(E, 0);
2372 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2373 SV, SVOffset, Alignment, isVolatile);
2374 CSEMap.InsertNode(N, IP);
2375 AllNodes.push_back(N);
2376 return SDOperand(N, 0);
2377}
2378
2379SDOperand
2380SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2381 SDOperand Offset, ISD::MemIndexedMode AM) {
2382 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2383 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2384 "Load is already a indexed load!");
2385 MVT::ValueType VT = OrigLoad.getValueType();
2386 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2387 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2388 FoldingSetNodeID ID;
2389 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2390 ID.AddInteger(AM);
2391 ID.AddInteger(LD->getExtensionType());
Chris Lattner4a22a672007-09-13 06:09:48 +00002392 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002393 ID.AddInteger(LD->getAlignment());
2394 ID.AddInteger(LD->isVolatile());
2395 void *IP = 0;
2396 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2397 return SDOperand(E, 0);
2398 SDNode *N = new LoadSDNode(Ops, VTs, AM,
2399 LD->getExtensionType(), LD->getLoadedVT(),
2400 LD->getSrcValue(), LD->getSrcValueOffset(),
2401 LD->getAlignment(), LD->isVolatile());
2402 CSEMap.InsertNode(N, IP);
2403 AllNodes.push_back(N);
2404 return SDOperand(N, 0);
2405}
2406
2407SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2408 SDOperand Ptr, const Value *SV, int SVOffset,
2409 bool isVolatile, unsigned Alignment) {
2410 MVT::ValueType VT = Val.getValueType();
2411
2412 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2413 const Type *Ty = 0;
2414 if (VT != MVT::iPTR) {
2415 Ty = MVT::getTypeForValueType(VT);
2416 } else if (SV) {
2417 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2418 assert(PT && "Value for store must be a pointer");
2419 Ty = PT->getElementType();
2420 }
2421 assert(Ty && "Could not get type information for store");
2422 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2423 }
2424 SDVTList VTs = getVTList(MVT::Other);
2425 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2426 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2427 FoldingSetNodeID ID;
2428 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2429 ID.AddInteger(ISD::UNINDEXED);
2430 ID.AddInteger(false);
Chris Lattner4a22a672007-09-13 06:09:48 +00002431 ID.AddInteger((unsigned int)VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002432 ID.AddInteger(Alignment);
2433 ID.AddInteger(isVolatile);
2434 void *IP = 0;
2435 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2436 return SDOperand(E, 0);
2437 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2438 VT, SV, SVOffset, Alignment, isVolatile);
2439 CSEMap.InsertNode(N, IP);
2440 AllNodes.push_back(N);
2441 return SDOperand(N, 0);
2442}
2443
2444SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2445 SDOperand Ptr, const Value *SV,
2446 int SVOffset, MVT::ValueType SVT,
2447 bool isVolatile, unsigned Alignment) {
2448 MVT::ValueType VT = Val.getValueType();
Duncan Sands06fcf652007-10-30 12:40:58 +00002449
2450 if (VT == SVT)
2451 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452
Duncan Sandsa9810f32007-10-16 09:56:48 +00002453 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2454 "Not a truncation?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002455 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2456 "Can't do FP-INT conversion!");
2457
2458 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2459 const Type *Ty = 0;
2460 if (VT != MVT::iPTR) {
2461 Ty = MVT::getTypeForValueType(VT);
2462 } else if (SV) {
2463 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2464 assert(PT && "Value for store must be a pointer");
2465 Ty = PT->getElementType();
2466 }
2467 assert(Ty && "Could not get type information for store");
2468 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2469 }
2470 SDVTList VTs = getVTList(MVT::Other);
2471 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2472 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2473 FoldingSetNodeID ID;
2474 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2475 ID.AddInteger(ISD::UNINDEXED);
Duncan Sands06fcf652007-10-30 12:40:58 +00002476 ID.AddInteger(1);
Chris Lattner4a22a672007-09-13 06:09:48 +00002477 ID.AddInteger((unsigned int)SVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002478 ID.AddInteger(Alignment);
2479 ID.AddInteger(isVolatile);
2480 void *IP = 0;
2481 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2482 return SDOperand(E, 0);
Duncan Sands06fcf652007-10-30 12:40:58 +00002483 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 SVT, SV, SVOffset, Alignment, isVolatile);
2485 CSEMap.InsertNode(N, IP);
2486 AllNodes.push_back(N);
2487 return SDOperand(N, 0);
2488}
2489
2490SDOperand
2491SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2492 SDOperand Offset, ISD::MemIndexedMode AM) {
2493 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2494 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2495 "Store is already a indexed store!");
2496 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2497 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2498 FoldingSetNodeID ID;
2499 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2500 ID.AddInteger(AM);
2501 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner4a22a672007-09-13 06:09:48 +00002502 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 ID.AddInteger(ST->getAlignment());
2504 ID.AddInteger(ST->isVolatile());
2505 void *IP = 0;
2506 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2507 return SDOperand(E, 0);
2508 SDNode *N = new StoreSDNode(Ops, VTs, AM,
2509 ST->isTruncatingStore(), ST->getStoredVT(),
2510 ST->getSrcValue(), ST->getSrcValueOffset(),
2511 ST->getAlignment(), ST->isVolatile());
2512 CSEMap.InsertNode(N, IP);
2513 AllNodes.push_back(N);
2514 return SDOperand(N, 0);
2515}
2516
2517SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2518 SDOperand Chain, SDOperand Ptr,
2519 SDOperand SV) {
2520 SDOperand Ops[] = { Chain, Ptr, SV };
2521 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2522}
2523
2524SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2525 const SDOperand *Ops, unsigned NumOps) {
2526 switch (NumOps) {
2527 case 0: return getNode(Opcode, VT);
2528 case 1: return getNode(Opcode, VT, Ops[0]);
2529 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2530 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2531 default: break;
2532 }
2533
2534 switch (Opcode) {
2535 default: break;
2536 case ISD::SELECT_CC: {
2537 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2538 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2539 "LHS and RHS of condition must have same type!");
2540 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2541 "True and False arms of SelectCC must have same type!");
2542 assert(Ops[2].getValueType() == VT &&
2543 "select_cc node must be of same type as true and false value!");
2544 break;
2545 }
2546 case ISD::BR_CC: {
2547 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2548 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2549 "LHS/RHS of comparison should match types!");
2550 break;
2551 }
2552 }
2553
2554 // Memoize nodes.
2555 SDNode *N;
2556 SDVTList VTs = getVTList(VT);
2557 if (VT != MVT::Flag) {
2558 FoldingSetNodeID ID;
2559 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2560 void *IP = 0;
2561 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2562 return SDOperand(E, 0);
2563 N = new SDNode(Opcode, VTs, Ops, NumOps);
2564 CSEMap.InsertNode(N, IP);
2565 } else {
2566 N = new SDNode(Opcode, VTs, Ops, NumOps);
2567 }
2568 AllNodes.push_back(N);
2569 return SDOperand(N, 0);
2570}
2571
2572SDOperand SelectionDAG::getNode(unsigned Opcode,
2573 std::vector<MVT::ValueType> &ResultTys,
2574 const SDOperand *Ops, unsigned NumOps) {
2575 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2576 Ops, NumOps);
2577}
2578
2579SDOperand SelectionDAG::getNode(unsigned Opcode,
2580 const MVT::ValueType *VTs, unsigned NumVTs,
2581 const SDOperand *Ops, unsigned NumOps) {
2582 if (NumVTs == 1)
2583 return getNode(Opcode, VTs[0], Ops, NumOps);
2584 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2585}
2586
2587SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2588 const SDOperand *Ops, unsigned NumOps) {
2589 if (VTList.NumVTs == 1)
2590 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2591
2592 switch (Opcode) {
2593 // FIXME: figure out how to safely handle things like
2594 // int foo(int x) { return 1 << (x & 255); }
2595 // int bar() { return foo(256); }
2596#if 0
2597 case ISD::SRA_PARTS:
2598 case ISD::SRL_PARTS:
2599 case ISD::SHL_PARTS:
2600 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2601 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2602 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2603 else if (N3.getOpcode() == ISD::AND)
2604 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2605 // If the and is only masking out bits that cannot effect the shift,
2606 // eliminate the and.
2607 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2608 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2609 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2610 }
2611 break;
2612#endif
2613 }
2614
2615 // Memoize the node unless it returns a flag.
2616 SDNode *N;
2617 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2618 FoldingSetNodeID ID;
2619 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2620 void *IP = 0;
2621 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2622 return SDOperand(E, 0);
2623 if (NumOps == 1)
2624 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2625 else if (NumOps == 2)
2626 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2627 else if (NumOps == 3)
2628 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2629 else
2630 N = new SDNode(Opcode, VTList, Ops, NumOps);
2631 CSEMap.InsertNode(N, IP);
2632 } else {
2633 if (NumOps == 1)
2634 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2635 else if (NumOps == 2)
2636 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2637 else if (NumOps == 3)
2638 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2639 else
2640 N = new SDNode(Opcode, VTList, Ops, NumOps);
2641 }
2642 AllNodes.push_back(N);
2643 return SDOperand(N, 0);
2644}
2645
Dan Gohman798d1272007-10-08 15:49:58 +00002646SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2647 return getNode(Opcode, VTList, 0, 0);
2648}
2649
2650SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2651 SDOperand N1) {
2652 SDOperand Ops[] = { N1 };
2653 return getNode(Opcode, VTList, Ops, 1);
2654}
2655
2656SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2657 SDOperand N1, SDOperand N2) {
2658 SDOperand Ops[] = { N1, N2 };
2659 return getNode(Opcode, VTList, Ops, 2);
2660}
2661
2662SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2663 SDOperand N1, SDOperand N2, SDOperand N3) {
2664 SDOperand Ops[] = { N1, N2, N3 };
2665 return getNode(Opcode, VTList, Ops, 3);
2666}
2667
2668SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2669 SDOperand N1, SDOperand N2, SDOperand N3,
2670 SDOperand N4) {
2671 SDOperand Ops[] = { N1, N2, N3, N4 };
2672 return getNode(Opcode, VTList, Ops, 4);
2673}
2674
2675SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2676 SDOperand N1, SDOperand N2, SDOperand N3,
2677 SDOperand N4, SDOperand N5) {
2678 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2679 return getNode(Opcode, VTList, Ops, 5);
2680}
2681
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002682SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00002683 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002684}
2685
2686SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2687 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2688 E = VTList.end(); I != E; ++I) {
2689 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2690 return makeVTList(&(*I)[0], 2);
2691 }
2692 std::vector<MVT::ValueType> V;
2693 V.push_back(VT1);
2694 V.push_back(VT2);
2695 VTList.push_front(V);
2696 return makeVTList(&(*VTList.begin())[0], 2);
2697}
2698SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2699 MVT::ValueType VT3) {
2700 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2701 E = VTList.end(); I != E; ++I) {
2702 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2703 (*I)[2] == VT3)
2704 return makeVTList(&(*I)[0], 3);
2705 }
2706 std::vector<MVT::ValueType> V;
2707 V.push_back(VT1);
2708 V.push_back(VT2);
2709 V.push_back(VT3);
2710 VTList.push_front(V);
2711 return makeVTList(&(*VTList.begin())[0], 3);
2712}
2713
2714SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2715 switch (NumVTs) {
2716 case 0: assert(0 && "Cannot have nodes without results!");
2717 case 1: return getVTList(VTs[0]);
2718 case 2: return getVTList(VTs[0], VTs[1]);
2719 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2720 default: break;
2721 }
2722
2723 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2724 E = VTList.end(); I != E; ++I) {
2725 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2726
2727 bool NoMatch = false;
2728 for (unsigned i = 2; i != NumVTs; ++i)
2729 if (VTs[i] != (*I)[i]) {
2730 NoMatch = true;
2731 break;
2732 }
2733 if (!NoMatch)
2734 return makeVTList(&*I->begin(), NumVTs);
2735 }
2736
2737 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2738 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2739}
2740
2741
2742/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2743/// specified operands. If the resultant node already exists in the DAG,
2744/// this does not modify the specified node, instead it returns the node that
2745/// already exists. If the resultant node does not exist in the DAG, the
2746/// input node is returned. As a degenerate case, if you specify the same
2747/// input operands as the node already has, the input node is returned.
2748SDOperand SelectionDAG::
2749UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2750 SDNode *N = InN.Val;
2751 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2752
2753 // Check to see if there is no change.
2754 if (Op == N->getOperand(0)) return InN;
2755
2756 // See if the modified node already exists.
2757 void *InsertPos = 0;
2758 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2759 return SDOperand(Existing, InN.ResNo);
2760
2761 // Nope it doesn't. Remove the node from it's current place in the maps.
2762 if (InsertPos)
2763 RemoveNodeFromCSEMaps(N);
2764
2765 // Now we update the operands.
2766 N->OperandList[0].Val->removeUser(N);
2767 Op.Val->addUser(N);
2768 N->OperandList[0] = Op;
2769
2770 // If this gets put into a CSE map, add it.
2771 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2772 return InN;
2773}
2774
2775SDOperand SelectionDAG::
2776UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2777 SDNode *N = InN.Val;
2778 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2779
2780 // Check to see if there is no change.
2781 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2782 return InN; // No operands changed, just return the input node.
2783
2784 // See if the modified node already exists.
2785 void *InsertPos = 0;
2786 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2787 return SDOperand(Existing, InN.ResNo);
2788
2789 // Nope it doesn't. Remove the node from it's current place in the maps.
2790 if (InsertPos)
2791 RemoveNodeFromCSEMaps(N);
2792
2793 // Now we update the operands.
2794 if (N->OperandList[0] != Op1) {
2795 N->OperandList[0].Val->removeUser(N);
2796 Op1.Val->addUser(N);
2797 N->OperandList[0] = Op1;
2798 }
2799 if (N->OperandList[1] != Op2) {
2800 N->OperandList[1].Val->removeUser(N);
2801 Op2.Val->addUser(N);
2802 N->OperandList[1] = Op2;
2803 }
2804
2805 // If this gets put into a CSE map, add it.
2806 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2807 return InN;
2808}
2809
2810SDOperand SelectionDAG::
2811UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2812 SDOperand Ops[] = { Op1, Op2, Op3 };
2813 return UpdateNodeOperands(N, Ops, 3);
2814}
2815
2816SDOperand SelectionDAG::
2817UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2818 SDOperand Op3, SDOperand Op4) {
2819 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2820 return UpdateNodeOperands(N, Ops, 4);
2821}
2822
2823SDOperand SelectionDAG::
2824UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2825 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2826 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2827 return UpdateNodeOperands(N, Ops, 5);
2828}
2829
2830
2831SDOperand SelectionDAG::
2832UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2833 SDNode *N = InN.Val;
2834 assert(N->getNumOperands() == NumOps &&
2835 "Update with wrong number of operands");
2836
2837 // Check to see if there is no change.
2838 bool AnyChange = false;
2839 for (unsigned i = 0; i != NumOps; ++i) {
2840 if (Ops[i] != N->getOperand(i)) {
2841 AnyChange = true;
2842 break;
2843 }
2844 }
2845
2846 // No operands changed, just return the input node.
2847 if (!AnyChange) return InN;
2848
2849 // See if the modified node already exists.
2850 void *InsertPos = 0;
2851 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2852 return SDOperand(Existing, InN.ResNo);
2853
2854 // Nope it doesn't. Remove the node from it's current place in the maps.
2855 if (InsertPos)
2856 RemoveNodeFromCSEMaps(N);
2857
2858 // Now we update the operands.
2859 for (unsigned i = 0; i != NumOps; ++i) {
2860 if (N->OperandList[i] != Ops[i]) {
2861 N->OperandList[i].Val->removeUser(N);
2862 Ops[i].Val->addUser(N);
2863 N->OperandList[i] = Ops[i];
2864 }
2865 }
2866
2867 // If this gets put into a CSE map, add it.
2868 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2869 return InN;
2870}
2871
2872
2873/// MorphNodeTo - This frees the operands of the current node, resets the
2874/// opcode, types, and operands to the specified value. This should only be
2875/// used by the SelectionDAG class.
2876void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2877 const SDOperand *Ops, unsigned NumOps) {
2878 NodeType = Opc;
2879 ValueList = L.VTs;
2880 NumValues = L.NumVTs;
2881
2882 // Clear the operands list, updating used nodes to remove this from their
2883 // use list.
2884 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2885 I->Val->removeUser(this);
2886
2887 // If NumOps is larger than the # of operands we currently have, reallocate
2888 // the operand list.
2889 if (NumOps > NumOperands) {
2890 if (OperandsNeedDelete)
2891 delete [] OperandList;
2892 OperandList = new SDOperand[NumOps];
2893 OperandsNeedDelete = true;
2894 }
2895
2896 // Assign the new operands.
2897 NumOperands = NumOps;
2898
2899 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2900 OperandList[i] = Ops[i];
2901 SDNode *N = OperandList[i].Val;
2902 N->Uses.push_back(this);
2903 }
2904}
2905
2906/// SelectNodeTo - These are used for target selectors to *mutate* the
2907/// specified node to have the specified return type, Target opcode, and
2908/// operands. Note that target opcodes are stored as
2909/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2910///
2911/// Note that SelectNodeTo returns the resultant node. If there is already a
2912/// node of the specified opcode and operands, it returns that node instead of
2913/// the current one.
2914SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2915 MVT::ValueType VT) {
2916 SDVTList VTs = getVTList(VT);
2917 FoldingSetNodeID ID;
2918 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2919 void *IP = 0;
2920 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2921 return ON;
2922
2923 RemoveNodeFromCSEMaps(N);
2924
2925 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2926
2927 CSEMap.InsertNode(N, IP);
2928 return N;
2929}
2930
2931SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2932 MVT::ValueType VT, SDOperand Op1) {
2933 // If an identical node already exists, use it.
2934 SDVTList VTs = getVTList(VT);
2935 SDOperand Ops[] = { Op1 };
2936
2937 FoldingSetNodeID ID;
2938 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2939 void *IP = 0;
2940 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2941 return ON;
2942
2943 RemoveNodeFromCSEMaps(N);
2944 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
2945 CSEMap.InsertNode(N, IP);
2946 return N;
2947}
2948
2949SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2950 MVT::ValueType VT, SDOperand Op1,
2951 SDOperand Op2) {
2952 // If an identical node already exists, use it.
2953 SDVTList VTs = getVTList(VT);
2954 SDOperand Ops[] = { Op1, Op2 };
2955
2956 FoldingSetNodeID ID;
2957 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2958 void *IP = 0;
2959 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2960 return ON;
2961
2962 RemoveNodeFromCSEMaps(N);
2963
2964 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
2965
2966 CSEMap.InsertNode(N, IP); // Memoize the new node.
2967 return N;
2968}
2969
2970SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2971 MVT::ValueType VT, SDOperand Op1,
2972 SDOperand Op2, SDOperand Op3) {
2973 // If an identical node already exists, use it.
2974 SDVTList VTs = getVTList(VT);
2975 SDOperand Ops[] = { Op1, Op2, Op3 };
2976 FoldingSetNodeID ID;
2977 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2978 void *IP = 0;
2979 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2980 return ON;
2981
2982 RemoveNodeFromCSEMaps(N);
2983
2984 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
2985
2986 CSEMap.InsertNode(N, IP); // Memoize the new node.
2987 return N;
2988}
2989
2990SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2991 MVT::ValueType VT, const SDOperand *Ops,
2992 unsigned NumOps) {
2993 // If an identical node already exists, use it.
2994 SDVTList VTs = getVTList(VT);
2995 FoldingSetNodeID ID;
2996 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
2997 void *IP = 0;
2998 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2999 return ON;
3000
3001 RemoveNodeFromCSEMaps(N);
3002 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3003
3004 CSEMap.InsertNode(N, IP); // Memoize the new node.
3005 return N;
3006}
3007
3008SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3009 MVT::ValueType VT1, MVT::ValueType VT2,
3010 SDOperand Op1, SDOperand Op2) {
3011 SDVTList VTs = getVTList(VT1, VT2);
3012 FoldingSetNodeID ID;
3013 SDOperand Ops[] = { Op1, Op2 };
3014 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3015 void *IP = 0;
3016 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3017 return ON;
3018
3019 RemoveNodeFromCSEMaps(N);
3020 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3021 CSEMap.InsertNode(N, IP); // Memoize the new node.
3022 return N;
3023}
3024
3025SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3026 MVT::ValueType VT1, MVT::ValueType VT2,
3027 SDOperand Op1, SDOperand Op2,
3028 SDOperand Op3) {
3029 // If an identical node already exists, use it.
3030 SDVTList VTs = getVTList(VT1, VT2);
3031 SDOperand Ops[] = { Op1, Op2, Op3 };
3032 FoldingSetNodeID ID;
3033 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3034 void *IP = 0;
3035 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3036 return ON;
3037
3038 RemoveNodeFromCSEMaps(N);
3039
3040 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3041 CSEMap.InsertNode(N, IP); // Memoize the new node.
3042 return N;
3043}
3044
3045
3046/// getTargetNode - These are used for target selectors to create a new node
3047/// with specified return type(s), target opcode, and operands.
3048///
3049/// Note that getTargetNode returns the resultant node. If there is already a
3050/// node of the specified opcode and operands, it returns that node instead of
3051/// the current one.
3052SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3053 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3054}
3055SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3056 SDOperand Op1) {
3057 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3058}
3059SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3060 SDOperand Op1, SDOperand Op2) {
3061 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3062}
3063SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3064 SDOperand Op1, SDOperand Op2,
3065 SDOperand Op3) {
3066 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3067}
3068SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3069 const SDOperand *Ops, unsigned NumOps) {
3070 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3071}
3072SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003073 MVT::ValueType VT2) {
3074 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3075 SDOperand Op;
3076 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3077}
3078SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003079 MVT::ValueType VT2, SDOperand Op1) {
3080 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3081 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3082}
3083SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3084 MVT::ValueType VT2, SDOperand Op1,
3085 SDOperand Op2) {
3086 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3087 SDOperand Ops[] = { Op1, Op2 };
3088 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3089}
3090SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3091 MVT::ValueType VT2, SDOperand Op1,
3092 SDOperand Op2, SDOperand Op3) {
3093 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3094 SDOperand Ops[] = { Op1, Op2, Op3 };
3095 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3096}
3097SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3098 MVT::ValueType VT2,
3099 const SDOperand *Ops, unsigned NumOps) {
3100 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3101 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3102}
3103SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3104 MVT::ValueType VT2, MVT::ValueType VT3,
3105 SDOperand Op1, SDOperand Op2) {
3106 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3107 SDOperand Ops[] = { Op1, Op2 };
3108 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3109}
3110SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3111 MVT::ValueType VT2, MVT::ValueType VT3,
3112 SDOperand Op1, SDOperand Op2,
3113 SDOperand Op3) {
3114 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3115 SDOperand Ops[] = { Op1, Op2, Op3 };
3116 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3117}
3118SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3119 MVT::ValueType VT2, MVT::ValueType VT3,
3120 const SDOperand *Ops, unsigned NumOps) {
3121 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3122 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3123}
Evan Chenge1d067e2007-09-12 23:39:49 +00003124SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3125 MVT::ValueType VT2, MVT::ValueType VT3,
3126 MVT::ValueType VT4,
3127 const SDOperand *Ops, unsigned NumOps) {
3128 std::vector<MVT::ValueType> VTList;
3129 VTList.push_back(VT1);
3130 VTList.push_back(VT2);
3131 VTList.push_back(VT3);
3132 VTList.push_back(VT4);
3133 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3134 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3135}
Evan Chenge3940912007-10-05 01:10:49 +00003136SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3137 std::vector<MVT::ValueType> &ResultTys,
3138 const SDOperand *Ops, unsigned NumOps) {
3139 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3140 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3141 Ops, NumOps).Val;
3142}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143
3144/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3145/// This can cause recursive merging of nodes in the DAG.
3146///
3147/// This version assumes From/To have a single result value.
3148///
3149void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3150 std::vector<SDNode*> *Deleted) {
3151 SDNode *From = FromN.Val, *To = ToN.Val;
3152 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3153 "Cannot replace with this method!");
3154 assert(From != To && "Cannot replace uses of with self");
3155
3156 while (!From->use_empty()) {
3157 // Process users until they are all gone.
3158 SDNode *U = *From->use_begin();
3159
3160 // This node is about to morph, remove its old self from the CSE maps.
3161 RemoveNodeFromCSEMaps(U);
3162
3163 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3164 I != E; ++I)
3165 if (I->Val == From) {
3166 From->removeUser(U);
3167 I->Val = To;
3168 To->addUser(U);
3169 }
3170
3171 // Now that we have modified U, add it back to the CSE maps. If it already
3172 // exists there, recursively merge the results together.
3173 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3174 ReplaceAllUsesWith(U, Existing, Deleted);
3175 // U is now dead.
3176 if (Deleted) Deleted->push_back(U);
3177 DeleteNodeNotInCSEMaps(U);
3178 }
3179 }
3180}
3181
3182/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3183/// This can cause recursive merging of nodes in the DAG.
3184///
3185/// This version assumes From/To have matching types and numbers of result
3186/// values.
3187///
3188void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3189 std::vector<SDNode*> *Deleted) {
3190 assert(From != To && "Cannot replace uses of with self");
3191 assert(From->getNumValues() == To->getNumValues() &&
3192 "Cannot use this version of ReplaceAllUsesWith!");
3193 if (From->getNumValues() == 1) { // If possible, use the faster version.
3194 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
3195 return;
3196 }
3197
3198 while (!From->use_empty()) {
3199 // Process users until they are all gone.
3200 SDNode *U = *From->use_begin();
3201
3202 // This node is about to morph, remove its old self from the CSE maps.
3203 RemoveNodeFromCSEMaps(U);
3204
3205 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3206 I != E; ++I)
3207 if (I->Val == From) {
3208 From->removeUser(U);
3209 I->Val = To;
3210 To->addUser(U);
3211 }
3212
3213 // Now that we have modified U, add it back to the CSE maps. If it already
3214 // exists there, recursively merge the results together.
3215 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3216 ReplaceAllUsesWith(U, Existing, Deleted);
3217 // U is now dead.
3218 if (Deleted) Deleted->push_back(U);
3219 DeleteNodeNotInCSEMaps(U);
3220 }
3221 }
3222}
3223
3224/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3225/// This can cause recursive merging of nodes in the DAG.
3226///
3227/// This version can replace From with any result values. To must match the
3228/// number and types of values returned by From.
3229void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3230 const SDOperand *To,
3231 std::vector<SDNode*> *Deleted) {
3232 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
3233 // Degenerate case handled above.
3234 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
3235 return;
3236 }
3237
3238 while (!From->use_empty()) {
3239 // Process users until they are all gone.
3240 SDNode *U = *From->use_begin();
3241
3242 // This node is about to morph, remove its old self from the CSE maps.
3243 RemoveNodeFromCSEMaps(U);
3244
3245 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3246 I != E; ++I)
3247 if (I->Val == From) {
3248 const SDOperand &ToOp = To[I->ResNo];
3249 From->removeUser(U);
3250 *I = ToOp;
3251 ToOp.Val->addUser(U);
3252 }
3253
3254 // Now that we have modified U, add it back to the CSE maps. If it already
3255 // exists there, recursively merge the results together.
3256 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3257 ReplaceAllUsesWith(U, Existing, Deleted);
3258 // U is now dead.
3259 if (Deleted) Deleted->push_back(U);
3260 DeleteNodeNotInCSEMaps(U);
3261 }
3262 }
3263}
3264
3265/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3266/// uses of other values produced by From.Val alone. The Deleted vector is
3267/// handled the same was as for ReplaceAllUsesWith.
3268void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner8a258202007-10-15 06:10:22 +00003269 std::vector<SDNode*> *Deleted) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 assert(From != To && "Cannot replace a value with itself");
3271 // Handle the simple, trivial, case efficiently.
3272 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner8a258202007-10-15 06:10:22 +00003273 ReplaceAllUsesWith(From, To, Deleted);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 return;
3275 }
3276
3277 // Get all of the users of From.Val. We want these in a nice,
3278 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3279 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3280
Chris Lattner8a258202007-10-15 06:10:22 +00003281 std::vector<SDNode*> LocalDeletionVector;
3282
3283 // Pick a deletion vector to use. If the user specified one, use theirs,
3284 // otherwise use a local one.
3285 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 while (!Users.empty()) {
3287 // We know that this user uses some value of From. If it is the right
3288 // value, update it.
3289 SDNode *User = Users.back();
3290 Users.pop_back();
3291
Chris Lattner8a258202007-10-15 06:10:22 +00003292 // Scan for an operand that matches From.
3293 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3294 for (; Op != E; ++Op)
3295 if (*Op == From) break;
3296
3297 // If there are no matches, the user must use some other result of From.
3298 if (Op == E) continue;
3299
3300 // Okay, we know this user needs to be updated. Remove its old self
3301 // from the CSE maps.
3302 RemoveNodeFromCSEMaps(User);
3303
3304 // Update all operands that match "From".
3305 for (; Op != E; ++Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 if (*Op == From) {
Chris Lattner8a258202007-10-15 06:10:22 +00003307 From.Val->removeUser(User);
3308 *Op = To;
3309 To.Val->addUser(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310 }
3311 }
Chris Lattner8a258202007-10-15 06:10:22 +00003312
3313 // Now that we have modified User, add it back to the CSE maps. If it
3314 // already exists there, recursively merge the results together.
3315 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3316 if (!Existing) continue; // Continue on to next user.
3317
3318 // If there was already an existing matching node, use ReplaceAllUsesWith
3319 // to replace the dead one with the existing one. However, this can cause
3320 // recursive merging of other unrelated nodes down the line. The merging
3321 // can cause deletion of nodes that used the old value. In this case,
3322 // we have to be certain to remove them from the Users set.
3323 unsigned NumDeleted = DeleteVector->size();
3324 ReplaceAllUsesWith(User, Existing, DeleteVector);
3325
3326 // User is now dead.
3327 DeleteVector->push_back(User);
3328 DeleteNodeNotInCSEMaps(User);
3329
3330 // We have to be careful here, because ReplaceAllUsesWith could have
3331 // deleted a user of From, which means there may be dangling pointers
3332 // in the "Users" setvector. Scan over the deleted node pointers and
3333 // remove them from the setvector.
3334 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3335 Users.remove((*DeleteVector)[i]);
3336
3337 // If the user doesn't need the set of deleted elements, don't retain them
3338 // to the next loop iteration.
3339 if (Deleted == 0)
3340 LocalDeletionVector.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003341 }
3342}
3343
3344
3345/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3346/// their allnodes order. It returns the maximum id.
3347unsigned SelectionDAG::AssignNodeIds() {
3348 unsigned Id = 0;
3349 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3350 SDNode *N = I;
3351 N->setNodeId(Id++);
3352 }
3353 return Id;
3354}
3355
3356/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3357/// based on their topological order. It returns the maximum id and a vector
3358/// of the SDNodes* in assigned order by reference.
3359unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3360 unsigned DAGSize = AllNodes.size();
3361 std::vector<unsigned> InDegree(DAGSize);
3362 std::vector<SDNode*> Sources;
3363
3364 // Use a two pass approach to avoid using a std::map which is slow.
3365 unsigned Id = 0;
3366 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3367 SDNode *N = I;
3368 N->setNodeId(Id++);
3369 unsigned Degree = N->use_size();
3370 InDegree[N->getNodeId()] = Degree;
3371 if (Degree == 0)
3372 Sources.push_back(N);
3373 }
3374
3375 TopOrder.clear();
3376 while (!Sources.empty()) {
3377 SDNode *N = Sources.back();
3378 Sources.pop_back();
3379 TopOrder.push_back(N);
3380 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3381 SDNode *P = I->Val;
3382 unsigned Degree = --InDegree[P->getNodeId()];
3383 if (Degree == 0)
3384 Sources.push_back(P);
3385 }
3386 }
3387
3388 // Second pass, assign the actual topological order as node ids.
3389 Id = 0;
3390 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3391 TI != TE; ++TI)
3392 (*TI)->setNodeId(Id++);
3393
3394 return Id;
3395}
3396
3397
3398
3399//===----------------------------------------------------------------------===//
3400// SDNode Class
3401//===----------------------------------------------------------------------===//
3402
3403// Out-of-line virtual method to give class a home.
3404void SDNode::ANCHOR() {}
3405void UnarySDNode::ANCHOR() {}
3406void BinarySDNode::ANCHOR() {}
3407void TernarySDNode::ANCHOR() {}
3408void HandleSDNode::ANCHOR() {}
3409void StringSDNode::ANCHOR() {}
3410void ConstantSDNode::ANCHOR() {}
3411void ConstantFPSDNode::ANCHOR() {}
3412void GlobalAddressSDNode::ANCHOR() {}
3413void FrameIndexSDNode::ANCHOR() {}
3414void JumpTableSDNode::ANCHOR() {}
3415void ConstantPoolSDNode::ANCHOR() {}
3416void BasicBlockSDNode::ANCHOR() {}
3417void SrcValueSDNode::ANCHOR() {}
3418void RegisterSDNode::ANCHOR() {}
3419void ExternalSymbolSDNode::ANCHOR() {}
3420void CondCodeSDNode::ANCHOR() {}
3421void VTSDNode::ANCHOR() {}
3422void LoadSDNode::ANCHOR() {}
3423void StoreSDNode::ANCHOR() {}
3424
3425HandleSDNode::~HandleSDNode() {
3426 SDVTList VTs = { 0, 0 };
3427 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3428}
3429
3430GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3431 MVT::ValueType VT, int o)
3432 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman53491e92007-07-23 20:24:29 +00003433 cast<GlobalVariable>(GA)->isThreadLocal() ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003434 // Thread Local
3435 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3436 // Non Thread Local
3437 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3438 getSDVTList(VT)), Offset(o) {
3439 TheGlobal = const_cast<GlobalValue*>(GA);
3440}
3441
3442/// Profile - Gather unique data for the node.
3443///
3444void SDNode::Profile(FoldingSetNodeID &ID) {
3445 AddNodeIDNode(ID, this);
3446}
3447
3448/// getValueTypeList - Return a pointer to the specified value type.
3449///
3450MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00003451 if (MVT::isExtendedVT(VT)) {
3452 static std::set<MVT::ValueType> EVTs;
3453 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3454 } else {
3455 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3456 VTs[VT] = VT;
3457 return &VTs[VT];
3458 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459}
Duncan Sandsa9810f32007-10-16 09:56:48 +00003460
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003461/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3462/// indicated value. This method ignores uses of other values defined by this
3463/// operation.
3464bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3465 assert(Value < getNumValues() && "Bad value!");
3466
3467 // If there is only one value, this is easy.
3468 if (getNumValues() == 1)
3469 return use_size() == NUses;
Evan Cheng0af04f72007-08-02 05:29:38 +00003470 if (use_size() < NUses) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003471
3472 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3473
3474 SmallPtrSet<SDNode*, 32> UsersHandled;
3475
3476 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3477 SDNode *User = *UI;
3478 if (User->getNumOperands() == 1 ||
3479 UsersHandled.insert(User)) // First time we've seen this?
3480 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3481 if (User->getOperand(i) == TheValue) {
3482 if (NUses == 0)
3483 return false; // too many uses
3484 --NUses;
3485 }
3486 }
3487
3488 // Found exactly the right number of uses?
3489 return NUses == 0;
3490}
3491
3492
Evan Cheng0af04f72007-08-02 05:29:38 +00003493/// hasAnyUseOfValue - Return true if there are any use of the indicated
3494/// value. This method ignores uses of other values defined by this operation.
3495bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3496 assert(Value < getNumValues() && "Bad value!");
3497
3498 if (use_size() == 0) return false;
3499
3500 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3501
3502 SmallPtrSet<SDNode*, 32> UsersHandled;
3503
3504 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3505 SDNode *User = *UI;
3506 if (User->getNumOperands() == 1 ||
3507 UsersHandled.insert(User)) // First time we've seen this?
3508 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3509 if (User->getOperand(i) == TheValue) {
3510 return true;
3511 }
3512 }
3513
3514 return false;
3515}
3516
3517
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003518/// isOnlyUse - Return true if this node is the only use of N.
3519///
3520bool SDNode::isOnlyUse(SDNode *N) const {
3521 bool Seen = false;
3522 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3523 SDNode *User = *I;
3524 if (User == this)
3525 Seen = true;
3526 else
3527 return false;
3528 }
3529
3530 return Seen;
3531}
3532
3533/// isOperand - Return true if this node is an operand of N.
3534///
3535bool SDOperand::isOperand(SDNode *N) const {
3536 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3537 if (*this == N->getOperand(i))
3538 return true;
3539 return false;
3540}
3541
3542bool SDNode::isOperand(SDNode *N) const {
3543 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3544 if (this == N->OperandList[i].Val)
3545 return true;
3546 return false;
3547}
3548
Chris Lattner10d94f92008-01-16 05:49:24 +00003549/// reachesChainWithoutSideEffects - Return true if this operand (which must
3550/// be a chain) reaches the specified operand without crossing any
3551/// side-effecting instructions. In practice, this looks through token
3552/// factors and non-volatile loads. In order to remain efficient, this only
3553/// looks a couple of nodes in, it does not do an exhaustive search.
3554bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3555 unsigned Depth) const {
3556 if (*this == Dest) return true;
3557
3558 // Don't search too deeply, we just want to be able to see through
3559 // TokenFactor's etc.
3560 if (Depth == 0) return false;
3561
3562 // If this is a token factor, all inputs to the TF happen in parallel. If any
3563 // of the operands of the TF reach dest, then we can do the xform.
3564 if (getOpcode() == ISD::TokenFactor) {
3565 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3566 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3567 return true;
3568 return false;
3569 }
3570
3571 // Loads don't have side effects, look through them.
3572 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3573 if (!Ld->isVolatile())
3574 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3575 }
3576 return false;
3577}
3578
3579
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003580static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3581 SmallPtrSet<SDNode *, 32> &Visited) {
3582 if (found || !Visited.insert(N))
3583 return;
3584
3585 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3586 SDNode *Op = N->getOperand(i).Val;
3587 if (Op == P) {
3588 found = true;
3589 return;
3590 }
3591 findPredecessor(Op, P, found, Visited);
3592 }
3593}
3594
3595/// isPredecessor - Return true if this node is a predecessor of N. This node
3596/// is either an operand of N or it can be reached by recursively traversing
3597/// up the operands.
3598/// NOTE: this is an expensive method. Use it carefully.
3599bool SDNode::isPredecessor(SDNode *N) const {
3600 SmallPtrSet<SDNode *, 32> Visited;
3601 bool found = false;
3602 findPredecessor(N, this, found, Visited);
3603 return found;
3604}
3605
3606uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3607 assert(Num < NumOperands && "Invalid child # of SDNode!");
3608 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3609}
3610
3611std::string SDNode::getOperationName(const SelectionDAG *G) const {
3612 switch (getOpcode()) {
3613 default:
3614 if (getOpcode() < ISD::BUILTIN_OP_END)
3615 return "<<Unknown DAG Node>>";
3616 else {
3617 if (G) {
3618 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3619 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner0c2a4f32008-01-07 03:13:06 +00003620 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003621
3622 TargetLowering &TLI = G->getTargetLoweringInfo();
3623 const char *Name =
3624 TLI.getTargetNodeName(getOpcode());
3625 if (Name) return Name;
3626 }
3627
3628 return "<<Unknown Target Node>>";
3629 }
3630
3631 case ISD::PCMARKER: return "PCMarker";
3632 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3633 case ISD::SRCVALUE: return "SrcValue";
3634 case ISD::EntryToken: return "EntryToken";
3635 case ISD::TokenFactor: return "TokenFactor";
3636 case ISD::AssertSext: return "AssertSext";
3637 case ISD::AssertZext: return "AssertZext";
3638
3639 case ISD::STRING: return "String";
3640 case ISD::BasicBlock: return "BasicBlock";
3641 case ISD::VALUETYPE: return "ValueType";
3642 case ISD::Register: return "Register";
3643
3644 case ISD::Constant: return "Constant";
3645 case ISD::ConstantFP: return "ConstantFP";
3646 case ISD::GlobalAddress: return "GlobalAddress";
3647 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3648 case ISD::FrameIndex: return "FrameIndex";
3649 case ISD::JumpTable: return "JumpTable";
3650 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3651 case ISD::RETURNADDR: return "RETURNADDR";
3652 case ISD::FRAMEADDR: return "FRAMEADDR";
3653 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3654 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3655 case ISD::EHSELECTION: return "EHSELECTION";
3656 case ISD::EH_RETURN: return "EH_RETURN";
3657 case ISD::ConstantPool: return "ConstantPool";
3658 case ISD::ExternalSymbol: return "ExternalSymbol";
3659 case ISD::INTRINSIC_WO_CHAIN: {
3660 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3661 return Intrinsic::getName((Intrinsic::ID)IID);
3662 }
3663 case ISD::INTRINSIC_VOID:
3664 case ISD::INTRINSIC_W_CHAIN: {
3665 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3666 return Intrinsic::getName((Intrinsic::ID)IID);
3667 }
3668
3669 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3670 case ISD::TargetConstant: return "TargetConstant";
3671 case ISD::TargetConstantFP:return "TargetConstantFP";
3672 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3673 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3674 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3675 case ISD::TargetJumpTable: return "TargetJumpTable";
3676 case ISD::TargetConstantPool: return "TargetConstantPool";
3677 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3678
3679 case ISD::CopyToReg: return "CopyToReg";
3680 case ISD::CopyFromReg: return "CopyFromReg";
3681 case ISD::UNDEF: return "undef";
3682 case ISD::MERGE_VALUES: return "merge_values";
3683 case ISD::INLINEASM: return "inlineasm";
3684 case ISD::LABEL: return "label";
3685 case ISD::HANDLENODE: return "handlenode";
3686 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3687 case ISD::CALL: return "call";
3688
3689 // Unary operators
3690 case ISD::FABS: return "fabs";
3691 case ISD::FNEG: return "fneg";
3692 case ISD::FSQRT: return "fsqrt";
3693 case ISD::FSIN: return "fsin";
3694 case ISD::FCOS: return "fcos";
3695 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00003696 case ISD::FPOW: return "fpow";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003697
3698 // Binary operators
3699 case ISD::ADD: return "add";
3700 case ISD::SUB: return "sub";
3701 case ISD::MUL: return "mul";
3702 case ISD::MULHU: return "mulhu";
3703 case ISD::MULHS: return "mulhs";
3704 case ISD::SDIV: return "sdiv";
3705 case ISD::UDIV: return "udiv";
3706 case ISD::SREM: return "srem";
3707 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00003708 case ISD::SMUL_LOHI: return "smul_lohi";
3709 case ISD::UMUL_LOHI: return "umul_lohi";
3710 case ISD::SDIVREM: return "sdivrem";
3711 case ISD::UDIVREM: return "divrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003712 case ISD::AND: return "and";
3713 case ISD::OR: return "or";
3714 case ISD::XOR: return "xor";
3715 case ISD::SHL: return "shl";
3716 case ISD::SRA: return "sra";
3717 case ISD::SRL: return "srl";
3718 case ISD::ROTL: return "rotl";
3719 case ISD::ROTR: return "rotr";
3720 case ISD::FADD: return "fadd";
3721 case ISD::FSUB: return "fsub";
3722 case ISD::FMUL: return "fmul";
3723 case ISD::FDIV: return "fdiv";
3724 case ISD::FREM: return "frem";
3725 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00003726 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003727
3728 case ISD::SETCC: return "setcc";
3729 case ISD::SELECT: return "select";
3730 case ISD::SELECT_CC: return "select_cc";
3731 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3732 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3733 case ISD::CONCAT_VECTORS: return "concat_vectors";
3734 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3735 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3736 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3737 case ISD::CARRY_FALSE: return "carry_false";
3738 case ISD::ADDC: return "addc";
3739 case ISD::ADDE: return "adde";
3740 case ISD::SUBC: return "subc";
3741 case ISD::SUBE: return "sube";
3742 case ISD::SHL_PARTS: return "shl_parts";
3743 case ISD::SRA_PARTS: return "sra_parts";
3744 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lambb768c2e2007-07-26 07:34:40 +00003745
3746 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3747 case ISD::INSERT_SUBREG: return "insert_subreg";
3748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 // Conversion operators.
3750 case ISD::SIGN_EXTEND: return "sign_extend";
3751 case ISD::ZERO_EXTEND: return "zero_extend";
3752 case ISD::ANY_EXTEND: return "any_extend";
3753 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3754 case ISD::TRUNCATE: return "truncate";
3755 case ISD::FP_ROUND: return "fp_round";
Anton Korobeynikovc915e272007-11-15 23:25:33 +00003756 case ISD::FLT_ROUNDS: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003757 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3758 case ISD::FP_EXTEND: return "fp_extend";
3759
3760 case ISD::SINT_TO_FP: return "sint_to_fp";
3761 case ISD::UINT_TO_FP: return "uint_to_fp";
3762 case ISD::FP_TO_SINT: return "fp_to_sint";
3763 case ISD::FP_TO_UINT: return "fp_to_uint";
3764 case ISD::BIT_CONVERT: return "bit_convert";
3765
3766 // Control flow instructions
3767 case ISD::BR: return "br";
3768 case ISD::BRIND: return "brind";
3769 case ISD::BR_JT: return "br_jt";
3770 case ISD::BRCOND: return "brcond";
3771 case ISD::BR_CC: return "br_cc";
3772 case ISD::RET: return "ret";
3773 case ISD::CALLSEQ_START: return "callseq_start";
3774 case ISD::CALLSEQ_END: return "callseq_end";
3775
3776 // Other operators
3777 case ISD::LOAD: return "load";
3778 case ISD::STORE: return "store";
3779 case ISD::VAARG: return "vaarg";
3780 case ISD::VACOPY: return "vacopy";
3781 case ISD::VAEND: return "vaend";
3782 case ISD::VASTART: return "vastart";
3783 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3784 case ISD::EXTRACT_ELEMENT: return "extract_element";
3785 case ISD::BUILD_PAIR: return "build_pair";
3786 case ISD::STACKSAVE: return "stacksave";
3787 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003788 case ISD::TRAP: return "trap";
3789
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790 // Block memory operations.
3791 case ISD::MEMSET: return "memset";
3792 case ISD::MEMCPY: return "memcpy";
3793 case ISD::MEMMOVE: return "memmove";
3794
3795 // Bit manipulation
3796 case ISD::BSWAP: return "bswap";
3797 case ISD::CTPOP: return "ctpop";
3798 case ISD::CTTZ: return "cttz";
3799 case ISD::CTLZ: return "ctlz";
3800
3801 // Debug info
3802 case ISD::LOCATION: return "location";
3803 case ISD::DEBUG_LOC: return "debug_loc";
3804
Duncan Sands38947cd2007-07-27 12:58:54 +00003805 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00003806 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00003807
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003808 case ISD::CONDCODE:
3809 switch (cast<CondCodeSDNode>(this)->get()) {
3810 default: assert(0 && "Unknown setcc condition!");
3811 case ISD::SETOEQ: return "setoeq";
3812 case ISD::SETOGT: return "setogt";
3813 case ISD::SETOGE: return "setoge";
3814 case ISD::SETOLT: return "setolt";
3815 case ISD::SETOLE: return "setole";
3816 case ISD::SETONE: return "setone";
3817
3818 case ISD::SETO: return "seto";
3819 case ISD::SETUO: return "setuo";
3820 case ISD::SETUEQ: return "setue";
3821 case ISD::SETUGT: return "setugt";
3822 case ISD::SETUGE: return "setuge";
3823 case ISD::SETULT: return "setult";
3824 case ISD::SETULE: return "setule";
3825 case ISD::SETUNE: return "setune";
3826
3827 case ISD::SETEQ: return "seteq";
3828 case ISD::SETGT: return "setgt";
3829 case ISD::SETGE: return "setge";
3830 case ISD::SETLT: return "setlt";
3831 case ISD::SETLE: return "setle";
3832 case ISD::SETNE: return "setne";
3833 }
3834 }
3835}
3836
3837const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
3838 switch (AM) {
3839 default:
3840 return "";
3841 case ISD::PRE_INC:
3842 return "<pre-inc>";
3843 case ISD::PRE_DEC:
3844 return "<pre-dec>";
3845 case ISD::POST_INC:
3846 return "<post-inc>";
3847 case ISD::POST_DEC:
3848 return "<post-dec>";
3849 }
3850}
3851
3852void SDNode::dump() const { dump(0); }
3853void SDNode::dump(const SelectionDAG *G) const {
3854 cerr << (void*)this << ": ";
3855
3856 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3857 if (i) cerr << ",";
3858 if (getValueType(i) == MVT::Other)
3859 cerr << "ch";
3860 else
3861 cerr << MVT::getValueTypeString(getValueType(i));
3862 }
3863 cerr << " = " << getOperationName(G);
3864
3865 cerr << " ";
3866 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3867 if (i) cerr << ", ";
3868 cerr << (void*)getOperand(i).Val;
3869 if (unsigned RN = getOperand(i).ResNo)
3870 cerr << ":" << RN;
3871 }
3872
Evan Chengaad43a02007-12-11 02:08:35 +00003873 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3874 SDNode *Mask = getOperand(2).Val;
3875 cerr << "<";
3876 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3877 if (i) cerr << ",";
3878 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3879 cerr << "u";
3880 else
3881 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3882 }
3883 cerr << ">";
3884 }
3885
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003886 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3887 cerr << "<" << CSDN->getValue() << ">";
3888 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00003889 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3890 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3891 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3892 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3893 else {
3894 cerr << "<APFloat(";
3895 CSDN->getValueAPF().convertToAPInt().dump();
3896 cerr << ")>";
3897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003898 } else if (const GlobalAddressSDNode *GADN =
3899 dyn_cast<GlobalAddressSDNode>(this)) {
3900 int offset = GADN->getOffset();
3901 cerr << "<";
3902 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
3903 if (offset > 0)
3904 cerr << " + " << offset;
3905 else
3906 cerr << " " << offset;
3907 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
3908 cerr << "<" << FIDN->getIndex() << ">";
3909 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
3910 cerr << "<" << JTDN->getIndex() << ">";
3911 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
3912 int offset = CP->getOffset();
3913 if (CP->isMachineConstantPoolEntry())
3914 cerr << "<" << *CP->getMachineCPVal() << ">";
3915 else
3916 cerr << "<" << *CP->getConstVal() << ">";
3917 if (offset > 0)
3918 cerr << " + " << offset;
3919 else
3920 cerr << " " << offset;
3921 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
3922 cerr << "<";
3923 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3924 if (LBB)
3925 cerr << LBB->getName() << " ";
3926 cerr << (const void*)BBDN->getBasicBlock() << ">";
3927 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
3928 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
3929 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3930 } else {
3931 cerr << " #" << R->getReg();
3932 }
3933 } else if (const ExternalSymbolSDNode *ES =
3934 dyn_cast<ExternalSymbolSDNode>(this)) {
3935 cerr << "'" << ES->getSymbol() << "'";
3936 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3937 if (M->getValue())
3938 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3939 else
3940 cerr << "<null:" << M->getOffset() << ">";
3941 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3942 cerr << ":" << MVT::getValueTypeString(N->getVT());
3943 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng034c4f82007-12-18 19:06:30 +00003944 const Value *SrcValue = LD->getSrcValue();
3945 int SrcOffset = LD->getSrcValueOffset();
3946 cerr << " <";
3947 if (SrcValue)
3948 cerr << SrcValue;
3949 else
3950 cerr << "null";
3951 cerr << ":" << SrcOffset << ">";
3952
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003953 bool doExt = true;
3954 switch (LD->getExtensionType()) {
3955 default: doExt = false; break;
3956 case ISD::EXTLOAD:
3957 cerr << " <anyext ";
3958 break;
3959 case ISD::SEXTLOAD:
3960 cerr << " <sext ";
3961 break;
3962 case ISD::ZEXTLOAD:
3963 cerr << " <zext ";
3964 break;
3965 }
3966 if (doExt)
3967 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
3968
3969 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00003970 if (*AM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003971 cerr << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00003972 if (LD->isVolatile())
3973 cerr << " <volatile>";
3974 cerr << " alignment=" << LD->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003975 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng7196a7b2007-12-18 07:02:08 +00003976 const Value *SrcValue = ST->getSrcValue();
3977 int SrcOffset = ST->getSrcValueOffset();
3978 cerr << " <";
3979 if (SrcValue)
3980 cerr << SrcValue;
3981 else
3982 cerr << "null";
3983 cerr << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00003984
3985 if (ST->isTruncatingStore())
3986 cerr << " <trunc "
3987 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
3988
3989 const char *AM = getIndexedModeName(ST->getAddressingMode());
3990 if (*AM)
3991 cerr << " " << AM;
3992 if (ST->isVolatile())
3993 cerr << " <volatile>";
3994 cerr << " alignment=" << ST->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003995 }
3996}
3997
3998static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
3999 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4000 if (N->getOperand(i).Val->hasOneUse())
4001 DumpNodes(N->getOperand(i).Val, indent+2, G);
4002 else
4003 cerr << "\n" << std::string(indent+2, ' ')
4004 << (void*)N->getOperand(i).Val << ": <multiple use>";
4005
4006
4007 cerr << "\n" << std::string(indent, ' ');
4008 N->dump(G);
4009}
4010
4011void SelectionDAG::dump() const {
4012 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
4013 std::vector<const SDNode*> Nodes;
4014 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4015 I != E; ++I)
4016 Nodes.push_back(I);
4017
4018 std::sort(Nodes.begin(), Nodes.end());
4019
4020 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4021 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
4022 DumpNodes(Nodes[i], 2, this);
4023 }
4024
4025 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4026
4027 cerr << "\n\n";
4028}
4029
4030const Type *ConstantPoolSDNode::getType() const {
4031 if (isMachineConstantPoolEntry())
4032 return Val.MachineCPVal->getType();
4033 return Val.ConstVal->getType();
4034}