blob: a62ea53142061acb272b7c3291eaa90426c9ead5 [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"
Evan Cheng2e28d622008-02-02 04:07:54 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Support/MathExtras.h"
26#include "llvm/Target/MRegisterInfo.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetLowering.h"
29#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/ADT/SetVector.h"
32#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsa9810f32007-10-16 09:56:48 +000033#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/ADT/SmallVector.h"
35#include "llvm/ADT/StringExtras.h"
36#include <algorithm>
37#include <cmath>
38using namespace llvm;
39
40/// makeVTList - Return an instance of the SDVTList struct initialized with the
41/// specified members.
42static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
43 SDVTList Res = {VTs, NumVTs};
44 return Res;
45}
46
Chris Lattner7bcb18f2008-02-03 06:49:24 +000047SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
48
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049//===----------------------------------------------------------------------===//
50// ConstantFPSDNode Class
51//===----------------------------------------------------------------------===//
52
53/// isExactlyValue - We don't rely on operator== working on double values, as
54/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
55/// As such, this method can be used to do an exact bit-for-bit comparison of
56/// two floating point values.
Dale Johannesenc53301c2007-08-26 01:18:27 +000057bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen7f2c1d12007-08-25 22:10:57 +000058 return Value.bitwiseIsEqual(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059}
60
Dale Johannesenbbe2b702007-08-30 00:23:21 +000061bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
62 const APFloat& Val) {
63 // convert modifies in place, so make a copy.
64 APFloat Val2 = APFloat(Val);
65 switch (VT) {
66 default:
67 return false; // These can't be represented as floating point!
68
69 // FIXME rounding mode needs to be more flexible
70 case MVT::f32:
71 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
72 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
73 APFloat::opOK;
74 case MVT::f64:
75 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
76 &Val2.getSemantics() == &APFloat::IEEEdouble ||
77 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
78 APFloat::opOK;
79 // TODO: Figure out how to test if we can use a shorter type instead!
80 case MVT::f80:
81 case MVT::f128:
82 case MVT::ppcf128:
83 return true;
84 }
85}
86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087//===----------------------------------------------------------------------===//
88// ISD Namespace
89//===----------------------------------------------------------------------===//
90
91/// isBuildVectorAllOnes - Return true if the specified node is a
92/// BUILD_VECTOR where all of the elements are ~0 or undef.
93bool ISD::isBuildVectorAllOnes(const SDNode *N) {
94 // Look through a bit convert.
95 if (N->getOpcode() == ISD::BIT_CONVERT)
96 N = N->getOperand(0).Val;
97
98 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
99
100 unsigned i = 0, e = N->getNumOperands();
101
102 // Skip over all of the undef values.
103 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
104 ++i;
105
106 // Do not accept an all-undef vector.
107 if (i == e) return false;
108
109 // Do not accept build_vectors that aren't all constants or which have non-~0
110 // elements.
111 SDOperand NotZero = N->getOperand(i);
112 if (isa<ConstantSDNode>(NotZero)) {
113 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
114 return false;
115 } else if (isa<ConstantFPSDNode>(NotZero)) {
116 MVT::ValueType VT = NotZero.getValueType();
117 if (VT== MVT::f64) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000118 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
119 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 return false;
121 } else {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +0000122 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
123 getValueAPF().convertToAPInt().getZExtValue() !=
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 (uint32_t)-1)
125 return false;
126 }
127 } else
128 return false;
129
130 // Okay, we have at least one ~0 value, check to see if the rest match or are
131 // undefs.
132 for (++i; i != e; ++i)
133 if (N->getOperand(i) != NotZero &&
134 N->getOperand(i).getOpcode() != ISD::UNDEF)
135 return false;
136 return true;
137}
138
139
140/// isBuildVectorAllZeros - Return true if the specified node is a
141/// BUILD_VECTOR where all of the elements are 0 or undef.
142bool ISD::isBuildVectorAllZeros(const SDNode *N) {
143 // Look through a bit convert.
144 if (N->getOpcode() == ISD::BIT_CONVERT)
145 N = N->getOperand(0).Val;
146
147 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
148
149 unsigned i = 0, e = N->getNumOperands();
150
151 // Skip over all of the undef values.
152 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
153 ++i;
154
155 // Do not accept an all-undef vector.
156 if (i == e) return false;
157
158 // Do not accept build_vectors that aren't all constants or which have non-~0
159 // elements.
160 SDOperand Zero = N->getOperand(i);
161 if (isa<ConstantSDNode>(Zero)) {
162 if (!cast<ConstantSDNode>(Zero)->isNullValue())
163 return false;
164 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000165 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 return false;
167 } else
168 return false;
169
170 // Okay, we have at least one ~0 value, check to see if the rest match or are
171 // undefs.
172 for (++i; i != e; ++i)
173 if (N->getOperand(i) != Zero &&
174 N->getOperand(i).getOpcode() != ISD::UNDEF)
175 return false;
176 return true;
177}
178
Evan Cheng13d1c292008-01-31 09:59:15 +0000179/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengee6db0f2008-02-04 23:10:38 +0000180/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Cheng13d1c292008-01-31 09:59:15 +0000181/// is 0).
182bool ISD::isDebugLabel(const SDNode *N) {
183 SDOperand Zero;
184 if (N->getOpcode() == ISD::LABEL)
185 Zero = N->getOperand(2);
186 else if (N->isTargetOpcode() &&
187 N->getTargetOpcode() == TargetInstrInfo::LABEL)
188 // Chain moved to last operand.
189 Zero = N->getOperand(1);
190 else
191 return false;
192 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
193}
194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
196/// when given the operation for (X op Y).
197ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
198 // To perform this operation, we just need to swap the L and G bits of the
199 // operation.
200 unsigned OldL = (Operation >> 2) & 1;
201 unsigned OldG = (Operation >> 1) & 1;
202 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
203 (OldL << 1) | // New G bit
204 (OldG << 2)); // New L bit.
205}
206
207/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
208/// 'op' is a valid SetCC operation.
209ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
210 unsigned Operation = Op;
211 if (isInteger)
212 Operation ^= 7; // Flip L, G, E bits, but not U.
213 else
214 Operation ^= 15; // Flip all of the condition bits.
215 if (Operation > ISD::SETTRUE2)
216 Operation &= ~8; // Don't let N and U bits get set.
217 return ISD::CondCode(Operation);
218}
219
220
221/// isSignedOp - For an integer comparison, return 1 if the comparison is a
222/// signed operation and 2 if the result is an unsigned comparison. Return zero
223/// if the operation does not depend on the sign of the input (setne and seteq).
224static int isSignedOp(ISD::CondCode Opcode) {
225 switch (Opcode) {
226 default: assert(0 && "Illegal integer setcc operation!");
227 case ISD::SETEQ:
228 case ISD::SETNE: return 0;
229 case ISD::SETLT:
230 case ISD::SETLE:
231 case ISD::SETGT:
232 case ISD::SETGE: return 1;
233 case ISD::SETULT:
234 case ISD::SETULE:
235 case ISD::SETUGT:
236 case ISD::SETUGE: return 2;
237 }
238}
239
240/// getSetCCOrOperation - Return the result of a logical OR between different
241/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
242/// returns SETCC_INVALID if it is not possible to represent the resultant
243/// comparison.
244ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
245 bool isInteger) {
246 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
247 // Cannot fold a signed integer setcc with an unsigned integer setcc.
248 return ISD::SETCC_INVALID;
249
250 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
251
252 // If the N and U bits get set then the resultant comparison DOES suddenly
253 // care about orderedness, and is true when ordered.
254 if (Op > ISD::SETTRUE2)
255 Op &= ~16; // Clear the U bit if the N bit is set.
256
257 // Canonicalize illegal integer setcc's.
258 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
259 Op = ISD::SETNE;
260
261 return ISD::CondCode(Op);
262}
263
264/// getSetCCAndOperation - Return the result of a logical AND between different
265/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
266/// function returns zero if it is not possible to represent the resultant
267/// comparison.
268ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
269 bool isInteger) {
270 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
271 // Cannot fold a signed setcc with an unsigned setcc.
272 return ISD::SETCC_INVALID;
273
274 // Combine all of the condition bits.
275 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
276
277 // Canonicalize illegal integer setcc's.
278 if (isInteger) {
279 switch (Result) {
280 default: break;
281 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
282 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
283 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
284 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
285 }
286 }
287
288 return Result;
289}
290
291const TargetMachine &SelectionDAG::getTarget() const {
292 return TLI.getTargetMachine();
293}
294
295//===----------------------------------------------------------------------===//
296// SDNode Profile Support
297//===----------------------------------------------------------------------===//
298
299/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
300///
301static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
302 ID.AddInteger(OpC);
303}
304
305/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
306/// solely with their pointer.
307void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
308 ID.AddPointer(VTList.VTs);
309}
310
311/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
312///
313static void AddNodeIDOperands(FoldingSetNodeID &ID,
314 const SDOperand *Ops, unsigned NumOps) {
315 for (; NumOps; --NumOps, ++Ops) {
316 ID.AddPointer(Ops->Val);
317 ID.AddInteger(Ops->ResNo);
318 }
319}
320
321static void AddNodeIDNode(FoldingSetNodeID &ID,
322 unsigned short OpC, SDVTList VTList,
323 const SDOperand *OpList, unsigned N) {
324 AddNodeIDOpcode(ID, OpC);
325 AddNodeIDValueTypes(ID, VTList);
326 AddNodeIDOperands(ID, OpList, N);
327}
328
329/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
330/// data.
331static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
332 AddNodeIDOpcode(ID, N->getOpcode());
333 // Add the return value info.
334 AddNodeIDValueTypes(ID, N->getVTList());
335 // Add the operand info.
336 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
337
338 // Handle SDNode leafs with special info.
339 switch (N->getOpcode()) {
340 default: break; // Normal nodes don't need extra info.
341 case ISD::TargetConstant:
342 case ISD::Constant:
343 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
344 break;
345 case ISD::TargetConstantFP:
Dale Johannesendf8a8312007-08-31 04:03:46 +0000346 case ISD::ConstantFP: {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000347 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 break;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000349 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 case ISD::TargetGlobalAddress:
351 case ISD::GlobalAddress:
352 case ISD::TargetGlobalTLSAddress:
353 case ISD::GlobalTLSAddress: {
354 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
355 ID.AddPointer(GA->getGlobal());
356 ID.AddInteger(GA->getOffset());
357 break;
358 }
359 case ISD::BasicBlock:
360 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
361 break;
362 case ISD::Register:
363 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
364 break;
Dan Gohman12a9c082008-02-06 22:27:42 +0000365 case ISD::SRCVALUE:
366 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
367 break;
368 case ISD::MEMOPERAND: {
369 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
370 ID.AddPointer(MO.getValue());
371 ID.AddInteger(MO.getFlags());
372 ID.AddInteger(MO.getOffset());
373 ID.AddInteger(MO.getSize());
374 ID.AddInteger(MO.getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 break;
376 }
377 case ISD::FrameIndex:
378 case ISD::TargetFrameIndex:
379 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
380 break;
381 case ISD::JumpTable:
382 case ISD::TargetJumpTable:
383 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
384 break;
385 case ISD::ConstantPool:
386 case ISD::TargetConstantPool: {
387 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
388 ID.AddInteger(CP->getAlignment());
389 ID.AddInteger(CP->getOffset());
390 if (CP->isMachineConstantPoolEntry())
391 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
392 else
393 ID.AddPointer(CP->getConstVal());
394 break;
395 }
396 case ISD::LOAD: {
397 LoadSDNode *LD = cast<LoadSDNode>(N);
398 ID.AddInteger(LD->getAddressingMode());
399 ID.AddInteger(LD->getExtensionType());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000400 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 ID.AddInteger(LD->getAlignment());
402 ID.AddInteger(LD->isVolatile());
403 break;
404 }
405 case ISD::STORE: {
406 StoreSDNode *ST = cast<StoreSDNode>(N);
407 ID.AddInteger(ST->getAddressingMode());
408 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000409 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 ID.AddInteger(ST->getAlignment());
411 ID.AddInteger(ST->isVolatile());
412 break;
413 }
414 }
415}
416
417//===----------------------------------------------------------------------===//
418// SelectionDAG Class
419//===----------------------------------------------------------------------===//
420
421/// RemoveDeadNodes - This method deletes all unreachable nodes in the
422/// SelectionDAG.
423void SelectionDAG::RemoveDeadNodes() {
424 // Create a dummy node (which is not added to allnodes), that adds a reference
425 // to the root node, preventing it from being deleted.
426 HandleSDNode Dummy(getRoot());
427
428 SmallVector<SDNode*, 128> DeadNodes;
429
430 // Add all obviously-dead nodes to the DeadNodes worklist.
431 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
432 if (I->use_empty())
433 DeadNodes.push_back(I);
434
435 // Process the worklist, deleting the nodes and adding their uses to the
436 // worklist.
437 while (!DeadNodes.empty()) {
438 SDNode *N = DeadNodes.back();
439 DeadNodes.pop_back();
440
441 // Take the node out of the appropriate CSE map.
442 RemoveNodeFromCSEMaps(N);
443
444 // Next, brutally remove the operand list. This is safe to do, as there are
445 // no cycles in the graph.
446 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
447 SDNode *Operand = I->Val;
448 Operand->removeUser(N);
449
450 // Now that we removed this operand, see if there are no uses of it left.
451 if (Operand->use_empty())
452 DeadNodes.push_back(Operand);
453 }
454 if (N->OperandsNeedDelete)
455 delete[] N->OperandList;
456 N->OperandList = 0;
457 N->NumOperands = 0;
458
459 // Finally, remove N itself.
460 AllNodes.erase(N);
461 }
462
463 // If the root changed (e.g. it was a dead load, update the root).
464 setRoot(Dummy.getValue());
465}
466
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000467void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 SmallVector<SDNode*, 16> DeadNodes;
469 DeadNodes.push_back(N);
470
471 // Process the worklist, deleting the nodes and adding their uses to the
472 // worklist.
473 while (!DeadNodes.empty()) {
474 SDNode *N = DeadNodes.back();
475 DeadNodes.pop_back();
476
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000477 if (UpdateListener)
478 UpdateListener->NodeDeleted(N);
479
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 // Take the node out of the appropriate CSE map.
481 RemoveNodeFromCSEMaps(N);
482
483 // Next, brutally remove the operand list. This is safe to do, as there are
484 // no cycles in the graph.
485 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
486 SDNode *Operand = I->Val;
487 Operand->removeUser(N);
488
489 // Now that we removed this operand, see if there are no uses of it left.
490 if (Operand->use_empty())
491 DeadNodes.push_back(Operand);
492 }
493 if (N->OperandsNeedDelete)
494 delete[] N->OperandList;
495 N->OperandList = 0;
496 N->NumOperands = 0;
497
498 // Finally, remove N itself.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 AllNodes.erase(N);
500 }
501}
502
503void SelectionDAG::DeleteNode(SDNode *N) {
504 assert(N->use_empty() && "Cannot delete a node that is not dead!");
505
506 // First take this out of the appropriate CSE map.
507 RemoveNodeFromCSEMaps(N);
508
509 // Finally, remove uses due to operands of this node, remove from the
510 // AllNodes list, and delete the node.
511 DeleteNodeNotInCSEMaps(N);
512}
513
514void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
515
516 // Remove it from the AllNodes list.
517 AllNodes.remove(N);
518
519 // Drop all of the operands and decrement used nodes use counts.
520 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
521 I->Val->removeUser(N);
522 if (N->OperandsNeedDelete)
523 delete[] N->OperandList;
524 N->OperandList = 0;
525 N->NumOperands = 0;
526
527 delete N;
528}
529
530/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
531/// correspond to it. This is useful when we're about to delete or repurpose
532/// the node. We don't want future request for structurally identical nodes
533/// to return N anymore.
534void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
535 bool Erased = false;
536 switch (N->getOpcode()) {
537 case ISD::HANDLENODE: return; // noop.
538 case ISD::STRING:
539 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
540 break;
541 case ISD::CONDCODE:
542 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
543 "Cond code doesn't exist!");
544 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
545 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
546 break;
547 case ISD::ExternalSymbol:
548 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
549 break;
550 case ISD::TargetExternalSymbol:
551 Erased =
552 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
553 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000554 case ISD::VALUETYPE: {
555 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
556 if (MVT::isExtendedVT(VT)) {
557 Erased = ExtendedValueTypeNodes.erase(VT);
558 } else {
559 Erased = ValueTypeNodes[VT] != 0;
560 ValueTypeNodes[VT] = 0;
561 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000563 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 default:
565 // Remove it from the CSE Map.
566 Erased = CSEMap.RemoveNode(N);
567 break;
568 }
569#ifndef NDEBUG
570 // Verify that the node was actually in one of the CSE maps, unless it has a
571 // flag result (which cannot be CSE'd) or is one of the special cases that are
572 // not subject to CSE.
573 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
574 !N->isTargetOpcode()) {
575 N->dump(this);
576 cerr << "\n";
577 assert(0 && "Node is not in map!");
578 }
579#endif
580}
581
582/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
583/// has been taken out and modified in some way. If the specified node already
584/// exists in the CSE maps, do not modify the maps, but return the existing node
585/// instead. If it doesn't exist, add it and return null.
586///
587SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
588 assert(N->getNumOperands() && "This is a leaf node!");
589 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
590 return 0; // Never add these nodes.
591
592 // Check that remaining values produced are not flags.
593 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
594 if (N->getValueType(i) == MVT::Flag)
595 return 0; // Never CSE anything that produces a flag.
596
597 SDNode *New = CSEMap.GetOrInsertNode(N);
598 if (New != N) return New; // Node already existed.
599 return 0;
600}
601
602/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
603/// were replaced with those specified. If this node is never memoized,
604/// return null, otherwise return a pointer to the slot it would take. If a
605/// node already exists with these operands, the slot will be non-null.
606SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
607 void *&InsertPos) {
608 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
609 return 0; // Never add these nodes.
610
611 // Check that remaining values produced are not flags.
612 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
613 if (N->getValueType(i) == MVT::Flag)
614 return 0; // Never CSE anything that produces a flag.
615
616 SDOperand Ops[] = { Op };
617 FoldingSetNodeID ID;
618 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
619 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
620}
621
622/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
623/// were replaced with those specified. If this node is never memoized,
624/// return null, otherwise return a pointer to the slot it would take. If a
625/// node already exists with these operands, the slot will be non-null.
626SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
627 SDOperand Op1, SDOperand Op2,
628 void *&InsertPos) {
629 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
630 return 0; // Never add these nodes.
631
632 // Check that remaining values produced are not flags.
633 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
634 if (N->getValueType(i) == MVT::Flag)
635 return 0; // Never CSE anything that produces a flag.
636
637 SDOperand Ops[] = { Op1, Op2 };
638 FoldingSetNodeID ID;
639 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
640 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
641}
642
643
644/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
645/// were replaced with those specified. If this node is never memoized,
646/// return null, otherwise return a pointer to the slot it would take. If a
647/// node already exists with these operands, the slot will be non-null.
648SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
649 const SDOperand *Ops,unsigned NumOps,
650 void *&InsertPos) {
651 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
652 return 0; // Never add these nodes.
653
654 // Check that remaining values produced are not flags.
655 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
656 if (N->getValueType(i) == MVT::Flag)
657 return 0; // Never CSE anything that produces a flag.
658
659 FoldingSetNodeID ID;
660 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
661
662 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
663 ID.AddInteger(LD->getAddressingMode());
664 ID.AddInteger(LD->getExtensionType());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000665 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 ID.AddInteger(LD->getAlignment());
667 ID.AddInteger(LD->isVolatile());
668 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
669 ID.AddInteger(ST->getAddressingMode());
670 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000671 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 ID.AddInteger(ST->getAlignment());
673 ID.AddInteger(ST->isVolatile());
674 }
675
676 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
677}
678
679
680SelectionDAG::~SelectionDAG() {
681 while (!AllNodes.empty()) {
682 SDNode *N = AllNodes.begin();
683 N->SetNextInBucket(0);
684 if (N->OperandsNeedDelete)
685 delete [] N->OperandList;
686 N->OperandList = 0;
687 N->NumOperands = 0;
688 AllNodes.pop_front();
689 }
690}
691
692SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
693 if (Op.getValueType() == VT) return Op;
694 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
695 return getNode(ISD::AND, Op.getValueType(), Op,
696 getConstant(Imm, Op.getValueType()));
697}
698
699SDOperand SelectionDAG::getString(const std::string &Val) {
700 StringSDNode *&N = StringNodes[Val];
701 if (!N) {
702 N = new StringSDNode(Val);
703 AllNodes.push_back(N);
704 }
705 return SDOperand(N, 0);
706}
707
708SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
709 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000710
711 MVT::ValueType EltVT =
712 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713
714 // Mask out any bits that are not valid for this constant.
Dan Gohman5b9d6412007-12-12 22:21:26 +0000715 Val &= MVT::getIntVTBitMask(EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
717 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
718 FoldingSetNodeID ID;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000719 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 ID.AddInteger(Val);
721 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000722 SDNode *N = NULL;
723 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
724 if (!MVT::isVector(VT))
725 return SDOperand(N, 0);
726 if (!N) {
727 N = new ConstantSDNode(isT, Val, EltVT);
728 CSEMap.InsertNode(N, IP);
729 AllNodes.push_back(N);
730 }
731
732 SDOperand Result(N, 0);
733 if (MVT::isVector(VT)) {
734 SmallVector<SDOperand, 8> Ops;
735 Ops.assign(MVT::getVectorNumElements(VT), Result);
736 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
737 }
738 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739}
740
Chris Lattner5872a362008-01-17 07:00:52 +0000741SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
742 return getConstant(Val, TLI.getPointerTy(), isTarget);
743}
744
745
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000746SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 bool isTarget) {
748 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000749
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 MVT::ValueType EltVT =
751 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752
753 // Do the map lookup using the actual bit pattern for the floating point
754 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
755 // we don't have issues with SNANs.
756 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
757 FoldingSetNodeID ID;
758 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000759 ID.AddAPFloat(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 void *IP = 0;
761 SDNode *N = NULL;
762 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
763 if (!MVT::isVector(VT))
764 return SDOperand(N, 0);
765 if (!N) {
Dale Johannesen2fc20782007-09-14 22:26:36 +0000766 N = new ConstantFPSDNode(isTarget, V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 CSEMap.InsertNode(N, IP);
768 AllNodes.push_back(N);
769 }
770
771 SDOperand Result(N, 0);
772 if (MVT::isVector(VT)) {
773 SmallVector<SDOperand, 8> Ops;
774 Ops.assign(MVT::getVectorNumElements(VT), Result);
775 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
776 }
777 return Result;
778}
779
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000780SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
781 bool isTarget) {
782 MVT::ValueType EltVT =
783 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
784 if (EltVT==MVT::f32)
785 return getConstantFP(APFloat((float)Val), VT, isTarget);
786 else
787 return getConstantFP(APFloat(Val), VT, isTarget);
788}
789
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
791 MVT::ValueType VT, int Offset,
792 bool isTargetGA) {
793 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
794 unsigned Opc;
795 if (GVar && GVar->isThreadLocal())
796 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
797 else
798 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
799 FoldingSetNodeID ID;
800 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
801 ID.AddPointer(GV);
802 ID.AddInteger(Offset);
803 void *IP = 0;
804 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
805 return SDOperand(E, 0);
806 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
807 CSEMap.InsertNode(N, IP);
808 AllNodes.push_back(N);
809 return SDOperand(N, 0);
810}
811
812SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
813 bool isTarget) {
814 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
815 FoldingSetNodeID ID;
816 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
817 ID.AddInteger(FI);
818 void *IP = 0;
819 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
820 return SDOperand(E, 0);
821 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
822 CSEMap.InsertNode(N, IP);
823 AllNodes.push_back(N);
824 return SDOperand(N, 0);
825}
826
827SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
828 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
829 FoldingSetNodeID ID;
830 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
831 ID.AddInteger(JTI);
832 void *IP = 0;
833 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
834 return SDOperand(E, 0);
835 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
836 CSEMap.InsertNode(N, IP);
837 AllNodes.push_back(N);
838 return SDOperand(N, 0);
839}
840
841SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
842 unsigned Alignment, int Offset,
843 bool isTarget) {
844 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
845 FoldingSetNodeID ID;
846 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
847 ID.AddInteger(Alignment);
848 ID.AddInteger(Offset);
849 ID.AddPointer(C);
850 void *IP = 0;
851 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
852 return SDOperand(E, 0);
853 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
854 CSEMap.InsertNode(N, IP);
855 AllNodes.push_back(N);
856 return SDOperand(N, 0);
857}
858
859
860SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
861 MVT::ValueType VT,
862 unsigned Alignment, int Offset,
863 bool isTarget) {
864 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
865 FoldingSetNodeID ID;
866 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
867 ID.AddInteger(Alignment);
868 ID.AddInteger(Offset);
869 C->AddSelectionDAGCSEId(ID);
870 void *IP = 0;
871 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
872 return SDOperand(E, 0);
873 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
874 CSEMap.InsertNode(N, IP);
875 AllNodes.push_back(N);
876 return SDOperand(N, 0);
877}
878
879
880SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
881 FoldingSetNodeID ID;
882 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
883 ID.AddPointer(MBB);
884 void *IP = 0;
885 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
886 return SDOperand(E, 0);
887 SDNode *N = new BasicBlockSDNode(MBB);
888 CSEMap.InsertNode(N, IP);
889 AllNodes.push_back(N);
890 return SDOperand(N, 0);
891}
892
893SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000894 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 ValueTypeNodes.resize(VT+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896
Duncan Sandsd7307a92007-10-17 13:49:58 +0000897 SDNode *&N = MVT::isExtendedVT(VT) ?
898 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
899
900 if (N) return SDOperand(N, 0);
901 N = new VTSDNode(VT);
902 AllNodes.push_back(N);
903 return SDOperand(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904}
905
906SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
907 SDNode *&N = ExternalSymbols[Sym];
908 if (N) return SDOperand(N, 0);
909 N = new ExternalSymbolSDNode(false, Sym, VT);
910 AllNodes.push_back(N);
911 return SDOperand(N, 0);
912}
913
914SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
915 MVT::ValueType VT) {
916 SDNode *&N = TargetExternalSymbols[Sym];
917 if (N) return SDOperand(N, 0);
918 N = new ExternalSymbolSDNode(true, Sym, VT);
919 AllNodes.push_back(N);
920 return SDOperand(N, 0);
921}
922
923SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
924 if ((unsigned)Cond >= CondCodeNodes.size())
925 CondCodeNodes.resize(Cond+1);
926
927 if (CondCodeNodes[Cond] == 0) {
928 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
929 AllNodes.push_back(CondCodeNodes[Cond]);
930 }
931 return SDOperand(CondCodeNodes[Cond], 0);
932}
933
934SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
935 FoldingSetNodeID ID;
936 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
937 ID.AddInteger(RegNo);
938 void *IP = 0;
939 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
940 return SDOperand(E, 0);
941 SDNode *N = new RegisterSDNode(RegNo, VT);
942 CSEMap.InsertNode(N, IP);
943 AllNodes.push_back(N);
944 return SDOperand(N, 0);
945}
946
Dan Gohman12a9c082008-02-06 22:27:42 +0000947SDOperand SelectionDAG::getSrcValue(const Value *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 assert((!V || isa<PointerType>(V->getType())) &&
949 "SrcValue is not a pointer?");
950
951 FoldingSetNodeID ID;
952 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
953 ID.AddPointer(V);
Dan Gohman12a9c082008-02-06 22:27:42 +0000954
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955 void *IP = 0;
956 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
957 return SDOperand(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +0000958
959 SDNode *N = new SrcValueSDNode(V);
960 CSEMap.InsertNode(N, IP);
961 AllNodes.push_back(N);
962 return SDOperand(N, 0);
963}
964
965SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
966 const Value *v = MO.getValue();
967 assert((!v || isa<PointerType>(v->getType())) &&
968 "SrcValue is not a pointer?");
969
970 FoldingSetNodeID ID;
971 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
972 ID.AddPointer(v);
973 ID.AddInteger(MO.getFlags());
974 ID.AddInteger(MO.getOffset());
975 ID.AddInteger(MO.getSize());
976 ID.AddInteger(MO.getAlignment());
977
978 void *IP = 0;
979 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
980 return SDOperand(E, 0);
981
982 SDNode *N = new MemOperandSDNode(MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 CSEMap.InsertNode(N, IP);
984 AllNodes.push_back(N);
985 return SDOperand(N, 0);
986}
987
Chris Lattner53f5aee2007-10-15 17:47:20 +0000988/// CreateStackTemporary - Create a stack temporary, suitable for holding the
989/// specified value type.
990SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
991 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
992 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
993 const Type *Ty = MVT::getTypeForValueType(VT);
994 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
995 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
996 return getFrameIndex(FrameIdx, TLI.getPointerTy());
997}
998
999
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1001 SDOperand N2, ISD::CondCode Cond) {
1002 // These setcc operations always fold.
1003 switch (Cond) {
1004 default: break;
1005 case ISD::SETFALSE:
1006 case ISD::SETFALSE2: return getConstant(0, VT);
1007 case ISD::SETTRUE:
1008 case ISD::SETTRUE2: return getConstant(1, VT);
1009
1010 case ISD::SETOEQ:
1011 case ISD::SETOGT:
1012 case ISD::SETOGE:
1013 case ISD::SETOLT:
1014 case ISD::SETOLE:
1015 case ISD::SETONE:
1016 case ISD::SETO:
1017 case ISD::SETUO:
1018 case ISD::SETUEQ:
1019 case ISD::SETUNE:
1020 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1021 break;
1022 }
1023
1024 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
1025 uint64_t C2 = N2C->getValue();
1026 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1027 uint64_t C1 = N1C->getValue();
1028
1029 // Sign extend the operands if required
1030 if (ISD::isSignedIntSetCC(Cond)) {
1031 C1 = N1C->getSignExtended();
1032 C2 = N2C->getSignExtended();
1033 }
1034
1035 switch (Cond) {
1036 default: assert(0 && "Unknown integer setcc!");
1037 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1038 case ISD::SETNE: return getConstant(C1 != C2, VT);
1039 case ISD::SETULT: return getConstant(C1 < C2, VT);
1040 case ISD::SETUGT: return getConstant(C1 > C2, VT);
1041 case ISD::SETULE: return getConstant(C1 <= C2, VT);
1042 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
1043 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
1044 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
1045 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
1046 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
1047 }
1048 }
1049 }
1050 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1051 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001052 // No compile time operations on this type yet.
1053 if (N1C->getValueType(0) == MVT::ppcf128)
1054 return SDOperand();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001055
1056 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001058 default: break;
Dale Johannesen76844472007-08-31 17:03:33 +00001059 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1060 return getNode(ISD::UNDEF, VT);
1061 // fall through
1062 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1063 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1064 return getNode(ISD::UNDEF, VT);
1065 // fall through
1066 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001067 R==APFloat::cmpLessThan, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001068 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1069 return getNode(ISD::UNDEF, VT);
1070 // fall through
1071 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1072 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1073 return getNode(ISD::UNDEF, VT);
1074 // fall through
1075 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1076 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1077 return getNode(ISD::UNDEF, VT);
1078 // fall through
1079 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001080 R==APFloat::cmpEqual, VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001081 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1082 return getNode(ISD::UNDEF, VT);
1083 // fall through
1084 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001085 R==APFloat::cmpEqual, VT);
1086 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1087 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1088 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1089 R==APFloat::cmpEqual, VT);
1090 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1091 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1092 R==APFloat::cmpLessThan, VT);
1093 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1094 R==APFloat::cmpUnordered, VT);
1095 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1096 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 }
1098 } else {
1099 // Ensure that the constant occurs on the RHS.
1100 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
1101 }
1102
1103 // Could not fold it.
1104 return SDOperand();
1105}
1106
1107/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1108/// this predicate to simplify operations downstream. Mask is known to be zero
1109/// for bits that V cannot have.
1110bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1111 unsigned Depth) const {
1112 // The masks are not wide enough to represent this type! Should use APInt.
1113 if (Op.getValueType() == MVT::i128)
1114 return false;
1115
1116 uint64_t KnownZero, KnownOne;
1117 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1118 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1119 return (KnownZero & Mask) == Mask;
1120}
1121
1122/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1123/// known to be either zero or one and return them in the KnownZero/KnownOne
1124/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1125/// processing.
1126void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1127 uint64_t &KnownZero, uint64_t &KnownOne,
1128 unsigned Depth) const {
1129 KnownZero = KnownOne = 0; // Don't know anything.
1130 if (Depth == 6 || Mask == 0)
1131 return; // Limit search depth.
1132
1133 // The masks are not wide enough to represent this type! Should use APInt.
1134 if (Op.getValueType() == MVT::i128)
1135 return;
1136
1137 uint64_t KnownZero2, KnownOne2;
1138
1139 switch (Op.getOpcode()) {
1140 case ISD::Constant:
1141 // We know all of the bits for a constant!
1142 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1143 KnownZero = ~KnownOne & Mask;
1144 return;
1145 case ISD::AND:
1146 // If either the LHS or the RHS are Zero, the result is zero.
1147 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1148 Mask &= ~KnownZero;
1149 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1150 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1151 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1152
1153 // Output known-1 bits are only known if set in both the LHS & RHS.
1154 KnownOne &= KnownOne2;
1155 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1156 KnownZero |= KnownZero2;
1157 return;
1158 case ISD::OR:
1159 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1160 Mask &= ~KnownOne;
1161 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1162 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1163 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1164
1165 // Output known-0 bits are only known if clear in both the LHS & RHS.
1166 KnownZero &= KnownZero2;
1167 // Output known-1 are known to be set if set in either the LHS | RHS.
1168 KnownOne |= KnownOne2;
1169 return;
1170 case ISD::XOR: {
1171 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1172 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1173 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1174 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1175
1176 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1177 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1178 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1179 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1180 KnownZero = KnownZeroOut;
1181 return;
1182 }
1183 case ISD::SELECT:
1184 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1185 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1186 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1187 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1188
1189 // Only known if known in both the LHS and RHS.
1190 KnownOne &= KnownOne2;
1191 KnownZero &= KnownZero2;
1192 return;
1193 case ISD::SELECT_CC:
1194 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1195 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1196 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1197 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1198
1199 // Only known if known in both the LHS and RHS.
1200 KnownOne &= KnownOne2;
1201 KnownZero &= KnownZero2;
1202 return;
1203 case ISD::SETCC:
1204 // If we know the result of a setcc has the top bits zero, use this info.
1205 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1206 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1207 return;
1208 case ISD::SHL:
1209 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1210 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1211 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1212 KnownZero, KnownOne, Depth+1);
1213 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1214 KnownZero <<= SA->getValue();
1215 KnownOne <<= SA->getValue();
1216 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1217 }
1218 return;
1219 case ISD::SRL:
1220 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1221 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1222 MVT::ValueType VT = Op.getValueType();
1223 unsigned ShAmt = SA->getValue();
1224
1225 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1226 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1227 KnownZero, KnownOne, Depth+1);
1228 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1229 KnownZero &= TypeMask;
1230 KnownOne &= TypeMask;
1231 KnownZero >>= ShAmt;
1232 KnownOne >>= ShAmt;
1233
1234 uint64_t HighBits = (1ULL << ShAmt)-1;
1235 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1236 KnownZero |= HighBits; // High bits known zero.
1237 }
1238 return;
1239 case ISD::SRA:
1240 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1241 MVT::ValueType VT = Op.getValueType();
1242 unsigned ShAmt = SA->getValue();
1243
1244 // Compute the new bits that are at the top now.
1245 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1246
1247 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1248 // If any of the demanded bits are produced by the sign extension, we also
1249 // demand the input sign bit.
1250 uint64_t HighBits = (1ULL << ShAmt)-1;
1251 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1252 if (HighBits & Mask)
1253 InDemandedMask |= MVT::getIntVTSignBit(VT);
1254
1255 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1256 Depth+1);
1257 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1258 KnownZero &= TypeMask;
1259 KnownOne &= TypeMask;
1260 KnownZero >>= ShAmt;
1261 KnownOne >>= ShAmt;
1262
1263 // Handle the sign bits.
1264 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1265 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1266
1267 if (KnownZero & SignBit) {
1268 KnownZero |= HighBits; // New bits are known zero.
1269 } else if (KnownOne & SignBit) {
1270 KnownOne |= HighBits; // New bits are known one.
1271 }
1272 }
1273 return;
1274 case ISD::SIGN_EXTEND_INREG: {
1275 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1276
1277 // Sign extension. Compute the demanded bits in the result that are not
1278 // present in the input.
1279 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1280
1281 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1282 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1283
1284 // If the sign extended bits are demanded, we know that the sign
1285 // bit is demanded.
1286 if (NewBits)
1287 InputDemandedBits |= InSignBit;
1288
1289 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1290 KnownZero, KnownOne, Depth+1);
1291 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1292
1293 // If the sign bit of the input is known set or clear, then we know the
1294 // top bits of the result.
1295 if (KnownZero & InSignBit) { // Input sign bit known clear
1296 KnownZero |= NewBits;
1297 KnownOne &= ~NewBits;
1298 } else if (KnownOne & InSignBit) { // Input sign bit known set
1299 KnownOne |= NewBits;
1300 KnownZero &= ~NewBits;
1301 } else { // Input sign bit unknown
1302 KnownZero &= ~NewBits;
1303 KnownOne &= ~NewBits;
1304 }
1305 return;
1306 }
1307 case ISD::CTTZ:
1308 case ISD::CTLZ:
1309 case ISD::CTPOP: {
1310 MVT::ValueType VT = Op.getValueType();
1311 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1312 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1313 KnownOne = 0;
1314 return;
1315 }
1316 case ISD::LOAD: {
1317 if (ISD::isZEXTLoad(Op.Val)) {
1318 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001319 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1321 }
1322 return;
1323 }
1324 case ISD::ZERO_EXTEND: {
1325 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1326 uint64_t NewBits = (~InMask) & Mask;
1327 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1328 KnownOne, Depth+1);
1329 KnownZero |= NewBits & Mask;
1330 KnownOne &= ~NewBits;
1331 return;
1332 }
1333 case ISD::SIGN_EXTEND: {
1334 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1335 unsigned InBits = MVT::getSizeInBits(InVT);
1336 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1337 uint64_t InSignBit = 1ULL << (InBits-1);
1338 uint64_t NewBits = (~InMask) & Mask;
1339 uint64_t InDemandedBits = Mask & InMask;
1340
1341 // If any of the sign extended bits are demanded, we know that the sign
1342 // bit is demanded.
1343 if (NewBits & Mask)
1344 InDemandedBits |= InSignBit;
1345
1346 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1347 KnownOne, Depth+1);
1348 // If the sign bit is known zero or one, the top bits match.
1349 if (KnownZero & InSignBit) {
1350 KnownZero |= NewBits;
1351 KnownOne &= ~NewBits;
1352 } else if (KnownOne & InSignBit) {
1353 KnownOne |= NewBits;
1354 KnownZero &= ~NewBits;
1355 } else { // Otherwise, top bits aren't known.
1356 KnownOne &= ~NewBits;
1357 KnownZero &= ~NewBits;
1358 }
1359 return;
1360 }
1361 case ISD::ANY_EXTEND: {
1362 MVT::ValueType VT = Op.getOperand(0).getValueType();
1363 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1364 KnownZero, KnownOne, Depth+1);
1365 return;
1366 }
1367 case ISD::TRUNCATE: {
1368 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1369 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1370 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1371 KnownZero &= OutMask;
1372 KnownOne &= OutMask;
1373 break;
1374 }
1375 case ISD::AssertZext: {
1376 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1377 uint64_t InMask = MVT::getIntVTBitMask(VT);
1378 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1379 KnownOne, Depth+1);
1380 KnownZero |= (~InMask) & Mask;
1381 return;
1382 }
Chris Lattner13f06832007-12-22 21:26:52 +00001383 case ISD::FGETSIGN:
1384 // All bits are zero except the low bit.
1385 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1386 return;
1387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 case ISD::ADD: {
1389 // If either the LHS or the RHS are Zero, the result is zero.
1390 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1391 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1392 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1393 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1394
1395 // Output known-0 bits are known if clear or set in both the low clear bits
1396 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1397 // low 3 bits clear.
1398 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1399 CountTrailingZeros_64(~KnownZero2));
1400
1401 KnownZero = (1ULL << KnownZeroOut) - 1;
1402 KnownOne = 0;
1403 return;
1404 }
1405 case ISD::SUB: {
1406 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1407 if (!CLHS) return;
1408
1409 // We know that the top bits of C-X are clear if X contains less bits
1410 // than C (i.e. no wrap-around can happen). For example, 20-X is
1411 // positive if we can prove that X is >= 0 and < 16.
1412 MVT::ValueType VT = CLHS->getValueType(0);
1413 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1414 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1415 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1416 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1417 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1418
1419 // If all of the MaskV bits are known to be zero, then we know the output
1420 // top bits are zero, because we now know that the output is from [0-C].
1421 if ((KnownZero & MaskV) == MaskV) {
1422 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1423 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1424 KnownOne = 0; // No one bits known.
1425 } else {
1426 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1427 }
1428 }
1429 return;
1430 }
1431 default:
1432 // Allow the target to implement this method for its nodes.
1433 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1434 case ISD::INTRINSIC_WO_CHAIN:
1435 case ISD::INTRINSIC_W_CHAIN:
1436 case ISD::INTRINSIC_VOID:
1437 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1438 }
1439 return;
1440 }
1441}
1442
1443/// ComputeNumSignBits - Return the number of times the sign bit of the
1444/// register is replicated into the other bits. We know that at least 1 bit
1445/// is always equal to the sign bit (itself), but other cases can give us
1446/// information. For example, immediately after an "SRA X, 2", we know that
1447/// the top 3 bits are all equal to each other, so we return 3.
1448unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1449 MVT::ValueType VT = Op.getValueType();
1450 assert(MVT::isInteger(VT) && "Invalid VT!");
1451 unsigned VTBits = MVT::getSizeInBits(VT);
1452 unsigned Tmp, Tmp2;
1453
1454 if (Depth == 6)
1455 return 1; // Limit search depth.
1456
1457 switch (Op.getOpcode()) {
1458 default: break;
1459 case ISD::AssertSext:
1460 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1461 return VTBits-Tmp+1;
1462 case ISD::AssertZext:
1463 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1464 return VTBits-Tmp;
1465
1466 case ISD::Constant: {
1467 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1468 // If negative, invert the bits, then look at it.
1469 if (Val & MVT::getIntVTSignBit(VT))
1470 Val = ~Val;
1471
1472 // Shift the bits so they are the leading bits in the int64_t.
1473 Val <<= 64-VTBits;
1474
1475 // Return # leading zeros. We use 'min' here in case Val was zero before
1476 // shifting. We don't want to return '64' as for an i32 "0".
1477 return std::min(VTBits, CountLeadingZeros_64(Val));
1478 }
1479
1480 case ISD::SIGN_EXTEND:
1481 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1482 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1483
1484 case ISD::SIGN_EXTEND_INREG:
1485 // Max of the input and what this extends.
1486 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1487 Tmp = VTBits-Tmp+1;
1488
1489 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1490 return std::max(Tmp, Tmp2);
1491
1492 case ISD::SRA:
1493 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1494 // SRA X, C -> adds C sign bits.
1495 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1496 Tmp += C->getValue();
1497 if (Tmp > VTBits) Tmp = VTBits;
1498 }
1499 return Tmp;
1500 case ISD::SHL:
1501 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1502 // shl destroys sign bits.
1503 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1504 if (C->getValue() >= VTBits || // Bad shift.
1505 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1506 return Tmp - C->getValue();
1507 }
1508 break;
1509 case ISD::AND:
1510 case ISD::OR:
1511 case ISD::XOR: // NOT is handled here.
1512 // Logical binary ops preserve the number of sign bits.
1513 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1514 if (Tmp == 1) return 1; // Early out.
1515 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1516 return std::min(Tmp, Tmp2);
1517
1518 case ISD::SELECT:
1519 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1520 if (Tmp == 1) return 1; // Early out.
1521 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1522 return std::min(Tmp, Tmp2);
1523
1524 case ISD::SETCC:
1525 // If setcc returns 0/-1, all bits are sign bits.
1526 if (TLI.getSetCCResultContents() ==
1527 TargetLowering::ZeroOrNegativeOneSetCCResult)
1528 return VTBits;
1529 break;
1530 case ISD::ROTL:
1531 case ISD::ROTR:
1532 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1533 unsigned RotAmt = C->getValue() & (VTBits-1);
1534
1535 // Handle rotate right by N like a rotate left by 32-N.
1536 if (Op.getOpcode() == ISD::ROTR)
1537 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1538
1539 // If we aren't rotating out all of the known-in sign bits, return the
1540 // number that are left. This handles rotl(sext(x), 1) for example.
1541 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1542 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1543 }
1544 break;
1545 case ISD::ADD:
1546 // Add can have at most one carry bit. Thus we know that the output
1547 // is, at worst, one more bit than the inputs.
1548 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1549 if (Tmp == 1) return 1; // Early out.
1550
1551 // Special case decrementing a value (ADD X, -1):
1552 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1553 if (CRHS->isAllOnesValue()) {
1554 uint64_t KnownZero, KnownOne;
1555 uint64_t Mask = MVT::getIntVTBitMask(VT);
1556 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1557
1558 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1559 // sign bits set.
1560 if ((KnownZero|1) == Mask)
1561 return VTBits;
1562
1563 // If we are subtracting one from a positive number, there is no carry
1564 // out of the result.
1565 if (KnownZero & MVT::getIntVTSignBit(VT))
1566 return Tmp;
1567 }
1568
1569 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1570 if (Tmp2 == 1) return 1;
1571 return std::min(Tmp, Tmp2)-1;
1572 break;
1573
1574 case ISD::SUB:
1575 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1576 if (Tmp2 == 1) return 1;
1577
1578 // Handle NEG.
1579 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1580 if (CLHS->getValue() == 0) {
1581 uint64_t KnownZero, KnownOne;
1582 uint64_t Mask = MVT::getIntVTBitMask(VT);
1583 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1584 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1585 // sign bits set.
1586 if ((KnownZero|1) == Mask)
1587 return VTBits;
1588
1589 // If the input is known to be positive (the sign bit is known clear),
1590 // the output of the NEG has the same number of sign bits as the input.
1591 if (KnownZero & MVT::getIntVTSignBit(VT))
1592 return Tmp2;
1593
1594 // Otherwise, we treat this like a SUB.
1595 }
1596
1597 // Sub can have at most one carry bit. Thus we know that the output
1598 // is, at worst, one more bit than the inputs.
1599 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1600 if (Tmp == 1) return 1; // Early out.
1601 return std::min(Tmp, Tmp2)-1;
1602 break;
1603 case ISD::TRUNCATE:
1604 // FIXME: it's tricky to do anything useful for this, but it is an important
1605 // case for targets like X86.
1606 break;
1607 }
1608
1609 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1610 if (Op.getOpcode() == ISD::LOAD) {
1611 LoadSDNode *LD = cast<LoadSDNode>(Op);
1612 unsigned ExtType = LD->getExtensionType();
1613 switch (ExtType) {
1614 default: break;
1615 case ISD::SEXTLOAD: // '17' bits known
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001616 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 return VTBits-Tmp+1;
1618 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohman9a4c92c2008-01-30 00:15:11 +00001619 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001620 return VTBits-Tmp;
1621 }
1622 }
1623
1624 // Allow the target to implement this method for its nodes.
1625 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1626 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1627 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1628 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1629 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1630 if (NumBits > 1) return NumBits;
1631 }
1632
1633 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1634 // use this information.
1635 uint64_t KnownZero, KnownOne;
1636 uint64_t Mask = MVT::getIntVTBitMask(VT);
1637 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1638
1639 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1640 if (KnownZero & SignBit) { // SignBit is 0
1641 Mask = KnownZero;
1642 } else if (KnownOne & SignBit) { // SignBit is 1;
1643 Mask = KnownOne;
1644 } else {
1645 // Nothing known.
1646 return 1;
1647 }
1648
1649 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1650 // the number of identical bits in the top of the input value.
1651 Mask ^= ~0ULL;
1652 Mask <<= 64-VTBits;
1653 // Return # leading zeros. We use 'min' here in case Val was zero before
1654 // shifting. We don't want to return '64' as for an i32 "0".
1655 return std::min(VTBits, CountLeadingZeros_64(Mask));
1656}
1657
1658
Evan Cheng2e28d622008-02-02 04:07:54 +00001659bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1660 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1661 if (!GA) return false;
1662 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1663 if (!GV) return false;
1664 MachineModuleInfo *MMI = getMachineModuleInfo();
1665 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1666}
1667
1668
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001669/// getNode - Gets or creates the specified node.
1670///
1671SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
1672 FoldingSetNodeID ID;
1673 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
1674 void *IP = 0;
1675 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1676 return SDOperand(E, 0);
1677 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
1678 CSEMap.InsertNode(N, IP);
1679
1680 AllNodes.push_back(N);
1681 return SDOperand(N, 0);
1682}
1683
1684SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1685 SDOperand Operand) {
1686 unsigned Tmp1;
1687 // Constant fold unary operations with an integer constant operand.
1688 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1689 uint64_t Val = C->getValue();
1690 switch (Opcode) {
1691 default: break;
1692 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1693 case ISD::ANY_EXTEND:
1694 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1695 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001696 case ISD::UINT_TO_FP:
1697 case ISD::SINT_TO_FP: {
1698 const uint64_t zero[] = {0, 0};
Dale Johannesenb89072e2007-10-16 23:38:29 +00001699 // No compile time operations on this type.
1700 if (VT==MVT::ppcf128)
1701 break;
Dale Johannesen958b08b2007-09-19 23:55:34 +00001702 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Booth4bdd45a2007-10-07 11:45:55 +00001703 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesena6f79742007-09-21 22:09:37 +00001704 MVT::getSizeInBits(Operand.getValueType()),
1705 Opcode==ISD::SINT_TO_FP,
Dale Johannesen87fa68f2007-09-30 18:19:03 +00001706 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00001707 return getConstantFP(apf, VT);
1708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 case ISD::BIT_CONVERT:
1710 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
1711 return getConstantFP(BitsToFloat(Val), VT);
1712 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
1713 return getConstantFP(BitsToDouble(Val), VT);
1714 break;
1715 case ISD::BSWAP:
1716 switch(VT) {
1717 default: assert(0 && "Invalid bswap!"); break;
1718 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1719 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1720 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1721 }
1722 break;
1723 case ISD::CTPOP:
1724 switch(VT) {
1725 default: assert(0 && "Invalid ctpop!"); break;
1726 case MVT::i1: return getConstant(Val != 0, VT);
1727 case MVT::i8:
1728 Tmp1 = (unsigned)Val & 0xFF;
1729 return getConstant(CountPopulation_32(Tmp1), VT);
1730 case MVT::i16:
1731 Tmp1 = (unsigned)Val & 0xFFFF;
1732 return getConstant(CountPopulation_32(Tmp1), VT);
1733 case MVT::i32:
1734 return getConstant(CountPopulation_32((unsigned)Val), VT);
1735 case MVT::i64:
1736 return getConstant(CountPopulation_64(Val), VT);
1737 }
1738 case ISD::CTLZ:
1739 switch(VT) {
1740 default: assert(0 && "Invalid ctlz!"); break;
1741 case MVT::i1: return getConstant(Val == 0, VT);
1742 case MVT::i8:
1743 Tmp1 = (unsigned)Val & 0xFF;
1744 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1745 case MVT::i16:
1746 Tmp1 = (unsigned)Val & 0xFFFF;
1747 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1748 case MVT::i32:
1749 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1750 case MVT::i64:
1751 return getConstant(CountLeadingZeros_64(Val), VT);
1752 }
1753 case ISD::CTTZ:
1754 switch(VT) {
1755 default: assert(0 && "Invalid cttz!"); break;
1756 case MVT::i1: return getConstant(Val == 0, VT);
1757 case MVT::i8:
1758 Tmp1 = (unsigned)Val | 0x100;
1759 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1760 case MVT::i16:
1761 Tmp1 = (unsigned)Val | 0x10000;
1762 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1763 case MVT::i32:
1764 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1765 case MVT::i64:
1766 return getConstant(CountTrailingZeros_64(Val), VT);
1767 }
1768 }
1769 }
1770
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001771 // Constant fold unary operations with a floating point constant operand.
1772 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1773 APFloat V = C->getValueAPF(); // make copy
Chris Lattner5872a362008-01-17 07:00:52 +00001774 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00001775 switch (Opcode) {
1776 case ISD::FNEG:
1777 V.changeSign();
1778 return getConstantFP(V, VT);
1779 case ISD::FABS:
1780 V.clearSign();
1781 return getConstantFP(V, VT);
1782 case ISD::FP_ROUND:
1783 case ISD::FP_EXTEND:
1784 // This can return overflow, underflow, or inexact; we don't care.
1785 // FIXME need to be more flexible about rounding mode.
1786 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1787 VT==MVT::f64 ? APFloat::IEEEdouble :
1788 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1789 VT==MVT::f128 ? APFloat::IEEEquad :
1790 APFloat::Bogus,
1791 APFloat::rmNearestTiesToEven);
1792 return getConstantFP(V, VT);
1793 case ISD::FP_TO_SINT:
1794 case ISD::FP_TO_UINT: {
1795 integerPart x;
1796 assert(integerPartWidth >= 64);
1797 // FIXME need to be more flexible about rounding mode.
1798 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1799 Opcode==ISD::FP_TO_SINT,
1800 APFloat::rmTowardZero);
1801 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1802 break;
1803 return getConstant(x, VT);
1804 }
1805 case ISD::BIT_CONVERT:
1806 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1807 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1808 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1809 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001810 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00001811 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001812 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00001813 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001814
1815 unsigned OpOpcode = Operand.Val->getOpcode();
1816 switch (Opcode) {
1817 case ISD::TokenFactor:
1818 return Operand; // Factor of one node? No factor.
Chris Lattner5872a362008-01-17 07:00:52 +00001819 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001820 case ISD::FP_EXTEND:
1821 assert(MVT::isFloatingPoint(VT) &&
1822 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00001823 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001824 break;
Chris Lattner5872a362008-01-17 07:00:52 +00001825 case ISD::SIGN_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1827 "Invalid SIGN_EXTEND!");
1828 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001829 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1830 && "Invalid sext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1832 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1833 break;
1834 case ISD::ZERO_EXTEND:
1835 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1836 "Invalid ZERO_EXTEND!");
1837 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001838 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1839 && "Invalid zext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001840 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
1841 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
1842 break;
1843 case ISD::ANY_EXTEND:
1844 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1845 "Invalid ANY_EXTEND!");
1846 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsa9810f32007-10-16 09:56:48 +00001847 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1848 && "Invalid anyext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001849 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1850 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1851 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1852 break;
1853 case ISD::TRUNCATE:
1854 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1855 "Invalid TRUNCATE!");
1856 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsa9810f32007-10-16 09:56:48 +00001857 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1858 && "Invalid truncate node, src < dst!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001859 if (OpOpcode == ISD::TRUNCATE)
1860 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1861 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1862 OpOpcode == ISD::ANY_EXTEND) {
1863 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsa9810f32007-10-16 09:56:48 +00001864 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1865 < MVT::getSizeInBits(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001866 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsa9810f32007-10-16 09:56:48 +00001867 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1868 > MVT::getSizeInBits(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1870 else
1871 return Operand.Val->getOperand(0);
1872 }
1873 break;
1874 case ISD::BIT_CONVERT:
1875 // Basic sanity checking.
1876 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
1877 && "Cannot BIT_CONVERT between types of different sizes!");
1878 if (VT == Operand.getValueType()) return Operand; // noop conversion.
1879 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1880 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
1881 if (OpOpcode == ISD::UNDEF)
1882 return getNode(ISD::UNDEF, VT);
1883 break;
1884 case ISD::SCALAR_TO_VECTOR:
1885 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1886 MVT::getVectorElementType(VT) == Operand.getValueType() &&
1887 "Illegal SCALAR_TO_VECTOR node!");
1888 break;
1889 case ISD::FNEG:
1890 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1891 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
1892 Operand.Val->getOperand(0));
1893 if (OpOpcode == ISD::FNEG) // --X -> X
1894 return Operand.Val->getOperand(0);
1895 break;
1896 case ISD::FABS:
1897 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1898 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1899 break;
1900 }
1901
1902 SDNode *N;
1903 SDVTList VTs = getVTList(VT);
1904 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1905 FoldingSetNodeID ID;
1906 SDOperand Ops[1] = { Operand };
1907 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
1908 void *IP = 0;
1909 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1910 return SDOperand(E, 0);
1911 N = new UnarySDNode(Opcode, VTs, Operand);
1912 CSEMap.InsertNode(N, IP);
1913 } else {
1914 N = new UnarySDNode(Opcode, VTs, Operand);
1915 }
1916 AllNodes.push_back(N);
1917 return SDOperand(N, 0);
1918}
1919
1920
1921
1922SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1923 SDOperand N1, SDOperand N2) {
Chris Lattnercc126e32008-01-22 19:09:33 +00001924 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1925 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001926 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00001927 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001928 case ISD::TokenFactor:
1929 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1930 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00001931 // Fold trivial token factors.
1932 if (N1.getOpcode() == ISD::EntryToken) return N2;
1933 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001934 break;
1935 case ISD::AND:
Chris Lattnercc126e32008-01-22 19:09:33 +00001936 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1937 N1.getValueType() == VT && "Binary operator types must match!");
1938 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1939 // worth handling here.
1940 if (N2C && N2C->getValue() == 0)
1941 return N2;
Chris Lattner8aa8a5e2008-01-26 01:05:42 +00001942 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1943 return N1;
Chris Lattnercc126e32008-01-22 19:09:33 +00001944 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001945 case ISD::OR:
1946 case ISD::XOR:
Chris Lattnercc126e32008-01-22 19:09:33 +00001947 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1948 N1.getValueType() == VT && "Binary operator types must match!");
1949 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1950 // worth handling here.
1951 if (N2C && N2C->getValue() == 0)
1952 return N1;
1953 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001954 case ISD::UDIV:
1955 case ISD::UREM:
1956 case ISD::MULHU:
1957 case ISD::MULHS:
1958 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1959 // fall through
1960 case ISD::ADD:
1961 case ISD::SUB:
1962 case ISD::MUL:
1963 case ISD::SDIV:
1964 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965 case ISD::FADD:
1966 case ISD::FSUB:
1967 case ISD::FMUL:
1968 case ISD::FDIV:
1969 case ISD::FREM:
1970 assert(N1.getValueType() == N2.getValueType() &&
1971 N1.getValueType() == VT && "Binary operator types must match!");
1972 break;
1973 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1974 assert(N1.getValueType() == VT &&
1975 MVT::isFloatingPoint(N1.getValueType()) &&
1976 MVT::isFloatingPoint(N2.getValueType()) &&
1977 "Invalid FCOPYSIGN!");
1978 break;
1979 case ISD::SHL:
1980 case ISD::SRA:
1981 case ISD::SRL:
1982 case ISD::ROTL:
1983 case ISD::ROTR:
1984 assert(VT == N1.getValueType() &&
1985 "Shift operators return type must be the same as their first arg");
1986 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
1987 VT != MVT::i1 && "Shifts only work on integers");
1988 break;
1989 case ISD::FP_ROUND_INREG: {
1990 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1991 assert(VT == N1.getValueType() && "Not an inreg round!");
1992 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1993 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsa9810f32007-10-16 09:56:48 +00001994 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1995 "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00001996 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 break;
1998 }
Chris Lattner5872a362008-01-17 07:00:52 +00001999 case ISD::FP_ROUND:
2000 assert(MVT::isFloatingPoint(VT) &&
2001 MVT::isFloatingPoint(N1.getValueType()) &&
2002 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2003 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002004 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00002005 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002006 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00002007 case ISD::AssertZext: {
2008 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2009 assert(VT == N1.getValueType() && "Not an inreg extend!");
2010 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2011 "Cannot *_EXTEND_INREG FP types");
2012 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2013 "Not extending!");
2014 break;
2015 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 case ISD::SIGN_EXTEND_INREG: {
2017 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2018 assert(VT == N1.getValueType() && "Not an inreg extend!");
2019 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2020 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsa9810f32007-10-16 09:56:48 +00002021 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2022 "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002023 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002024
Chris Lattnercc126e32008-01-22 19:09:33 +00002025 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 int64_t Val = N1C->getValue();
2027 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
2028 Val <<= 64-FromBits;
2029 Val >>= 64-FromBits;
2030 return getConstant(Val, VT);
2031 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002032 break;
2033 }
2034 case ISD::EXTRACT_VECTOR_ELT:
2035 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2036
2037 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2038 // expanding copies of large vectors from registers.
2039 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2040 N1.getNumOperands() > 0) {
2041 unsigned Factor =
2042 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2043 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2044 N1.getOperand(N2C->getValue() / Factor),
2045 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2046 }
2047
2048 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2049 // expanding large vector constants.
2050 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2051 return N1.getOperand(N2C->getValue());
2052
2053 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2054 // operations are lowered to scalars.
2055 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2056 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2057 if (IEC == N2C)
2058 return N1.getOperand(1);
2059 else
2060 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2061 }
2062 break;
2063 case ISD::EXTRACT_ELEMENT:
2064 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002065
Chris Lattnercc126e32008-01-22 19:09:33 +00002066 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2067 // 64-bit integers into 32-bit parts. Instead of building the extract of
2068 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2069 if (N1.getOpcode() == ISD::BUILD_PAIR)
2070 return N1.getOperand(N2C->getValue());
2071
2072 // EXTRACT_ELEMENT of a constant int is also very common.
2073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2074 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2075 return getConstant(C->getValue() >> Shift, VT);
2076 }
2077 break;
2078 }
2079
2080 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002081 if (N2C) {
2082 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
2083 switch (Opcode) {
2084 case ISD::ADD: return getConstant(C1 + C2, VT);
2085 case ISD::SUB: return getConstant(C1 - C2, VT);
2086 case ISD::MUL: return getConstant(C1 * C2, VT);
2087 case ISD::UDIV:
2088 if (C2) return getConstant(C1 / C2, VT);
2089 break;
2090 case ISD::UREM :
2091 if (C2) return getConstant(C1 % C2, VT);
2092 break;
2093 case ISD::SDIV :
2094 if (C2) return getConstant(N1C->getSignExtended() /
2095 N2C->getSignExtended(), VT);
2096 break;
2097 case ISD::SREM :
2098 if (C2) return getConstant(N1C->getSignExtended() %
2099 N2C->getSignExtended(), VT);
2100 break;
2101 case ISD::AND : return getConstant(C1 & C2, VT);
2102 case ISD::OR : return getConstant(C1 | C2, VT);
2103 case ISD::XOR : return getConstant(C1 ^ C2, VT);
2104 case ISD::SHL : return getConstant(C1 << C2, VT);
2105 case ISD::SRL : return getConstant(C1 >> C2, VT);
2106 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
2107 case ISD::ROTL :
2108 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
2109 VT);
2110 case ISD::ROTR :
2111 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
2112 VT);
2113 default: break;
2114 }
2115 } else { // Cannonicalize constant to RHS if commutative
2116 if (isCommutativeBinOp(Opcode)) {
2117 std::swap(N1C, N2C);
2118 std::swap(N1, N2);
2119 }
2120 }
2121 }
2122
Chris Lattnercc126e32008-01-22 19:09:33 +00002123 // Constant fold FP operations.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002124 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2125 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
2126 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002127 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2128 // Cannonicalize constant to RHS if commutative
2129 std::swap(N1CFP, N2CFP);
2130 std::swap(N1, N2);
2131 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002132 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2133 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002134 switch (Opcode) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002135 case ISD::FADD:
2136 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002137 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002138 return getConstantFP(V1, VT);
2139 break;
2140 case ISD::FSUB:
2141 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2142 if (s!=APFloat::opInvalidOp)
2143 return getConstantFP(V1, VT);
2144 break;
2145 case ISD::FMUL:
2146 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2147 if (s!=APFloat::opInvalidOp)
2148 return getConstantFP(V1, VT);
2149 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002151 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2152 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2153 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 break;
2155 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002156 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2157 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2158 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002160 case ISD::FCOPYSIGN:
2161 V1.copySign(V2);
2162 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 default: break;
2164 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002165 }
2166 }
2167
2168 // Canonicalize an UNDEF to the RHS, even over a constant.
2169 if (N1.getOpcode() == ISD::UNDEF) {
2170 if (isCommutativeBinOp(Opcode)) {
2171 std::swap(N1, N2);
2172 } else {
2173 switch (Opcode) {
2174 case ISD::FP_ROUND_INREG:
2175 case ISD::SIGN_EXTEND_INREG:
2176 case ISD::SUB:
2177 case ISD::FSUB:
2178 case ISD::FDIV:
2179 case ISD::FREM:
2180 case ISD::SRA:
2181 return N1; // fold op(undef, arg2) -> undef
2182 case ISD::UDIV:
2183 case ISD::SDIV:
2184 case ISD::UREM:
2185 case ISD::SREM:
2186 case ISD::SRL:
2187 case ISD::SHL:
2188 if (!MVT::isVector(VT))
2189 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2190 // For vectors, we can't easily build an all zero vector, just return
2191 // the LHS.
2192 return N2;
2193 }
2194 }
2195 }
2196
2197 // Fold a bunch of operators when the RHS is undef.
2198 if (N2.getOpcode() == ISD::UNDEF) {
2199 switch (Opcode) {
2200 case ISD::ADD:
2201 case ISD::ADDC:
2202 case ISD::ADDE:
2203 case ISD::SUB:
2204 case ISD::FADD:
2205 case ISD::FSUB:
2206 case ISD::FMUL:
2207 case ISD::FDIV:
2208 case ISD::FREM:
2209 case ISD::UDIV:
2210 case ISD::SDIV:
2211 case ISD::UREM:
2212 case ISD::SREM:
2213 case ISD::XOR:
2214 return N2; // fold op(arg1, undef) -> undef
2215 case ISD::MUL:
2216 case ISD::AND:
2217 case ISD::SRL:
2218 case ISD::SHL:
2219 if (!MVT::isVector(VT))
2220 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2221 // For vectors, we can't easily build an all zero vector, just return
2222 // the LHS.
2223 return N1;
2224 case ISD::OR:
2225 if (!MVT::isVector(VT))
2226 return getConstant(MVT::getIntVTBitMask(VT), VT);
2227 // For vectors, we can't easily build an all one vector, just return
2228 // the LHS.
2229 return N1;
2230 case ISD::SRA:
2231 return N1;
2232 }
2233 }
2234
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002235 // Memoize this node if possible.
2236 SDNode *N;
2237 SDVTList VTs = getVTList(VT);
2238 if (VT != MVT::Flag) {
2239 SDOperand Ops[] = { N1, N2 };
2240 FoldingSetNodeID ID;
2241 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2242 void *IP = 0;
2243 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2244 return SDOperand(E, 0);
2245 N = new BinarySDNode(Opcode, VTs, N1, N2);
2246 CSEMap.InsertNode(N, IP);
2247 } else {
2248 N = new BinarySDNode(Opcode, VTs, N1, N2);
2249 }
2250
2251 AllNodes.push_back(N);
2252 return SDOperand(N, 0);
2253}
2254
2255SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2256 SDOperand N1, SDOperand N2, SDOperand N3) {
2257 // Perform various simplifications.
2258 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2259 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
2260 switch (Opcode) {
2261 case ISD::SETCC: {
2262 // Use FoldSetCC to simplify SETCC's.
2263 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
2264 if (Simp.Val) return Simp;
2265 break;
2266 }
2267 case ISD::SELECT:
2268 if (N1C)
2269 if (N1C->getValue())
2270 return N2; // select true, X, Y -> X
2271 else
2272 return N3; // select false, X, Y -> Y
2273
2274 if (N2 == N3) return N2; // select C, X, X -> X
2275 break;
2276 case ISD::BRCOND:
2277 if (N2C)
2278 if (N2C->getValue()) // Unconditional branch
2279 return getNode(ISD::BR, MVT::Other, N1, N3);
2280 else
2281 return N1; // Never-taken branch
2282 break;
2283 case ISD::VECTOR_SHUFFLE:
2284 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2285 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2286 N3.getOpcode() == ISD::BUILD_VECTOR &&
2287 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2288 "Illegal VECTOR_SHUFFLE node!");
2289 break;
2290 case ISD::BIT_CONVERT:
2291 // Fold bit_convert nodes from a type to themselves.
2292 if (N1.getValueType() == VT)
2293 return N1;
2294 break;
2295 }
2296
2297 // Memoize node if it doesn't produce a flag.
2298 SDNode *N;
2299 SDVTList VTs = getVTList(VT);
2300 if (VT != MVT::Flag) {
2301 SDOperand Ops[] = { N1, N2, N3 };
2302 FoldingSetNodeID ID;
2303 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2304 void *IP = 0;
2305 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2306 return SDOperand(E, 0);
2307 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2308 CSEMap.InsertNode(N, IP);
2309 } else {
2310 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
2311 }
2312 AllNodes.push_back(N);
2313 return SDOperand(N, 0);
2314}
2315
2316SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2317 SDOperand N1, SDOperand N2, SDOperand N3,
2318 SDOperand N4) {
2319 SDOperand Ops[] = { N1, N2, N3, N4 };
2320 return getNode(Opcode, VT, Ops, 4);
2321}
2322
2323SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2324 SDOperand N1, SDOperand N2, SDOperand N3,
2325 SDOperand N4, SDOperand N5) {
2326 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2327 return getNode(Opcode, VT, Ops, 5);
2328}
2329
Rafael Espindola80825902007-10-19 10:41:11 +00002330SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2331 SDOperand Src, SDOperand Size,
2332 SDOperand Align,
2333 SDOperand AlwaysInline) {
2334 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2335 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2336}
2337
2338SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2339 SDOperand Src, SDOperand Size,
2340 SDOperand Align,
2341 SDOperand AlwaysInline) {
2342 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2343 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2344}
2345
2346SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2347 SDOperand Src, SDOperand Size,
2348 SDOperand Align,
2349 SDOperand AlwaysInline) {
2350 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2351 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2352}
2353
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002354SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2355 SDOperand Chain, SDOperand Ptr,
2356 const Value *SV, int SVOffset,
2357 bool isVolatile, unsigned Alignment) {
2358 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2359 const Type *Ty = 0;
2360 if (VT != MVT::iPTR) {
2361 Ty = MVT::getTypeForValueType(VT);
2362 } else if (SV) {
2363 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2364 assert(PT && "Value for load must be a pointer");
2365 Ty = PT->getElementType();
2366 }
2367 assert(Ty && "Could not get type information for load");
2368 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2369 }
2370 SDVTList VTs = getVTList(VT, MVT::Other);
2371 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2372 SDOperand Ops[] = { Chain, Ptr, Undef };
2373 FoldingSetNodeID ID;
2374 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2375 ID.AddInteger(ISD::UNINDEXED);
2376 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner4a22a672007-09-13 06:09:48 +00002377 ID.AddInteger((unsigned int)VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378 ID.AddInteger(Alignment);
2379 ID.AddInteger(isVolatile);
2380 void *IP = 0;
2381 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2382 return SDOperand(E, 0);
2383 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
2384 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2385 isVolatile);
2386 CSEMap.InsertNode(N, IP);
2387 AllNodes.push_back(N);
2388 return SDOperand(N, 0);
2389}
2390
2391SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
2392 SDOperand Chain, SDOperand Ptr,
2393 const Value *SV,
2394 int SVOffset, MVT::ValueType EVT,
2395 bool isVolatile, unsigned Alignment) {
2396 // If they are asking for an extending load from/to the same thing, return a
2397 // normal load.
2398 if (VT == EVT)
Duncan Sands9b614742007-10-19 13:05:40 +00002399 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002400
2401 if (MVT::isVector(VT))
2402 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
2403 else
Duncan Sandsa9810f32007-10-16 09:56:48 +00002404 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2405 "Should only be an extending load, not truncating!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002406 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2407 "Cannot sign/zero extend a FP/Vector load!");
2408 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2409 "Cannot convert from FP to Int or Int -> FP!");
2410
2411 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2412 const Type *Ty = 0;
2413 if (VT != MVT::iPTR) {
2414 Ty = MVT::getTypeForValueType(VT);
2415 } else if (SV) {
2416 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2417 assert(PT && "Value for load must be a pointer");
2418 Ty = PT->getElementType();
2419 }
2420 assert(Ty && "Could not get type information for load");
2421 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2422 }
2423 SDVTList VTs = getVTList(VT, MVT::Other);
2424 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2425 SDOperand Ops[] = { Chain, Ptr, Undef };
2426 FoldingSetNodeID ID;
2427 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2428 ID.AddInteger(ISD::UNINDEXED);
2429 ID.AddInteger(ExtType);
Chris Lattner4a22a672007-09-13 06:09:48 +00002430 ID.AddInteger((unsigned int)EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 ID.AddInteger(Alignment);
2432 ID.AddInteger(isVolatile);
2433 void *IP = 0;
2434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2435 return SDOperand(E, 0);
2436 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
2437 SV, SVOffset, Alignment, isVolatile);
2438 CSEMap.InsertNode(N, IP);
2439 AllNodes.push_back(N);
2440 return SDOperand(N, 0);
2441}
2442
2443SDOperand
2444SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2445 SDOperand Offset, ISD::MemIndexedMode AM) {
2446 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
2447 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2448 "Load is already a indexed load!");
2449 MVT::ValueType VT = OrigLoad.getValueType();
2450 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
2451 SDOperand Ops[] = { LD->getChain(), Base, Offset };
2452 FoldingSetNodeID ID;
2453 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
2454 ID.AddInteger(AM);
2455 ID.AddInteger(LD->getExtensionType());
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002456 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 ID.AddInteger(LD->getAlignment());
2458 ID.AddInteger(LD->isVolatile());
2459 void *IP = 0;
2460 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2461 return SDOperand(E, 0);
2462 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002463 LD->getExtensionType(), LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002464 LD->getSrcValue(), LD->getSrcValueOffset(),
2465 LD->getAlignment(), LD->isVolatile());
2466 CSEMap.InsertNode(N, IP);
2467 AllNodes.push_back(N);
2468 return SDOperand(N, 0);
2469}
2470
2471SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
2472 SDOperand Ptr, const Value *SV, int SVOffset,
2473 bool isVolatile, unsigned Alignment) {
2474 MVT::ValueType VT = Val.getValueType();
2475
2476 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2477 const Type *Ty = 0;
2478 if (VT != MVT::iPTR) {
2479 Ty = MVT::getTypeForValueType(VT);
2480 } else if (SV) {
2481 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2482 assert(PT && "Value for store must be a pointer");
2483 Ty = PT->getElementType();
2484 }
2485 assert(Ty && "Could not get type information for store");
2486 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2487 }
2488 SDVTList VTs = getVTList(MVT::Other);
2489 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2490 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2491 FoldingSetNodeID ID;
2492 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2493 ID.AddInteger(ISD::UNINDEXED);
2494 ID.AddInteger(false);
Chris Lattner4a22a672007-09-13 06:09:48 +00002495 ID.AddInteger((unsigned int)VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 ID.AddInteger(Alignment);
2497 ID.AddInteger(isVolatile);
2498 void *IP = 0;
2499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2500 return SDOperand(E, 0);
2501 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
2502 VT, SV, SVOffset, Alignment, isVolatile);
2503 CSEMap.InsertNode(N, IP);
2504 AllNodes.push_back(N);
2505 return SDOperand(N, 0);
2506}
2507
2508SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
2509 SDOperand Ptr, const Value *SV,
2510 int SVOffset, MVT::ValueType SVT,
2511 bool isVolatile, unsigned Alignment) {
2512 MVT::ValueType VT = Val.getValueType();
Duncan Sands06fcf652007-10-30 12:40:58 +00002513
2514 if (VT == SVT)
2515 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516
Duncan Sandsa9810f32007-10-16 09:56:48 +00002517 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2518 "Not a truncation?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2520 "Can't do FP-INT conversion!");
2521
2522 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2523 const Type *Ty = 0;
2524 if (VT != MVT::iPTR) {
2525 Ty = MVT::getTypeForValueType(VT);
2526 } else if (SV) {
2527 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2528 assert(PT && "Value for store must be a pointer");
2529 Ty = PT->getElementType();
2530 }
2531 assert(Ty && "Could not get type information for store");
2532 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2533 }
2534 SDVTList VTs = getVTList(MVT::Other);
2535 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
2536 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
2537 FoldingSetNodeID ID;
2538 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2539 ID.AddInteger(ISD::UNINDEXED);
Duncan Sands06fcf652007-10-30 12:40:58 +00002540 ID.AddInteger(1);
Chris Lattner4a22a672007-09-13 06:09:48 +00002541 ID.AddInteger((unsigned int)SVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002542 ID.AddInteger(Alignment);
2543 ID.AddInteger(isVolatile);
2544 void *IP = 0;
2545 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2546 return SDOperand(E, 0);
Duncan Sands06fcf652007-10-30 12:40:58 +00002547 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 SVT, SV, SVOffset, Alignment, isVolatile);
2549 CSEMap.InsertNode(N, IP);
2550 AllNodes.push_back(N);
2551 return SDOperand(N, 0);
2552}
2553
2554SDOperand
2555SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2556 SDOperand Offset, ISD::MemIndexedMode AM) {
2557 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2558 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2559 "Store is already a indexed store!");
2560 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2561 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2562 FoldingSetNodeID ID;
2563 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2564 ID.AddInteger(AM);
2565 ID.AddInteger(ST->isTruncatingStore());
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002566 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002567 ID.AddInteger(ST->getAlignment());
2568 ID.AddInteger(ST->isVolatile());
2569 void *IP = 0;
2570 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2571 return SDOperand(E, 0);
2572 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohman9a4c92c2008-01-30 00:15:11 +00002573 ST->isTruncatingStore(), ST->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002574 ST->getSrcValue(), ST->getSrcValueOffset(),
2575 ST->getAlignment(), ST->isVolatile());
2576 CSEMap.InsertNode(N, IP);
2577 AllNodes.push_back(N);
2578 return SDOperand(N, 0);
2579}
2580
2581SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2582 SDOperand Chain, SDOperand Ptr,
2583 SDOperand SV) {
2584 SDOperand Ops[] = { Chain, Ptr, SV };
2585 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
2586}
2587
2588SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2589 const SDOperand *Ops, unsigned NumOps) {
2590 switch (NumOps) {
2591 case 0: return getNode(Opcode, VT);
2592 case 1: return getNode(Opcode, VT, Ops[0]);
2593 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2594 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
2595 default: break;
2596 }
2597
2598 switch (Opcode) {
2599 default: break;
2600 case ISD::SELECT_CC: {
2601 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
2602 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2603 "LHS and RHS of condition must have same type!");
2604 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2605 "True and False arms of SelectCC must have same type!");
2606 assert(Ops[2].getValueType() == VT &&
2607 "select_cc node must be of same type as true and false value!");
2608 break;
2609 }
2610 case ISD::BR_CC: {
2611 assert(NumOps == 5 && "BR_CC takes 5 operands!");
2612 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2613 "LHS/RHS of comparison should match types!");
2614 break;
2615 }
2616 }
2617
2618 // Memoize nodes.
2619 SDNode *N;
2620 SDVTList VTs = getVTList(VT);
2621 if (VT != MVT::Flag) {
2622 FoldingSetNodeID ID;
2623 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
2624 void *IP = 0;
2625 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2626 return SDOperand(E, 0);
2627 N = new SDNode(Opcode, VTs, Ops, NumOps);
2628 CSEMap.InsertNode(N, IP);
2629 } else {
2630 N = new SDNode(Opcode, VTs, Ops, NumOps);
2631 }
2632 AllNodes.push_back(N);
2633 return SDOperand(N, 0);
2634}
2635
2636SDOperand SelectionDAG::getNode(unsigned Opcode,
2637 std::vector<MVT::ValueType> &ResultTys,
2638 const SDOperand *Ops, unsigned NumOps) {
2639 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2640 Ops, NumOps);
2641}
2642
2643SDOperand SelectionDAG::getNode(unsigned Opcode,
2644 const MVT::ValueType *VTs, unsigned NumVTs,
2645 const SDOperand *Ops, unsigned NumOps) {
2646 if (NumVTs == 1)
2647 return getNode(Opcode, VTs[0], Ops, NumOps);
2648 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2649}
2650
2651SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2652 const SDOperand *Ops, unsigned NumOps) {
2653 if (VTList.NumVTs == 1)
2654 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
2655
2656 switch (Opcode) {
2657 // FIXME: figure out how to safely handle things like
2658 // int foo(int x) { return 1 << (x & 255); }
2659 // int bar() { return foo(256); }
2660#if 0
2661 case ISD::SRA_PARTS:
2662 case ISD::SRL_PARTS:
2663 case ISD::SHL_PARTS:
2664 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
2665 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
2666 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2667 else if (N3.getOpcode() == ISD::AND)
2668 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2669 // If the and is only masking out bits that cannot effect the shift,
2670 // eliminate the and.
2671 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2672 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2673 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2674 }
2675 break;
2676#endif
2677 }
2678
2679 // Memoize the node unless it returns a flag.
2680 SDNode *N;
2681 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
2682 FoldingSetNodeID ID;
2683 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
2684 void *IP = 0;
2685 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2686 return SDOperand(E, 0);
2687 if (NumOps == 1)
2688 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2689 else if (NumOps == 2)
2690 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2691 else if (NumOps == 3)
2692 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2693 else
2694 N = new SDNode(Opcode, VTList, Ops, NumOps);
2695 CSEMap.InsertNode(N, IP);
2696 } else {
2697 if (NumOps == 1)
2698 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2699 else if (NumOps == 2)
2700 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2701 else if (NumOps == 3)
2702 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2703 else
2704 N = new SDNode(Opcode, VTList, Ops, NumOps);
2705 }
2706 AllNodes.push_back(N);
2707 return SDOperand(N, 0);
2708}
2709
Dan Gohman798d1272007-10-08 15:49:58 +00002710SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2711 return getNode(Opcode, VTList, 0, 0);
2712}
2713
2714SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2715 SDOperand N1) {
2716 SDOperand Ops[] = { N1 };
2717 return getNode(Opcode, VTList, Ops, 1);
2718}
2719
2720SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2721 SDOperand N1, SDOperand N2) {
2722 SDOperand Ops[] = { N1, N2 };
2723 return getNode(Opcode, VTList, Ops, 2);
2724}
2725
2726SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2727 SDOperand N1, SDOperand N2, SDOperand N3) {
2728 SDOperand Ops[] = { N1, N2, N3 };
2729 return getNode(Opcode, VTList, Ops, 3);
2730}
2731
2732SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2733 SDOperand N1, SDOperand N2, SDOperand N3,
2734 SDOperand N4) {
2735 SDOperand Ops[] = { N1, N2, N3, N4 };
2736 return getNode(Opcode, VTList, Ops, 4);
2737}
2738
2739SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2740 SDOperand N1, SDOperand N2, SDOperand N3,
2741 SDOperand N4, SDOperand N5) {
2742 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2743 return getNode(Opcode, VTList, Ops, 5);
2744}
2745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00002747 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748}
2749
2750SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
2751 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2752 E = VTList.end(); I != E; ++I) {
2753 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
2754 return makeVTList(&(*I)[0], 2);
2755 }
2756 std::vector<MVT::ValueType> V;
2757 V.push_back(VT1);
2758 V.push_back(VT2);
2759 VTList.push_front(V);
2760 return makeVTList(&(*VTList.begin())[0], 2);
2761}
2762SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2763 MVT::ValueType VT3) {
2764 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2765 E = VTList.end(); I != E; ++I) {
2766 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2767 (*I)[2] == VT3)
2768 return makeVTList(&(*I)[0], 3);
2769 }
2770 std::vector<MVT::ValueType> V;
2771 V.push_back(VT1);
2772 V.push_back(VT2);
2773 V.push_back(VT3);
2774 VTList.push_front(V);
2775 return makeVTList(&(*VTList.begin())[0], 3);
2776}
2777
2778SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2779 switch (NumVTs) {
2780 case 0: assert(0 && "Cannot have nodes without results!");
2781 case 1: return getVTList(VTs[0]);
2782 case 2: return getVTList(VTs[0], VTs[1]);
2783 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2784 default: break;
2785 }
2786
2787 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2788 E = VTList.end(); I != E; ++I) {
2789 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2790
2791 bool NoMatch = false;
2792 for (unsigned i = 2; i != NumVTs; ++i)
2793 if (VTs[i] != (*I)[i]) {
2794 NoMatch = true;
2795 break;
2796 }
2797 if (!NoMatch)
2798 return makeVTList(&*I->begin(), NumVTs);
2799 }
2800
2801 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2802 return makeVTList(&*VTList.begin()->begin(), NumVTs);
2803}
2804
2805
2806/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2807/// specified operands. If the resultant node already exists in the DAG,
2808/// this does not modify the specified node, instead it returns the node that
2809/// already exists. If the resultant node does not exist in the DAG, the
2810/// input node is returned. As a degenerate case, if you specify the same
2811/// input operands as the node already has, the input node is returned.
2812SDOperand SelectionDAG::
2813UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2814 SDNode *N = InN.Val;
2815 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2816
2817 // Check to see if there is no change.
2818 if (Op == N->getOperand(0)) return InN;
2819
2820 // See if the modified node already exists.
2821 void *InsertPos = 0;
2822 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2823 return SDOperand(Existing, InN.ResNo);
2824
2825 // Nope it doesn't. Remove the node from it's current place in the maps.
2826 if (InsertPos)
2827 RemoveNodeFromCSEMaps(N);
2828
2829 // Now we update the operands.
2830 N->OperandList[0].Val->removeUser(N);
2831 Op.Val->addUser(N);
2832 N->OperandList[0] = Op;
2833
2834 // If this gets put into a CSE map, add it.
2835 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2836 return InN;
2837}
2838
2839SDOperand SelectionDAG::
2840UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2841 SDNode *N = InN.Val;
2842 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2843
2844 // Check to see if there is no change.
2845 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2846 return InN; // No operands changed, just return the input node.
2847
2848 // See if the modified node already exists.
2849 void *InsertPos = 0;
2850 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2851 return SDOperand(Existing, InN.ResNo);
2852
2853 // Nope it doesn't. Remove the node from it's current place in the maps.
2854 if (InsertPos)
2855 RemoveNodeFromCSEMaps(N);
2856
2857 // Now we update the operands.
2858 if (N->OperandList[0] != Op1) {
2859 N->OperandList[0].Val->removeUser(N);
2860 Op1.Val->addUser(N);
2861 N->OperandList[0] = Op1;
2862 }
2863 if (N->OperandList[1] != Op2) {
2864 N->OperandList[1].Val->removeUser(N);
2865 Op2.Val->addUser(N);
2866 N->OperandList[1] = Op2;
2867 }
2868
2869 // If this gets put into a CSE map, add it.
2870 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2871 return InN;
2872}
2873
2874SDOperand SelectionDAG::
2875UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2876 SDOperand Ops[] = { Op1, Op2, Op3 };
2877 return UpdateNodeOperands(N, Ops, 3);
2878}
2879
2880SDOperand SelectionDAG::
2881UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2882 SDOperand Op3, SDOperand Op4) {
2883 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2884 return UpdateNodeOperands(N, Ops, 4);
2885}
2886
2887SDOperand SelectionDAG::
2888UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2889 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2890 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2891 return UpdateNodeOperands(N, Ops, 5);
2892}
2893
2894
2895SDOperand SelectionDAG::
2896UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
2897 SDNode *N = InN.Val;
2898 assert(N->getNumOperands() == NumOps &&
2899 "Update with wrong number of operands");
2900
2901 // Check to see if there is no change.
2902 bool AnyChange = false;
2903 for (unsigned i = 0; i != NumOps; ++i) {
2904 if (Ops[i] != N->getOperand(i)) {
2905 AnyChange = true;
2906 break;
2907 }
2908 }
2909
2910 // No operands changed, just return the input node.
2911 if (!AnyChange) return InN;
2912
2913 // See if the modified node already exists.
2914 void *InsertPos = 0;
2915 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
2916 return SDOperand(Existing, InN.ResNo);
2917
2918 // Nope it doesn't. Remove the node from it's current place in the maps.
2919 if (InsertPos)
2920 RemoveNodeFromCSEMaps(N);
2921
2922 // Now we update the operands.
2923 for (unsigned i = 0; i != NumOps; ++i) {
2924 if (N->OperandList[i] != Ops[i]) {
2925 N->OperandList[i].Val->removeUser(N);
2926 Ops[i].Val->addUser(N);
2927 N->OperandList[i] = Ops[i];
2928 }
2929 }
2930
2931 // If this gets put into a CSE map, add it.
2932 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
2933 return InN;
2934}
2935
2936
2937/// MorphNodeTo - This frees the operands of the current node, resets the
2938/// opcode, types, and operands to the specified value. This should only be
2939/// used by the SelectionDAG class.
2940void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2941 const SDOperand *Ops, unsigned NumOps) {
2942 NodeType = Opc;
2943 ValueList = L.VTs;
2944 NumValues = L.NumVTs;
2945
2946 // Clear the operands list, updating used nodes to remove this from their
2947 // use list.
2948 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2949 I->Val->removeUser(this);
2950
2951 // If NumOps is larger than the # of operands we currently have, reallocate
2952 // the operand list.
2953 if (NumOps > NumOperands) {
2954 if (OperandsNeedDelete)
2955 delete [] OperandList;
2956 OperandList = new SDOperand[NumOps];
2957 OperandsNeedDelete = true;
2958 }
2959
2960 // Assign the new operands.
2961 NumOperands = NumOps;
2962
2963 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2964 OperandList[i] = Ops[i];
2965 SDNode *N = OperandList[i].Val;
2966 N->Uses.push_back(this);
2967 }
2968}
2969
2970/// SelectNodeTo - These are used for target selectors to *mutate* the
2971/// specified node to have the specified return type, Target opcode, and
2972/// operands. Note that target opcodes are stored as
2973/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
2974///
2975/// Note that SelectNodeTo returns the resultant node. If there is already a
2976/// node of the specified opcode and operands, it returns that node instead of
2977/// the current one.
2978SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2979 MVT::ValueType VT) {
2980 SDVTList VTs = getVTList(VT);
2981 FoldingSetNodeID ID;
2982 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2983 void *IP = 0;
2984 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2985 return ON;
2986
2987 RemoveNodeFromCSEMaps(N);
2988
2989 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
2990
2991 CSEMap.InsertNode(N, IP);
2992 return N;
2993}
2994
2995SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2996 MVT::ValueType VT, SDOperand Op1) {
2997 // If an identical node already exists, use it.
2998 SDVTList VTs = getVTList(VT);
2999 SDOperand Ops[] = { Op1 };
3000
3001 FoldingSetNodeID ID;
3002 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3003 void *IP = 0;
3004 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3005 return ON;
3006
3007 RemoveNodeFromCSEMaps(N);
3008 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
3009 CSEMap.InsertNode(N, IP);
3010 return N;
3011}
3012
3013SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3014 MVT::ValueType VT, SDOperand Op1,
3015 SDOperand Op2) {
3016 // If an identical node already exists, use it.
3017 SDVTList VTs = getVTList(VT);
3018 SDOperand Ops[] = { Op1, Op2 };
3019
3020 FoldingSetNodeID ID;
3021 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3022 void *IP = 0;
3023 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3024 return ON;
3025
3026 RemoveNodeFromCSEMaps(N);
3027
3028 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3029
3030 CSEMap.InsertNode(N, IP); // Memoize the new node.
3031 return N;
3032}
3033
3034SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3035 MVT::ValueType VT, SDOperand Op1,
3036 SDOperand Op2, SDOperand Op3) {
3037 // If an identical node already exists, use it.
3038 SDVTList VTs = getVTList(VT);
3039 SDOperand Ops[] = { Op1, Op2, Op3 };
3040 FoldingSetNodeID ID;
3041 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3042 void *IP = 0;
3043 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3044 return ON;
3045
3046 RemoveNodeFromCSEMaps(N);
3047
3048 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3049
3050 CSEMap.InsertNode(N, IP); // Memoize the new node.
3051 return N;
3052}
3053
3054SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3055 MVT::ValueType VT, const SDOperand *Ops,
3056 unsigned NumOps) {
3057 // If an identical node already exists, use it.
3058 SDVTList VTs = getVTList(VT);
3059 FoldingSetNodeID ID;
3060 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3061 void *IP = 0;
3062 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3063 return ON;
3064
3065 RemoveNodeFromCSEMaps(N);
3066 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
3067
3068 CSEMap.InsertNode(N, IP); // Memoize the new node.
3069 return N;
3070}
3071
3072SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3073 MVT::ValueType VT1, MVT::ValueType VT2,
3074 SDOperand Op1, SDOperand Op2) {
3075 SDVTList VTs = getVTList(VT1, VT2);
3076 FoldingSetNodeID ID;
3077 SDOperand Ops[] = { Op1, Op2 };
3078 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3079 void *IP = 0;
3080 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3081 return ON;
3082
3083 RemoveNodeFromCSEMaps(N);
3084 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
3085 CSEMap.InsertNode(N, IP); // Memoize the new node.
3086 return N;
3087}
3088
3089SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3090 MVT::ValueType VT1, MVT::ValueType VT2,
3091 SDOperand Op1, SDOperand Op2,
3092 SDOperand Op3) {
3093 // If an identical node already exists, use it.
3094 SDVTList VTs = getVTList(VT1, VT2);
3095 SDOperand Ops[] = { Op1, Op2, Op3 };
3096 FoldingSetNodeID ID;
3097 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3098 void *IP = 0;
3099 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3100 return ON;
3101
3102 RemoveNodeFromCSEMaps(N);
3103
3104 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
3105 CSEMap.InsertNode(N, IP); // Memoize the new node.
3106 return N;
3107}
3108
3109
3110/// getTargetNode - These are used for target selectors to create a new node
3111/// with specified return type(s), target opcode, and operands.
3112///
3113/// Note that getTargetNode returns the resultant node. If there is already a
3114/// node of the specified opcode and operands, it returns that node instead of
3115/// the current one.
3116SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3117 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3118}
3119SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3120 SDOperand Op1) {
3121 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3122}
3123SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3124 SDOperand Op1, SDOperand Op2) {
3125 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3126}
3127SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3128 SDOperand Op1, SDOperand Op2,
3129 SDOperand Op3) {
3130 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3131}
3132SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3133 const SDOperand *Ops, unsigned NumOps) {
3134 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
3135}
3136SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen3d8578b2007-10-10 01:01:31 +00003137 MVT::ValueType VT2) {
3138 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3139 SDOperand Op;
3140 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3141}
3142SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143 MVT::ValueType VT2, SDOperand Op1) {
3144 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3145 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
3146}
3147SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3148 MVT::ValueType VT2, SDOperand Op1,
3149 SDOperand Op2) {
3150 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3151 SDOperand Ops[] = { Op1, Op2 };
3152 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
3153}
3154SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3155 MVT::ValueType VT2, SDOperand Op1,
3156 SDOperand Op2, SDOperand Op3) {
3157 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3158 SDOperand Ops[] = { Op1, Op2, Op3 };
3159 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
3160}
3161SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3162 MVT::ValueType VT2,
3163 const SDOperand *Ops, unsigned NumOps) {
3164 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3165 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
3166}
3167SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3168 MVT::ValueType VT2, MVT::ValueType VT3,
3169 SDOperand Op1, SDOperand Op2) {
3170 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3171 SDOperand Ops[] = { Op1, Op2 };
3172 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
3173}
3174SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3175 MVT::ValueType VT2, MVT::ValueType VT3,
3176 SDOperand Op1, SDOperand Op2,
3177 SDOperand Op3) {
3178 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3179 SDOperand Ops[] = { Op1, Op2, Op3 };
3180 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3181}
3182SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3183 MVT::ValueType VT2, MVT::ValueType VT3,
3184 const SDOperand *Ops, unsigned NumOps) {
3185 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3186 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
3187}
Evan Chenge1d067e2007-09-12 23:39:49 +00003188SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3189 MVT::ValueType VT2, MVT::ValueType VT3,
3190 MVT::ValueType VT4,
3191 const SDOperand *Ops, unsigned NumOps) {
3192 std::vector<MVT::ValueType> VTList;
3193 VTList.push_back(VT1);
3194 VTList.push_back(VT2);
3195 VTList.push_back(VT3);
3196 VTList.push_back(VT4);
3197 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3198 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3199}
Evan Chenge3940912007-10-05 01:10:49 +00003200SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3201 std::vector<MVT::ValueType> &ResultTys,
3202 const SDOperand *Ops, unsigned NumOps) {
3203 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3204 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3205 Ops, NumOps).Val;
3206}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003207
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003208
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003209/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3210/// This can cause recursive merging of nodes in the DAG.
3211///
Chris Lattnerdca329f2008-02-03 03:35:22 +00003212/// This version assumes From has a single result value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213///
Chris Lattnerdca329f2008-02-03 03:35:22 +00003214void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003215 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00003216 SDNode *From = FromN.Val;
Chris Lattnerdca329f2008-02-03 03:35:22 +00003217 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003218 "Cannot replace with this method!");
Chris Lattnerdca329f2008-02-03 03:35:22 +00003219 assert(From != To.Val && "Cannot replace uses of with self");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220
3221 while (!From->use_empty()) {
3222 // Process users until they are all gone.
3223 SDNode *U = *From->use_begin();
3224
3225 // This node is about to morph, remove its old self from the CSE maps.
3226 RemoveNodeFromCSEMaps(U);
3227
3228 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3229 I != E; ++I)
3230 if (I->Val == From) {
3231 From->removeUser(U);
Chris Lattnerdca329f2008-02-03 03:35:22 +00003232 *I = To;
3233 To.Val->addUser(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003234 }
3235
3236 // Now that we have modified U, add it back to the CSE maps. If it already
3237 // exists there, recursively merge the results together.
3238 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003239 ReplaceAllUsesWith(U, Existing, UpdateListener);
3240 // U is now dead. Inform the listener if it exists and delete it.
3241 if (UpdateListener)
3242 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003244 } else {
3245 // If the node doesn't already exist, we updated it. Inform a listener if
3246 // it exists.
3247 if (UpdateListener)
3248 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249 }
3250 }
3251}
3252
3253/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3254/// This can cause recursive merging of nodes in the DAG.
3255///
3256/// This version assumes From/To have matching types and numbers of result
3257/// values.
3258///
3259void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003260 DAGUpdateListener *UpdateListener) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003261 assert(From != To && "Cannot replace uses of with self");
3262 assert(From->getNumValues() == To->getNumValues() &&
3263 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattnerdca329f2008-02-03 03:35:22 +00003264 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003265 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3266 UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003267
3268 while (!From->use_empty()) {
3269 // Process users until they are all gone.
3270 SDNode *U = *From->use_begin();
3271
3272 // This node is about to morph, remove its old self from the CSE maps.
3273 RemoveNodeFromCSEMaps(U);
3274
3275 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3276 I != E; ++I)
3277 if (I->Val == From) {
3278 From->removeUser(U);
3279 I->Val = To;
3280 To->addUser(U);
3281 }
3282
3283 // Now that we have modified U, add it back to the CSE maps. If it already
3284 // exists there, recursively merge the results together.
3285 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003286 ReplaceAllUsesWith(U, Existing, UpdateListener);
3287 // U is now dead. Inform the listener if it exists and delete it.
3288 if (UpdateListener)
3289 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003291 } else {
3292 // If the node doesn't already exist, we updated it. Inform a listener if
3293 // it exists.
3294 if (UpdateListener)
3295 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 }
3297 }
3298}
3299
3300/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3301/// This can cause recursive merging of nodes in the DAG.
3302///
3303/// This version can replace From with any result values. To must match the
3304/// number and types of values returned by From.
3305void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
3306 const SDOperand *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003307 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00003308 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003309 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310
3311 while (!From->use_empty()) {
3312 // Process users until they are all gone.
3313 SDNode *U = *From->use_begin();
3314
3315 // This node is about to morph, remove its old self from the CSE maps.
3316 RemoveNodeFromCSEMaps(U);
3317
3318 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3319 I != E; ++I)
3320 if (I->Val == From) {
3321 const SDOperand &ToOp = To[I->ResNo];
3322 From->removeUser(U);
3323 *I = ToOp;
3324 ToOp.Val->addUser(U);
3325 }
3326
3327 // Now that we have modified U, add it back to the CSE maps. If it already
3328 // exists there, recursively merge the results together.
3329 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003330 ReplaceAllUsesWith(U, Existing, UpdateListener);
3331 // U is now dead. Inform the listener if it exists and delete it.
3332 if (UpdateListener)
3333 UpdateListener->NodeDeleted(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334 DeleteNodeNotInCSEMaps(U);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003335 } else {
3336 // If the node doesn't already exist, we updated it. Inform a listener if
3337 // it exists.
3338 if (UpdateListener)
3339 UpdateListener->NodeUpdated(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 }
3341 }
3342}
3343
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003344namespace {
3345 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3346 /// any deleted nodes from the set passed into its constructor and recursively
3347 /// notifies another update listener if specified.
3348 class ChainedSetUpdaterListener :
3349 public SelectionDAG::DAGUpdateListener {
3350 SmallSetVector<SDNode*, 16> &Set;
3351 SelectionDAG::DAGUpdateListener *Chain;
3352 public:
3353 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3354 SelectionDAG::DAGUpdateListener *chain)
3355 : Set(set), Chain(chain) {}
3356
3357 virtual void NodeDeleted(SDNode *N) {
3358 Set.remove(N);
3359 if (Chain) Chain->NodeDeleted(N);
3360 }
3361 virtual void NodeUpdated(SDNode *N) {
3362 if (Chain) Chain->NodeUpdated(N);
3363 }
3364 };
3365}
3366
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003367/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3368/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003369/// handled the same way as for ReplaceAllUsesWith.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003370void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003371 DAGUpdateListener *UpdateListener){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003372 assert(From != To && "Cannot replace a value with itself");
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 // Handle the simple, trivial, case efficiently.
Chris Lattnerdca329f2008-02-03 03:35:22 +00003375 if (From.Val->getNumValues() == 1) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003376 ReplaceAllUsesWith(From, To, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003377 return;
3378 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003379
3380 if (From.use_empty()) return;
3381
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003382 // Get all of the users of From.Val. We want these in a nice,
3383 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3384 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
3385
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003386 // When one of the recursive merges deletes nodes from the graph, we need to
3387 // make sure that UpdateListener is notified *and* that the node is removed
3388 // from Users if present. CSUL does this.
3389 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner8a258202007-10-15 06:10:22 +00003390
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391 while (!Users.empty()) {
3392 // We know that this user uses some value of From. If it is the right
3393 // value, update it.
3394 SDNode *User = Users.back();
3395 Users.pop_back();
3396
Chris Lattner8a258202007-10-15 06:10:22 +00003397 // Scan for an operand that matches From.
3398 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3399 for (; Op != E; ++Op)
3400 if (*Op == From) break;
3401
3402 // If there are no matches, the user must use some other result of From.
3403 if (Op == E) continue;
3404
3405 // Okay, we know this user needs to be updated. Remove its old self
3406 // from the CSE maps.
3407 RemoveNodeFromCSEMaps(User);
3408
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003409 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner8a258202007-10-15 06:10:22 +00003410 for (; Op != E; ++Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003411 if (*Op == From) {
Chris Lattner8a258202007-10-15 06:10:22 +00003412 From.Val->removeUser(User);
3413 *Op = To;
3414 To.Val->addUser(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003415 }
3416 }
Chris Lattner8a258202007-10-15 06:10:22 +00003417
3418 // Now that we have modified User, add it back to the CSE maps. If it
3419 // already exists there, recursively merge the results together.
3420 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003421 if (!Existing) {
3422 if (UpdateListener) UpdateListener->NodeUpdated(User);
3423 continue; // Continue on to next user.
3424 }
Chris Lattner8a258202007-10-15 06:10:22 +00003425
3426 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003427 // to replace the dead one with the existing one. This can cause
Chris Lattner8a258202007-10-15 06:10:22 +00003428 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003429 // can cause deletion of nodes that used the old value. To handle this, we
3430 // use CSUL to remove them from the Users set.
3431 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner8a258202007-10-15 06:10:22 +00003432
Chris Lattner7bcb18f2008-02-03 06:49:24 +00003433 // User is now dead. Notify a listener if present.
3434 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner8a258202007-10-15 06:10:22 +00003435 DeleteNodeNotInCSEMaps(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003436 }
3437}
3438
3439
3440/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3441/// their allnodes order. It returns the maximum id.
3442unsigned SelectionDAG::AssignNodeIds() {
3443 unsigned Id = 0;
3444 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3445 SDNode *N = I;
3446 N->setNodeId(Id++);
3447 }
3448 return Id;
3449}
3450
3451/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
3452/// based on their topological order. It returns the maximum id and a vector
3453/// of the SDNodes* in assigned order by reference.
3454unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
3455 unsigned DAGSize = AllNodes.size();
3456 std::vector<unsigned> InDegree(DAGSize);
3457 std::vector<SDNode*> Sources;
3458
3459 // Use a two pass approach to avoid using a std::map which is slow.
3460 unsigned Id = 0;
3461 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3462 SDNode *N = I;
3463 N->setNodeId(Id++);
3464 unsigned Degree = N->use_size();
3465 InDegree[N->getNodeId()] = Degree;
3466 if (Degree == 0)
3467 Sources.push_back(N);
3468 }
3469
3470 TopOrder.clear();
3471 while (!Sources.empty()) {
3472 SDNode *N = Sources.back();
3473 Sources.pop_back();
3474 TopOrder.push_back(N);
3475 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3476 SDNode *P = I->Val;
3477 unsigned Degree = --InDegree[P->getNodeId()];
3478 if (Degree == 0)
3479 Sources.push_back(P);
3480 }
3481 }
3482
3483 // Second pass, assign the actual topological order as node ids.
3484 Id = 0;
3485 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3486 TI != TE; ++TI)
3487 (*TI)->setNodeId(Id++);
3488
3489 return Id;
3490}
3491
3492
3493
3494//===----------------------------------------------------------------------===//
3495// SDNode Class
3496//===----------------------------------------------------------------------===//
3497
3498// Out-of-line virtual method to give class a home.
3499void SDNode::ANCHOR() {}
3500void UnarySDNode::ANCHOR() {}
3501void BinarySDNode::ANCHOR() {}
3502void TernarySDNode::ANCHOR() {}
3503void HandleSDNode::ANCHOR() {}
3504void StringSDNode::ANCHOR() {}
3505void ConstantSDNode::ANCHOR() {}
3506void ConstantFPSDNode::ANCHOR() {}
3507void GlobalAddressSDNode::ANCHOR() {}
3508void FrameIndexSDNode::ANCHOR() {}
3509void JumpTableSDNode::ANCHOR() {}
3510void ConstantPoolSDNode::ANCHOR() {}
3511void BasicBlockSDNode::ANCHOR() {}
3512void SrcValueSDNode::ANCHOR() {}
Dan Gohman12a9c082008-02-06 22:27:42 +00003513void MemOperandSDNode::ANCHOR() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003514void RegisterSDNode::ANCHOR() {}
3515void ExternalSymbolSDNode::ANCHOR() {}
3516void CondCodeSDNode::ANCHOR() {}
3517void VTSDNode::ANCHOR() {}
3518void LoadSDNode::ANCHOR() {}
3519void StoreSDNode::ANCHOR() {}
3520
3521HandleSDNode::~HandleSDNode() {
3522 SDVTList VTs = { 0, 0 };
3523 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
3524}
3525
3526GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3527 MVT::ValueType VT, int o)
3528 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman53491e92007-07-23 20:24:29 +00003529 cast<GlobalVariable>(GA)->isThreadLocal() ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003530 // Thread Local
3531 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3532 // Non Thread Local
3533 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3534 getSDVTList(VT)), Offset(o) {
3535 TheGlobal = const_cast<GlobalValue*>(GA);
3536}
3537
Dan Gohman12a9c082008-02-06 22:27:42 +00003538/// getMemOperand - Return a MemOperand object describing the memory
3539/// reference performed by this load or store.
3540MemOperand LSBaseSDNode::getMemOperand() const {
3541 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3542 int Flags =
3543 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3544 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3545
3546 // Check if the load references a frame index, and does not have
3547 // an SV attached.
3548 const FrameIndexSDNode *FI =
3549 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3550 if (!getSrcValue() && FI)
Dan Gohmanfb020b62008-02-07 18:41:25 +00003551 return MemOperand(PseudoSourceValue::getFixedStack(), Flags,
Dan Gohman12a9c082008-02-06 22:27:42 +00003552 FI->getIndex(), Size, Alignment);
3553 else
3554 return MemOperand(getSrcValue(), Flags,
3555 getSrcValueOffset(), Size, Alignment);
3556}
3557
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003558/// Profile - Gather unique data for the node.
3559///
3560void SDNode::Profile(FoldingSetNodeID &ID) {
3561 AddNodeIDNode(ID, this);
3562}
3563
3564/// getValueTypeList - Return a pointer to the specified value type.
3565///
Dan Gohman8cdf7892008-02-08 03:26:46 +00003566const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00003567 if (MVT::isExtendedVT(VT)) {
3568 static std::set<MVT::ValueType> EVTs;
Dan Gohman8cdf7892008-02-08 03:26:46 +00003569 return &(*EVTs.insert(VT).first);
Duncan Sandsa9810f32007-10-16 09:56:48 +00003570 } else {
3571 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3572 VTs[VT] = VT;
3573 return &VTs[VT];
3574 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003575}
Duncan Sandsa9810f32007-10-16 09:56:48 +00003576
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003577/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3578/// indicated value. This method ignores uses of other values defined by this
3579/// operation.
3580bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
3581 assert(Value < getNumValues() && "Bad value!");
3582
3583 // If there is only one value, this is easy.
3584 if (getNumValues() == 1)
3585 return use_size() == NUses;
Evan Cheng0af04f72007-08-02 05:29:38 +00003586 if (use_size() < NUses) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003587
3588 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3589
3590 SmallPtrSet<SDNode*, 32> UsersHandled;
3591
3592 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3593 SDNode *User = *UI;
3594 if (User->getNumOperands() == 1 ||
3595 UsersHandled.insert(User)) // First time we've seen this?
3596 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3597 if (User->getOperand(i) == TheValue) {
3598 if (NUses == 0)
3599 return false; // too many uses
3600 --NUses;
3601 }
3602 }
3603
3604 // Found exactly the right number of uses?
3605 return NUses == 0;
3606}
3607
3608
Evan Cheng0af04f72007-08-02 05:29:38 +00003609/// hasAnyUseOfValue - Return true if there are any use of the indicated
3610/// value. This method ignores uses of other values defined by this operation.
3611bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3612 assert(Value < getNumValues() && "Bad value!");
3613
Dan Gohman301f4052008-01-29 13:02:09 +00003614 if (use_empty()) return false;
Evan Cheng0af04f72007-08-02 05:29:38 +00003615
3616 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3617
3618 SmallPtrSet<SDNode*, 32> UsersHandled;
3619
3620 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3621 SDNode *User = *UI;
3622 if (User->getNumOperands() == 1 ||
3623 UsersHandled.insert(User)) // First time we've seen this?
3624 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3625 if (User->getOperand(i) == TheValue) {
3626 return true;
3627 }
3628 }
3629
3630 return false;
3631}
3632
3633
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003634/// isOnlyUse - Return true if this node is the only use of N.
3635///
3636bool SDNode::isOnlyUse(SDNode *N) const {
3637 bool Seen = false;
3638 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3639 SDNode *User = *I;
3640 if (User == this)
3641 Seen = true;
3642 else
3643 return false;
3644 }
3645
3646 return Seen;
3647}
3648
3649/// isOperand - Return true if this node is an operand of N.
3650///
3651bool SDOperand::isOperand(SDNode *N) const {
3652 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3653 if (*this == N->getOperand(i))
3654 return true;
3655 return false;
3656}
3657
3658bool SDNode::isOperand(SDNode *N) const {
3659 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3660 if (this == N->OperandList[i].Val)
3661 return true;
3662 return false;
3663}
3664
Chris Lattner10d94f92008-01-16 05:49:24 +00003665/// reachesChainWithoutSideEffects - Return true if this operand (which must
3666/// be a chain) reaches the specified operand without crossing any
3667/// side-effecting instructions. In practice, this looks through token
3668/// factors and non-volatile loads. In order to remain efficient, this only
3669/// looks a couple of nodes in, it does not do an exhaustive search.
3670bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3671 unsigned Depth) const {
3672 if (*this == Dest) return true;
3673
3674 // Don't search too deeply, we just want to be able to see through
3675 // TokenFactor's etc.
3676 if (Depth == 0) return false;
3677
3678 // If this is a token factor, all inputs to the TF happen in parallel. If any
3679 // of the operands of the TF reach dest, then we can do the xform.
3680 if (getOpcode() == ISD::TokenFactor) {
3681 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3682 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3683 return true;
3684 return false;
3685 }
3686
3687 // Loads don't have side effects, look through them.
3688 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3689 if (!Ld->isVolatile())
3690 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3691 }
3692 return false;
3693}
3694
3695
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003696static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
3697 SmallPtrSet<SDNode *, 32> &Visited) {
3698 if (found || !Visited.insert(N))
3699 return;
3700
3701 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3702 SDNode *Op = N->getOperand(i).Val;
3703 if (Op == P) {
3704 found = true;
3705 return;
3706 }
3707 findPredecessor(Op, P, found, Visited);
3708 }
3709}
3710
3711/// isPredecessor - Return true if this node is a predecessor of N. This node
3712/// is either an operand of N or it can be reached by recursively traversing
3713/// up the operands.
3714/// NOTE: this is an expensive method. Use it carefully.
3715bool SDNode::isPredecessor(SDNode *N) const {
3716 SmallPtrSet<SDNode *, 32> Visited;
3717 bool found = false;
3718 findPredecessor(N, this, found, Visited);
3719 return found;
3720}
3721
3722uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3723 assert(Num < NumOperands && "Invalid child # of SDNode!");
3724 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3725}
3726
3727std::string SDNode::getOperationName(const SelectionDAG *G) const {
3728 switch (getOpcode()) {
3729 default:
3730 if (getOpcode() < ISD::BUILTIN_OP_END)
3731 return "<<Unknown DAG Node>>";
3732 else {
3733 if (G) {
3734 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
3735 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner0c2a4f32008-01-07 03:13:06 +00003736 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737
3738 TargetLowering &TLI = G->getTargetLoweringInfo();
3739 const char *Name =
3740 TLI.getTargetNodeName(getOpcode());
3741 if (Name) return Name;
3742 }
3743
3744 return "<<Unknown Target Node>>";
3745 }
3746
3747 case ISD::PCMARKER: return "PCMarker";
3748 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
3749 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman12a9c082008-02-06 22:27:42 +00003750 case ISD::MEMOPERAND: return "MemOperand";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 case ISD::EntryToken: return "EntryToken";
3752 case ISD::TokenFactor: return "TokenFactor";
3753 case ISD::AssertSext: return "AssertSext";
3754 case ISD::AssertZext: return "AssertZext";
3755
3756 case ISD::STRING: return "String";
3757 case ISD::BasicBlock: return "BasicBlock";
3758 case ISD::VALUETYPE: return "ValueType";
3759 case ISD::Register: return "Register";
3760
3761 case ISD::Constant: return "Constant";
3762 case ISD::ConstantFP: return "ConstantFP";
3763 case ISD::GlobalAddress: return "GlobalAddress";
3764 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
3765 case ISD::FrameIndex: return "FrameIndex";
3766 case ISD::JumpTable: return "JumpTable";
3767 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
3768 case ISD::RETURNADDR: return "RETURNADDR";
3769 case ISD::FRAMEADDR: return "FRAMEADDR";
3770 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
3771 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3772 case ISD::EHSELECTION: return "EHSELECTION";
3773 case ISD::EH_RETURN: return "EH_RETURN";
3774 case ISD::ConstantPool: return "ConstantPool";
3775 case ISD::ExternalSymbol: return "ExternalSymbol";
3776 case ISD::INTRINSIC_WO_CHAIN: {
3777 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3778 return Intrinsic::getName((Intrinsic::ID)IID);
3779 }
3780 case ISD::INTRINSIC_VOID:
3781 case ISD::INTRINSIC_W_CHAIN: {
3782 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
3783 return Intrinsic::getName((Intrinsic::ID)IID);
3784 }
3785
3786 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
3787 case ISD::TargetConstant: return "TargetConstant";
3788 case ISD::TargetConstantFP:return "TargetConstantFP";
3789 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
3790 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
3791 case ISD::TargetFrameIndex: return "TargetFrameIndex";
3792 case ISD::TargetJumpTable: return "TargetJumpTable";
3793 case ISD::TargetConstantPool: return "TargetConstantPool";
3794 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
3795
3796 case ISD::CopyToReg: return "CopyToReg";
3797 case ISD::CopyFromReg: return "CopyFromReg";
3798 case ISD::UNDEF: return "undef";
3799 case ISD::MERGE_VALUES: return "merge_values";
3800 case ISD::INLINEASM: return "inlineasm";
3801 case ISD::LABEL: return "label";
Evan Cheng2e28d622008-02-02 04:07:54 +00003802 case ISD::DECLARE: return "declare";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003803 case ISD::HANDLENODE: return "handlenode";
3804 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
3805 case ISD::CALL: return "call";
3806
3807 // Unary operators
3808 case ISD::FABS: return "fabs";
3809 case ISD::FNEG: return "fneg";
3810 case ISD::FSQRT: return "fsqrt";
3811 case ISD::FSIN: return "fsin";
3812 case ISD::FCOS: return "fcos";
3813 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00003814 case ISD::FPOW: return "fpow";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003815
3816 // Binary operators
3817 case ISD::ADD: return "add";
3818 case ISD::SUB: return "sub";
3819 case ISD::MUL: return "mul";
3820 case ISD::MULHU: return "mulhu";
3821 case ISD::MULHS: return "mulhs";
3822 case ISD::SDIV: return "sdiv";
3823 case ISD::UDIV: return "udiv";
3824 case ISD::SREM: return "srem";
3825 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00003826 case ISD::SMUL_LOHI: return "smul_lohi";
3827 case ISD::UMUL_LOHI: return "umul_lohi";
3828 case ISD::SDIVREM: return "sdivrem";
3829 case ISD::UDIVREM: return "divrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003830 case ISD::AND: return "and";
3831 case ISD::OR: return "or";
3832 case ISD::XOR: return "xor";
3833 case ISD::SHL: return "shl";
3834 case ISD::SRA: return "sra";
3835 case ISD::SRL: return "srl";
3836 case ISD::ROTL: return "rotl";
3837 case ISD::ROTR: return "rotr";
3838 case ISD::FADD: return "fadd";
3839 case ISD::FSUB: return "fsub";
3840 case ISD::FMUL: return "fmul";
3841 case ISD::FDIV: return "fdiv";
3842 case ISD::FREM: return "frem";
3843 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00003844 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003845
3846 case ISD::SETCC: return "setcc";
3847 case ISD::SELECT: return "select";
3848 case ISD::SELECT_CC: return "select_cc";
3849 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
3850 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
3851 case ISD::CONCAT_VECTORS: return "concat_vectors";
3852 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
3853 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
3854 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
3855 case ISD::CARRY_FALSE: return "carry_false";
3856 case ISD::ADDC: return "addc";
3857 case ISD::ADDE: return "adde";
3858 case ISD::SUBC: return "subc";
3859 case ISD::SUBE: return "sube";
3860 case ISD::SHL_PARTS: return "shl_parts";
3861 case ISD::SRA_PARTS: return "sra_parts";
3862 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lambb768c2e2007-07-26 07:34:40 +00003863
3864 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3865 case ISD::INSERT_SUBREG: return "insert_subreg";
3866
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867 // Conversion operators.
3868 case ISD::SIGN_EXTEND: return "sign_extend";
3869 case ISD::ZERO_EXTEND: return "zero_extend";
3870 case ISD::ANY_EXTEND: return "any_extend";
3871 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
3872 case ISD::TRUNCATE: return "truncate";
3873 case ISD::FP_ROUND: return "fp_round";
Dan Gohman819574c2008-01-31 00:41:03 +00003874 case ISD::FLT_ROUNDS_: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
3876 case ISD::FP_EXTEND: return "fp_extend";
3877
3878 case ISD::SINT_TO_FP: return "sint_to_fp";
3879 case ISD::UINT_TO_FP: return "uint_to_fp";
3880 case ISD::FP_TO_SINT: return "fp_to_sint";
3881 case ISD::FP_TO_UINT: return "fp_to_uint";
3882 case ISD::BIT_CONVERT: return "bit_convert";
3883
3884 // Control flow instructions
3885 case ISD::BR: return "br";
3886 case ISD::BRIND: return "brind";
3887 case ISD::BR_JT: return "br_jt";
3888 case ISD::BRCOND: return "brcond";
3889 case ISD::BR_CC: return "br_cc";
3890 case ISD::RET: return "ret";
3891 case ISD::CALLSEQ_START: return "callseq_start";
3892 case ISD::CALLSEQ_END: return "callseq_end";
3893
3894 // Other operators
3895 case ISD::LOAD: return "load";
3896 case ISD::STORE: return "store";
3897 case ISD::VAARG: return "vaarg";
3898 case ISD::VACOPY: return "vacopy";
3899 case ISD::VAEND: return "vaend";
3900 case ISD::VASTART: return "vastart";
3901 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
3902 case ISD::EXTRACT_ELEMENT: return "extract_element";
3903 case ISD::BUILD_PAIR: return "build_pair";
3904 case ISD::STACKSAVE: return "stacksave";
3905 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00003906 case ISD::TRAP: return "trap";
3907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 // Block memory operations.
3909 case ISD::MEMSET: return "memset";
3910 case ISD::MEMCPY: return "memcpy";
3911 case ISD::MEMMOVE: return "memmove";
3912
3913 // Bit manipulation
3914 case ISD::BSWAP: return "bswap";
3915 case ISD::CTPOP: return "ctpop";
3916 case ISD::CTTZ: return "cttz";
3917 case ISD::CTLZ: return "ctlz";
3918
3919 // Debug info
3920 case ISD::LOCATION: return "location";
3921 case ISD::DEBUG_LOC: return "debug_loc";
3922
Duncan Sands38947cd2007-07-27 12:58:54 +00003923 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00003924 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00003925
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003926 case ISD::CONDCODE:
3927 switch (cast<CondCodeSDNode>(this)->get()) {
3928 default: assert(0 && "Unknown setcc condition!");
3929 case ISD::SETOEQ: return "setoeq";
3930 case ISD::SETOGT: return "setogt";
3931 case ISD::SETOGE: return "setoge";
3932 case ISD::SETOLT: return "setolt";
3933 case ISD::SETOLE: return "setole";
3934 case ISD::SETONE: return "setone";
3935
3936 case ISD::SETO: return "seto";
3937 case ISD::SETUO: return "setuo";
3938 case ISD::SETUEQ: return "setue";
3939 case ISD::SETUGT: return "setugt";
3940 case ISD::SETUGE: return "setuge";
3941 case ISD::SETULT: return "setult";
3942 case ISD::SETULE: return "setule";
3943 case ISD::SETUNE: return "setune";
3944
3945 case ISD::SETEQ: return "seteq";
3946 case ISD::SETGT: return "setgt";
3947 case ISD::SETGE: return "setge";
3948 case ISD::SETLT: return "setlt";
3949 case ISD::SETLE: return "setle";
3950 case ISD::SETNE: return "setne";
3951 }
3952 }
3953}
3954
3955const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
3956 switch (AM) {
3957 default:
3958 return "";
3959 case ISD::PRE_INC:
3960 return "<pre-inc>";
3961 case ISD::PRE_DEC:
3962 return "<pre-dec>";
3963 case ISD::POST_INC:
3964 return "<post-inc>";
3965 case ISD::POST_DEC:
3966 return "<post-dec>";
3967 }
3968}
3969
3970void SDNode::dump() const { dump(0); }
3971void SDNode::dump(const SelectionDAG *G) const {
3972 cerr << (void*)this << ": ";
3973
3974 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3975 if (i) cerr << ",";
3976 if (getValueType(i) == MVT::Other)
3977 cerr << "ch";
3978 else
3979 cerr << MVT::getValueTypeString(getValueType(i));
3980 }
3981 cerr << " = " << getOperationName(G);
3982
3983 cerr << " ";
3984 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3985 if (i) cerr << ", ";
3986 cerr << (void*)getOperand(i).Val;
3987 if (unsigned RN = getOperand(i).ResNo)
3988 cerr << ":" << RN;
3989 }
3990
Evan Chengaad43a02007-12-11 02:08:35 +00003991 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3992 SDNode *Mask = getOperand(2).Val;
3993 cerr << "<";
3994 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3995 if (i) cerr << ",";
3996 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3997 cerr << "u";
3998 else
3999 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4000 }
4001 cerr << ">";
4002 }
4003
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004004 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
4005 cerr << "<" << CSDN->getValue() << ">";
4006 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00004007 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4008 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4009 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4010 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4011 else {
4012 cerr << "<APFloat(";
4013 CSDN->getValueAPF().convertToAPInt().dump();
4014 cerr << ")>";
4015 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 } else if (const GlobalAddressSDNode *GADN =
4017 dyn_cast<GlobalAddressSDNode>(this)) {
4018 int offset = GADN->getOffset();
4019 cerr << "<";
4020 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
4021 if (offset > 0)
4022 cerr << " + " << offset;
4023 else
4024 cerr << " " << offset;
4025 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
4026 cerr << "<" << FIDN->getIndex() << ">";
4027 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
4028 cerr << "<" << JTDN->getIndex() << ">";
4029 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
4030 int offset = CP->getOffset();
4031 if (CP->isMachineConstantPoolEntry())
4032 cerr << "<" << *CP->getMachineCPVal() << ">";
4033 else
4034 cerr << "<" << *CP->getConstVal() << ">";
4035 if (offset > 0)
4036 cerr << " + " << offset;
4037 else
4038 cerr << " " << offset;
4039 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
4040 cerr << "<";
4041 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4042 if (LBB)
4043 cerr << LBB->getName() << " ";
4044 cerr << (const void*)BBDN->getBasicBlock() << ">";
4045 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
4046 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
4047 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
4048 } else {
4049 cerr << " #" << R->getReg();
4050 }
4051 } else if (const ExternalSymbolSDNode *ES =
4052 dyn_cast<ExternalSymbolSDNode>(this)) {
4053 cerr << "'" << ES->getSymbol() << "'";
4054 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4055 if (M->getValue())
Dan Gohman12a9c082008-02-06 22:27:42 +00004056 cerr << "<" << M->getValue() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 else
Dan Gohman12a9c082008-02-06 22:27:42 +00004058 cerr << "<null>";
4059 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4060 if (M->MO.getValue())
4061 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4062 else
4063 cerr << "<null:" << M->MO.getOffset() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
4065 cerr << ":" << MVT::getValueTypeString(N->getVT());
4066 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng034c4f82007-12-18 19:06:30 +00004067 const Value *SrcValue = LD->getSrcValue();
4068 int SrcOffset = LD->getSrcValueOffset();
4069 cerr << " <";
4070 if (SrcValue)
4071 cerr << SrcValue;
4072 else
4073 cerr << "null";
4074 cerr << ":" << SrcOffset << ">";
4075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004076 bool doExt = true;
4077 switch (LD->getExtensionType()) {
4078 default: doExt = false; break;
4079 case ISD::EXTLOAD:
4080 cerr << " <anyext ";
4081 break;
4082 case ISD::SEXTLOAD:
4083 cerr << " <sext ";
4084 break;
4085 case ISD::ZEXTLOAD:
4086 cerr << " <zext ";
4087 break;
4088 }
4089 if (doExt)
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004090 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091
4092 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00004093 if (*AM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 cerr << " " << AM;
Evan Cheng034c4f82007-12-18 19:06:30 +00004095 if (LD->isVolatile())
4096 cerr << " <volatile>";
4097 cerr << " alignment=" << LD->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng7196a7b2007-12-18 07:02:08 +00004099 const Value *SrcValue = ST->getSrcValue();
4100 int SrcOffset = ST->getSrcValueOffset();
4101 cerr << " <";
4102 if (SrcValue)
4103 cerr << SrcValue;
4104 else
4105 cerr << "null";
4106 cerr << ":" << SrcOffset << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00004107
4108 if (ST->isTruncatingStore())
4109 cerr << " <trunc "
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004110 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng034c4f82007-12-18 19:06:30 +00004111
4112 const char *AM = getIndexedModeName(ST->getAddressingMode());
4113 if (*AM)
4114 cerr << " " << AM;
4115 if (ST->isVolatile())
4116 cerr << " <volatile>";
4117 cerr << " alignment=" << ST->getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118 }
4119}
4120
4121static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
4122 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4123 if (N->getOperand(i).Val->hasOneUse())
4124 DumpNodes(N->getOperand(i).Val, indent+2, G);
4125 else
4126 cerr << "\n" << std::string(indent+2, ' ')
4127 << (void*)N->getOperand(i).Val << ": <multiple use>";
4128
4129
4130 cerr << "\n" << std::string(indent, ' ');
4131 N->dump(G);
4132}
4133
4134void SelectionDAG::dump() const {
4135 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
4136 std::vector<const SDNode*> Nodes;
4137 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4138 I != E; ++I)
4139 Nodes.push_back(I);
4140
4141 std::sort(Nodes.begin(), Nodes.end());
4142
4143 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4144 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
4145 DumpNodes(Nodes[i], 2, this);
4146 }
4147
4148 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
4149
4150 cerr << "\n\n";
4151}
4152
4153const Type *ConstantPoolSDNode::getType() const {
4154 if (isMachineConstantPoolEntry())
4155 return Val.MachineCPVal->getType();
4156 return Val.ConstVal->getType();
4157}