blob: d544e8e37c8b53e258859e0f7660aefe96a2483a [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000022#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000023#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000024#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000025#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000026#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000028#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000029#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000030#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000031#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000032#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000033#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner0b3e5252006-08-15 19:11:05 +000036/// makeVTList - Return an instance of the SDVTList struct initialized with the
37/// specified members.
38static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
39 SDVTList Res = {VTs, NumVTs};
40 return Res;
41}
42
Jim Laskey58b968b2005-08-17 20:08:02 +000043//===----------------------------------------------------------------------===//
44// ConstantFPSDNode Class
45//===----------------------------------------------------------------------===//
46
47/// isExactlyValue - We don't rely on operator== working on double values, as
48/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
49/// As such, this method can be used to do an exact bit-for-bit comparison of
50/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000051bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000052 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000053}
54
Dale Johannesenf04afdb2007-08-30 00:23:21 +000055bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
56 const APFloat& Val) {
57 // convert modifies in place, so make a copy.
58 APFloat Val2 = APFloat(Val);
59 switch (VT) {
60 default:
61 return false; // These can't be represented as floating point!
62
63 // FIXME rounding mode needs to be more flexible
64 case MVT::f32:
65 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
66 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
67 APFloat::opOK;
68 case MVT::f64:
69 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
70 &Val2.getSemantics() == &APFloat::IEEEdouble ||
71 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
72 APFloat::opOK;
73 // TODO: Figure out how to test if we can use a shorter type instead!
74 case MVT::f80:
75 case MVT::f128:
76 case MVT::ppcf128:
77 return true;
78 }
79}
80
Jim Laskey58b968b2005-08-17 20:08:02 +000081//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000082// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000083//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000084
Evan Chenga8df1662006-03-27 06:58:47 +000085/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000086/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000087bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000088 // Look through a bit convert.
89 if (N->getOpcode() == ISD::BIT_CONVERT)
90 N = N->getOperand(0).Val;
91
Evan Chenga8df1662006-03-27 06:58:47 +000092 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000093
94 unsigned i = 0, e = N->getNumOperands();
95
96 // Skip over all of the undef values.
97 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
98 ++i;
99
100 // Do not accept an all-undef vector.
101 if (i == e) return false;
102
103 // Do not accept build_vectors that aren't all constants or which have non-~0
104 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000105 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000106 if (isa<ConstantSDNode>(NotZero)) {
107 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
108 return false;
109 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000110 MVT::ValueType VT = NotZero.getValueType();
111 if (VT== MVT::f64) {
112 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
113 (uint64_t)-1)
114 return false;
115 } else {
116 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
117 (uint32_t)-1)
118 return false;
119 }
Evan Chenga8df1662006-03-27 06:58:47 +0000120 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000121 return false;
122
123 // Okay, we have at least one ~0 value, check to see if the rest match or are
124 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000125 for (++i; i != e; ++i)
126 if (N->getOperand(i) != NotZero &&
127 N->getOperand(i).getOpcode() != ISD::UNDEF)
128 return false;
129 return true;
130}
131
132
Evan Cheng4a147842006-03-26 09:50:58 +0000133/// isBuildVectorAllZeros - Return true if the specified node is a
134/// BUILD_VECTOR where all of the elements are 0 or undef.
135bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000136 // Look through a bit convert.
137 if (N->getOpcode() == ISD::BIT_CONVERT)
138 N = N->getOperand(0).Val;
139
Evan Cheng4a147842006-03-26 09:50:58 +0000140 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000141
142 unsigned i = 0, e = N->getNumOperands();
143
144 // Skip over all of the undef values.
145 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
146 ++i;
147
148 // Do not accept an all-undef vector.
149 if (i == e) return false;
150
151 // Do not accept build_vectors that aren't all constants or which have non-~0
152 // elements.
153 SDOperand Zero = N->getOperand(i);
154 if (isa<ConstantSDNode>(Zero)) {
155 if (!cast<ConstantSDNode>(Zero)->isNullValue())
156 return false;
157 } else if (isa<ConstantFPSDNode>(Zero)) {
158 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
159 return false;
160 } else
161 return false;
162
163 // Okay, we have at least one ~0 value, check to see if the rest match or are
164 // undefs.
165 for (++i; i != e; ++i)
166 if (N->getOperand(i) != Zero &&
167 N->getOperand(i).getOpcode() != ISD::UNDEF)
168 return false;
169 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000170}
171
Chris Lattnerc3aae252005-01-07 07:46:32 +0000172/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
173/// when given the operation for (X op Y).
174ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
175 // To perform this operation, we just need to swap the L and G bits of the
176 // operation.
177 unsigned OldL = (Operation >> 2) & 1;
178 unsigned OldG = (Operation >> 1) & 1;
179 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
180 (OldL << 1) | // New G bit
181 (OldG << 2)); // New L bit.
182}
183
184/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
185/// 'op' is a valid SetCC operation.
186ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
187 unsigned Operation = Op;
188 if (isInteger)
189 Operation ^= 7; // Flip L, G, E bits, but not U.
190 else
191 Operation ^= 15; // Flip all of the condition bits.
192 if (Operation > ISD::SETTRUE2)
193 Operation &= ~8; // Don't let N and U bits get set.
194 return ISD::CondCode(Operation);
195}
196
197
198/// isSignedOp - For an integer comparison, return 1 if the comparison is a
199/// signed operation and 2 if the result is an unsigned comparison. Return zero
200/// if the operation does not depend on the sign of the input (setne and seteq).
201static int isSignedOp(ISD::CondCode Opcode) {
202 switch (Opcode) {
203 default: assert(0 && "Illegal integer setcc operation!");
204 case ISD::SETEQ:
205 case ISD::SETNE: return 0;
206 case ISD::SETLT:
207 case ISD::SETLE:
208 case ISD::SETGT:
209 case ISD::SETGE: return 1;
210 case ISD::SETULT:
211 case ISD::SETULE:
212 case ISD::SETUGT:
213 case ISD::SETUGE: return 2;
214 }
215}
216
217/// getSetCCOrOperation - Return the result of a logical OR between different
218/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
219/// returns SETCC_INVALID if it is not possible to represent the resultant
220/// comparison.
221ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
222 bool isInteger) {
223 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
224 // Cannot fold a signed integer setcc with an unsigned integer setcc.
225 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000226
Chris Lattnerc3aae252005-01-07 07:46:32 +0000227 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000228
Chris Lattnerc3aae252005-01-07 07:46:32 +0000229 // If the N and U bits get set then the resultant comparison DOES suddenly
230 // care about orderedness, and is true when ordered.
231 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000232 Op &= ~16; // Clear the U bit if the N bit is set.
233
234 // Canonicalize illegal integer setcc's.
235 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
236 Op = ISD::SETNE;
237
Chris Lattnerc3aae252005-01-07 07:46:32 +0000238 return ISD::CondCode(Op);
239}
240
241/// getSetCCAndOperation - Return the result of a logical AND between different
242/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
243/// function returns zero if it is not possible to represent the resultant
244/// comparison.
245ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
246 bool isInteger) {
247 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
248 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000249 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000250
251 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000252 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
253
254 // Canonicalize illegal integer setcc's.
255 if (isInteger) {
256 switch (Result) {
257 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000258 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
259 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
260 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
261 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000262 }
263 }
264
265 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000266}
267
Chris Lattnerb48da392005-01-23 04:39:44 +0000268const TargetMachine &SelectionDAG::getTarget() const {
269 return TLI.getTargetMachine();
270}
271
Jim Laskey58b968b2005-08-17 20:08:02 +0000272//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000273// SDNode Profile Support
274//===----------------------------------------------------------------------===//
275
Jim Laskeydef69b92006-10-27 23:52:51 +0000276/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
277///
Jim Laskey583bd472006-10-27 23:46:08 +0000278static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
279 ID.AddInteger(OpC);
280}
281
282/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
283/// solely with their pointer.
284void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
285 ID.AddPointer(VTList.VTs);
286}
287
Jim Laskeydef69b92006-10-27 23:52:51 +0000288/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
289///
Jim Laskey583bd472006-10-27 23:46:08 +0000290static void AddNodeIDOperands(FoldingSetNodeID &ID,
291 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000292 for (; NumOps; --NumOps, ++Ops) {
293 ID.AddPointer(Ops->Val);
294 ID.AddInteger(Ops->ResNo);
295 }
Jim Laskey583bd472006-10-27 23:46:08 +0000296}
297
Jim Laskey583bd472006-10-27 23:46:08 +0000298static void AddNodeIDNode(FoldingSetNodeID &ID,
299 unsigned short OpC, SDVTList VTList,
300 const SDOperand *OpList, unsigned N) {
301 AddNodeIDOpcode(ID, OpC);
302 AddNodeIDValueTypes(ID, VTList);
303 AddNodeIDOperands(ID, OpList, N);
304}
305
Jim Laskeydef69b92006-10-27 23:52:51 +0000306/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
307/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000308static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
309 AddNodeIDOpcode(ID, N->getOpcode());
310 // Add the return value info.
311 AddNodeIDValueTypes(ID, N->getVTList());
312 // Add the operand info.
313 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
314
315 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000316 switch (N->getOpcode()) {
317 default: break; // Normal nodes don't need extra info.
318 case ISD::TargetConstant:
319 case ISD::Constant:
320 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
321 break;
322 case ISD::TargetConstantFP:
323 case ISD::ConstantFP:
324 ID.AddDouble(cast<ConstantFPSDNode>(N)->getValue());
325 break;
326 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000327 case ISD::GlobalAddress:
328 case ISD::TargetGlobalTLSAddress:
329 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000330 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
331 ID.AddPointer(GA->getGlobal());
332 ID.AddInteger(GA->getOffset());
333 break;
334 }
335 case ISD::BasicBlock:
336 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
337 break;
338 case ISD::Register:
339 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
340 break;
341 case ISD::SRCVALUE: {
342 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
343 ID.AddPointer(SV->getValue());
344 ID.AddInteger(SV->getOffset());
345 break;
346 }
347 case ISD::FrameIndex:
348 case ISD::TargetFrameIndex:
349 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
350 break;
351 case ISD::JumpTable:
352 case ISD::TargetJumpTable:
353 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
354 break;
355 case ISD::ConstantPool:
356 case ISD::TargetConstantPool: {
357 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
358 ID.AddInteger(CP->getAlignment());
359 ID.AddInteger(CP->getOffset());
360 if (CP->isMachineConstantPoolEntry())
361 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
362 else
363 ID.AddPointer(CP->getConstVal());
364 break;
365 }
366 case ISD::LOAD: {
367 LoadSDNode *LD = cast<LoadSDNode>(N);
368 ID.AddInteger(LD->getAddressingMode());
369 ID.AddInteger(LD->getExtensionType());
370 ID.AddInteger(LD->getLoadedVT());
371 ID.AddPointer(LD->getSrcValue());
372 ID.AddInteger(LD->getSrcValueOffset());
373 ID.AddInteger(LD->getAlignment());
374 ID.AddInteger(LD->isVolatile());
375 break;
376 }
377 case ISD::STORE: {
378 StoreSDNode *ST = cast<StoreSDNode>(N);
379 ID.AddInteger(ST->getAddressingMode());
380 ID.AddInteger(ST->isTruncatingStore());
381 ID.AddInteger(ST->getStoredVT());
382 ID.AddPointer(ST->getSrcValue());
383 ID.AddInteger(ST->getSrcValueOffset());
384 ID.AddInteger(ST->getAlignment());
385 ID.AddInteger(ST->isVolatile());
386 break;
387 }
Jim Laskey583bd472006-10-27 23:46:08 +0000388 }
389}
390
391//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000392// SelectionDAG Class
393//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000394
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000395/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000396/// SelectionDAG.
397void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000398 // Create a dummy node (which is not added to allnodes), that adds a reference
399 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000400 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000401
Chris Lattner190a4182006-08-04 17:45:20 +0000402 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000403
Chris Lattner190a4182006-08-04 17:45:20 +0000404 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000405 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000406 if (I->use_empty())
407 DeadNodes.push_back(I);
408
409 // Process the worklist, deleting the nodes and adding their uses to the
410 // worklist.
411 while (!DeadNodes.empty()) {
412 SDNode *N = DeadNodes.back();
413 DeadNodes.pop_back();
414
415 // Take the node out of the appropriate CSE map.
416 RemoveNodeFromCSEMaps(N);
417
418 // Next, brutally remove the operand list. This is safe to do, as there are
419 // no cycles in the graph.
420 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
421 SDNode *Operand = I->Val;
422 Operand->removeUser(N);
423
424 // Now that we removed this operand, see if there are no uses of it left.
425 if (Operand->use_empty())
426 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000427 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000428 if (N->OperandsNeedDelete)
429 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000430 N->OperandList = 0;
431 N->NumOperands = 0;
432
433 // Finally, remove N itself.
434 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000435 }
436
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000437 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000438 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000439}
440
Evan Cheng130a6472006-10-12 20:34:05 +0000441void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
442 SmallVector<SDNode*, 16> DeadNodes;
443 DeadNodes.push_back(N);
444
445 // Process the worklist, deleting the nodes and adding their uses to the
446 // worklist.
447 while (!DeadNodes.empty()) {
448 SDNode *N = DeadNodes.back();
449 DeadNodes.pop_back();
450
451 // Take the node out of the appropriate CSE map.
452 RemoveNodeFromCSEMaps(N);
453
454 // Next, brutally remove the operand list. This is safe to do, as there are
455 // no cycles in the graph.
456 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
457 SDNode *Operand = I->Val;
458 Operand->removeUser(N);
459
460 // Now that we removed this operand, see if there are no uses of it left.
461 if (Operand->use_empty())
462 DeadNodes.push_back(Operand);
463 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000464 if (N->OperandsNeedDelete)
465 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000466 N->OperandList = 0;
467 N->NumOperands = 0;
468
469 // Finally, remove N itself.
470 Deleted.push_back(N);
471 AllNodes.erase(N);
472 }
473}
474
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000475void SelectionDAG::DeleteNode(SDNode *N) {
476 assert(N->use_empty() && "Cannot delete a node that is not dead!");
477
478 // First take this out of the appropriate CSE map.
479 RemoveNodeFromCSEMaps(N);
480
Chris Lattner1e111c72005-09-07 05:37:01 +0000481 // Finally, remove uses due to operands of this node, remove from the
482 // AllNodes list, and delete the node.
483 DeleteNodeNotInCSEMaps(N);
484}
485
486void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
487
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000488 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000489 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000490
491 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000492 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
493 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000494 if (N->OperandsNeedDelete)
495 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000496 N->OperandList = 0;
497 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000498
499 delete N;
500}
501
Chris Lattner149c58c2005-08-16 18:17:10 +0000502/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
503/// correspond to it. This is useful when we're about to delete or repurpose
504/// the node. We don't want future request for structurally identical nodes
505/// to return N anymore.
506void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000507 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000508 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000509 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000510 case ISD::STRING:
511 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
512 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000513 case ISD::CONDCODE:
514 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
515 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000516 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000517 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
518 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000519 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000520 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000521 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000522 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000523 Erased =
524 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000525 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000526 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000527 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000528 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
529 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000530 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000531 // Remove it from the CSE Map.
532 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000533 break;
534 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000535#ifndef NDEBUG
536 // Verify that the node was actually in one of the CSE maps, unless it has a
537 // flag result (which cannot be CSE'd) or is one of the special cases that are
538 // not subject to CSE.
539 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000540 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000541 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000542 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000543 assert(0 && "Node is not in map!");
544 }
545#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000546}
547
Chris Lattner8b8749f2005-08-17 19:00:20 +0000548/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
549/// has been taken out and modified in some way. If the specified node already
550/// exists in the CSE maps, do not modify the maps, but return the existing node
551/// instead. If it doesn't exist, add it and return null.
552///
553SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
554 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000555 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000556 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000557
Chris Lattner9f8cc692005-12-19 22:21:21 +0000558 // Check that remaining values produced are not flags.
559 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
560 if (N->getValueType(i) == MVT::Flag)
561 return 0; // Never CSE anything that produces a flag.
562
Chris Lattnera5682852006-08-07 23:03:03 +0000563 SDNode *New = CSEMap.GetOrInsertNode(N);
564 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000565 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000566}
567
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000568/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
569/// were replaced with those specified. If this node is never memoized,
570/// return null, otherwise return a pointer to the slot it would take. If a
571/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000572SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
573 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000574 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000575 return 0; // Never add these nodes.
576
577 // Check that remaining values produced are not flags.
578 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
579 if (N->getValueType(i) == MVT::Flag)
580 return 0; // Never CSE anything that produces a flag.
581
Chris Lattner63e3f142007-02-04 07:28:00 +0000582 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000583 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000584 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000585 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000586}
587
588/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
589/// were replaced with those specified. If this node is never memoized,
590/// return null, otherwise return a pointer to the slot it would take. If a
591/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000592SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
593 SDOperand Op1, SDOperand Op2,
594 void *&InsertPos) {
595 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
596 return 0; // Never add these nodes.
597
598 // Check that remaining values produced are not flags.
599 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
600 if (N->getValueType(i) == MVT::Flag)
601 return 0; // Never CSE anything that produces a flag.
602
Chris Lattner63e3f142007-02-04 07:28:00 +0000603 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000604 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000605 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000606 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
607}
608
609
610/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
611/// were replaced with those specified. If this node is never memoized,
612/// return null, otherwise return a pointer to the slot it would take. If a
613/// node already exists with these operands, the slot will be non-null.
614SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000615 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000616 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000617 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000618 return 0; // Never add these nodes.
619
620 // Check that remaining values produced are not flags.
621 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
622 if (N->getValueType(i) == MVT::Flag)
623 return 0; // Never CSE anything that produces a flag.
624
Jim Laskey583bd472006-10-27 23:46:08 +0000625 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000626 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000627
Evan Cheng9629aba2006-10-11 01:47:58 +0000628 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
629 ID.AddInteger(LD->getAddressingMode());
630 ID.AddInteger(LD->getExtensionType());
Evan Cheng2e49f092006-10-11 07:10:22 +0000631 ID.AddInteger(LD->getLoadedVT());
Evan Cheng9629aba2006-10-11 01:47:58 +0000632 ID.AddPointer(LD->getSrcValue());
633 ID.AddInteger(LD->getSrcValueOffset());
634 ID.AddInteger(LD->getAlignment());
635 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000636 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
637 ID.AddInteger(ST->getAddressingMode());
638 ID.AddInteger(ST->isTruncatingStore());
639 ID.AddInteger(ST->getStoredVT());
640 ID.AddPointer(ST->getSrcValue());
641 ID.AddInteger(ST->getSrcValueOffset());
642 ID.AddInteger(ST->getAlignment());
643 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000644 }
Jim Laskey583bd472006-10-27 23:46:08 +0000645
Chris Lattnera5682852006-08-07 23:03:03 +0000646 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000647}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000648
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000649
Chris Lattner78ec3112003-08-11 14:57:33 +0000650SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000651 while (!AllNodes.empty()) {
652 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000653 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000654 if (N->OperandsNeedDelete)
655 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000656 N->OperandList = 0;
657 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000658 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000659 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000660}
661
Chris Lattner0f2287b2005-04-13 02:38:18 +0000662SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000663 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000664 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000665 return getNode(ISD::AND, Op.getValueType(), Op,
666 getConstant(Imm, Op.getValueType()));
667}
668
Chris Lattner36ce6912005-11-29 06:21:05 +0000669SDOperand SelectionDAG::getString(const std::string &Val) {
670 StringSDNode *&N = StringNodes[Val];
671 if (!N) {
672 N = new StringSDNode(Val);
673 AllNodes.push_back(N);
674 }
675 return SDOperand(N, 0);
676}
677
Chris Lattner61b09412006-08-11 21:01:22 +0000678SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000679 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000680 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000681
Chris Lattner61b09412006-08-11 21:01:22 +0000682 // Mask out any bits that are not valid for this constant.
683 Val &= MVT::getIntVTBitMask(VT);
684
685 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000686 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000687 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000688 ID.AddInteger(Val);
689 void *IP = 0;
690 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
691 return SDOperand(E, 0);
692 SDNode *N = new ConstantSDNode(isT, Val, VT);
693 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000694 AllNodes.push_back(N);
695 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000696}
697
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000698SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000699 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000700 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000701
Dan Gohman7f321562007-06-25 16:23:39 +0000702 MVT::ValueType EltVT =
703 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000704 bool isDouble = (EltVT == MVT::f64);
705 double Val = isDouble ? V.convertToDouble() : (double)V.convertToFloat();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706
Chris Lattnerd8658612005-02-17 20:17:32 +0000707 // Do the map lookup using the actual bit pattern for the floating point
708 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
709 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000710 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000711 // ?? Should we store float/double/longdouble separately in ID?
Jim Laskey583bd472006-10-27 23:46:08 +0000712 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000713 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Jim Laskey583bd472006-10-27 23:46:08 +0000714 ID.AddDouble(Val);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000715 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000716 SDNode *N = NULL;
717 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
718 if (!MVT::isVector(VT))
719 return SDOperand(N, 0);
720 if (!N) {
721 N = new ConstantFPSDNode(isTarget, Val, EltVT);
722 CSEMap.InsertNode(N, IP);
723 AllNodes.push_back(N);
724 }
725
Dan Gohman7f321562007-06-25 16:23:39 +0000726 SDOperand Result(N, 0);
727 if (MVT::isVector(VT)) {
728 SmallVector<SDOperand, 8> Ops;
729 Ops.assign(MVT::getVectorNumElements(VT), Result);
730 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
731 }
732 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733}
734
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000735SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
736 bool isTarget) {
737 MVT::ValueType EltVT =
738 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
739 if (EltVT==MVT::f32)
740 return getConstantFP(APFloat((float)Val), VT, isTarget);
741 else
742 return getConstantFP(APFloat(Val), VT, isTarget);
743}
744
Chris Lattnerc3aae252005-01-07 07:46:32 +0000745SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000746 MVT::ValueType VT, int Offset,
747 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000748 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
749 unsigned Opc;
750 if (GVar && GVar->isThreadLocal())
751 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
752 else
753 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000754 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000755 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000756 ID.AddPointer(GV);
757 ID.AddInteger(Offset);
758 void *IP = 0;
759 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
760 return SDOperand(E, 0);
761 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
762 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000763 AllNodes.push_back(N);
764 return SDOperand(N, 0);
765}
766
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000767SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
768 bool isTarget) {
769 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000770 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000771 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000772 ID.AddInteger(FI);
773 void *IP = 0;
774 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
775 return SDOperand(E, 0);
776 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
777 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000778 AllNodes.push_back(N);
779 return SDOperand(N, 0);
780}
781
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000782SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
783 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000784 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000785 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000786 ID.AddInteger(JTI);
787 void *IP = 0;
788 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
789 return SDOperand(E, 0);
790 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
791 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000792 AllNodes.push_back(N);
793 return SDOperand(N, 0);
794}
795
Evan Chengb8973bd2006-01-31 22:23:14 +0000796SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000797 unsigned Alignment, int Offset,
798 bool isTarget) {
799 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000800 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000801 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000802 ID.AddInteger(Alignment);
803 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000804 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000805 void *IP = 0;
806 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
807 return SDOperand(E, 0);
808 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
809 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000810 AllNodes.push_back(N);
811 return SDOperand(N, 0);
812}
813
Chris Lattnerc3aae252005-01-07 07:46:32 +0000814
Evan Chengd6594ae2006-09-12 21:00:35 +0000815SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
816 MVT::ValueType VT,
817 unsigned Alignment, int Offset,
818 bool isTarget) {
819 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000820 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000821 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000822 ID.AddInteger(Alignment);
823 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000824 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000825 void *IP = 0;
826 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
827 return SDOperand(E, 0);
828 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
829 CSEMap.InsertNode(N, IP);
830 AllNodes.push_back(N);
831 return SDOperand(N, 0);
832}
833
834
Chris Lattnerc3aae252005-01-07 07:46:32 +0000835SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000836 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000837 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000838 ID.AddPointer(MBB);
839 void *IP = 0;
840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
841 return SDOperand(E, 0);
842 SDNode *N = new BasicBlockSDNode(MBB);
843 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000844 AllNodes.push_back(N);
845 return SDOperand(N, 0);
846}
847
Chris Lattner15e4b012005-07-10 00:07:11 +0000848SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
849 if ((unsigned)VT >= ValueTypeNodes.size())
850 ValueTypeNodes.resize(VT+1);
851 if (ValueTypeNodes[VT] == 0) {
852 ValueTypeNodes[VT] = new VTSDNode(VT);
853 AllNodes.push_back(ValueTypeNodes[VT]);
854 }
855
856 return SDOperand(ValueTypeNodes[VT], 0);
857}
858
Chris Lattnerc3aae252005-01-07 07:46:32 +0000859SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
860 SDNode *&N = ExternalSymbols[Sym];
861 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000862 N = new ExternalSymbolSDNode(false, Sym, VT);
863 AllNodes.push_back(N);
864 return SDOperand(N, 0);
865}
866
Chris Lattner809ec112006-01-28 10:09:25 +0000867SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
868 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000869 SDNode *&N = TargetExternalSymbols[Sym];
870 if (N) return SDOperand(N, 0);
871 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000872 AllNodes.push_back(N);
873 return SDOperand(N, 0);
874}
875
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000876SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
877 if ((unsigned)Cond >= CondCodeNodes.size())
878 CondCodeNodes.resize(Cond+1);
879
Chris Lattner079a27a2005-08-09 20:40:02 +0000880 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000881 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000882 AllNodes.push_back(CondCodeNodes[Cond]);
883 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000884 return SDOperand(CondCodeNodes[Cond], 0);
885}
886
Chris Lattner0fdd7682005-08-30 22:38:38 +0000887SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000888 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000889 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000890 ID.AddInteger(RegNo);
891 void *IP = 0;
892 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
893 return SDOperand(E, 0);
894 SDNode *N = new RegisterSDNode(RegNo, VT);
895 CSEMap.InsertNode(N, IP);
896 AllNodes.push_back(N);
897 return SDOperand(N, 0);
898}
899
900SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
901 assert((!V || isa<PointerType>(V->getType())) &&
902 "SrcValue is not a pointer?");
903
Jim Laskey583bd472006-10-27 23:46:08 +0000904 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000905 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000906 ID.AddPointer(V);
907 ID.AddInteger(Offset);
908 void *IP = 0;
909 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
910 return SDOperand(E, 0);
911 SDNode *N = new SrcValueSDNode(V, Offset);
912 CSEMap.InsertNode(N, IP);
913 AllNodes.push_back(N);
914 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000915}
916
Chris Lattner51dabfb2006-10-14 00:41:01 +0000917SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
918 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000919 // These setcc operations always fold.
920 switch (Cond) {
921 default: break;
922 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000923 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000924 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000925 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000926
927 case ISD::SETOEQ:
928 case ISD::SETOGT:
929 case ISD::SETOGE:
930 case ISD::SETOLT:
931 case ISD::SETOLE:
932 case ISD::SETONE:
933 case ISD::SETO:
934 case ISD::SETUO:
935 case ISD::SETUEQ:
936 case ISD::SETUNE:
937 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
938 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000940
Chris Lattner67255a12005-04-07 18:14:58 +0000941 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
942 uint64_t C2 = N2C->getValue();
943 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
944 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000945
Chris Lattnerc3aae252005-01-07 07:46:32 +0000946 // Sign extend the operands if required
947 if (ISD::isSignedIntSetCC(Cond)) {
948 C1 = N1C->getSignExtended();
949 C2 = N2C->getSignExtended();
950 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000951
Chris Lattnerc3aae252005-01-07 07:46:32 +0000952 switch (Cond) {
953 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000954 case ISD::SETEQ: return getConstant(C1 == C2, VT);
955 case ISD::SETNE: return getConstant(C1 != C2, VT);
956 case ISD::SETULT: return getConstant(C1 < C2, VT);
957 case ISD::SETUGT: return getConstant(C1 > C2, VT);
958 case ISD::SETULE: return getConstant(C1 <= C2, VT);
959 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
960 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
961 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
962 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
963 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000964 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000965 }
Chris Lattner67255a12005-04-07 18:14:58 +0000966 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
968 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
969 double C1 = N1C->getValue(), C2 = N2C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000970
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 switch (Cond) {
972 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000973 case ISD::SETEQ: return getConstant(C1 == C2, VT);
974 case ISD::SETNE: return getConstant(C1 != C2, VT);
975 case ISD::SETLT: return getConstant(C1 < C2, VT);
976 case ISD::SETGT: return getConstant(C1 > C2, VT);
977 case ISD::SETLE: return getConstant(C1 <= C2, VT);
978 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000979 }
980 } else {
981 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000982 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000984
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000985 // Could not fold it.
986 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987}
988
Dan Gohmanea859be2007-06-22 14:59:07 +0000989/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
990/// this predicate to simplify operations downstream. Mask is known to be zero
991/// for bits that V cannot have.
992bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
993 unsigned Depth) const {
994 // The masks are not wide enough to represent this type! Should use APInt.
995 if (Op.getValueType() == MVT::i128)
996 return false;
997
998 uint64_t KnownZero, KnownOne;
999 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1000 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1001 return (KnownZero & Mask) == Mask;
1002}
1003
1004/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1005/// known to be either zero or one and return them in the KnownZero/KnownOne
1006/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1007/// processing.
1008void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1009 uint64_t &KnownZero, uint64_t &KnownOne,
1010 unsigned Depth) const {
1011 KnownZero = KnownOne = 0; // Don't know anything.
1012 if (Depth == 6 || Mask == 0)
1013 return; // Limit search depth.
1014
1015 // The masks are not wide enough to represent this type! Should use APInt.
1016 if (Op.getValueType() == MVT::i128)
1017 return;
1018
1019 uint64_t KnownZero2, KnownOne2;
1020
1021 switch (Op.getOpcode()) {
1022 case ISD::Constant:
1023 // We know all of the bits for a constant!
1024 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1025 KnownZero = ~KnownOne & Mask;
1026 return;
1027 case ISD::AND:
1028 // If either the LHS or the RHS are Zero, the result is zero.
1029 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1030 Mask &= ~KnownZero;
1031 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1032 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1033 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1034
1035 // Output known-1 bits are only known if set in both the LHS & RHS.
1036 KnownOne &= KnownOne2;
1037 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1038 KnownZero |= KnownZero2;
1039 return;
1040 case ISD::OR:
1041 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1042 Mask &= ~KnownOne;
1043 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1044 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1045 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1046
1047 // Output known-0 bits are only known if clear in both the LHS & RHS.
1048 KnownZero &= KnownZero2;
1049 // Output known-1 are known to be set if set in either the LHS | RHS.
1050 KnownOne |= KnownOne2;
1051 return;
1052 case ISD::XOR: {
1053 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1054 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1055 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1056 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1057
1058 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1059 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1060 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1061 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1062 KnownZero = KnownZeroOut;
1063 return;
1064 }
1065 case ISD::SELECT:
1066 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1067 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1068 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1069 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1070
1071 // Only known if known in both the LHS and RHS.
1072 KnownOne &= KnownOne2;
1073 KnownZero &= KnownZero2;
1074 return;
1075 case ISD::SELECT_CC:
1076 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1077 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1078 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1079 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1080
1081 // Only known if known in both the LHS and RHS.
1082 KnownOne &= KnownOne2;
1083 KnownZero &= KnownZero2;
1084 return;
1085 case ISD::SETCC:
1086 // If we know the result of a setcc has the top bits zero, use this info.
1087 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1088 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1089 return;
1090 case ISD::SHL:
1091 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1092 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1093 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1094 KnownZero, KnownOne, Depth+1);
1095 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1096 KnownZero <<= SA->getValue();
1097 KnownOne <<= SA->getValue();
1098 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1099 }
1100 return;
1101 case ISD::SRL:
1102 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1103 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1104 MVT::ValueType VT = Op.getValueType();
1105 unsigned ShAmt = SA->getValue();
1106
1107 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1108 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1109 KnownZero, KnownOne, Depth+1);
1110 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1111 KnownZero &= TypeMask;
1112 KnownOne &= TypeMask;
1113 KnownZero >>= ShAmt;
1114 KnownOne >>= ShAmt;
1115
1116 uint64_t HighBits = (1ULL << ShAmt)-1;
1117 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1118 KnownZero |= HighBits; // High bits known zero.
1119 }
1120 return;
1121 case ISD::SRA:
1122 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1123 MVT::ValueType VT = Op.getValueType();
1124 unsigned ShAmt = SA->getValue();
1125
1126 // Compute the new bits that are at the top now.
1127 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1128
1129 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1130 // If any of the demanded bits are produced by the sign extension, we also
1131 // demand the input sign bit.
1132 uint64_t HighBits = (1ULL << ShAmt)-1;
1133 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1134 if (HighBits & Mask)
1135 InDemandedMask |= MVT::getIntVTSignBit(VT);
1136
1137 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1138 Depth+1);
1139 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1140 KnownZero &= TypeMask;
1141 KnownOne &= TypeMask;
1142 KnownZero >>= ShAmt;
1143 KnownOne >>= ShAmt;
1144
1145 // Handle the sign bits.
1146 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1147 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1148
1149 if (KnownZero & SignBit) {
1150 KnownZero |= HighBits; // New bits are known zero.
1151 } else if (KnownOne & SignBit) {
1152 KnownOne |= HighBits; // New bits are known one.
1153 }
1154 }
1155 return;
1156 case ISD::SIGN_EXTEND_INREG: {
1157 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1158
1159 // Sign extension. Compute the demanded bits in the result that are not
1160 // present in the input.
1161 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1162
1163 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1164 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1165
1166 // If the sign extended bits are demanded, we know that the sign
1167 // bit is demanded.
1168 if (NewBits)
1169 InputDemandedBits |= InSignBit;
1170
1171 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1172 KnownZero, KnownOne, Depth+1);
1173 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1174
1175 // If the sign bit of the input is known set or clear, then we know the
1176 // top bits of the result.
1177 if (KnownZero & InSignBit) { // Input sign bit known clear
1178 KnownZero |= NewBits;
1179 KnownOne &= ~NewBits;
1180 } else if (KnownOne & InSignBit) { // Input sign bit known set
1181 KnownOne |= NewBits;
1182 KnownZero &= ~NewBits;
1183 } else { // Input sign bit unknown
1184 KnownZero &= ~NewBits;
1185 KnownOne &= ~NewBits;
1186 }
1187 return;
1188 }
1189 case ISD::CTTZ:
1190 case ISD::CTLZ:
1191 case ISD::CTPOP: {
1192 MVT::ValueType VT = Op.getValueType();
1193 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1194 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1195 KnownOne = 0;
1196 return;
1197 }
1198 case ISD::LOAD: {
1199 if (ISD::isZEXTLoad(Op.Val)) {
1200 LoadSDNode *LD = cast<LoadSDNode>(Op);
1201 MVT::ValueType VT = LD->getLoadedVT();
1202 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1203 }
1204 return;
1205 }
1206 case ISD::ZERO_EXTEND: {
1207 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1208 uint64_t NewBits = (~InMask) & Mask;
1209 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1210 KnownOne, Depth+1);
1211 KnownZero |= NewBits & Mask;
1212 KnownOne &= ~NewBits;
1213 return;
1214 }
1215 case ISD::SIGN_EXTEND: {
1216 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1217 unsigned InBits = MVT::getSizeInBits(InVT);
1218 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1219 uint64_t InSignBit = 1ULL << (InBits-1);
1220 uint64_t NewBits = (~InMask) & Mask;
1221 uint64_t InDemandedBits = Mask & InMask;
1222
1223 // If any of the sign extended bits are demanded, we know that the sign
1224 // bit is demanded.
1225 if (NewBits & Mask)
1226 InDemandedBits |= InSignBit;
1227
1228 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1229 KnownOne, Depth+1);
1230 // If the sign bit is known zero or one, the top bits match.
1231 if (KnownZero & InSignBit) {
1232 KnownZero |= NewBits;
1233 KnownOne &= ~NewBits;
1234 } else if (KnownOne & InSignBit) {
1235 KnownOne |= NewBits;
1236 KnownZero &= ~NewBits;
1237 } else { // Otherwise, top bits aren't known.
1238 KnownOne &= ~NewBits;
1239 KnownZero &= ~NewBits;
1240 }
1241 return;
1242 }
1243 case ISD::ANY_EXTEND: {
1244 MVT::ValueType VT = Op.getOperand(0).getValueType();
1245 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1246 KnownZero, KnownOne, Depth+1);
1247 return;
1248 }
1249 case ISD::TRUNCATE: {
1250 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1251 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1252 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1253 KnownZero &= OutMask;
1254 KnownOne &= OutMask;
1255 break;
1256 }
1257 case ISD::AssertZext: {
1258 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1259 uint64_t InMask = MVT::getIntVTBitMask(VT);
1260 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1261 KnownOne, Depth+1);
1262 KnownZero |= (~InMask) & Mask;
1263 return;
1264 }
1265 case ISD::ADD: {
1266 // If either the LHS or the RHS are Zero, the result is zero.
1267 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1268 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1269 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1270 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1271
1272 // Output known-0 bits are known if clear or set in both the low clear bits
1273 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1274 // low 3 bits clear.
1275 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1276 CountTrailingZeros_64(~KnownZero2));
1277
1278 KnownZero = (1ULL << KnownZeroOut) - 1;
1279 KnownOne = 0;
1280 return;
1281 }
1282 case ISD::SUB: {
1283 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1284 if (!CLHS) return;
1285
1286 // We know that the top bits of C-X are clear if X contains less bits
1287 // than C (i.e. no wrap-around can happen). For example, 20-X is
1288 // positive if we can prove that X is >= 0 and < 16.
1289 MVT::ValueType VT = CLHS->getValueType(0);
1290 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1291 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1292 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1293 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1294 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1295
1296 // If all of the MaskV bits are known to be zero, then we know the output
1297 // top bits are zero, because we now know that the output is from [0-C].
1298 if ((KnownZero & MaskV) == MaskV) {
1299 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1300 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1301 KnownOne = 0; // No one bits known.
1302 } else {
1303 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1304 }
1305 }
1306 return;
1307 }
1308 default:
1309 // Allow the target to implement this method for its nodes.
1310 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1311 case ISD::INTRINSIC_WO_CHAIN:
1312 case ISD::INTRINSIC_W_CHAIN:
1313 case ISD::INTRINSIC_VOID:
1314 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1315 }
1316 return;
1317 }
1318}
1319
1320/// ComputeNumSignBits - Return the number of times the sign bit of the
1321/// register is replicated into the other bits. We know that at least 1 bit
1322/// is always equal to the sign bit (itself), but other cases can give us
1323/// information. For example, immediately after an "SRA X, 2", we know that
1324/// the top 3 bits are all equal to each other, so we return 3.
1325unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1326 MVT::ValueType VT = Op.getValueType();
1327 assert(MVT::isInteger(VT) && "Invalid VT!");
1328 unsigned VTBits = MVT::getSizeInBits(VT);
1329 unsigned Tmp, Tmp2;
1330
1331 if (Depth == 6)
1332 return 1; // Limit search depth.
1333
1334 switch (Op.getOpcode()) {
1335 default: break;
1336 case ISD::AssertSext:
1337 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1338 return VTBits-Tmp+1;
1339 case ISD::AssertZext:
1340 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1341 return VTBits-Tmp;
1342
1343 case ISD::Constant: {
1344 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1345 // If negative, invert the bits, then look at it.
1346 if (Val & MVT::getIntVTSignBit(VT))
1347 Val = ~Val;
1348
1349 // Shift the bits so they are the leading bits in the int64_t.
1350 Val <<= 64-VTBits;
1351
1352 // Return # leading zeros. We use 'min' here in case Val was zero before
1353 // shifting. We don't want to return '64' as for an i32 "0".
1354 return std::min(VTBits, CountLeadingZeros_64(Val));
1355 }
1356
1357 case ISD::SIGN_EXTEND:
1358 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1359 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1360
1361 case ISD::SIGN_EXTEND_INREG:
1362 // Max of the input and what this extends.
1363 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1364 Tmp = VTBits-Tmp+1;
1365
1366 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1367 return std::max(Tmp, Tmp2);
1368
1369 case ISD::SRA:
1370 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1371 // SRA X, C -> adds C sign bits.
1372 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1373 Tmp += C->getValue();
1374 if (Tmp > VTBits) Tmp = VTBits;
1375 }
1376 return Tmp;
1377 case ISD::SHL:
1378 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1379 // shl destroys sign bits.
1380 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1381 if (C->getValue() >= VTBits || // Bad shift.
1382 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1383 return Tmp - C->getValue();
1384 }
1385 break;
1386 case ISD::AND:
1387 case ISD::OR:
1388 case ISD::XOR: // NOT is handled here.
1389 // Logical binary ops preserve the number of sign bits.
1390 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1391 if (Tmp == 1) return 1; // Early out.
1392 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1393 return std::min(Tmp, Tmp2);
1394
1395 case ISD::SELECT:
1396 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1397 if (Tmp == 1) return 1; // Early out.
1398 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1399 return std::min(Tmp, Tmp2);
1400
1401 case ISD::SETCC:
1402 // If setcc returns 0/-1, all bits are sign bits.
1403 if (TLI.getSetCCResultContents() ==
1404 TargetLowering::ZeroOrNegativeOneSetCCResult)
1405 return VTBits;
1406 break;
1407 case ISD::ROTL:
1408 case ISD::ROTR:
1409 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1410 unsigned RotAmt = C->getValue() & (VTBits-1);
1411
1412 // Handle rotate right by N like a rotate left by 32-N.
1413 if (Op.getOpcode() == ISD::ROTR)
1414 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1415
1416 // If we aren't rotating out all of the known-in sign bits, return the
1417 // number that are left. This handles rotl(sext(x), 1) for example.
1418 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1419 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1420 }
1421 break;
1422 case ISD::ADD:
1423 // Add can have at most one carry bit. Thus we know that the output
1424 // is, at worst, one more bit than the inputs.
1425 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1426 if (Tmp == 1) return 1; // Early out.
1427
1428 // Special case decrementing a value (ADD X, -1):
1429 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1430 if (CRHS->isAllOnesValue()) {
1431 uint64_t KnownZero, KnownOne;
1432 uint64_t Mask = MVT::getIntVTBitMask(VT);
1433 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1434
1435 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1436 // sign bits set.
1437 if ((KnownZero|1) == Mask)
1438 return VTBits;
1439
1440 // If we are subtracting one from a positive number, there is no carry
1441 // out of the result.
1442 if (KnownZero & MVT::getIntVTSignBit(VT))
1443 return Tmp;
1444 }
1445
1446 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1447 if (Tmp2 == 1) return 1;
1448 return std::min(Tmp, Tmp2)-1;
1449 break;
1450
1451 case ISD::SUB:
1452 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1453 if (Tmp2 == 1) return 1;
1454
1455 // Handle NEG.
1456 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1457 if (CLHS->getValue() == 0) {
1458 uint64_t KnownZero, KnownOne;
1459 uint64_t Mask = MVT::getIntVTBitMask(VT);
1460 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1461 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1462 // sign bits set.
1463 if ((KnownZero|1) == Mask)
1464 return VTBits;
1465
1466 // If the input is known to be positive (the sign bit is known clear),
1467 // the output of the NEG has the same number of sign bits as the input.
1468 if (KnownZero & MVT::getIntVTSignBit(VT))
1469 return Tmp2;
1470
1471 // Otherwise, we treat this like a SUB.
1472 }
1473
1474 // Sub can have at most one carry bit. Thus we know that the output
1475 // is, at worst, one more bit than the inputs.
1476 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1477 if (Tmp == 1) return 1; // Early out.
1478 return std::min(Tmp, Tmp2)-1;
1479 break;
1480 case ISD::TRUNCATE:
1481 // FIXME: it's tricky to do anything useful for this, but it is an important
1482 // case for targets like X86.
1483 break;
1484 }
1485
1486 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1487 if (Op.getOpcode() == ISD::LOAD) {
1488 LoadSDNode *LD = cast<LoadSDNode>(Op);
1489 unsigned ExtType = LD->getExtensionType();
1490 switch (ExtType) {
1491 default: break;
1492 case ISD::SEXTLOAD: // '17' bits known
1493 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1494 return VTBits-Tmp+1;
1495 case ISD::ZEXTLOAD: // '16' bits known
1496 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1497 return VTBits-Tmp;
1498 }
1499 }
1500
1501 // Allow the target to implement this method for its nodes.
1502 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1503 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1504 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1505 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1506 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1507 if (NumBits > 1) return NumBits;
1508 }
1509
1510 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1511 // use this information.
1512 uint64_t KnownZero, KnownOne;
1513 uint64_t Mask = MVT::getIntVTBitMask(VT);
1514 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1515
1516 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1517 if (KnownZero & SignBit) { // SignBit is 0
1518 Mask = KnownZero;
1519 } else if (KnownOne & SignBit) { // SignBit is 1;
1520 Mask = KnownOne;
1521 } else {
1522 // Nothing known.
1523 return 1;
1524 }
1525
1526 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1527 // the number of identical bits in the top of the input value.
1528 Mask ^= ~0ULL;
1529 Mask <<= 64-VTBits;
1530 // Return # leading zeros. We use 'min' here in case Val was zero before
1531 // shifting. We don't want to return '64' as for an i32 "0".
1532 return std::min(VTBits, CountLeadingZeros_64(Mask));
1533}
1534
Chris Lattner51dabfb2006-10-14 00:41:01 +00001535
Chris Lattnerc3aae252005-01-07 07:46:32 +00001536/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001537///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001538SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001539 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001540 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001541 void *IP = 0;
1542 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1543 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001544 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001545 CSEMap.InsertNode(N, IP);
1546
1547 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001548 return SDOperand(N, 0);
1549}
1550
Chris Lattnerc3aae252005-01-07 07:46:32 +00001551SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1552 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001553 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001554 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001555 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1556 uint64_t Val = C->getValue();
1557 switch (Opcode) {
1558 default: break;
1559 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001560 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001561 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1562 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001563 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1564 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001565 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001566 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001567 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001568 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001569 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001570 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001571 case ISD::BSWAP:
1572 switch(VT) {
1573 default: assert(0 && "Invalid bswap!"); break;
1574 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1575 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1576 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1577 }
1578 break;
1579 case ISD::CTPOP:
1580 switch(VT) {
1581 default: assert(0 && "Invalid ctpop!"); break;
1582 case MVT::i1: return getConstant(Val != 0, VT);
1583 case MVT::i8:
1584 Tmp1 = (unsigned)Val & 0xFF;
1585 return getConstant(CountPopulation_32(Tmp1), VT);
1586 case MVT::i16:
1587 Tmp1 = (unsigned)Val & 0xFFFF;
1588 return getConstant(CountPopulation_32(Tmp1), VT);
1589 case MVT::i32:
1590 return getConstant(CountPopulation_32((unsigned)Val), VT);
1591 case MVT::i64:
1592 return getConstant(CountPopulation_64(Val), VT);
1593 }
1594 case ISD::CTLZ:
1595 switch(VT) {
1596 default: assert(0 && "Invalid ctlz!"); break;
1597 case MVT::i1: return getConstant(Val == 0, VT);
1598 case MVT::i8:
1599 Tmp1 = (unsigned)Val & 0xFF;
1600 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1601 case MVT::i16:
1602 Tmp1 = (unsigned)Val & 0xFFFF;
1603 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1604 case MVT::i32:
1605 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1606 case MVT::i64:
1607 return getConstant(CountLeadingZeros_64(Val), VT);
1608 }
1609 case ISD::CTTZ:
1610 switch(VT) {
1611 default: assert(0 && "Invalid cttz!"); break;
1612 case MVT::i1: return getConstant(Val == 0, VT);
1613 case MVT::i8:
1614 Tmp1 = (unsigned)Val | 0x100;
1615 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1616 case MVT::i16:
1617 Tmp1 = (unsigned)Val | 0x10000;
1618 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1619 case MVT::i32:
1620 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1621 case MVT::i64:
1622 return getConstant(CountTrailingZeros_64(Val), VT);
1623 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001624 }
1625 }
1626
Chris Lattner94683772005-12-23 05:30:37 +00001627 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001628 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1629 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001630 case ISD::FNEG:
1631 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001632 case ISD::FABS:
1633 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001634 case ISD::FP_ROUND:
1635 case ISD::FP_EXTEND:
1636 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001637 case ISD::FP_TO_SINT:
1638 return getConstant((int64_t)C->getValue(), VT);
1639 case ISD::FP_TO_UINT:
1640 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001641 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001642 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001643 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001644 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001645 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001646 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001647 }
1648
1649 unsigned OpOpcode = Operand.Val->getOpcode();
1650 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001651 case ISD::TokenFactor:
1652 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001653 case ISD::FP_ROUND:
1654 case ISD::FP_EXTEND:
1655 assert(MVT::isFloatingPoint(VT) &&
1656 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1657 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001658 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001659 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1660 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001661 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001662 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001663 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1664 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1665 break;
1666 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001667 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1668 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001669 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001670 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001671 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001672 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001673 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001674 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001675 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1676 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001677 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001678 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001679 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1680 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1681 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1682 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001683 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001684 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1685 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001686 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001687 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001688 if (OpOpcode == ISD::TRUNCATE)
1689 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001690 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1691 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001692 // If the source is smaller than the dest, we still need an extend.
1693 if (Operand.Val->getOperand(0).getValueType() < VT)
1694 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1695 else if (Operand.Val->getOperand(0).getValueType() > VT)
1696 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1697 else
1698 return Operand.Val->getOperand(0);
1699 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001700 break;
Chris Lattner35481892005-12-23 00:16:34 +00001701 case ISD::BIT_CONVERT:
1702 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001703 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001704 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001705 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001706 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1707 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001708 if (OpOpcode == ISD::UNDEF)
1709 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001710 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001711 case ISD::SCALAR_TO_VECTOR:
1712 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001713 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001714 "Illegal SCALAR_TO_VECTOR node!");
1715 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001716 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001717 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1718 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001719 Operand.Val->getOperand(0));
1720 if (OpOpcode == ISD::FNEG) // --X -> X
1721 return Operand.Val->getOperand(0);
1722 break;
1723 case ISD::FABS:
1724 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1725 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1726 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001727 }
1728
Chris Lattner43247a12005-08-25 19:12:10 +00001729 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001730 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001731 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001732 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001733 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001734 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001735 void *IP = 0;
1736 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1737 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001738 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001739 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001740 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001741 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001742 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001743 AllNodes.push_back(N);
1744 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001745}
1746
Chris Lattner36019aa2005-04-18 03:48:41 +00001747
1748
Chris Lattnerc3aae252005-01-07 07:46:32 +00001749SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1750 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001751#ifndef NDEBUG
1752 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001753 case ISD::TokenFactor:
1754 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1755 N2.getValueType() == MVT::Other && "Invalid token factor!");
1756 break;
Chris Lattner76365122005-01-16 02:23:22 +00001757 case ISD::AND:
1758 case ISD::OR:
1759 case ISD::XOR:
1760 case ISD::UDIV:
1761 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001762 case ISD::MULHU:
1763 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001764 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1765 // fall through
1766 case ISD::ADD:
1767 case ISD::SUB:
1768 case ISD::MUL:
1769 case ISD::SDIV:
1770 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001771 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1772 // fall through.
1773 case ISD::FADD:
1774 case ISD::FSUB:
1775 case ISD::FMUL:
1776 case ISD::FDIV:
1777 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001778 assert(N1.getValueType() == N2.getValueType() &&
1779 N1.getValueType() == VT && "Binary operator types must match!");
1780 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001781 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1782 assert(N1.getValueType() == VT &&
1783 MVT::isFloatingPoint(N1.getValueType()) &&
1784 MVT::isFloatingPoint(N2.getValueType()) &&
1785 "Invalid FCOPYSIGN!");
1786 break;
Chris Lattner76365122005-01-16 02:23:22 +00001787 case ISD::SHL:
1788 case ISD::SRA:
1789 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001790 case ISD::ROTL:
1791 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001792 assert(VT == N1.getValueType() &&
1793 "Shift operators return type must be the same as their first arg");
1794 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001795 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001796 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001797 case ISD::FP_ROUND_INREG: {
1798 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1799 assert(VT == N1.getValueType() && "Not an inreg round!");
1800 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1801 "Cannot FP_ROUND_INREG integer types");
1802 assert(EVT <= VT && "Not rounding down!");
1803 break;
1804 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001805 case ISD::AssertSext:
1806 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001807 case ISD::SIGN_EXTEND_INREG: {
1808 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1809 assert(VT == N1.getValueType() && "Not an inreg extend!");
1810 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1811 "Cannot *_EXTEND_INREG FP types");
1812 assert(EVT <= VT && "Not extending!");
1813 }
1814
Chris Lattner76365122005-01-16 02:23:22 +00001815 default: break;
1816 }
1817#endif
1818
Chris Lattnerc3aae252005-01-07 07:46:32 +00001819 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1820 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1821 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001822 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1823 int64_t Val = N1C->getValue();
1824 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1825 Val <<= 64-FromBits;
1826 Val >>= 64-FromBits;
1827 return getConstant(Val, VT);
1828 }
1829
Chris Lattnerc3aae252005-01-07 07:46:32 +00001830 if (N2C) {
1831 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1832 switch (Opcode) {
1833 case ISD::ADD: return getConstant(C1 + C2, VT);
1834 case ISD::SUB: return getConstant(C1 - C2, VT);
1835 case ISD::MUL: return getConstant(C1 * C2, VT);
1836 case ISD::UDIV:
1837 if (C2) return getConstant(C1 / C2, VT);
1838 break;
1839 case ISD::UREM :
1840 if (C2) return getConstant(C1 % C2, VT);
1841 break;
1842 case ISD::SDIV :
1843 if (C2) return getConstant(N1C->getSignExtended() /
1844 N2C->getSignExtended(), VT);
1845 break;
1846 case ISD::SREM :
1847 if (C2) return getConstant(N1C->getSignExtended() %
1848 N2C->getSignExtended(), VT);
1849 break;
1850 case ISD::AND : return getConstant(C1 & C2, VT);
1851 case ISD::OR : return getConstant(C1 | C2, VT);
1852 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001853 case ISD::SHL : return getConstant(C1 << C2, VT);
1854 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001855 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001856 case ISD::ROTL :
1857 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1858 VT);
1859 case ISD::ROTR :
1860 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1861 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001862 default: break;
1863 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001864 } else { // Cannonicalize constant to RHS if commutative
1865 if (isCommutativeBinOp(Opcode)) {
1866 std::swap(N1C, N2C);
1867 std::swap(N1, N2);
1868 }
1869 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001870 }
1871
1872 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1873 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001874 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001875 if (N2CFP) {
1876 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1877 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001878 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1879 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1880 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1881 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001882 if (C2) return getConstantFP(C1 / C2, VT);
1883 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001884 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001885 if (C2) return getConstantFP(fmod(C1, C2), VT);
1886 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001887 case ISD::FCOPYSIGN: {
1888 union {
1889 double F;
1890 uint64_t I;
1891 } u1;
Chris Lattner3c232c82006-03-05 23:57:58 +00001892 u1.F = C1;
Chris Lattner964dd862007-04-25 00:00:45 +00001893 if (int64_t(DoubleToBits(C2)) < 0) // Sign bit of RHS set?
Chris Lattner3c232c82006-03-05 23:57:58 +00001894 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1895 else
1896 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1897 return getConstantFP(u1.F, VT);
1898 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001899 default: break;
1900 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001901 } else { // Cannonicalize constant to RHS if commutative
1902 if (isCommutativeBinOp(Opcode)) {
1903 std::swap(N1CFP, N2CFP);
1904 std::swap(N1, N2);
1905 }
1906 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001907 }
Chris Lattner62b57722006-04-20 05:39:12 +00001908
1909 // Canonicalize an UNDEF to the RHS, even over a constant.
1910 if (N1.getOpcode() == ISD::UNDEF) {
1911 if (isCommutativeBinOp(Opcode)) {
1912 std::swap(N1, N2);
1913 } else {
1914 switch (Opcode) {
1915 case ISD::FP_ROUND_INREG:
1916 case ISD::SIGN_EXTEND_INREG:
1917 case ISD::SUB:
1918 case ISD::FSUB:
1919 case ISD::FDIV:
1920 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001921 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001922 return N1; // fold op(undef, arg2) -> undef
1923 case ISD::UDIV:
1924 case ISD::SDIV:
1925 case ISD::UREM:
1926 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001927 case ISD::SRL:
1928 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00001929 if (!MVT::isVector(VT))
1930 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1931 // For vectors, we can't easily build an all zero vector, just return
1932 // the LHS.
1933 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00001934 }
1935 }
1936 }
1937
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001938 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001939 if (N2.getOpcode() == ISD::UNDEF) {
1940 switch (Opcode) {
1941 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00001942 case ISD::ADDC:
1943 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00001944 case ISD::SUB:
1945 case ISD::FADD:
1946 case ISD::FSUB:
1947 case ISD::FMUL:
1948 case ISD::FDIV:
1949 case ISD::FREM:
1950 case ISD::UDIV:
1951 case ISD::SDIV:
1952 case ISD::UREM:
1953 case ISD::SREM:
1954 case ISD::XOR:
1955 return N2; // fold op(arg1, undef) -> undef
1956 case ISD::MUL:
1957 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001958 case ISD::SRL:
1959 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00001960 if (!MVT::isVector(VT))
1961 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1962 // For vectors, we can't easily build an all zero vector, just return
1963 // the LHS.
1964 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001965 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00001966 if (!MVT::isVector(VT))
1967 return getConstant(MVT::getIntVTBitMask(VT), VT);
1968 // For vectors, we can't easily build an all one vector, just return
1969 // the LHS.
1970 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00001971 case ISD::SRA:
1972 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001973 }
1974 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001975
Chris Lattner51dabfb2006-10-14 00:41:01 +00001976 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001977 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00001978 case ISD::TokenFactor:
1979 // Fold trivial token factors.
1980 if (N1.getOpcode() == ISD::EntryToken) return N2;
1981 if (N2.getOpcode() == ISD::EntryToken) return N1;
1982 break;
1983
Chris Lattner51dabfb2006-10-14 00:41:01 +00001984 case ISD::AND:
1985 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1986 // worth handling here.
1987 if (N2C && N2C->getValue() == 0)
1988 return N2;
1989 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00001990 case ISD::OR:
1991 case ISD::XOR:
1992 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1993 // worth handling here.
1994 if (N2C && N2C->getValue() == 0)
1995 return N1;
1996 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001997 case ISD::FP_ROUND_INREG:
1998 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1999 break;
2000 case ISD::SIGN_EXTEND_INREG: {
2001 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2002 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002003 break;
2004 }
Dan Gohman7f321562007-06-25 16:23:39 +00002005 case ISD::EXTRACT_VECTOR_ELT:
2006 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2007
Dan Gohman743d3a72007-07-10 18:20:44 +00002008 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002009 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002010 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2011 N1.getNumOperands() > 0) {
2012 unsigned Factor =
2013 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2014 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2015 N1.getOperand(N2C->getValue() / Factor),
2016 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002017 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002018
Dan Gohman7f321562007-06-25 16:23:39 +00002019 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2020 // expanding large vector constants.
2021 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2022 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002023
2024 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2025 // operations are lowered to scalars.
2026 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2027 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2028 if (IEC == N2C)
2029 return N1.getOperand(1);
2030 else
2031 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2032 }
Dan Gohman7f321562007-06-25 16:23:39 +00002033 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002034 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002035 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2036
Chris Lattner5c6621c2006-09-19 04:51:23 +00002037 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2038 // 64-bit integers into 32-bit parts. Instead of building the extract of
2039 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002040 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002041 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002042
2043 // EXTRACT_ELEMENT of a constant int is also very common.
2044 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2045 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2046 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002047 }
2048 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002049
Nate Begemaneea805e2005-04-13 21:23:31 +00002050 // FIXME: figure out how to safely handle things like
2051 // int foo(int x) { return 1 << (x & 255); }
2052 // int bar() { return foo(256); }
2053#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002054 case ISD::SHL:
2055 case ISD::SRL:
2056 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002057 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002058 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002059 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002060 else if (N2.getOpcode() == ISD::AND)
2061 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2062 // If the and is only masking out bits that cannot effect the shift,
2063 // eliminate the and.
2064 unsigned NumBits = MVT::getSizeInBits(VT);
2065 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2066 return getNode(Opcode, VT, N1, N2.getOperand(0));
2067 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002068 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002069#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002070 }
2071
Chris Lattner27e9b412005-05-11 18:57:39 +00002072 // Memoize this node if possible.
2073 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002074 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002075 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002076 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002077 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002078 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002079 void *IP = 0;
2080 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2081 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002082 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002083 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002084 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002085 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002086 }
2087
Chris Lattnerc3aae252005-01-07 07:46:32 +00002088 AllNodes.push_back(N);
2089 return SDOperand(N, 0);
2090}
2091
Chris Lattnerc3aae252005-01-07 07:46:32 +00002092SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2093 SDOperand N1, SDOperand N2, SDOperand N3) {
2094 // Perform various simplifications.
2095 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2096 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002097 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002098 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002099 // Use FoldSetCC to simplify SETCC's.
2100 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002101 if (Simp.Val) return Simp;
2102 break;
2103 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002104 case ISD::SELECT:
2105 if (N1C)
2106 if (N1C->getValue())
2107 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002108 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002109 return N3; // select false, X, Y -> Y
2110
2111 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002112 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002113 case ISD::BRCOND:
2114 if (N2C)
2115 if (N2C->getValue()) // Unconditional branch
2116 return getNode(ISD::BR, MVT::Other, N1, N3);
2117 else
2118 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002119 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002120 case ISD::VECTOR_SHUFFLE:
2121 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2122 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2123 N3.getOpcode() == ISD::BUILD_VECTOR &&
2124 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2125 "Illegal VECTOR_SHUFFLE node!");
2126 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002127 case ISD::BIT_CONVERT:
2128 // Fold bit_convert nodes from a type to themselves.
2129 if (N1.getValueType() == VT)
2130 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002131 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002132 }
2133
Chris Lattner43247a12005-08-25 19:12:10 +00002134 // Memoize node if it doesn't produce a flag.
2135 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002136 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002137 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002138 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002139 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002140 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002141 void *IP = 0;
2142 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2143 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002144 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002145 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002146 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002147 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002148 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002149 AllNodes.push_back(N);
2150 return SDOperand(N, 0);
2151}
2152
2153SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002154 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002155 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002156 SDOperand Ops[] = { N1, N2, N3, N4 };
2157 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002158}
2159
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002160SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2161 SDOperand N1, SDOperand N2, SDOperand N3,
2162 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002163 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2164 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002165}
2166
Evan Cheng7038daf2005-12-10 00:37:58 +00002167SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2168 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002169 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002170 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002171 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2172 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002173 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002174 Ty = MVT::getTypeForValueType(VT);
2175 } else if (SV) {
2176 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2177 assert(PT && "Value for load must be a pointer");
2178 Ty = PT->getElementType();
2179 }
2180 assert(Ty && "Could not get type information for load");
2181 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2182 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002183 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002184 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002185 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002186 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002187 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002188 ID.AddInteger(ISD::UNINDEXED);
2189 ID.AddInteger(ISD::NON_EXTLOAD);
2190 ID.AddInteger(VT);
2191 ID.AddPointer(SV);
2192 ID.AddInteger(SVOffset);
2193 ID.AddInteger(Alignment);
2194 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002195 void *IP = 0;
2196 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2197 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002198 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002199 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2200 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002201 CSEMap.InsertNode(N, IP);
2202 AllNodes.push_back(N);
2203 return SDOperand(N, 0);
2204}
2205
2206SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002207 SDOperand Chain, SDOperand Ptr,
2208 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002209 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002210 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002211 // If they are asking for an extending load from/to the same thing, return a
2212 // normal load.
2213 if (VT == EVT)
2214 ExtType = ISD::NON_EXTLOAD;
2215
2216 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002217 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002218 else
2219 assert(EVT < VT && "Should only be an extending load, not truncating!");
2220 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2221 "Cannot sign/zero extend a FP/Vector load!");
2222 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2223 "Cannot convert from FP to Int or Int -> FP!");
2224
Dan Gohman575e2f42007-06-04 15:49:41 +00002225 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2226 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002227 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002228 Ty = MVT::getTypeForValueType(VT);
2229 } else if (SV) {
2230 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2231 assert(PT && "Value for load must be a pointer");
2232 Ty = PT->getElementType();
2233 }
2234 assert(Ty && "Could not get type information for load");
2235 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2236 }
Evan Cheng466685d2006-10-09 20:57:25 +00002237 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002238 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002239 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002240 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002241 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002242 ID.AddInteger(ISD::UNINDEXED);
2243 ID.AddInteger(ExtType);
2244 ID.AddInteger(EVT);
2245 ID.AddPointer(SV);
2246 ID.AddInteger(SVOffset);
2247 ID.AddInteger(Alignment);
2248 ID.AddInteger(isVolatile);
2249 void *IP = 0;
2250 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2251 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002252 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002253 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002254 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002255 AllNodes.push_back(N);
2256 return SDOperand(N, 0);
2257}
2258
Evan Cheng144d8f02006-11-09 17:55:04 +00002259SDOperand
2260SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2261 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002262 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002263 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2264 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002265 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002266 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002267 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002268 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002269 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002270 ID.AddInteger(AM);
2271 ID.AddInteger(LD->getExtensionType());
2272 ID.AddInteger(LD->getLoadedVT());
2273 ID.AddPointer(LD->getSrcValue());
2274 ID.AddInteger(LD->getSrcValueOffset());
2275 ID.AddInteger(LD->getAlignment());
2276 ID.AddInteger(LD->isVolatile());
2277 void *IP = 0;
2278 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2279 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002280 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002281 LD->getExtensionType(), LD->getLoadedVT(),
2282 LD->getSrcValue(), LD->getSrcValueOffset(),
2283 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002284 CSEMap.InsertNode(N, IP);
2285 AllNodes.push_back(N);
2286 return SDOperand(N, 0);
2287}
2288
Jeff Cohend41b30d2006-11-05 19:31:28 +00002289SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002290 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002291 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002292 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002293
Dan Gohman575e2f42007-06-04 15:49:41 +00002294 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2295 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002296 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002297 Ty = MVT::getTypeForValueType(VT);
2298 } else if (SV) {
2299 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2300 assert(PT && "Value for store must be a pointer");
2301 Ty = PT->getElementType();
2302 }
2303 assert(Ty && "Could not get type information for store");
2304 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2305 }
Evan Chengad071e12006-10-05 22:57:11 +00002306 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002307 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002308 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002309 FoldingSetNodeID ID;
2310 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002311 ID.AddInteger(ISD::UNINDEXED);
2312 ID.AddInteger(false);
2313 ID.AddInteger(VT);
2314 ID.AddPointer(SV);
2315 ID.AddInteger(SVOffset);
2316 ID.AddInteger(Alignment);
2317 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002318 void *IP = 0;
2319 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2320 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002321 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002322 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002323 CSEMap.InsertNode(N, IP);
2324 AllNodes.push_back(N);
2325 return SDOperand(N, 0);
2326}
2327
Jeff Cohend41b30d2006-11-05 19:31:28 +00002328SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002329 SDOperand Ptr, const Value *SV,
2330 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002331 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002332 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002333 bool isTrunc = VT != SVT;
2334
2335 assert(VT > SVT && "Not a truncation?");
2336 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2337 "Can't do FP-INT conversion!");
2338
Dan Gohman575e2f42007-06-04 15:49:41 +00002339 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2340 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002341 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002342 Ty = MVT::getTypeForValueType(VT);
2343 } else if (SV) {
2344 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2345 assert(PT && "Value for store must be a pointer");
2346 Ty = PT->getElementType();
2347 }
2348 assert(Ty && "Could not get type information for store");
2349 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2350 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002351 SDVTList VTs = getVTList(MVT::Other);
2352 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002353 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002354 FoldingSetNodeID ID;
2355 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002356 ID.AddInteger(ISD::UNINDEXED);
2357 ID.AddInteger(isTrunc);
2358 ID.AddInteger(SVT);
2359 ID.AddPointer(SV);
2360 ID.AddInteger(SVOffset);
2361 ID.AddInteger(Alignment);
2362 ID.AddInteger(isVolatile);
2363 void *IP = 0;
2364 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2365 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002366 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002367 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002368 CSEMap.InsertNode(N, IP);
2369 AllNodes.push_back(N);
2370 return SDOperand(N, 0);
2371}
2372
Evan Cheng144d8f02006-11-09 17:55:04 +00002373SDOperand
2374SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2375 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002376 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2377 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2378 "Store is already a indexed store!");
2379 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2380 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2381 FoldingSetNodeID ID;
2382 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2383 ID.AddInteger(AM);
2384 ID.AddInteger(ST->isTruncatingStore());
2385 ID.AddInteger(ST->getStoredVT());
2386 ID.AddPointer(ST->getSrcValue());
2387 ID.AddInteger(ST->getSrcValueOffset());
2388 ID.AddInteger(ST->getAlignment());
2389 ID.AddInteger(ST->isVolatile());
2390 void *IP = 0;
2391 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2392 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002393 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002394 ST->isTruncatingStore(), ST->getStoredVT(),
2395 ST->getSrcValue(), ST->getSrcValueOffset(),
2396 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002397 CSEMap.InsertNode(N, IP);
2398 AllNodes.push_back(N);
2399 return SDOperand(N, 0);
2400}
2401
Nate Begemanacc398c2006-01-25 18:21:52 +00002402SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2403 SDOperand Chain, SDOperand Ptr,
2404 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002405 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002406 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002407}
2408
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002409SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002410 const SDOperand *Ops, unsigned NumOps) {
2411 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002412 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002413 case 1: return getNode(Opcode, VT, Ops[0]);
2414 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2415 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002416 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002417 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002418
Chris Lattneref847df2005-04-09 03:27:28 +00002419 switch (Opcode) {
2420 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002421 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002422 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002423 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2424 "LHS and RHS of condition must have same type!");
2425 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2426 "True and False arms of SelectCC must have same type!");
2427 assert(Ops[2].getValueType() == VT &&
2428 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002429 break;
2430 }
2431 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002432 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002433 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2434 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002435 break;
2436 }
Chris Lattneref847df2005-04-09 03:27:28 +00002437 }
2438
Chris Lattner385328c2005-05-14 07:42:29 +00002439 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002440 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002441 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002442 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002443 FoldingSetNodeID ID;
2444 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002445 void *IP = 0;
2446 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2447 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002448 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002449 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002450 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002451 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002452 }
Chris Lattneref847df2005-04-09 03:27:28 +00002453 AllNodes.push_back(N);
2454 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002455}
2456
Chris Lattner89c34632005-05-14 06:20:26 +00002457SDOperand SelectionDAG::getNode(unsigned Opcode,
2458 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002459 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002460 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2461 Ops, NumOps);
2462}
2463
2464SDOperand SelectionDAG::getNode(unsigned Opcode,
2465 const MVT::ValueType *VTs, unsigned NumVTs,
2466 const SDOperand *Ops, unsigned NumOps) {
2467 if (NumVTs == 1)
2468 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002469 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2470}
2471
2472SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2473 const SDOperand *Ops, unsigned NumOps) {
2474 if (VTList.NumVTs == 1)
2475 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002476
Chris Lattner5f056bf2005-07-10 01:55:33 +00002477 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002478 // FIXME: figure out how to safely handle things like
2479 // int foo(int x) { return 1 << (x & 255); }
2480 // int bar() { return foo(256); }
2481#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002482 case ISD::SRA_PARTS:
2483 case ISD::SRL_PARTS:
2484 case ISD::SHL_PARTS:
2485 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002486 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002487 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2488 else if (N3.getOpcode() == ISD::AND)
2489 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2490 // If the and is only masking out bits that cannot effect the shift,
2491 // eliminate the and.
2492 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2493 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2494 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2495 }
2496 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002497#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002498 }
Chris Lattner89c34632005-05-14 06:20:26 +00002499
Chris Lattner43247a12005-08-25 19:12:10 +00002500 // Memoize the node unless it returns a flag.
2501 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002502 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002503 FoldingSetNodeID ID;
2504 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002505 void *IP = 0;
2506 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2507 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002508 if (NumOps == 1)
2509 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2510 else if (NumOps == 2)
2511 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2512 else if (NumOps == 3)
2513 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2514 else
2515 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002516 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002517 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002518 if (NumOps == 1)
2519 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2520 else if (NumOps == 2)
2521 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2522 else if (NumOps == 3)
2523 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2524 else
2525 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002526 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002527 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002528 return SDOperand(N, 0);
2529}
2530
Chris Lattner70046e92006-08-15 17:46:01 +00002531SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Dan Gohman6595cb32007-06-27 16:08:04 +00002532 if (!MVT::isExtendedVT(VT))
Dan Gohman7f321562007-06-25 16:23:39 +00002533 return makeVTList(SDNode::getValueTypeList(VT), 1);
2534
2535 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2536 E = VTList.end(); I != E; ++I) {
2537 if (I->size() == 1 && (*I)[0] == VT)
2538 return makeVTList(&(*I)[0], 1);
2539 }
2540 std::vector<MVT::ValueType> V;
2541 V.push_back(VT);
2542 VTList.push_front(V);
2543 return makeVTList(&(*VTList.begin())[0], 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002544}
2545
Chris Lattner70046e92006-08-15 17:46:01 +00002546SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002547 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2548 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002549 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002550 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002551 }
2552 std::vector<MVT::ValueType> V;
2553 V.push_back(VT1);
2554 V.push_back(VT2);
2555 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002556 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002557}
Chris Lattner70046e92006-08-15 17:46:01 +00002558SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2559 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002560 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002561 E = VTList.end(); I != E; ++I) {
2562 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2563 (*I)[2] == VT3)
2564 return makeVTList(&(*I)[0], 3);
2565 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002566 std::vector<MVT::ValueType> V;
2567 V.push_back(VT1);
2568 V.push_back(VT2);
2569 V.push_back(VT3);
2570 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002571 return makeVTList(&(*VTList.begin())[0], 3);
2572}
2573
2574SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2575 switch (NumVTs) {
2576 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002577 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002578 case 2: return getVTList(VTs[0], VTs[1]);
2579 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2580 default: break;
2581 }
2582
2583 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2584 E = VTList.end(); I != E; ++I) {
2585 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2586
2587 bool NoMatch = false;
2588 for (unsigned i = 2; i != NumVTs; ++i)
2589 if (VTs[i] != (*I)[i]) {
2590 NoMatch = true;
2591 break;
2592 }
2593 if (!NoMatch)
2594 return makeVTList(&*I->begin(), NumVTs);
2595 }
2596
2597 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2598 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002599}
2600
2601
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002602/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2603/// specified operands. If the resultant node already exists in the DAG,
2604/// this does not modify the specified node, instead it returns the node that
2605/// already exists. If the resultant node does not exist in the DAG, the
2606/// input node is returned. As a degenerate case, if you specify the same
2607/// input operands as the node already has, the input node is returned.
2608SDOperand SelectionDAG::
2609UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2610 SDNode *N = InN.Val;
2611 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2612
2613 // Check to see if there is no change.
2614 if (Op == N->getOperand(0)) return InN;
2615
2616 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002617 void *InsertPos = 0;
2618 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2619 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002620
2621 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002622 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002623 RemoveNodeFromCSEMaps(N);
2624
2625 // Now we update the operands.
2626 N->OperandList[0].Val->removeUser(N);
2627 Op.Val->addUser(N);
2628 N->OperandList[0] = Op;
2629
2630 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002631 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002632 return InN;
2633}
2634
2635SDOperand SelectionDAG::
2636UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2637 SDNode *N = InN.Val;
2638 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2639
2640 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002641 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2642 return InN; // No operands changed, just return the input node.
2643
2644 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002645 void *InsertPos = 0;
2646 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2647 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002648
2649 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002650 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002651 RemoveNodeFromCSEMaps(N);
2652
2653 // Now we update the operands.
2654 if (N->OperandList[0] != Op1) {
2655 N->OperandList[0].Val->removeUser(N);
2656 Op1.Val->addUser(N);
2657 N->OperandList[0] = Op1;
2658 }
2659 if (N->OperandList[1] != Op2) {
2660 N->OperandList[1].Val->removeUser(N);
2661 Op2.Val->addUser(N);
2662 N->OperandList[1] = Op2;
2663 }
2664
2665 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002666 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002667 return InN;
2668}
2669
2670SDOperand SelectionDAG::
2671UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002672 SDOperand Ops[] = { Op1, Op2, Op3 };
2673 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002674}
2675
2676SDOperand SelectionDAG::
2677UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2678 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002679 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2680 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002681}
2682
2683SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002684UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2685 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002686 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2687 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002688}
2689
2690
2691SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002692UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002693 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002694 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002695 "Update with wrong number of operands");
2696
2697 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002698 bool AnyChange = false;
2699 for (unsigned i = 0; i != NumOps; ++i) {
2700 if (Ops[i] != N->getOperand(i)) {
2701 AnyChange = true;
2702 break;
2703 }
2704 }
2705
2706 // No operands changed, just return the input node.
2707 if (!AnyChange) return InN;
2708
2709 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002710 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002711 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002712 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002713
2714 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002715 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002716 RemoveNodeFromCSEMaps(N);
2717
2718 // Now we update the operands.
2719 for (unsigned i = 0; i != NumOps; ++i) {
2720 if (N->OperandList[i] != Ops[i]) {
2721 N->OperandList[i].Val->removeUser(N);
2722 Ops[i].Val->addUser(N);
2723 N->OperandList[i] = Ops[i];
2724 }
2725 }
2726
2727 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002728 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002729 return InN;
2730}
2731
2732
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002733/// MorphNodeTo - This frees the operands of the current node, resets the
2734/// opcode, types, and operands to the specified value. This should only be
2735/// used by the SelectionDAG class.
2736void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2737 const SDOperand *Ops, unsigned NumOps) {
2738 NodeType = Opc;
2739 ValueList = L.VTs;
2740 NumValues = L.NumVTs;
2741
2742 // Clear the operands list, updating used nodes to remove this from their
2743 // use list.
2744 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2745 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002746
2747 // If NumOps is larger than the # of operands we currently have, reallocate
2748 // the operand list.
2749 if (NumOps > NumOperands) {
2750 if (OperandsNeedDelete)
2751 delete [] OperandList;
2752 OperandList = new SDOperand[NumOps];
2753 OperandsNeedDelete = true;
2754 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002755
2756 // Assign the new operands.
2757 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002758
2759 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2760 OperandList[i] = Ops[i];
2761 SDNode *N = OperandList[i].Val;
2762 N->Uses.push_back(this);
2763 }
2764}
Chris Lattner149c58c2005-08-16 18:17:10 +00002765
2766/// SelectNodeTo - These are used for target selectors to *mutate* the
2767/// specified node to have the specified return type, Target opcode, and
2768/// operands. Note that target opcodes are stored as
2769/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002770///
2771/// Note that SelectNodeTo returns the resultant node. If there is already a
2772/// node of the specified opcode and operands, it returns that node instead of
2773/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002774SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2775 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002776 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002777 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002778 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002779 void *IP = 0;
2780 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002781 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002782
Chris Lattner7651fa42005-08-24 23:00:29 +00002783 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002784
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002785 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002786
Chris Lattner4a283e92006-08-11 18:38:11 +00002787 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002788 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002789}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002790
Evan Cheng95514ba2006-08-26 08:00:10 +00002791SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2792 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002793 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002794 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002795 SDOperand Ops[] = { Op1 };
2796
Jim Laskey583bd472006-10-27 23:46:08 +00002797 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002798 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002799 void *IP = 0;
2800 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002801 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002802
Chris Lattner149c58c2005-08-16 18:17:10 +00002803 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002804 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002805 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002806 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002807}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002808
Evan Cheng95514ba2006-08-26 08:00:10 +00002809SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2810 MVT::ValueType VT, SDOperand Op1,
2811 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002812 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002813 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002814 SDOperand Ops[] = { Op1, Op2 };
2815
Jim Laskey583bd472006-10-27 23:46:08 +00002816 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002817 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002818 void *IP = 0;
2819 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002820 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002821
Chris Lattner149c58c2005-08-16 18:17:10 +00002822 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002823
Chris Lattner63e3f142007-02-04 07:28:00 +00002824 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002825
Chris Lattnera5682852006-08-07 23:03:03 +00002826 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002827 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002828}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002829
Evan Cheng95514ba2006-08-26 08:00:10 +00002830SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2831 MVT::ValueType VT, SDOperand Op1,
2832 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002833 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002834 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002835 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002836 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002837 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002838 void *IP = 0;
2839 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002840 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002841
Chris Lattner149c58c2005-08-16 18:17:10 +00002842 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002843
Chris Lattner63e3f142007-02-04 07:28:00 +00002844 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002845
Chris Lattnera5682852006-08-07 23:03:03 +00002846 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002847 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002848}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002849
Evan Cheng95514ba2006-08-26 08:00:10 +00002850SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002851 MVT::ValueType VT, const SDOperand *Ops,
2852 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002853 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002854 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002855 FoldingSetNodeID ID;
2856 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002857 void *IP = 0;
2858 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002859 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002860
Chris Lattner6b09a292005-08-21 18:49:33 +00002861 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002862 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002863
Chris Lattnera5682852006-08-07 23:03:03 +00002864 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002865 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002866}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002867
Evan Cheng95514ba2006-08-26 08:00:10 +00002868SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2869 MVT::ValueType VT1, MVT::ValueType VT2,
2870 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002871 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002872 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002873 SDOperand Ops[] = { Op1, Op2 };
2874 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002875 void *IP = 0;
2876 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002877 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002878
Chris Lattner0fb094f2005-11-19 01:44:53 +00002879 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002880 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002881 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002882 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002883}
2884
Evan Cheng95514ba2006-08-26 08:00:10 +00002885SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2886 MVT::ValueType VT1, MVT::ValueType VT2,
2887 SDOperand Op1, SDOperand Op2,
2888 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002889 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002890 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00002891 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002892 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002893 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002894 void *IP = 0;
2895 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002896 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002897
Chris Lattner0fb094f2005-11-19 01:44:53 +00002898 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002899
Chris Lattner63e3f142007-02-04 07:28:00 +00002900 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002901 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002902 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002903}
2904
Chris Lattner0fb094f2005-11-19 01:44:53 +00002905
Evan Cheng6ae46c42006-02-09 07:15:23 +00002906/// getTargetNode - These are used for target selectors to create a new node
2907/// with specified return type(s), target opcode, and operands.
2908///
2909/// Note that getTargetNode returns the resultant node. If there is already a
2910/// node of the specified opcode and operands, it returns that node instead of
2911/// the current one.
2912SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2913 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2914}
2915SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2916 SDOperand Op1) {
2917 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2918}
2919SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2920 SDOperand Op1, SDOperand Op2) {
2921 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2922}
2923SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002924 SDOperand Op1, SDOperand Op2,
2925 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002926 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2927}
2928SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002929 const SDOperand *Ops, unsigned NumOps) {
2930 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002931}
2932SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2933 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002934 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002935 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002936}
2937SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002938 MVT::ValueType VT2, SDOperand Op1,
2939 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002940 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002941 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002942 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002943}
2944SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002945 MVT::ValueType VT2, SDOperand Op1,
2946 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002947 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002948 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002949 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002950}
Evan Cheng694481e2006-08-27 08:08:54 +00002951SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2952 MVT::ValueType VT2,
2953 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002954 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002955 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002956}
2957SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2958 MVT::ValueType VT2, MVT::ValueType VT3,
2959 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002960 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002961 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002962 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002963}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00002964SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2965 MVT::ValueType VT2, MVT::ValueType VT3,
2966 SDOperand Op1, SDOperand Op2,
2967 SDOperand Op3) {
2968 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2969 SDOperand Ops[] = { Op1, Op2, Op3 };
2970 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
2971}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002972SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002973 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002974 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002975 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2976 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002977}
2978
Evan Cheng99157a02006-08-07 22:13:29 +00002979/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002980/// This can cause recursive merging of nodes in the DAG.
2981///
Chris Lattner8b52f212005-08-26 18:36:28 +00002982/// This version assumes From/To have a single result value.
2983///
Chris Lattner1e111c72005-09-07 05:37:01 +00002984void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2985 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002986 SDNode *From = FromN.Val, *To = ToN.Val;
2987 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2988 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002989 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002990
Chris Lattner8b8749f2005-08-17 19:00:20 +00002991 while (!From->use_empty()) {
2992 // Process users until they are all gone.
2993 SDNode *U = *From->use_begin();
2994
2995 // This node is about to morph, remove its old self from the CSE maps.
2996 RemoveNodeFromCSEMaps(U);
2997
Chris Lattner65113b22005-11-08 22:07:03 +00002998 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2999 I != E; ++I)
3000 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003001 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003002 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003003 To->addUser(U);
3004 }
3005
3006 // Now that we have modified U, add it back to the CSE maps. If it already
3007 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003008 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3009 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003010 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003011 if (Deleted) Deleted->push_back(U);
3012 DeleteNodeNotInCSEMaps(U);
3013 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003014 }
3015}
3016
3017/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3018/// This can cause recursive merging of nodes in the DAG.
3019///
3020/// This version assumes From/To have matching types and numbers of result
3021/// values.
3022///
Chris Lattner1e111c72005-09-07 05:37:01 +00003023void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3024 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003025 assert(From != To && "Cannot replace uses of with self");
3026 assert(From->getNumValues() == To->getNumValues() &&
3027 "Cannot use this version of ReplaceAllUsesWith!");
3028 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003029 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003030 return;
3031 }
3032
3033 while (!From->use_empty()) {
3034 // Process users until they are all gone.
3035 SDNode *U = *From->use_begin();
3036
3037 // This node is about to morph, remove its old self from the CSE maps.
3038 RemoveNodeFromCSEMaps(U);
3039
Chris Lattner65113b22005-11-08 22:07:03 +00003040 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3041 I != E; ++I)
3042 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003043 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003044 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003045 To->addUser(U);
3046 }
3047
3048 // Now that we have modified U, add it back to the CSE maps. If it already
3049 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003050 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3051 ReplaceAllUsesWith(U, Existing, Deleted);
3052 // U is now dead.
3053 if (Deleted) Deleted->push_back(U);
3054 DeleteNodeNotInCSEMaps(U);
3055 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003056 }
3057}
3058
Chris Lattner8b52f212005-08-26 18:36:28 +00003059/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3060/// This can cause recursive merging of nodes in the DAG.
3061///
3062/// This version can replace From with any result values. To must match the
3063/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003064void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003065 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003066 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003067 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003068 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003069 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003070 return;
3071 }
3072
3073 while (!From->use_empty()) {
3074 // Process users until they are all gone.
3075 SDNode *U = *From->use_begin();
3076
3077 // This node is about to morph, remove its old self from the CSE maps.
3078 RemoveNodeFromCSEMaps(U);
3079
Chris Lattner65113b22005-11-08 22:07:03 +00003080 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3081 I != E; ++I)
3082 if (I->Val == From) {
3083 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003084 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003085 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003086 ToOp.Val->addUser(U);
3087 }
3088
3089 // Now that we have modified U, add it back to the CSE maps. If it already
3090 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003091 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3092 ReplaceAllUsesWith(U, Existing, Deleted);
3093 // U is now dead.
3094 if (Deleted) Deleted->push_back(U);
3095 DeleteNodeNotInCSEMaps(U);
3096 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003097 }
3098}
3099
Chris Lattner012f2412006-02-17 21:58:01 +00003100/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3101/// uses of other values produced by From.Val alone. The Deleted vector is
3102/// handled the same was as for ReplaceAllUsesWith.
3103void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3104 std::vector<SDNode*> &Deleted) {
3105 assert(From != To && "Cannot replace a value with itself");
3106 // Handle the simple, trivial, case efficiently.
3107 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3108 ReplaceAllUsesWith(From, To, &Deleted);
3109 return;
3110 }
3111
Chris Lattnercf5640b2007-02-04 00:14:31 +00003112 // Get all of the users of From.Val. We want these in a nice,
3113 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3114 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003115
3116 while (!Users.empty()) {
3117 // We know that this user uses some value of From. If it is the right
3118 // value, update it.
3119 SDNode *User = Users.back();
3120 Users.pop_back();
3121
3122 for (SDOperand *Op = User->OperandList,
3123 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
3124 if (*Op == From) {
3125 // Okay, we know this user needs to be updated. Remove its old self
3126 // from the CSE maps.
3127 RemoveNodeFromCSEMaps(User);
3128
3129 // Update all operands that match "From".
3130 for (; Op != E; ++Op) {
3131 if (*Op == From) {
3132 From.Val->removeUser(User);
3133 *Op = To;
3134 To.Val->addUser(User);
3135 }
3136 }
3137
3138 // Now that we have modified User, add it back to the CSE maps. If it
3139 // already exists there, recursively merge the results together.
3140 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
3141 unsigned NumDeleted = Deleted.size();
3142 ReplaceAllUsesWith(User, Existing, &Deleted);
3143
3144 // User is now dead.
3145 Deleted.push_back(User);
3146 DeleteNodeNotInCSEMaps(User);
3147
3148 // We have to be careful here, because ReplaceAllUsesWith could have
3149 // deleted a user of From, which means there may be dangling pointers
3150 // in the "Users" setvector. Scan over the deleted node pointers and
3151 // remove them from the setvector.
3152 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
3153 Users.remove(Deleted[i]);
3154 }
3155 break; // Exit the operand scanning loop.
3156 }
3157 }
3158 }
3159}
3160
Chris Lattner7b2880c2005-08-24 22:44:39 +00003161
Evan Chenge6f35d82006-08-01 08:20:41 +00003162/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3163/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003164unsigned SelectionDAG::AssignNodeIds() {
3165 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003166 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3167 SDNode *N = I;
3168 N->setNodeId(Id++);
3169 }
3170 return Id;
3171}
3172
Evan Chenge6f35d82006-08-01 08:20:41 +00003173/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003174/// based on their topological order. It returns the maximum id and a vector
3175/// of the SDNodes* in assigned order by reference.
3176unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003177 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003178 std::vector<unsigned> InDegree(DAGSize);
3179 std::vector<SDNode*> Sources;
3180
3181 // Use a two pass approach to avoid using a std::map which is slow.
3182 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003183 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3184 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003185 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003186 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003187 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003188 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003189 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003190 }
3191
Evan Cheng99157a02006-08-07 22:13:29 +00003192 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003193 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003194 SDNode *N = Sources.back();
3195 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003196 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003197 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3198 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003199 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003200 if (Degree == 0)
3201 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003202 }
3203 }
3204
Evan Chengc384d6c2006-08-02 22:00:34 +00003205 // Second pass, assign the actual topological order as node ids.
3206 Id = 0;
3207 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3208 TI != TE; ++TI)
3209 (*TI)->setNodeId(Id++);
3210
3211 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003212}
3213
3214
Evan Cheng091cba12006-07-27 06:39:06 +00003215
Jim Laskey58b968b2005-08-17 20:08:02 +00003216//===----------------------------------------------------------------------===//
3217// SDNode Class
3218//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003219
Chris Lattner917d2c92006-07-19 00:00:37 +00003220// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003221void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003222void UnarySDNode::ANCHOR() {}
3223void BinarySDNode::ANCHOR() {}
3224void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003225void HandleSDNode::ANCHOR() {}
3226void StringSDNode::ANCHOR() {}
3227void ConstantSDNode::ANCHOR() {}
3228void ConstantFPSDNode::ANCHOR() {}
3229void GlobalAddressSDNode::ANCHOR() {}
3230void FrameIndexSDNode::ANCHOR() {}
3231void JumpTableSDNode::ANCHOR() {}
3232void ConstantPoolSDNode::ANCHOR() {}
3233void BasicBlockSDNode::ANCHOR() {}
3234void SrcValueSDNode::ANCHOR() {}
3235void RegisterSDNode::ANCHOR() {}
3236void ExternalSymbolSDNode::ANCHOR() {}
3237void CondCodeSDNode::ANCHOR() {}
3238void VTSDNode::ANCHOR() {}
3239void LoadSDNode::ANCHOR() {}
3240void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003241
Chris Lattner48b85922007-02-04 02:41:42 +00003242HandleSDNode::~HandleSDNode() {
3243 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003244 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003245}
3246
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003247GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3248 MVT::ValueType VT, int o)
3249 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003250 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003251 // Thread Local
3252 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3253 // Non Thread Local
3254 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3255 getSDVTList(VT)), Offset(o) {
3256 TheGlobal = const_cast<GlobalValue*>(GA);
3257}
Chris Lattner48b85922007-02-04 02:41:42 +00003258
Jim Laskey583bd472006-10-27 23:46:08 +00003259/// Profile - Gather unique data for the node.
3260///
3261void SDNode::Profile(FoldingSetNodeID &ID) {
3262 AddNodeIDNode(ID, this);
3263}
3264
Chris Lattnera3255112005-11-08 23:30:28 +00003265/// getValueTypeList - Return a pointer to the specified value type.
3266///
3267MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3268 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3269 VTs[VT] = VT;
3270 return &VTs[VT];
3271}
Chris Lattnera5682852006-08-07 23:03:03 +00003272
Chris Lattner5c884562005-01-12 18:37:47 +00003273/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3274/// indicated value. This method ignores uses of other values defined by this
3275/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003276bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003277 assert(Value < getNumValues() && "Bad value!");
3278
3279 // If there is only one value, this is easy.
3280 if (getNumValues() == 1)
3281 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003282 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003283
Evan Cheng4ee62112006-02-05 06:29:23 +00003284 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003285
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003286 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003287
Chris Lattnerf83482d2006-08-16 20:59:32 +00003288 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003289 SDNode *User = *UI;
3290 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003291 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003292 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3293 if (User->getOperand(i) == TheValue) {
3294 if (NUses == 0)
3295 return false; // too many uses
3296 --NUses;
3297 }
3298 }
3299
3300 // Found exactly the right number of uses?
3301 return NUses == 0;
3302}
3303
3304
Evan Cheng33d55952007-08-02 05:29:38 +00003305/// hasAnyUseOfValue - Return true if there are any use of the indicated
3306/// value. This method ignores uses of other values defined by this operation.
3307bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3308 assert(Value < getNumValues() && "Bad value!");
3309
3310 if (use_size() == 0) return false;
3311
3312 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3313
3314 SmallPtrSet<SDNode*, 32> UsersHandled;
3315
3316 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3317 SDNode *User = *UI;
3318 if (User->getNumOperands() == 1 ||
3319 UsersHandled.insert(User)) // First time we've seen this?
3320 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3321 if (User->getOperand(i) == TheValue) {
3322 return true;
3323 }
3324 }
3325
3326 return false;
3327}
3328
3329
Evan Chenge6e97e62006-11-03 07:31:32 +00003330/// isOnlyUse - Return true if this node is the only use of N.
3331///
Evan Cheng4ee62112006-02-05 06:29:23 +00003332bool SDNode::isOnlyUse(SDNode *N) const {
3333 bool Seen = false;
3334 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3335 SDNode *User = *I;
3336 if (User == this)
3337 Seen = true;
3338 else
3339 return false;
3340 }
3341
3342 return Seen;
3343}
3344
Evan Chenge6e97e62006-11-03 07:31:32 +00003345/// isOperand - Return true if this node is an operand of N.
3346///
Evan Chengbfa284f2006-03-03 06:42:32 +00003347bool SDOperand::isOperand(SDNode *N) const {
3348 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3349 if (*this == N->getOperand(i))
3350 return true;
3351 return false;
3352}
3353
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003354bool SDNode::isOperand(SDNode *N) const {
3355 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3356 if (this == N->OperandList[i].Val)
3357 return true;
3358 return false;
3359}
Evan Cheng4ee62112006-02-05 06:29:23 +00003360
Evan Chengc5fc57d2006-11-03 03:05:24 +00003361static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003362 SmallPtrSet<SDNode *, 32> &Visited) {
3363 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003364 return;
3365
3366 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3367 SDNode *Op = N->getOperand(i).Val;
3368 if (Op == P) {
3369 found = true;
3370 return;
3371 }
3372 findPredecessor(Op, P, found, Visited);
3373 }
3374}
3375
Evan Chenge6e97e62006-11-03 07:31:32 +00003376/// isPredecessor - Return true if this node is a predecessor of N. This node
3377/// is either an operand of N or it can be reached by recursively traversing
3378/// up the operands.
3379/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003380bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003381 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003382 bool found = false;
3383 findPredecessor(N, this, found, Visited);
3384 return found;
3385}
3386
Evan Chengc5484282006-10-04 00:56:09 +00003387uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3388 assert(Num < NumOperands && "Invalid child # of SDNode!");
3389 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3390}
3391
Reid Spencer577cc322007-04-01 07:32:19 +00003392std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003393 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003394 default:
3395 if (getOpcode() < ISD::BUILTIN_OP_END)
3396 return "<<Unknown DAG Node>>";
3397 else {
Evan Cheng72261582005-12-20 06:22:03 +00003398 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003399 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003400 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3401 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003402
Evan Cheng72261582005-12-20 06:22:03 +00003403 TargetLowering &TLI = G->getTargetLoweringInfo();
3404 const char *Name =
3405 TLI.getTargetNodeName(getOpcode());
3406 if (Name) return Name;
3407 }
3408
3409 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003410 }
3411
Andrew Lenharth95762122005-03-31 21:24:06 +00003412 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003413 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003414 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003415 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003416 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003417 case ISD::AssertSext: return "AssertSext";
3418 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003419
3420 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003421 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003422 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003423 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003424
3425 case ISD::Constant: return "Constant";
3426 case ISD::ConstantFP: return "ConstantFP";
3427 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003428 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003429 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003430 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003431 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003432 case ISD::RETURNADDR: return "RETURNADDR";
3433 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003434 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003435 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3436 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003437 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003438 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003439 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003440 case ISD::INTRINSIC_WO_CHAIN: {
3441 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3442 return Intrinsic::getName((Intrinsic::ID)IID);
3443 }
3444 case ISD::INTRINSIC_VOID:
3445 case ISD::INTRINSIC_W_CHAIN: {
3446 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003447 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003448 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003449
Chris Lattnerb2827b02006-03-19 00:52:58 +00003450 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003451 case ISD::TargetConstant: return "TargetConstant";
3452 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003453 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003454 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003455 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003456 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003457 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003458 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003459
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003460 case ISD::CopyToReg: return "CopyToReg";
3461 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003462 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003463 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003464 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003465 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003466 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003467 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003468 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003469
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003470 // Unary operators
3471 case ISD::FABS: return "fabs";
3472 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003473 case ISD::FSQRT: return "fsqrt";
3474 case ISD::FSIN: return "fsin";
3475 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003476 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003477
3478 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003479 case ISD::ADD: return "add";
3480 case ISD::SUB: return "sub";
3481 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003482 case ISD::MULHU: return "mulhu";
3483 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003484 case ISD::SDIV: return "sdiv";
3485 case ISD::UDIV: return "udiv";
3486 case ISD::SREM: return "srem";
3487 case ISD::UREM: return "urem";
3488 case ISD::AND: return "and";
3489 case ISD::OR: return "or";
3490 case ISD::XOR: return "xor";
3491 case ISD::SHL: return "shl";
3492 case ISD::SRA: return "sra";
3493 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003494 case ISD::ROTL: return "rotl";
3495 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003496 case ISD::FADD: return "fadd";
3497 case ISD::FSUB: return "fsub";
3498 case ISD::FMUL: return "fmul";
3499 case ISD::FDIV: return "fdiv";
3500 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003501 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003502
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003503 case ISD::SETCC: return "setcc";
3504 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003505 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003506 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003507 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003508 case ISD::CONCAT_VECTORS: return "concat_vectors";
3509 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003510 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003511 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003512 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003513 case ISD::ADDC: return "addc";
3514 case ISD::ADDE: return "adde";
3515 case ISD::SUBC: return "subc";
3516 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003517 case ISD::SHL_PARTS: return "shl_parts";
3518 case ISD::SRA_PARTS: return "sra_parts";
3519 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003520
3521 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3522 case ISD::INSERT_SUBREG: return "insert_subreg";
3523
Chris Lattner7f644642005-04-28 21:44:03 +00003524 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003525 case ISD::SIGN_EXTEND: return "sign_extend";
3526 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003527 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003528 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003529 case ISD::TRUNCATE: return "truncate";
3530 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00003531 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003532 case ISD::FP_EXTEND: return "fp_extend";
3533
3534 case ISD::SINT_TO_FP: return "sint_to_fp";
3535 case ISD::UINT_TO_FP: return "uint_to_fp";
3536 case ISD::FP_TO_SINT: return "fp_to_sint";
3537 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003538 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003539
3540 // Control flow instructions
3541 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003542 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003543 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003544 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003545 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003546 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003547 case ISD::CALLSEQ_START: return "callseq_start";
3548 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003549
3550 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003551 case ISD::LOAD: return "load";
3552 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003553 case ISD::VAARG: return "vaarg";
3554 case ISD::VACOPY: return "vacopy";
3555 case ISD::VAEND: return "vaend";
3556 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003557 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003558 case ISD::EXTRACT_ELEMENT: return "extract_element";
3559 case ISD::BUILD_PAIR: return "build_pair";
3560 case ISD::STACKSAVE: return "stacksave";
3561 case ISD::STACKRESTORE: return "stackrestore";
3562
3563 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003564 case ISD::MEMSET: return "memset";
3565 case ISD::MEMCPY: return "memcpy";
3566 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003567
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003568 // Bit manipulation
3569 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003570 case ISD::CTPOP: return "ctpop";
3571 case ISD::CTTZ: return "cttz";
3572 case ISD::CTLZ: return "ctlz";
3573
Chris Lattner36ce6912005-11-29 06:21:05 +00003574 // Debug info
3575 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003576 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003577
Duncan Sands36397f52007-07-27 12:58:54 +00003578 // Trampolines
3579 case ISD::ADJUST_TRAMP: return "adjust_tramp";
3580 case ISD::TRAMPOLINE: return "trampoline";
3581
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003582 case ISD::CONDCODE:
3583 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003584 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003585 case ISD::SETOEQ: return "setoeq";
3586 case ISD::SETOGT: return "setogt";
3587 case ISD::SETOGE: return "setoge";
3588 case ISD::SETOLT: return "setolt";
3589 case ISD::SETOLE: return "setole";
3590 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003591
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003592 case ISD::SETO: return "seto";
3593 case ISD::SETUO: return "setuo";
3594 case ISD::SETUEQ: return "setue";
3595 case ISD::SETUGT: return "setugt";
3596 case ISD::SETUGE: return "setuge";
3597 case ISD::SETULT: return "setult";
3598 case ISD::SETULE: return "setule";
3599 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003600
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003601 case ISD::SETEQ: return "seteq";
3602 case ISD::SETGT: return "setgt";
3603 case ISD::SETGE: return "setge";
3604 case ISD::SETLT: return "setlt";
3605 case ISD::SETLE: return "setle";
3606 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003607 }
3608 }
3609}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003610
Evan Cheng144d8f02006-11-09 17:55:04 +00003611const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003612 switch (AM) {
3613 default:
3614 return "";
3615 case ISD::PRE_INC:
3616 return "<pre-inc>";
3617 case ISD::PRE_DEC:
3618 return "<pre-dec>";
3619 case ISD::POST_INC:
3620 return "<post-inc>";
3621 case ISD::POST_DEC:
3622 return "<post-dec>";
3623 }
3624}
3625
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003626void SDNode::dump() const { dump(0); }
3627void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003628 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003629
3630 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003631 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003632 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003633 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003634 else
Bill Wendling832171c2006-12-07 20:04:42 +00003635 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003636 }
Bill Wendling832171c2006-12-07 20:04:42 +00003637 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003638
Bill Wendling832171c2006-12-07 20:04:42 +00003639 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003640 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003641 if (i) cerr << ", ";
3642 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003643 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003644 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003645 }
3646
3647 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003648 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003649 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003650 cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003651 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003652 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003653 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003654 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003655 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003656 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003657 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003658 else
Bill Wendling832171c2006-12-07 20:04:42 +00003659 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003660 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003661 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003662 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003663 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003664 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003665 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003666 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003667 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003668 else
Bill Wendling832171c2006-12-07 20:04:42 +00003669 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003670 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003671 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003672 else
Bill Wendling832171c2006-12-07 20:04:42 +00003673 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003674 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003675 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003676 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3677 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003678 cerr << LBB->getName() << " ";
3679 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003680 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003681 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003682 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003683 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003684 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003685 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003686 } else if (const ExternalSymbolSDNode *ES =
3687 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003688 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003689 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3690 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003691 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003692 else
Bill Wendling832171c2006-12-07 20:04:42 +00003693 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003694 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003695 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003696 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3697 bool doExt = true;
3698 switch (LD->getExtensionType()) {
3699 default: doExt = false; break;
3700 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003701 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003702 break;
3703 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003704 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003705 break;
3706 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003707 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003708 break;
3709 }
3710 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003711 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003712
Evan Cheng144d8f02006-11-09 17:55:04 +00003713 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003714 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003715 cerr << " " << AM;
Evan Cheng2caccca2006-10-17 21:14:32 +00003716 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3717 if (ST->isTruncatingStore())
Bill Wendling832171c2006-12-07 20:04:42 +00003718 cerr << " <trunc "
3719 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00003720
Evan Cheng144d8f02006-11-09 17:55:04 +00003721 const char *AM = getIndexedModeName(ST->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003722 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003723 cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003724 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003725}
3726
Chris Lattnerde202b32005-11-09 23:47:37 +00003727static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003728 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3729 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003730 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003731 else
Bill Wendling832171c2006-12-07 20:04:42 +00003732 cerr << "\n" << std::string(indent+2, ' ')
3733 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003734
Chris Lattnerea946cd2005-01-09 20:38:33 +00003735
Bill Wendling832171c2006-12-07 20:04:42 +00003736 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003737 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003738}
3739
Chris Lattnerc3aae252005-01-07 07:46:32 +00003740void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003741 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003742 std::vector<const SDNode*> Nodes;
3743 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3744 I != E; ++I)
3745 Nodes.push_back(I);
3746
Chris Lattner49d24712005-01-09 20:26:36 +00003747 std::sort(Nodes.begin(), Nodes.end());
3748
3749 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003750 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003751 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003752 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003753
Jim Laskey26f7fa72006-10-17 19:33:52 +00003754 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003755
Bill Wendling832171c2006-12-07 20:04:42 +00003756 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003757}
3758
Evan Chengd6594ae2006-09-12 21:00:35 +00003759const Type *ConstantPoolSDNode::getType() const {
3760 if (isMachineConstantPoolEntry())
3761 return Val.MachineCPVal->getType();
3762 return Val.ConstVal->getType();
3763}