blob: ed1777e34ad29bac7f8fef8c55138bccf6c9367c [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) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000112 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
113 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000114 return false;
115 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000116 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
117 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000118 (uint32_t)-1)
119 return false;
120 }
Evan Chenga8df1662006-03-27 06:58:47 +0000121 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000122 return false;
123
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
125 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
129 return false;
130 return true;
131}
132
133
Evan Cheng4a147842006-03-26 09:50:58 +0000134/// isBuildVectorAllZeros - Return true if the specified node is a
135/// BUILD_VECTOR where all of the elements are 0 or undef.
136bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
140
Evan Cheng4a147842006-03-26 09:50:58 +0000141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000142
143 unsigned i = 0, e = N->getNumOperands();
144
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
147 ++i;
148
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
151
152 // Do not accept build_vectors that aren't all constants or which have non-~0
153 // elements.
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
157 return false;
158 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000160 return false;
161 } else
162 return false;
163
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
165 // undefs.
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
169 return false;
170 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000171}
172
Chris Lattnerc3aae252005-01-07 07:46:32 +0000173/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
174/// when given the operation for (X op Y).
175ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
176 // To perform this operation, we just need to swap the L and G bits of the
177 // operation.
178 unsigned OldL = (Operation >> 2) & 1;
179 unsigned OldG = (Operation >> 1) & 1;
180 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
181 (OldL << 1) | // New G bit
182 (OldG << 2)); // New L bit.
183}
184
185/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
186/// 'op' is a valid SetCC operation.
187ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
188 unsigned Operation = Op;
189 if (isInteger)
190 Operation ^= 7; // Flip L, G, E bits, but not U.
191 else
192 Operation ^= 15; // Flip all of the condition bits.
193 if (Operation > ISD::SETTRUE2)
194 Operation &= ~8; // Don't let N and U bits get set.
195 return ISD::CondCode(Operation);
196}
197
198
199/// isSignedOp - For an integer comparison, return 1 if the comparison is a
200/// signed operation and 2 if the result is an unsigned comparison. Return zero
201/// if the operation does not depend on the sign of the input (setne and seteq).
202static int isSignedOp(ISD::CondCode Opcode) {
203 switch (Opcode) {
204 default: assert(0 && "Illegal integer setcc operation!");
205 case ISD::SETEQ:
206 case ISD::SETNE: return 0;
207 case ISD::SETLT:
208 case ISD::SETLE:
209 case ISD::SETGT:
210 case ISD::SETGE: return 1;
211 case ISD::SETULT:
212 case ISD::SETULE:
213 case ISD::SETUGT:
214 case ISD::SETUGE: return 2;
215 }
216}
217
218/// getSetCCOrOperation - Return the result of a logical OR between different
219/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
220/// returns SETCC_INVALID if it is not possible to represent the resultant
221/// comparison.
222ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
223 bool isInteger) {
224 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
225 // Cannot fold a signed integer setcc with an unsigned integer setcc.
226 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000227
Chris Lattnerc3aae252005-01-07 07:46:32 +0000228 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000229
Chris Lattnerc3aae252005-01-07 07:46:32 +0000230 // If the N and U bits get set then the resultant comparison DOES suddenly
231 // care about orderedness, and is true when ordered.
232 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000233 Op &= ~16; // Clear the U bit if the N bit is set.
234
235 // Canonicalize illegal integer setcc's.
236 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
237 Op = ISD::SETNE;
238
Chris Lattnerc3aae252005-01-07 07:46:32 +0000239 return ISD::CondCode(Op);
240}
241
242/// getSetCCAndOperation - Return the result of a logical AND between different
243/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
244/// function returns zero if it is not possible to represent the resultant
245/// comparison.
246ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
247 bool isInteger) {
248 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
249 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000250 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000251
252 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000253 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
254
255 // Canonicalize illegal integer setcc's.
256 if (isInteger) {
257 switch (Result) {
258 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000259 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
260 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
261 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
262 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000263 }
264 }
265
266 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000267}
268
Chris Lattnerb48da392005-01-23 04:39:44 +0000269const TargetMachine &SelectionDAG::getTarget() const {
270 return TLI.getTargetMachine();
271}
272
Jim Laskey58b968b2005-08-17 20:08:02 +0000273//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000274// SDNode Profile Support
275//===----------------------------------------------------------------------===//
276
Jim Laskeydef69b92006-10-27 23:52:51 +0000277/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
278///
Jim Laskey583bd472006-10-27 23:46:08 +0000279static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
280 ID.AddInteger(OpC);
281}
282
283/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
284/// solely with their pointer.
285void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
286 ID.AddPointer(VTList.VTs);
287}
288
Jim Laskeydef69b92006-10-27 23:52:51 +0000289/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
290///
Jim Laskey583bd472006-10-27 23:46:08 +0000291static void AddNodeIDOperands(FoldingSetNodeID &ID,
292 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000293 for (; NumOps; --NumOps, ++Ops) {
294 ID.AddPointer(Ops->Val);
295 ID.AddInteger(Ops->ResNo);
296 }
Jim Laskey583bd472006-10-27 23:46:08 +0000297}
298
Jim Laskey583bd472006-10-27 23:46:08 +0000299static void AddNodeIDNode(FoldingSetNodeID &ID,
300 unsigned short OpC, SDVTList VTList,
301 const SDOperand *OpList, unsigned N) {
302 AddNodeIDOpcode(ID, OpC);
303 AddNodeIDValueTypes(ID, VTList);
304 AddNodeIDOperands(ID, OpList, N);
305}
306
Jim Laskeydef69b92006-10-27 23:52:51 +0000307/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
308/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000309static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
310 AddNodeIDOpcode(ID, N->getOpcode());
311 // Add the return value info.
312 AddNodeIDValueTypes(ID, N->getVTList());
313 // Add the operand info.
314 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
315
316 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000317 switch (N->getOpcode()) {
318 default: break; // Normal nodes don't need extra info.
319 case ISD::TargetConstant:
320 case ISD::Constant:
321 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
322 break;
323 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000324 case ISD::ConstantFP: {
325 APFloat V = cast<ConstantFPSDNode>(N)->getValueAPF();
326 if (&V.getSemantics() == &APFloat::IEEEdouble)
327 ID.AddDouble(V.convertToDouble());
328 else if (&V.getSemantics() == &APFloat::IEEEsingle)
329 ID.AddDouble((double)V.convertToFloat());
330 else
331 assert(0);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000332 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000333 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000334 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000335 case ISD::GlobalAddress:
336 case ISD::TargetGlobalTLSAddress:
337 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000338 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
339 ID.AddPointer(GA->getGlobal());
340 ID.AddInteger(GA->getOffset());
341 break;
342 }
343 case ISD::BasicBlock:
344 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
345 break;
346 case ISD::Register:
347 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
348 break;
349 case ISD::SRCVALUE: {
350 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
351 ID.AddPointer(SV->getValue());
352 ID.AddInteger(SV->getOffset());
353 break;
354 }
355 case ISD::FrameIndex:
356 case ISD::TargetFrameIndex:
357 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
358 break;
359 case ISD::JumpTable:
360 case ISD::TargetJumpTable:
361 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
362 break;
363 case ISD::ConstantPool:
364 case ISD::TargetConstantPool: {
365 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
366 ID.AddInteger(CP->getAlignment());
367 ID.AddInteger(CP->getOffset());
368 if (CP->isMachineConstantPoolEntry())
369 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
370 else
371 ID.AddPointer(CP->getConstVal());
372 break;
373 }
374 case ISD::LOAD: {
375 LoadSDNode *LD = cast<LoadSDNode>(N);
376 ID.AddInteger(LD->getAddressingMode());
377 ID.AddInteger(LD->getExtensionType());
378 ID.AddInteger(LD->getLoadedVT());
379 ID.AddPointer(LD->getSrcValue());
380 ID.AddInteger(LD->getSrcValueOffset());
381 ID.AddInteger(LD->getAlignment());
382 ID.AddInteger(LD->isVolatile());
383 break;
384 }
385 case ISD::STORE: {
386 StoreSDNode *ST = cast<StoreSDNode>(N);
387 ID.AddInteger(ST->getAddressingMode());
388 ID.AddInteger(ST->isTruncatingStore());
389 ID.AddInteger(ST->getStoredVT());
390 ID.AddPointer(ST->getSrcValue());
391 ID.AddInteger(ST->getSrcValueOffset());
392 ID.AddInteger(ST->getAlignment());
393 ID.AddInteger(ST->isVolatile());
394 break;
395 }
Jim Laskey583bd472006-10-27 23:46:08 +0000396 }
397}
398
399//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000400// SelectionDAG Class
401//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000402
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000403/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000404/// SelectionDAG.
405void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000406 // Create a dummy node (which is not added to allnodes), that adds a reference
407 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000408 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000409
Chris Lattner190a4182006-08-04 17:45:20 +0000410 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000411
Chris Lattner190a4182006-08-04 17:45:20 +0000412 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000413 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000414 if (I->use_empty())
415 DeadNodes.push_back(I);
416
417 // Process the worklist, deleting the nodes and adding their uses to the
418 // worklist.
419 while (!DeadNodes.empty()) {
420 SDNode *N = DeadNodes.back();
421 DeadNodes.pop_back();
422
423 // Take the node out of the appropriate CSE map.
424 RemoveNodeFromCSEMaps(N);
425
426 // Next, brutally remove the operand list. This is safe to do, as there are
427 // no cycles in the graph.
428 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
429 SDNode *Operand = I->Val;
430 Operand->removeUser(N);
431
432 // Now that we removed this operand, see if there are no uses of it left.
433 if (Operand->use_empty())
434 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000435 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000436 if (N->OperandsNeedDelete)
437 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000438 N->OperandList = 0;
439 N->NumOperands = 0;
440
441 // Finally, remove N itself.
442 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000443 }
444
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000445 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000446 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000447}
448
Evan Cheng130a6472006-10-12 20:34:05 +0000449void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
450 SmallVector<SDNode*, 16> DeadNodes;
451 DeadNodes.push_back(N);
452
453 // Process the worklist, deleting the nodes and adding their uses to the
454 // worklist.
455 while (!DeadNodes.empty()) {
456 SDNode *N = DeadNodes.back();
457 DeadNodes.pop_back();
458
459 // Take the node out of the appropriate CSE map.
460 RemoveNodeFromCSEMaps(N);
461
462 // Next, brutally remove the operand list. This is safe to do, as there are
463 // no cycles in the graph.
464 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
465 SDNode *Operand = I->Val;
466 Operand->removeUser(N);
467
468 // Now that we removed this operand, see if there are no uses of it left.
469 if (Operand->use_empty())
470 DeadNodes.push_back(Operand);
471 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000472 if (N->OperandsNeedDelete)
473 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000474 N->OperandList = 0;
475 N->NumOperands = 0;
476
477 // Finally, remove N itself.
478 Deleted.push_back(N);
479 AllNodes.erase(N);
480 }
481}
482
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000483void SelectionDAG::DeleteNode(SDNode *N) {
484 assert(N->use_empty() && "Cannot delete a node that is not dead!");
485
486 // First take this out of the appropriate CSE map.
487 RemoveNodeFromCSEMaps(N);
488
Chris Lattner1e111c72005-09-07 05:37:01 +0000489 // Finally, remove uses due to operands of this node, remove from the
490 // AllNodes list, and delete the node.
491 DeleteNodeNotInCSEMaps(N);
492}
493
494void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
495
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000496 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000497 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000498
499 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000500 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
501 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000502 if (N->OperandsNeedDelete)
503 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000504 N->OperandList = 0;
505 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000506
507 delete N;
508}
509
Chris Lattner149c58c2005-08-16 18:17:10 +0000510/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
511/// correspond to it. This is useful when we're about to delete or repurpose
512/// the node. We don't want future request for structurally identical nodes
513/// to return N anymore.
514void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000515 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000516 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000517 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000518 case ISD::STRING:
519 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
520 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000521 case ISD::CONDCODE:
522 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
523 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000524 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000525 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
526 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000527 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000528 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000529 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000530 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000531 Erased =
532 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000533 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000534 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000535 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000536 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
537 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000538 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000539 // Remove it from the CSE Map.
540 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000541 break;
542 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000543#ifndef NDEBUG
544 // Verify that the node was actually in one of the CSE maps, unless it has a
545 // flag result (which cannot be CSE'd) or is one of the special cases that are
546 // not subject to CSE.
547 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000548 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000549 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000550 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000551 assert(0 && "Node is not in map!");
552 }
553#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000554}
555
Chris Lattner8b8749f2005-08-17 19:00:20 +0000556/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
557/// has been taken out and modified in some way. If the specified node already
558/// exists in the CSE maps, do not modify the maps, but return the existing node
559/// instead. If it doesn't exist, add it and return null.
560///
561SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
562 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000563 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000564 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000565
Chris Lattner9f8cc692005-12-19 22:21:21 +0000566 // Check that remaining values produced are not flags.
567 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
568 if (N->getValueType(i) == MVT::Flag)
569 return 0; // Never CSE anything that produces a flag.
570
Chris Lattnera5682852006-08-07 23:03:03 +0000571 SDNode *New = CSEMap.GetOrInsertNode(N);
572 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000573 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000574}
575
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000576/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
577/// were replaced with those specified. If this node is never memoized,
578/// return null, otherwise return a pointer to the slot it would take. If a
579/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000580SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
581 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000582 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000583 return 0; // Never add these nodes.
584
585 // Check that remaining values produced are not flags.
586 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
587 if (N->getValueType(i) == MVT::Flag)
588 return 0; // Never CSE anything that produces a flag.
589
Chris Lattner63e3f142007-02-04 07:28:00 +0000590 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000591 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000592 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000593 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000594}
595
596/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
597/// were replaced with those specified. If this node is never memoized,
598/// return null, otherwise return a pointer to the slot it would take. If a
599/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000600SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
601 SDOperand Op1, SDOperand Op2,
602 void *&InsertPos) {
603 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
604 return 0; // Never add these nodes.
605
606 // Check that remaining values produced are not flags.
607 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
608 if (N->getValueType(i) == MVT::Flag)
609 return 0; // Never CSE anything that produces a flag.
610
Chris Lattner63e3f142007-02-04 07:28:00 +0000611 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000612 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000613 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000614 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
615}
616
617
618/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
619/// were replaced with those specified. If this node is never memoized,
620/// return null, otherwise return a pointer to the slot it would take. If a
621/// node already exists with these operands, the slot will be non-null.
622SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000623 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000624 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000625 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000626 return 0; // Never add these nodes.
627
628 // Check that remaining values produced are not flags.
629 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
630 if (N->getValueType(i) == MVT::Flag)
631 return 0; // Never CSE anything that produces a flag.
632
Jim Laskey583bd472006-10-27 23:46:08 +0000633 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000634 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000635
Evan Cheng9629aba2006-10-11 01:47:58 +0000636 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
637 ID.AddInteger(LD->getAddressingMode());
638 ID.AddInteger(LD->getExtensionType());
Evan Cheng2e49f092006-10-11 07:10:22 +0000639 ID.AddInteger(LD->getLoadedVT());
Evan Cheng9629aba2006-10-11 01:47:58 +0000640 ID.AddPointer(LD->getSrcValue());
641 ID.AddInteger(LD->getSrcValueOffset());
642 ID.AddInteger(LD->getAlignment());
643 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000644 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
645 ID.AddInteger(ST->getAddressingMode());
646 ID.AddInteger(ST->isTruncatingStore());
647 ID.AddInteger(ST->getStoredVT());
648 ID.AddPointer(ST->getSrcValue());
649 ID.AddInteger(ST->getSrcValueOffset());
650 ID.AddInteger(ST->getAlignment());
651 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000652 }
Jim Laskey583bd472006-10-27 23:46:08 +0000653
Chris Lattnera5682852006-08-07 23:03:03 +0000654 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000655}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000656
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000657
Chris Lattner78ec3112003-08-11 14:57:33 +0000658SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000659 while (!AllNodes.empty()) {
660 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000661 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000662 if (N->OperandsNeedDelete)
663 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000664 N->OperandList = 0;
665 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000666 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000667 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000668}
669
Chris Lattner0f2287b2005-04-13 02:38:18 +0000670SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000671 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000672 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000673 return getNode(ISD::AND, Op.getValueType(), Op,
674 getConstant(Imm, Op.getValueType()));
675}
676
Chris Lattner36ce6912005-11-29 06:21:05 +0000677SDOperand SelectionDAG::getString(const std::string &Val) {
678 StringSDNode *&N = StringNodes[Val];
679 if (!N) {
680 N = new StringSDNode(Val);
681 AllNodes.push_back(N);
682 }
683 return SDOperand(N, 0);
684}
685
Chris Lattner61b09412006-08-11 21:01:22 +0000686SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000687 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000688 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000689
Chris Lattner61b09412006-08-11 21:01:22 +0000690 // Mask out any bits that are not valid for this constant.
691 Val &= MVT::getIntVTBitMask(VT);
692
693 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000694 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000695 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000696 ID.AddInteger(Val);
697 void *IP = 0;
698 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
699 return SDOperand(E, 0);
700 SDNode *N = new ConstantSDNode(isT, Val, VT);
701 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000702 AllNodes.push_back(N);
703 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000704}
705
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000706SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000707 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000708 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000709
Dan Gohman7f321562007-06-25 16:23:39 +0000710 MVT::ValueType EltVT =
711 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000712 bool isDouble = (EltVT == MVT::f64);
713 double Val = isDouble ? V.convertToDouble() : (double)V.convertToFloat();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000714
Chris Lattnerd8658612005-02-17 20:17:32 +0000715 // Do the map lookup using the actual bit pattern for the floating point
716 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
717 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000718 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000719 // ?? Should we store float/double/longdouble separately in ID?
Jim Laskey583bd472006-10-27 23:46:08 +0000720 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000721 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Jim Laskey583bd472006-10-27 23:46:08 +0000722 ID.AddDouble(Val);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000723 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000724 SDNode *N = NULL;
725 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
726 if (!MVT::isVector(VT))
727 return SDOperand(N, 0);
728 if (!N) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000729 N = new ConstantFPSDNode(isTarget,
730 isDouble ? APFloat(Val) : APFloat((float)Val), EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000731 CSEMap.InsertNode(N, IP);
732 AllNodes.push_back(N);
733 }
734
Dan Gohman7f321562007-06-25 16:23:39 +0000735 SDOperand Result(N, 0);
736 if (MVT::isVector(VT)) {
737 SmallVector<SDOperand, 8> Ops;
738 Ops.assign(MVT::getVectorNumElements(VT), Result);
739 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
740 }
741 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000742}
743
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000744SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
745 bool isTarget) {
746 MVT::ValueType EltVT =
747 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
748 if (EltVT==MVT::f32)
749 return getConstantFP(APFloat((float)Val), VT, isTarget);
750 else
751 return getConstantFP(APFloat(Val), VT, isTarget);
752}
753
Chris Lattnerc3aae252005-01-07 07:46:32 +0000754SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000755 MVT::ValueType VT, int Offset,
756 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000757 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
758 unsigned Opc;
759 if (GVar && GVar->isThreadLocal())
760 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
761 else
762 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000763 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000764 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000765 ID.AddPointer(GV);
766 ID.AddInteger(Offset);
767 void *IP = 0;
768 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
769 return SDOperand(E, 0);
770 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
771 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000772 AllNodes.push_back(N);
773 return SDOperand(N, 0);
774}
775
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000776SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
777 bool isTarget) {
778 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000779 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000780 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000781 ID.AddInteger(FI);
782 void *IP = 0;
783 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
784 return SDOperand(E, 0);
785 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
786 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000787 AllNodes.push_back(N);
788 return SDOperand(N, 0);
789}
790
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000791SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
792 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000793 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000794 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000795 ID.AddInteger(JTI);
796 void *IP = 0;
797 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
798 return SDOperand(E, 0);
799 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
800 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000801 AllNodes.push_back(N);
802 return SDOperand(N, 0);
803}
804
Evan Chengb8973bd2006-01-31 22:23:14 +0000805SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000806 unsigned Alignment, int Offset,
807 bool isTarget) {
808 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000809 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000810 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000811 ID.AddInteger(Alignment);
812 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000813 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000814 void *IP = 0;
815 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
816 return SDOperand(E, 0);
817 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
818 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000819 AllNodes.push_back(N);
820 return SDOperand(N, 0);
821}
822
Chris Lattnerc3aae252005-01-07 07:46:32 +0000823
Evan Chengd6594ae2006-09-12 21:00:35 +0000824SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
825 MVT::ValueType VT,
826 unsigned Alignment, int Offset,
827 bool isTarget) {
828 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000829 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000830 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000831 ID.AddInteger(Alignment);
832 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000833 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000834 void *IP = 0;
835 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
836 return SDOperand(E, 0);
837 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
838 CSEMap.InsertNode(N, IP);
839 AllNodes.push_back(N);
840 return SDOperand(N, 0);
841}
842
843
Chris Lattnerc3aae252005-01-07 07:46:32 +0000844SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000845 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000846 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000847 ID.AddPointer(MBB);
848 void *IP = 0;
849 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
850 return SDOperand(E, 0);
851 SDNode *N = new BasicBlockSDNode(MBB);
852 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000853 AllNodes.push_back(N);
854 return SDOperand(N, 0);
855}
856
Chris Lattner15e4b012005-07-10 00:07:11 +0000857SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
858 if ((unsigned)VT >= ValueTypeNodes.size())
859 ValueTypeNodes.resize(VT+1);
860 if (ValueTypeNodes[VT] == 0) {
861 ValueTypeNodes[VT] = new VTSDNode(VT);
862 AllNodes.push_back(ValueTypeNodes[VT]);
863 }
864
865 return SDOperand(ValueTypeNodes[VT], 0);
866}
867
Chris Lattnerc3aae252005-01-07 07:46:32 +0000868SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
869 SDNode *&N = ExternalSymbols[Sym];
870 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000871 N = new ExternalSymbolSDNode(false, Sym, VT);
872 AllNodes.push_back(N);
873 return SDOperand(N, 0);
874}
875
Chris Lattner809ec112006-01-28 10:09:25 +0000876SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
877 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000878 SDNode *&N = TargetExternalSymbols[Sym];
879 if (N) return SDOperand(N, 0);
880 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000881 AllNodes.push_back(N);
882 return SDOperand(N, 0);
883}
884
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000885SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
886 if ((unsigned)Cond >= CondCodeNodes.size())
887 CondCodeNodes.resize(Cond+1);
888
Chris Lattner079a27a2005-08-09 20:40:02 +0000889 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000890 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000891 AllNodes.push_back(CondCodeNodes[Cond]);
892 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000893 return SDOperand(CondCodeNodes[Cond], 0);
894}
895
Chris Lattner0fdd7682005-08-30 22:38:38 +0000896SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000897 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000898 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000899 ID.AddInteger(RegNo);
900 void *IP = 0;
901 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
902 return SDOperand(E, 0);
903 SDNode *N = new RegisterSDNode(RegNo, VT);
904 CSEMap.InsertNode(N, IP);
905 AllNodes.push_back(N);
906 return SDOperand(N, 0);
907}
908
909SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
910 assert((!V || isa<PointerType>(V->getType())) &&
911 "SrcValue is not a pointer?");
912
Jim Laskey583bd472006-10-27 23:46:08 +0000913 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000914 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000915 ID.AddPointer(V);
916 ID.AddInteger(Offset);
917 void *IP = 0;
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new SrcValueSDNode(V, Offset);
921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000924}
925
Chris Lattner51dabfb2006-10-14 00:41:01 +0000926SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
927 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928 // These setcc operations always fold.
929 switch (Cond) {
930 default: break;
931 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000932 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000934 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000935
936 case ISD::SETOEQ:
937 case ISD::SETOGT:
938 case ISD::SETOGE:
939 case ISD::SETOLT:
940 case ISD::SETOLE:
941 case ISD::SETONE:
942 case ISD::SETO:
943 case ISD::SETUO:
944 case ISD::SETUEQ:
945 case ISD::SETUNE:
946 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
947 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000948 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000949
Chris Lattner67255a12005-04-07 18:14:58 +0000950 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
951 uint64_t C2 = N2C->getValue();
952 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
953 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000954
Chris Lattnerc3aae252005-01-07 07:46:32 +0000955 // Sign extend the operands if required
956 if (ISD::isSignedIntSetCC(Cond)) {
957 C1 = N1C->getSignExtended();
958 C2 = N2C->getSignExtended();
959 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000960
Chris Lattnerc3aae252005-01-07 07:46:32 +0000961 switch (Cond) {
962 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000963 case ISD::SETEQ: return getConstant(C1 == C2, VT);
964 case ISD::SETNE: return getConstant(C1 != C2, VT);
965 case ISD::SETULT: return getConstant(C1 < C2, VT);
966 case ISD::SETUGT: return getConstant(C1 > C2, VT);
967 case ISD::SETULE: return getConstant(C1 <= C2, VT);
968 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
969 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
970 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
971 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
972 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000973 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000974 }
Chris Lattner67255a12005-04-07 18:14:58 +0000975 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000976 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
977 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000978
979 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +0000980 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000981 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +0000982 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
983 return getNode(ISD::UNDEF, VT);
984 // fall through
985 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
986 case ISD::SETNE: if (R==APFloat::cmpUnordered)
987 return getNode(ISD::UNDEF, VT);
988 // fall through
989 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000990 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +0000991 case ISD::SETLT: if (R==APFloat::cmpUnordered)
992 return getNode(ISD::UNDEF, VT);
993 // fall through
994 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
995 case ISD::SETGT: if (R==APFloat::cmpUnordered)
996 return getNode(ISD::UNDEF, VT);
997 // fall through
998 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
999 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1000 return getNode(ISD::UNDEF, VT);
1001 // fall through
1002 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001003 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001004 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1005 return getNode(ISD::UNDEF, VT);
1006 // fall through
1007 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001008 R==APFloat::cmpEqual, VT);
1009 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1010 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1011 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1012 R==APFloat::cmpEqual, VT);
1013 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1014 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1015 R==APFloat::cmpLessThan, VT);
1016 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1017 R==APFloat::cmpUnordered, VT);
1018 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1019 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001020 }
1021 } else {
1022 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001023 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001024 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001025
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001026 // Could not fold it.
1027 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001028}
1029
Dan Gohmanea859be2007-06-22 14:59:07 +00001030/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1031/// this predicate to simplify operations downstream. Mask is known to be zero
1032/// for bits that V cannot have.
1033bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1034 unsigned Depth) const {
1035 // The masks are not wide enough to represent this type! Should use APInt.
1036 if (Op.getValueType() == MVT::i128)
1037 return false;
1038
1039 uint64_t KnownZero, KnownOne;
1040 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1041 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1042 return (KnownZero & Mask) == Mask;
1043}
1044
1045/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1046/// known to be either zero or one and return them in the KnownZero/KnownOne
1047/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1048/// processing.
1049void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1050 uint64_t &KnownZero, uint64_t &KnownOne,
1051 unsigned Depth) const {
1052 KnownZero = KnownOne = 0; // Don't know anything.
1053 if (Depth == 6 || Mask == 0)
1054 return; // Limit search depth.
1055
1056 // The masks are not wide enough to represent this type! Should use APInt.
1057 if (Op.getValueType() == MVT::i128)
1058 return;
1059
1060 uint64_t KnownZero2, KnownOne2;
1061
1062 switch (Op.getOpcode()) {
1063 case ISD::Constant:
1064 // We know all of the bits for a constant!
1065 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1066 KnownZero = ~KnownOne & Mask;
1067 return;
1068 case ISD::AND:
1069 // If either the LHS or the RHS are Zero, the result is zero.
1070 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1071 Mask &= ~KnownZero;
1072 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1073 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1074 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1075
1076 // Output known-1 bits are only known if set in both the LHS & RHS.
1077 KnownOne &= KnownOne2;
1078 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1079 KnownZero |= KnownZero2;
1080 return;
1081 case ISD::OR:
1082 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1083 Mask &= ~KnownOne;
1084 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1085 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1086 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1087
1088 // Output known-0 bits are only known if clear in both the LHS & RHS.
1089 KnownZero &= KnownZero2;
1090 // Output known-1 are known to be set if set in either the LHS | RHS.
1091 KnownOne |= KnownOne2;
1092 return;
1093 case ISD::XOR: {
1094 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1095 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1096 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1097 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1098
1099 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1100 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1101 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1102 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1103 KnownZero = KnownZeroOut;
1104 return;
1105 }
1106 case ISD::SELECT:
1107 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1108 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1109 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1110 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1111
1112 // Only known if known in both the LHS and RHS.
1113 KnownOne &= KnownOne2;
1114 KnownZero &= KnownZero2;
1115 return;
1116 case ISD::SELECT_CC:
1117 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1118 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1119 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1120 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1121
1122 // Only known if known in both the LHS and RHS.
1123 KnownOne &= KnownOne2;
1124 KnownZero &= KnownZero2;
1125 return;
1126 case ISD::SETCC:
1127 // If we know the result of a setcc has the top bits zero, use this info.
1128 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1129 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1130 return;
1131 case ISD::SHL:
1132 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1133 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1134 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1135 KnownZero, KnownOne, Depth+1);
1136 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1137 KnownZero <<= SA->getValue();
1138 KnownOne <<= SA->getValue();
1139 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1140 }
1141 return;
1142 case ISD::SRL:
1143 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1144 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1145 MVT::ValueType VT = Op.getValueType();
1146 unsigned ShAmt = SA->getValue();
1147
1148 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1149 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1150 KnownZero, KnownOne, Depth+1);
1151 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1152 KnownZero &= TypeMask;
1153 KnownOne &= TypeMask;
1154 KnownZero >>= ShAmt;
1155 KnownOne >>= ShAmt;
1156
1157 uint64_t HighBits = (1ULL << ShAmt)-1;
1158 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1159 KnownZero |= HighBits; // High bits known zero.
1160 }
1161 return;
1162 case ISD::SRA:
1163 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1164 MVT::ValueType VT = Op.getValueType();
1165 unsigned ShAmt = SA->getValue();
1166
1167 // Compute the new bits that are at the top now.
1168 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1169
1170 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1171 // If any of the demanded bits are produced by the sign extension, we also
1172 // demand the input sign bit.
1173 uint64_t HighBits = (1ULL << ShAmt)-1;
1174 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1175 if (HighBits & Mask)
1176 InDemandedMask |= MVT::getIntVTSignBit(VT);
1177
1178 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1179 Depth+1);
1180 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1181 KnownZero &= TypeMask;
1182 KnownOne &= TypeMask;
1183 KnownZero >>= ShAmt;
1184 KnownOne >>= ShAmt;
1185
1186 // Handle the sign bits.
1187 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1188 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1189
1190 if (KnownZero & SignBit) {
1191 KnownZero |= HighBits; // New bits are known zero.
1192 } else if (KnownOne & SignBit) {
1193 KnownOne |= HighBits; // New bits are known one.
1194 }
1195 }
1196 return;
1197 case ISD::SIGN_EXTEND_INREG: {
1198 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1199
1200 // Sign extension. Compute the demanded bits in the result that are not
1201 // present in the input.
1202 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1203
1204 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1205 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1206
1207 // If the sign extended bits are demanded, we know that the sign
1208 // bit is demanded.
1209 if (NewBits)
1210 InputDemandedBits |= InSignBit;
1211
1212 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1213 KnownZero, KnownOne, Depth+1);
1214 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1215
1216 // If the sign bit of the input is known set or clear, then we know the
1217 // top bits of the result.
1218 if (KnownZero & InSignBit) { // Input sign bit known clear
1219 KnownZero |= NewBits;
1220 KnownOne &= ~NewBits;
1221 } else if (KnownOne & InSignBit) { // Input sign bit known set
1222 KnownOne |= NewBits;
1223 KnownZero &= ~NewBits;
1224 } else { // Input sign bit unknown
1225 KnownZero &= ~NewBits;
1226 KnownOne &= ~NewBits;
1227 }
1228 return;
1229 }
1230 case ISD::CTTZ:
1231 case ISD::CTLZ:
1232 case ISD::CTPOP: {
1233 MVT::ValueType VT = Op.getValueType();
1234 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1235 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1236 KnownOne = 0;
1237 return;
1238 }
1239 case ISD::LOAD: {
1240 if (ISD::isZEXTLoad(Op.Val)) {
1241 LoadSDNode *LD = cast<LoadSDNode>(Op);
1242 MVT::ValueType VT = LD->getLoadedVT();
1243 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1244 }
1245 return;
1246 }
1247 case ISD::ZERO_EXTEND: {
1248 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1249 uint64_t NewBits = (~InMask) & Mask;
1250 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1251 KnownOne, Depth+1);
1252 KnownZero |= NewBits & Mask;
1253 KnownOne &= ~NewBits;
1254 return;
1255 }
1256 case ISD::SIGN_EXTEND: {
1257 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1258 unsigned InBits = MVT::getSizeInBits(InVT);
1259 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1260 uint64_t InSignBit = 1ULL << (InBits-1);
1261 uint64_t NewBits = (~InMask) & Mask;
1262 uint64_t InDemandedBits = Mask & InMask;
1263
1264 // If any of the sign extended bits are demanded, we know that the sign
1265 // bit is demanded.
1266 if (NewBits & Mask)
1267 InDemandedBits |= InSignBit;
1268
1269 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1270 KnownOne, Depth+1);
1271 // If the sign bit is known zero or one, the top bits match.
1272 if (KnownZero & InSignBit) {
1273 KnownZero |= NewBits;
1274 KnownOne &= ~NewBits;
1275 } else if (KnownOne & InSignBit) {
1276 KnownOne |= NewBits;
1277 KnownZero &= ~NewBits;
1278 } else { // Otherwise, top bits aren't known.
1279 KnownOne &= ~NewBits;
1280 KnownZero &= ~NewBits;
1281 }
1282 return;
1283 }
1284 case ISD::ANY_EXTEND: {
1285 MVT::ValueType VT = Op.getOperand(0).getValueType();
1286 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1287 KnownZero, KnownOne, Depth+1);
1288 return;
1289 }
1290 case ISD::TRUNCATE: {
1291 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1292 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1293 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1294 KnownZero &= OutMask;
1295 KnownOne &= OutMask;
1296 break;
1297 }
1298 case ISD::AssertZext: {
1299 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1300 uint64_t InMask = MVT::getIntVTBitMask(VT);
1301 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1302 KnownOne, Depth+1);
1303 KnownZero |= (~InMask) & Mask;
1304 return;
1305 }
1306 case ISD::ADD: {
1307 // If either the LHS or the RHS are Zero, the result is zero.
1308 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1309 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1310 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1311 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1312
1313 // Output known-0 bits are known if clear or set in both the low clear bits
1314 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1315 // low 3 bits clear.
1316 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1317 CountTrailingZeros_64(~KnownZero2));
1318
1319 KnownZero = (1ULL << KnownZeroOut) - 1;
1320 KnownOne = 0;
1321 return;
1322 }
1323 case ISD::SUB: {
1324 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1325 if (!CLHS) return;
1326
1327 // We know that the top bits of C-X are clear if X contains less bits
1328 // than C (i.e. no wrap-around can happen). For example, 20-X is
1329 // positive if we can prove that X is >= 0 and < 16.
1330 MVT::ValueType VT = CLHS->getValueType(0);
1331 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1332 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1333 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1334 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1335 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1336
1337 // If all of the MaskV bits are known to be zero, then we know the output
1338 // top bits are zero, because we now know that the output is from [0-C].
1339 if ((KnownZero & MaskV) == MaskV) {
1340 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1341 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1342 KnownOne = 0; // No one bits known.
1343 } else {
1344 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1345 }
1346 }
1347 return;
1348 }
1349 default:
1350 // Allow the target to implement this method for its nodes.
1351 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1352 case ISD::INTRINSIC_WO_CHAIN:
1353 case ISD::INTRINSIC_W_CHAIN:
1354 case ISD::INTRINSIC_VOID:
1355 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1356 }
1357 return;
1358 }
1359}
1360
1361/// ComputeNumSignBits - Return the number of times the sign bit of the
1362/// register is replicated into the other bits. We know that at least 1 bit
1363/// is always equal to the sign bit (itself), but other cases can give us
1364/// information. For example, immediately after an "SRA X, 2", we know that
1365/// the top 3 bits are all equal to each other, so we return 3.
1366unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1367 MVT::ValueType VT = Op.getValueType();
1368 assert(MVT::isInteger(VT) && "Invalid VT!");
1369 unsigned VTBits = MVT::getSizeInBits(VT);
1370 unsigned Tmp, Tmp2;
1371
1372 if (Depth == 6)
1373 return 1; // Limit search depth.
1374
1375 switch (Op.getOpcode()) {
1376 default: break;
1377 case ISD::AssertSext:
1378 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1379 return VTBits-Tmp+1;
1380 case ISD::AssertZext:
1381 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1382 return VTBits-Tmp;
1383
1384 case ISD::Constant: {
1385 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1386 // If negative, invert the bits, then look at it.
1387 if (Val & MVT::getIntVTSignBit(VT))
1388 Val = ~Val;
1389
1390 // Shift the bits so they are the leading bits in the int64_t.
1391 Val <<= 64-VTBits;
1392
1393 // Return # leading zeros. We use 'min' here in case Val was zero before
1394 // shifting. We don't want to return '64' as for an i32 "0".
1395 return std::min(VTBits, CountLeadingZeros_64(Val));
1396 }
1397
1398 case ISD::SIGN_EXTEND:
1399 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1400 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1401
1402 case ISD::SIGN_EXTEND_INREG:
1403 // Max of the input and what this extends.
1404 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1405 Tmp = VTBits-Tmp+1;
1406
1407 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1408 return std::max(Tmp, Tmp2);
1409
1410 case ISD::SRA:
1411 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1412 // SRA X, C -> adds C sign bits.
1413 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1414 Tmp += C->getValue();
1415 if (Tmp > VTBits) Tmp = VTBits;
1416 }
1417 return Tmp;
1418 case ISD::SHL:
1419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1420 // shl destroys sign bits.
1421 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1422 if (C->getValue() >= VTBits || // Bad shift.
1423 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1424 return Tmp - C->getValue();
1425 }
1426 break;
1427 case ISD::AND:
1428 case ISD::OR:
1429 case ISD::XOR: // NOT is handled here.
1430 // Logical binary ops preserve the number of sign bits.
1431 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1432 if (Tmp == 1) return 1; // Early out.
1433 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1434 return std::min(Tmp, Tmp2);
1435
1436 case ISD::SELECT:
1437 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1438 if (Tmp == 1) return 1; // Early out.
1439 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1440 return std::min(Tmp, Tmp2);
1441
1442 case ISD::SETCC:
1443 // If setcc returns 0/-1, all bits are sign bits.
1444 if (TLI.getSetCCResultContents() ==
1445 TargetLowering::ZeroOrNegativeOneSetCCResult)
1446 return VTBits;
1447 break;
1448 case ISD::ROTL:
1449 case ISD::ROTR:
1450 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1451 unsigned RotAmt = C->getValue() & (VTBits-1);
1452
1453 // Handle rotate right by N like a rotate left by 32-N.
1454 if (Op.getOpcode() == ISD::ROTR)
1455 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1456
1457 // If we aren't rotating out all of the known-in sign bits, return the
1458 // number that are left. This handles rotl(sext(x), 1) for example.
1459 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1460 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1461 }
1462 break;
1463 case ISD::ADD:
1464 // Add can have at most one carry bit. Thus we know that the output
1465 // is, at worst, one more bit than the inputs.
1466 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1467 if (Tmp == 1) return 1; // Early out.
1468
1469 // Special case decrementing a value (ADD X, -1):
1470 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1471 if (CRHS->isAllOnesValue()) {
1472 uint64_t KnownZero, KnownOne;
1473 uint64_t Mask = MVT::getIntVTBitMask(VT);
1474 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1475
1476 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1477 // sign bits set.
1478 if ((KnownZero|1) == Mask)
1479 return VTBits;
1480
1481 // If we are subtracting one from a positive number, there is no carry
1482 // out of the result.
1483 if (KnownZero & MVT::getIntVTSignBit(VT))
1484 return Tmp;
1485 }
1486
1487 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1488 if (Tmp2 == 1) return 1;
1489 return std::min(Tmp, Tmp2)-1;
1490 break;
1491
1492 case ISD::SUB:
1493 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1494 if (Tmp2 == 1) return 1;
1495
1496 // Handle NEG.
1497 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1498 if (CLHS->getValue() == 0) {
1499 uint64_t KnownZero, KnownOne;
1500 uint64_t Mask = MVT::getIntVTBitMask(VT);
1501 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1502 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1503 // sign bits set.
1504 if ((KnownZero|1) == Mask)
1505 return VTBits;
1506
1507 // If the input is known to be positive (the sign bit is known clear),
1508 // the output of the NEG has the same number of sign bits as the input.
1509 if (KnownZero & MVT::getIntVTSignBit(VT))
1510 return Tmp2;
1511
1512 // Otherwise, we treat this like a SUB.
1513 }
1514
1515 // Sub can have at most one carry bit. Thus we know that the output
1516 // is, at worst, one more bit than the inputs.
1517 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1518 if (Tmp == 1) return 1; // Early out.
1519 return std::min(Tmp, Tmp2)-1;
1520 break;
1521 case ISD::TRUNCATE:
1522 // FIXME: it's tricky to do anything useful for this, but it is an important
1523 // case for targets like X86.
1524 break;
1525 }
1526
1527 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1528 if (Op.getOpcode() == ISD::LOAD) {
1529 LoadSDNode *LD = cast<LoadSDNode>(Op);
1530 unsigned ExtType = LD->getExtensionType();
1531 switch (ExtType) {
1532 default: break;
1533 case ISD::SEXTLOAD: // '17' bits known
1534 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1535 return VTBits-Tmp+1;
1536 case ISD::ZEXTLOAD: // '16' bits known
1537 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1538 return VTBits-Tmp;
1539 }
1540 }
1541
1542 // Allow the target to implement this method for its nodes.
1543 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1544 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1545 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1546 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1547 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1548 if (NumBits > 1) return NumBits;
1549 }
1550
1551 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1552 // use this information.
1553 uint64_t KnownZero, KnownOne;
1554 uint64_t Mask = MVT::getIntVTBitMask(VT);
1555 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1556
1557 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1558 if (KnownZero & SignBit) { // SignBit is 0
1559 Mask = KnownZero;
1560 } else if (KnownOne & SignBit) { // SignBit is 1;
1561 Mask = KnownOne;
1562 } else {
1563 // Nothing known.
1564 return 1;
1565 }
1566
1567 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1568 // the number of identical bits in the top of the input value.
1569 Mask ^= ~0ULL;
1570 Mask <<= 64-VTBits;
1571 // Return # leading zeros. We use 'min' here in case Val was zero before
1572 // shifting. We don't want to return '64' as for an i32 "0".
1573 return std::min(VTBits, CountLeadingZeros_64(Mask));
1574}
1575
Chris Lattner51dabfb2006-10-14 00:41:01 +00001576
Chris Lattnerc3aae252005-01-07 07:46:32 +00001577/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001578///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001579SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001580 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001581 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001582 void *IP = 0;
1583 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1584 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001585 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001586 CSEMap.InsertNode(N, IP);
1587
1588 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001589 return SDOperand(N, 0);
1590}
1591
Chris Lattnerc3aae252005-01-07 07:46:32 +00001592SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1593 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001594 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001595 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001596 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1597 uint64_t Val = C->getValue();
1598 switch (Opcode) {
1599 default: break;
1600 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001601 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001602 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1603 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001604 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1605 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001606 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001607 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001608 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001609 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001610 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001611 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001612 case ISD::BSWAP:
1613 switch(VT) {
1614 default: assert(0 && "Invalid bswap!"); break;
1615 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1616 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1617 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1618 }
1619 break;
1620 case ISD::CTPOP:
1621 switch(VT) {
1622 default: assert(0 && "Invalid ctpop!"); break;
1623 case MVT::i1: return getConstant(Val != 0, VT);
1624 case MVT::i8:
1625 Tmp1 = (unsigned)Val & 0xFF;
1626 return getConstant(CountPopulation_32(Tmp1), VT);
1627 case MVT::i16:
1628 Tmp1 = (unsigned)Val & 0xFFFF;
1629 return getConstant(CountPopulation_32(Tmp1), VT);
1630 case MVT::i32:
1631 return getConstant(CountPopulation_32((unsigned)Val), VT);
1632 case MVT::i64:
1633 return getConstant(CountPopulation_64(Val), VT);
1634 }
1635 case ISD::CTLZ:
1636 switch(VT) {
1637 default: assert(0 && "Invalid ctlz!"); break;
1638 case MVT::i1: return getConstant(Val == 0, VT);
1639 case MVT::i8:
1640 Tmp1 = (unsigned)Val & 0xFF;
1641 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1642 case MVT::i16:
1643 Tmp1 = (unsigned)Val & 0xFFFF;
1644 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1645 case MVT::i32:
1646 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1647 case MVT::i64:
1648 return getConstant(CountLeadingZeros_64(Val), VT);
1649 }
1650 case ISD::CTTZ:
1651 switch(VT) {
1652 default: assert(0 && "Invalid cttz!"); break;
1653 case MVT::i1: return getConstant(Val == 0, VT);
1654 case MVT::i8:
1655 Tmp1 = (unsigned)Val | 0x100;
1656 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1657 case MVT::i16:
1658 Tmp1 = (unsigned)Val | 0x10000;
1659 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1660 case MVT::i32:
1661 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1662 case MVT::i64:
1663 return getConstant(CountTrailingZeros_64(Val), VT);
1664 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001665 }
1666 }
1667
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001668 // Constant fold unary operations with a floating point constant operand.
1669 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1670 APFloat V = C->getValueAPF(); // make copy
Chris Lattnerc3aae252005-01-07 07:46:32 +00001671 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001672 case ISD::FNEG:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001673 V.changeSign();
1674 return getConstantFP(V, VT);
Chris Lattner94683772005-12-23 05:30:37 +00001675 case ISD::FABS:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001676 V.clearSign();
1677 return getConstantFP(V, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001678 case ISD::FP_ROUND:
1679 case ISD::FP_EXTEND:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001680 // This can return overflow, underflow, or inexact; we don't care.
1681 // FIXME need to be more flexible about rounding mode.
1682 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1683 APFloat::IEEEdouble,
1684 APFloat::rmNearestTiesToEven);
1685 return getConstantFP(V, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001686 case ISD::FP_TO_SINT:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001687 case ISD::FP_TO_UINT: {
1688 integerPart x;
1689 assert(integerPartWidth >= 64);
1690 // FIXME need to be more flexible about rounding mode.
1691 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1692 Opcode==ISD::FP_TO_SINT,
1693 APFloat::rmTowardZero);
1694 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1695 break;
1696 return getConstant(x, VT);
1697 }
Chris Lattner94683772005-12-23 05:30:37 +00001698 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001699 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001700 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001701 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001702 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001703 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001704 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001705 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001706
1707 unsigned OpOpcode = Operand.Val->getOpcode();
1708 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001709 case ISD::TokenFactor:
1710 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001711 case ISD::FP_ROUND:
1712 case ISD::FP_EXTEND:
1713 assert(MVT::isFloatingPoint(VT) &&
1714 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1715 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001716 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001717 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1718 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001719 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001720 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001721 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1722 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1723 break;
1724 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001725 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1726 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001727 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001728 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001729 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001730 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001731 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001732 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001733 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1734 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001735 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001736 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001737 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1738 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1739 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1740 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001741 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001742 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1743 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001744 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001745 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001746 if (OpOpcode == ISD::TRUNCATE)
1747 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001748 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1749 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001750 // If the source is smaller than the dest, we still need an extend.
1751 if (Operand.Val->getOperand(0).getValueType() < VT)
1752 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1753 else if (Operand.Val->getOperand(0).getValueType() > VT)
1754 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1755 else
1756 return Operand.Val->getOperand(0);
1757 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001758 break;
Chris Lattner35481892005-12-23 00:16:34 +00001759 case ISD::BIT_CONVERT:
1760 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001761 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001762 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001763 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001764 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1765 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001766 if (OpOpcode == ISD::UNDEF)
1767 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001768 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001769 case ISD::SCALAR_TO_VECTOR:
1770 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001771 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001772 "Illegal SCALAR_TO_VECTOR node!");
1773 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001774 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001775 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1776 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001777 Operand.Val->getOperand(0));
1778 if (OpOpcode == ISD::FNEG) // --X -> X
1779 return Operand.Val->getOperand(0);
1780 break;
1781 case ISD::FABS:
1782 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1783 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1784 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001785 }
1786
Chris Lattner43247a12005-08-25 19:12:10 +00001787 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001788 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001789 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001790 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001791 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001792 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001793 void *IP = 0;
1794 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1795 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001796 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001797 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001798 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001799 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001800 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001801 AllNodes.push_back(N);
1802 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001803}
1804
Chris Lattner36019aa2005-04-18 03:48:41 +00001805
1806
Chris Lattnerc3aae252005-01-07 07:46:32 +00001807SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1808 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001809#ifndef NDEBUG
1810 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001811 case ISD::TokenFactor:
1812 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1813 N2.getValueType() == MVT::Other && "Invalid token factor!");
1814 break;
Chris Lattner76365122005-01-16 02:23:22 +00001815 case ISD::AND:
1816 case ISD::OR:
1817 case ISD::XOR:
1818 case ISD::UDIV:
1819 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001820 case ISD::MULHU:
1821 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001822 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1823 // fall through
1824 case ISD::ADD:
1825 case ISD::SUB:
1826 case ISD::MUL:
1827 case ISD::SDIV:
1828 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001829 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1830 // fall through.
1831 case ISD::FADD:
1832 case ISD::FSUB:
1833 case ISD::FMUL:
1834 case ISD::FDIV:
1835 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001836 assert(N1.getValueType() == N2.getValueType() &&
1837 N1.getValueType() == VT && "Binary operator types must match!");
1838 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001839 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1840 assert(N1.getValueType() == VT &&
1841 MVT::isFloatingPoint(N1.getValueType()) &&
1842 MVT::isFloatingPoint(N2.getValueType()) &&
1843 "Invalid FCOPYSIGN!");
1844 break;
Chris Lattner76365122005-01-16 02:23:22 +00001845 case ISD::SHL:
1846 case ISD::SRA:
1847 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001848 case ISD::ROTL:
1849 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001850 assert(VT == N1.getValueType() &&
1851 "Shift operators return type must be the same as their first arg");
1852 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001853 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001854 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001855 case ISD::FP_ROUND_INREG: {
1856 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1857 assert(VT == N1.getValueType() && "Not an inreg round!");
1858 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1859 "Cannot FP_ROUND_INREG integer types");
1860 assert(EVT <= VT && "Not rounding down!");
1861 break;
1862 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001863 case ISD::AssertSext:
1864 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001865 case ISD::SIGN_EXTEND_INREG: {
1866 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1867 assert(VT == N1.getValueType() && "Not an inreg extend!");
1868 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1869 "Cannot *_EXTEND_INREG FP types");
1870 assert(EVT <= VT && "Not extending!");
1871 }
1872
Chris Lattner76365122005-01-16 02:23:22 +00001873 default: break;
1874 }
1875#endif
1876
Chris Lattnerc3aae252005-01-07 07:46:32 +00001877 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1878 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1879 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001880 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1881 int64_t Val = N1C->getValue();
1882 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1883 Val <<= 64-FromBits;
1884 Val >>= 64-FromBits;
1885 return getConstant(Val, VT);
1886 }
1887
Chris Lattnerc3aae252005-01-07 07:46:32 +00001888 if (N2C) {
1889 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1890 switch (Opcode) {
1891 case ISD::ADD: return getConstant(C1 + C2, VT);
1892 case ISD::SUB: return getConstant(C1 - C2, VT);
1893 case ISD::MUL: return getConstant(C1 * C2, VT);
1894 case ISD::UDIV:
1895 if (C2) return getConstant(C1 / C2, VT);
1896 break;
1897 case ISD::UREM :
1898 if (C2) return getConstant(C1 % C2, VT);
1899 break;
1900 case ISD::SDIV :
1901 if (C2) return getConstant(N1C->getSignExtended() /
1902 N2C->getSignExtended(), VT);
1903 break;
1904 case ISD::SREM :
1905 if (C2) return getConstant(N1C->getSignExtended() %
1906 N2C->getSignExtended(), VT);
1907 break;
1908 case ISD::AND : return getConstant(C1 & C2, VT);
1909 case ISD::OR : return getConstant(C1 | C2, VT);
1910 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001911 case ISD::SHL : return getConstant(C1 << C2, VT);
1912 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001913 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001914 case ISD::ROTL :
1915 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1916 VT);
1917 case ISD::ROTR :
1918 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1919 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001920 default: break;
1921 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001922 } else { // Cannonicalize constant to RHS if commutative
1923 if (isCommutativeBinOp(Opcode)) {
1924 std::swap(N1C, N2C);
1925 std::swap(N1, N2);
1926 }
1927 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001928 }
1929
1930 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1931 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001932 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001933 if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001934 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1935 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001936 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001937 case ISD::FADD:
1938 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1939 if (s!=APFloat::opInvalidOp)
1940 return getConstantFP(V1, VT);
1941 break;
1942 case ISD::FSUB:
1943 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1944 if (s!=APFloat::opInvalidOp)
1945 return getConstantFP(V1, VT);
1946 break;
1947 case ISD::FMUL:
1948 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1949 if (s!=APFloat::opInvalidOp)
1950 return getConstantFP(V1, VT);
1951 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001952 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001953 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1954 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1955 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001956 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001957 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001958 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1959 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1960 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001961 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001962 case ISD::FCOPYSIGN:
1963 V1.copySign(V2);
1964 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001965 default: break;
1966 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001967 } else { // Cannonicalize constant to RHS if commutative
1968 if (isCommutativeBinOp(Opcode)) {
1969 std::swap(N1CFP, N2CFP);
1970 std::swap(N1, N2);
1971 }
1972 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001973 }
Chris Lattner62b57722006-04-20 05:39:12 +00001974
1975 // Canonicalize an UNDEF to the RHS, even over a constant.
1976 if (N1.getOpcode() == ISD::UNDEF) {
1977 if (isCommutativeBinOp(Opcode)) {
1978 std::swap(N1, N2);
1979 } else {
1980 switch (Opcode) {
1981 case ISD::FP_ROUND_INREG:
1982 case ISD::SIGN_EXTEND_INREG:
1983 case ISD::SUB:
1984 case ISD::FSUB:
1985 case ISD::FDIV:
1986 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001987 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001988 return N1; // fold op(undef, arg2) -> undef
1989 case ISD::UDIV:
1990 case ISD::SDIV:
1991 case ISD::UREM:
1992 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001993 case ISD::SRL:
1994 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00001995 if (!MVT::isVector(VT))
1996 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1997 // For vectors, we can't easily build an all zero vector, just return
1998 // the LHS.
1999 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002000 }
2001 }
2002 }
2003
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002004 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002005 if (N2.getOpcode() == ISD::UNDEF) {
2006 switch (Opcode) {
2007 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002008 case ISD::ADDC:
2009 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002010 case ISD::SUB:
2011 case ISD::FADD:
2012 case ISD::FSUB:
2013 case ISD::FMUL:
2014 case ISD::FDIV:
2015 case ISD::FREM:
2016 case ISD::UDIV:
2017 case ISD::SDIV:
2018 case ISD::UREM:
2019 case ISD::SREM:
2020 case ISD::XOR:
2021 return N2; // fold op(arg1, undef) -> undef
2022 case ISD::MUL:
2023 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002024 case ISD::SRL:
2025 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002026 if (!MVT::isVector(VT))
2027 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2028 // For vectors, we can't easily build an all zero vector, just return
2029 // the LHS.
2030 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002031 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002032 if (!MVT::isVector(VT))
2033 return getConstant(MVT::getIntVTBitMask(VT), VT);
2034 // For vectors, we can't easily build an all one vector, just return
2035 // the LHS.
2036 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002037 case ISD::SRA:
2038 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002039 }
2040 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002041
Chris Lattner51dabfb2006-10-14 00:41:01 +00002042 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002043 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00002044 case ISD::TokenFactor:
2045 // Fold trivial token factors.
2046 if (N1.getOpcode() == ISD::EntryToken) return N2;
2047 if (N2.getOpcode() == ISD::EntryToken) return N1;
2048 break;
2049
Chris Lattner51dabfb2006-10-14 00:41:01 +00002050 case ISD::AND:
2051 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2052 // worth handling here.
2053 if (N2C && N2C->getValue() == 0)
2054 return N2;
2055 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00002056 case ISD::OR:
2057 case ISD::XOR:
2058 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2059 // worth handling here.
2060 if (N2C && N2C->getValue() == 0)
2061 return N1;
2062 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002063 case ISD::FP_ROUND_INREG:
2064 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2065 break;
2066 case ISD::SIGN_EXTEND_INREG: {
2067 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2068 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002069 break;
2070 }
Dan Gohman7f321562007-06-25 16:23:39 +00002071 case ISD::EXTRACT_VECTOR_ELT:
2072 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2073
Dan Gohman743d3a72007-07-10 18:20:44 +00002074 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002075 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002076 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2077 N1.getNumOperands() > 0) {
2078 unsigned Factor =
2079 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2080 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2081 N1.getOperand(N2C->getValue() / Factor),
2082 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002083 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002084
Dan Gohman7f321562007-06-25 16:23:39 +00002085 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2086 // expanding large vector constants.
2087 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2088 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002089
2090 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2091 // operations are lowered to scalars.
2092 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2093 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2094 if (IEC == N2C)
2095 return N1.getOperand(1);
2096 else
2097 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2098 }
Dan Gohman7f321562007-06-25 16:23:39 +00002099 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002100 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002101 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2102
Chris Lattner5c6621c2006-09-19 04:51:23 +00002103 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2104 // 64-bit integers into 32-bit parts. Instead of building the extract of
2105 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002106 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002107 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002108
2109 // EXTRACT_ELEMENT of a constant int is also very common.
2110 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2111 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2112 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002113 }
2114 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002115
Nate Begemaneea805e2005-04-13 21:23:31 +00002116 // FIXME: figure out how to safely handle things like
2117 // int foo(int x) { return 1 << (x & 255); }
2118 // int bar() { return foo(256); }
2119#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002120 case ISD::SHL:
2121 case ISD::SRL:
2122 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002123 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002124 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002125 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002126 else if (N2.getOpcode() == ISD::AND)
2127 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2128 // If the and is only masking out bits that cannot effect the shift,
2129 // eliminate the and.
2130 unsigned NumBits = MVT::getSizeInBits(VT);
2131 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2132 return getNode(Opcode, VT, N1, N2.getOperand(0));
2133 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002134 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002135#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002136 }
2137
Chris Lattner27e9b412005-05-11 18:57:39 +00002138 // Memoize this node if possible.
2139 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002140 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002141 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002142 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002143 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002144 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002145 void *IP = 0;
2146 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2147 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002148 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002149 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002150 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002151 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002152 }
2153
Chris Lattnerc3aae252005-01-07 07:46:32 +00002154 AllNodes.push_back(N);
2155 return SDOperand(N, 0);
2156}
2157
Chris Lattnerc3aae252005-01-07 07:46:32 +00002158SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2159 SDOperand N1, SDOperand N2, SDOperand N3) {
2160 // Perform various simplifications.
2161 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2162 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002163 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002164 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002165 // Use FoldSetCC to simplify SETCC's.
2166 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002167 if (Simp.Val) return Simp;
2168 break;
2169 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002170 case ISD::SELECT:
2171 if (N1C)
2172 if (N1C->getValue())
2173 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002174 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002175 return N3; // select false, X, Y -> Y
2176
2177 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002178 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002179 case ISD::BRCOND:
2180 if (N2C)
2181 if (N2C->getValue()) // Unconditional branch
2182 return getNode(ISD::BR, MVT::Other, N1, N3);
2183 else
2184 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002185 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002186 case ISD::VECTOR_SHUFFLE:
2187 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2188 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2189 N3.getOpcode() == ISD::BUILD_VECTOR &&
2190 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2191 "Illegal VECTOR_SHUFFLE node!");
2192 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002193 case ISD::BIT_CONVERT:
2194 // Fold bit_convert nodes from a type to themselves.
2195 if (N1.getValueType() == VT)
2196 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002197 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002198 }
2199
Chris Lattner43247a12005-08-25 19:12:10 +00002200 // Memoize node if it doesn't produce a flag.
2201 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002202 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002203 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002204 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002205 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002206 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002207 void *IP = 0;
2208 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2209 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002210 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002211 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002212 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002213 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002214 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002215 AllNodes.push_back(N);
2216 return SDOperand(N, 0);
2217}
2218
2219SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002220 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002221 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002222 SDOperand Ops[] = { N1, N2, N3, N4 };
2223 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002224}
2225
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002226SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2227 SDOperand N1, SDOperand N2, SDOperand N3,
2228 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002229 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2230 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002231}
2232
Evan Cheng7038daf2005-12-10 00:37:58 +00002233SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2234 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002235 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002236 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002237 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2238 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002239 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002240 Ty = MVT::getTypeForValueType(VT);
2241 } else if (SV) {
2242 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2243 assert(PT && "Value for load must be a pointer");
2244 Ty = PT->getElementType();
2245 }
2246 assert(Ty && "Could not get type information for load");
2247 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2248 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002249 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002250 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002251 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002252 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002253 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002254 ID.AddInteger(ISD::UNINDEXED);
2255 ID.AddInteger(ISD::NON_EXTLOAD);
2256 ID.AddInteger(VT);
2257 ID.AddPointer(SV);
2258 ID.AddInteger(SVOffset);
2259 ID.AddInteger(Alignment);
2260 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002261 void *IP = 0;
2262 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2263 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002264 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002265 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2266 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002267 CSEMap.InsertNode(N, IP);
2268 AllNodes.push_back(N);
2269 return SDOperand(N, 0);
2270}
2271
2272SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002273 SDOperand Chain, SDOperand Ptr,
2274 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002275 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002276 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002277 // If they are asking for an extending load from/to the same thing, return a
2278 // normal load.
2279 if (VT == EVT)
2280 ExtType = ISD::NON_EXTLOAD;
2281
2282 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002283 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002284 else
2285 assert(EVT < VT && "Should only be an extending load, not truncating!");
2286 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2287 "Cannot sign/zero extend a FP/Vector load!");
2288 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2289 "Cannot convert from FP to Int or Int -> FP!");
2290
Dan Gohman575e2f42007-06-04 15:49:41 +00002291 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2292 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002293 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002294 Ty = MVT::getTypeForValueType(VT);
2295 } else if (SV) {
2296 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2297 assert(PT && "Value for load must be a pointer");
2298 Ty = PT->getElementType();
2299 }
2300 assert(Ty && "Could not get type information for load");
2301 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2302 }
Evan Cheng466685d2006-10-09 20:57:25 +00002303 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002304 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002305 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002306 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002307 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002308 ID.AddInteger(ISD::UNINDEXED);
2309 ID.AddInteger(ExtType);
2310 ID.AddInteger(EVT);
2311 ID.AddPointer(SV);
2312 ID.AddInteger(SVOffset);
2313 ID.AddInteger(Alignment);
2314 ID.AddInteger(isVolatile);
2315 void *IP = 0;
2316 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2317 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002318 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002319 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002320 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002321 AllNodes.push_back(N);
2322 return SDOperand(N, 0);
2323}
2324
Evan Cheng144d8f02006-11-09 17:55:04 +00002325SDOperand
2326SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2327 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002328 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002329 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2330 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002331 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002332 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002333 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002334 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002335 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002336 ID.AddInteger(AM);
2337 ID.AddInteger(LD->getExtensionType());
2338 ID.AddInteger(LD->getLoadedVT());
2339 ID.AddPointer(LD->getSrcValue());
2340 ID.AddInteger(LD->getSrcValueOffset());
2341 ID.AddInteger(LD->getAlignment());
2342 ID.AddInteger(LD->isVolatile());
2343 void *IP = 0;
2344 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2345 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002346 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002347 LD->getExtensionType(), LD->getLoadedVT(),
2348 LD->getSrcValue(), LD->getSrcValueOffset(),
2349 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002350 CSEMap.InsertNode(N, IP);
2351 AllNodes.push_back(N);
2352 return SDOperand(N, 0);
2353}
2354
Jeff Cohend41b30d2006-11-05 19:31:28 +00002355SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002356 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002357 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002358 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002359
Dan Gohman575e2f42007-06-04 15:49:41 +00002360 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2361 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002362 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002363 Ty = MVT::getTypeForValueType(VT);
2364 } else if (SV) {
2365 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2366 assert(PT && "Value for store must be a pointer");
2367 Ty = PT->getElementType();
2368 }
2369 assert(Ty && "Could not get type information for store");
2370 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2371 }
Evan Chengad071e12006-10-05 22:57:11 +00002372 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002373 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002374 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002375 FoldingSetNodeID ID;
2376 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002377 ID.AddInteger(ISD::UNINDEXED);
2378 ID.AddInteger(false);
2379 ID.AddInteger(VT);
2380 ID.AddPointer(SV);
2381 ID.AddInteger(SVOffset);
2382 ID.AddInteger(Alignment);
2383 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002384 void *IP = 0;
2385 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2386 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002387 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002388 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002389 CSEMap.InsertNode(N, IP);
2390 AllNodes.push_back(N);
2391 return SDOperand(N, 0);
2392}
2393
Jeff Cohend41b30d2006-11-05 19:31:28 +00002394SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002395 SDOperand Ptr, const Value *SV,
2396 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002397 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002398 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002399 bool isTrunc = VT != SVT;
2400
2401 assert(VT > SVT && "Not a truncation?");
2402 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2403 "Can't do FP-INT conversion!");
2404
Dan Gohman575e2f42007-06-04 15:49:41 +00002405 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2406 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002407 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002408 Ty = MVT::getTypeForValueType(VT);
2409 } else if (SV) {
2410 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2411 assert(PT && "Value for store must be a pointer");
2412 Ty = PT->getElementType();
2413 }
2414 assert(Ty && "Could not get type information for store");
2415 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2416 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002417 SDVTList VTs = getVTList(MVT::Other);
2418 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002419 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002420 FoldingSetNodeID ID;
2421 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002422 ID.AddInteger(ISD::UNINDEXED);
2423 ID.AddInteger(isTrunc);
2424 ID.AddInteger(SVT);
2425 ID.AddPointer(SV);
2426 ID.AddInteger(SVOffset);
2427 ID.AddInteger(Alignment);
2428 ID.AddInteger(isVolatile);
2429 void *IP = 0;
2430 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2431 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002432 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002433 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002434 CSEMap.InsertNode(N, IP);
2435 AllNodes.push_back(N);
2436 return SDOperand(N, 0);
2437}
2438
Evan Cheng144d8f02006-11-09 17:55:04 +00002439SDOperand
2440SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2441 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002442 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2443 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2444 "Store is already a indexed store!");
2445 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2446 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2447 FoldingSetNodeID ID;
2448 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2449 ID.AddInteger(AM);
2450 ID.AddInteger(ST->isTruncatingStore());
2451 ID.AddInteger(ST->getStoredVT());
2452 ID.AddPointer(ST->getSrcValue());
2453 ID.AddInteger(ST->getSrcValueOffset());
2454 ID.AddInteger(ST->getAlignment());
2455 ID.AddInteger(ST->isVolatile());
2456 void *IP = 0;
2457 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2458 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002459 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002460 ST->isTruncatingStore(), ST->getStoredVT(),
2461 ST->getSrcValue(), ST->getSrcValueOffset(),
2462 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002463 CSEMap.InsertNode(N, IP);
2464 AllNodes.push_back(N);
2465 return SDOperand(N, 0);
2466}
2467
Nate Begemanacc398c2006-01-25 18:21:52 +00002468SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2469 SDOperand Chain, SDOperand Ptr,
2470 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002471 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002472 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002473}
2474
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002475SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002476 const SDOperand *Ops, unsigned NumOps) {
2477 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002478 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002479 case 1: return getNode(Opcode, VT, Ops[0]);
2480 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2481 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002482 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002483 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002484
Chris Lattneref847df2005-04-09 03:27:28 +00002485 switch (Opcode) {
2486 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002487 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002488 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002489 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2490 "LHS and RHS of condition must have same type!");
2491 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2492 "True and False arms of SelectCC must have same type!");
2493 assert(Ops[2].getValueType() == VT &&
2494 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002495 break;
2496 }
2497 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002498 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002499 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2500 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002501 break;
2502 }
Chris Lattneref847df2005-04-09 03:27:28 +00002503 }
2504
Chris Lattner385328c2005-05-14 07:42:29 +00002505 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002506 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002507 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002508 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002509 FoldingSetNodeID ID;
2510 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002511 void *IP = 0;
2512 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2513 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002514 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002515 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002516 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002517 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002518 }
Chris Lattneref847df2005-04-09 03:27:28 +00002519 AllNodes.push_back(N);
2520 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002521}
2522
Chris Lattner89c34632005-05-14 06:20:26 +00002523SDOperand SelectionDAG::getNode(unsigned Opcode,
2524 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002525 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002526 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2527 Ops, NumOps);
2528}
2529
2530SDOperand SelectionDAG::getNode(unsigned Opcode,
2531 const MVT::ValueType *VTs, unsigned NumVTs,
2532 const SDOperand *Ops, unsigned NumOps) {
2533 if (NumVTs == 1)
2534 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002535 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2536}
2537
2538SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2539 const SDOperand *Ops, unsigned NumOps) {
2540 if (VTList.NumVTs == 1)
2541 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002542
Chris Lattner5f056bf2005-07-10 01:55:33 +00002543 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002544 // FIXME: figure out how to safely handle things like
2545 // int foo(int x) { return 1 << (x & 255); }
2546 // int bar() { return foo(256); }
2547#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002548 case ISD::SRA_PARTS:
2549 case ISD::SRL_PARTS:
2550 case ISD::SHL_PARTS:
2551 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002552 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002553 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2554 else if (N3.getOpcode() == ISD::AND)
2555 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2556 // If the and is only masking out bits that cannot effect the shift,
2557 // eliminate the and.
2558 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2559 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2560 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2561 }
2562 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002563#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002564 }
Chris Lattner89c34632005-05-14 06:20:26 +00002565
Chris Lattner43247a12005-08-25 19:12:10 +00002566 // Memoize the node unless it returns a flag.
2567 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002568 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002569 FoldingSetNodeID ID;
2570 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002571 void *IP = 0;
2572 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2573 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002574 if (NumOps == 1)
2575 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2576 else if (NumOps == 2)
2577 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2578 else if (NumOps == 3)
2579 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2580 else
2581 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002582 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002583 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002584 if (NumOps == 1)
2585 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2586 else if (NumOps == 2)
2587 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2588 else if (NumOps == 3)
2589 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2590 else
2591 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002592 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002593 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002594 return SDOperand(N, 0);
2595}
2596
Chris Lattner70046e92006-08-15 17:46:01 +00002597SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Dan Gohman6595cb32007-06-27 16:08:04 +00002598 if (!MVT::isExtendedVT(VT))
Dan Gohman7f321562007-06-25 16:23:39 +00002599 return makeVTList(SDNode::getValueTypeList(VT), 1);
2600
2601 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2602 E = VTList.end(); I != E; ++I) {
2603 if (I->size() == 1 && (*I)[0] == VT)
2604 return makeVTList(&(*I)[0], 1);
2605 }
2606 std::vector<MVT::ValueType> V;
2607 V.push_back(VT);
2608 VTList.push_front(V);
2609 return makeVTList(&(*VTList.begin())[0], 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002610}
2611
Chris Lattner70046e92006-08-15 17:46:01 +00002612SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002613 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2614 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002615 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002616 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002617 }
2618 std::vector<MVT::ValueType> V;
2619 V.push_back(VT1);
2620 V.push_back(VT2);
2621 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002622 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002623}
Chris Lattner70046e92006-08-15 17:46:01 +00002624SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2625 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002626 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002627 E = VTList.end(); I != E; ++I) {
2628 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2629 (*I)[2] == VT3)
2630 return makeVTList(&(*I)[0], 3);
2631 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002632 std::vector<MVT::ValueType> V;
2633 V.push_back(VT1);
2634 V.push_back(VT2);
2635 V.push_back(VT3);
2636 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002637 return makeVTList(&(*VTList.begin())[0], 3);
2638}
2639
2640SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2641 switch (NumVTs) {
2642 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002643 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002644 case 2: return getVTList(VTs[0], VTs[1]);
2645 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2646 default: break;
2647 }
2648
2649 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2650 E = VTList.end(); I != E; ++I) {
2651 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2652
2653 bool NoMatch = false;
2654 for (unsigned i = 2; i != NumVTs; ++i)
2655 if (VTs[i] != (*I)[i]) {
2656 NoMatch = true;
2657 break;
2658 }
2659 if (!NoMatch)
2660 return makeVTList(&*I->begin(), NumVTs);
2661 }
2662
2663 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2664 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002665}
2666
2667
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002668/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2669/// specified operands. If the resultant node already exists in the DAG,
2670/// this does not modify the specified node, instead it returns the node that
2671/// already exists. If the resultant node does not exist in the DAG, the
2672/// input node is returned. As a degenerate case, if you specify the same
2673/// input operands as the node already has, the input node is returned.
2674SDOperand SelectionDAG::
2675UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2676 SDNode *N = InN.Val;
2677 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2678
2679 // Check to see if there is no change.
2680 if (Op == N->getOperand(0)) return InN;
2681
2682 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002683 void *InsertPos = 0;
2684 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2685 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002686
2687 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002688 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002689 RemoveNodeFromCSEMaps(N);
2690
2691 // Now we update the operands.
2692 N->OperandList[0].Val->removeUser(N);
2693 Op.Val->addUser(N);
2694 N->OperandList[0] = Op;
2695
2696 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002697 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002698 return InN;
2699}
2700
2701SDOperand SelectionDAG::
2702UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2703 SDNode *N = InN.Val;
2704 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2705
2706 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002707 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2708 return InN; // No operands changed, just return the input node.
2709
2710 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002711 void *InsertPos = 0;
2712 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2713 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002714
2715 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002716 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002717 RemoveNodeFromCSEMaps(N);
2718
2719 // Now we update the operands.
2720 if (N->OperandList[0] != Op1) {
2721 N->OperandList[0].Val->removeUser(N);
2722 Op1.Val->addUser(N);
2723 N->OperandList[0] = Op1;
2724 }
2725 if (N->OperandList[1] != Op2) {
2726 N->OperandList[1].Val->removeUser(N);
2727 Op2.Val->addUser(N);
2728 N->OperandList[1] = Op2;
2729 }
2730
2731 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002732 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002733 return InN;
2734}
2735
2736SDOperand SelectionDAG::
2737UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002738 SDOperand Ops[] = { Op1, Op2, Op3 };
2739 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002740}
2741
2742SDOperand SelectionDAG::
2743UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2744 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002745 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2746 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002747}
2748
2749SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002750UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2751 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002752 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2753 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002754}
2755
2756
2757SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002758UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002759 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002760 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002761 "Update with wrong number of operands");
2762
2763 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002764 bool AnyChange = false;
2765 for (unsigned i = 0; i != NumOps; ++i) {
2766 if (Ops[i] != N->getOperand(i)) {
2767 AnyChange = true;
2768 break;
2769 }
2770 }
2771
2772 // No operands changed, just return the input node.
2773 if (!AnyChange) return InN;
2774
2775 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002776 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002777 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002778 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002779
2780 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002781 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002782 RemoveNodeFromCSEMaps(N);
2783
2784 // Now we update the operands.
2785 for (unsigned i = 0; i != NumOps; ++i) {
2786 if (N->OperandList[i] != Ops[i]) {
2787 N->OperandList[i].Val->removeUser(N);
2788 Ops[i].Val->addUser(N);
2789 N->OperandList[i] = Ops[i];
2790 }
2791 }
2792
2793 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002794 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002795 return InN;
2796}
2797
2798
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002799/// MorphNodeTo - This frees the operands of the current node, resets the
2800/// opcode, types, and operands to the specified value. This should only be
2801/// used by the SelectionDAG class.
2802void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2803 const SDOperand *Ops, unsigned NumOps) {
2804 NodeType = Opc;
2805 ValueList = L.VTs;
2806 NumValues = L.NumVTs;
2807
2808 // Clear the operands list, updating used nodes to remove this from their
2809 // use list.
2810 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2811 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002812
2813 // If NumOps is larger than the # of operands we currently have, reallocate
2814 // the operand list.
2815 if (NumOps > NumOperands) {
2816 if (OperandsNeedDelete)
2817 delete [] OperandList;
2818 OperandList = new SDOperand[NumOps];
2819 OperandsNeedDelete = true;
2820 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002821
2822 // Assign the new operands.
2823 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002824
2825 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2826 OperandList[i] = Ops[i];
2827 SDNode *N = OperandList[i].Val;
2828 N->Uses.push_back(this);
2829 }
2830}
Chris Lattner149c58c2005-08-16 18:17:10 +00002831
2832/// SelectNodeTo - These are used for target selectors to *mutate* the
2833/// specified node to have the specified return type, Target opcode, and
2834/// operands. Note that target opcodes are stored as
2835/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002836///
2837/// Note that SelectNodeTo returns the resultant node. If there is already a
2838/// node of the specified opcode and operands, it returns that node instead of
2839/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002840SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2841 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002842 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002843 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002844 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002845 void *IP = 0;
2846 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002847 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002848
Chris Lattner7651fa42005-08-24 23:00:29 +00002849 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002850
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002851 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002852
Chris Lattner4a283e92006-08-11 18:38:11 +00002853 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002854 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002855}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002856
Evan Cheng95514ba2006-08-26 08:00:10 +00002857SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2858 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002859 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002860 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002861 SDOperand Ops[] = { Op1 };
2862
Jim Laskey583bd472006-10-27 23:46:08 +00002863 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002864 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002865 void *IP = 0;
2866 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002867 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002868
Chris Lattner149c58c2005-08-16 18:17:10 +00002869 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002870 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002871 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002872 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002873}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002874
Evan Cheng95514ba2006-08-26 08:00:10 +00002875SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2876 MVT::ValueType VT, SDOperand Op1,
2877 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002878 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002879 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002880 SDOperand Ops[] = { Op1, Op2 };
2881
Jim Laskey583bd472006-10-27 23:46:08 +00002882 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002883 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002884 void *IP = 0;
2885 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002886 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002887
Chris Lattner149c58c2005-08-16 18:17:10 +00002888 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002889
Chris Lattner63e3f142007-02-04 07:28:00 +00002890 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002891
Chris Lattnera5682852006-08-07 23:03:03 +00002892 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002893 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002894}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002895
Evan Cheng95514ba2006-08-26 08:00:10 +00002896SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2897 MVT::ValueType VT, SDOperand Op1,
2898 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002899 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002900 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002901 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002902 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002903 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002904 void *IP = 0;
2905 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002906 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002907
Chris Lattner149c58c2005-08-16 18:17:10 +00002908 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002909
Chris Lattner63e3f142007-02-04 07:28:00 +00002910 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002911
Chris Lattnera5682852006-08-07 23:03:03 +00002912 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002913 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002914}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002915
Evan Cheng95514ba2006-08-26 08:00:10 +00002916SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002917 MVT::ValueType VT, const SDOperand *Ops,
2918 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002919 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002920 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002921 FoldingSetNodeID ID;
2922 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002923 void *IP = 0;
2924 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002925 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002926
Chris Lattner6b09a292005-08-21 18:49:33 +00002927 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002928 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002929
Chris Lattnera5682852006-08-07 23:03:03 +00002930 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002931 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002932}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002933
Evan Cheng95514ba2006-08-26 08:00:10 +00002934SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2935 MVT::ValueType VT1, MVT::ValueType VT2,
2936 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002937 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002938 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002939 SDOperand Ops[] = { Op1, Op2 };
2940 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002941 void *IP = 0;
2942 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002943 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002944
Chris Lattner0fb094f2005-11-19 01:44:53 +00002945 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002946 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002947 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002948 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002949}
2950
Evan Cheng95514ba2006-08-26 08:00:10 +00002951SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2952 MVT::ValueType VT1, MVT::ValueType VT2,
2953 SDOperand Op1, SDOperand Op2,
2954 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002955 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002956 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00002957 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002958 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002959 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002960 void *IP = 0;
2961 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002962 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002963
Chris Lattner0fb094f2005-11-19 01:44:53 +00002964 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002965
Chris Lattner63e3f142007-02-04 07:28:00 +00002966 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002967 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002968 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002969}
2970
Chris Lattner0fb094f2005-11-19 01:44:53 +00002971
Evan Cheng6ae46c42006-02-09 07:15:23 +00002972/// getTargetNode - These are used for target selectors to create a new node
2973/// with specified return type(s), target opcode, and operands.
2974///
2975/// Note that getTargetNode returns the resultant node. If there is already a
2976/// node of the specified opcode and operands, it returns that node instead of
2977/// the current one.
2978SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2979 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2980}
2981SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2982 SDOperand Op1) {
2983 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2984}
2985SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2986 SDOperand Op1, SDOperand Op2) {
2987 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2988}
2989SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002990 SDOperand Op1, SDOperand Op2,
2991 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002992 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2993}
2994SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002995 const SDOperand *Ops, unsigned NumOps) {
2996 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002997}
2998SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2999 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003000 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003001 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003002}
3003SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003004 MVT::ValueType VT2, SDOperand Op1,
3005 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003006 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003007 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003008 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003009}
3010SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003011 MVT::ValueType VT2, SDOperand Op1,
3012 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003013 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003014 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003015 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003016}
Evan Cheng694481e2006-08-27 08:08:54 +00003017SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3018 MVT::ValueType VT2,
3019 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003020 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003021 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003022}
3023SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3024 MVT::ValueType VT2, MVT::ValueType VT3,
3025 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003026 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003027 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003028 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003029}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003030SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3031 MVT::ValueType VT2, MVT::ValueType VT3,
3032 SDOperand Op1, SDOperand Op2,
3033 SDOperand Op3) {
3034 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3035 SDOperand Ops[] = { Op1, Op2, Op3 };
3036 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3037}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003038SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003039 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003040 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003041 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3042 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003043}
3044
Evan Cheng99157a02006-08-07 22:13:29 +00003045/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003046/// This can cause recursive merging of nodes in the DAG.
3047///
Chris Lattner8b52f212005-08-26 18:36:28 +00003048/// This version assumes From/To have a single result value.
3049///
Chris Lattner1e111c72005-09-07 05:37:01 +00003050void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3051 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003052 SDNode *From = FromN.Val, *To = ToN.Val;
3053 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3054 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003055 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003056
Chris Lattner8b8749f2005-08-17 19:00:20 +00003057 while (!From->use_empty()) {
3058 // Process users until they are all gone.
3059 SDNode *U = *From->use_begin();
3060
3061 // This node is about to morph, remove its old self from the CSE maps.
3062 RemoveNodeFromCSEMaps(U);
3063
Chris Lattner65113b22005-11-08 22:07:03 +00003064 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3065 I != E; ++I)
3066 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003067 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003068 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003069 To->addUser(U);
3070 }
3071
3072 // Now that we have modified U, add it back to the CSE maps. If it already
3073 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003074 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3075 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003076 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003077 if (Deleted) Deleted->push_back(U);
3078 DeleteNodeNotInCSEMaps(U);
3079 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003080 }
3081}
3082
3083/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3084/// This can cause recursive merging of nodes in the DAG.
3085///
3086/// This version assumes From/To have matching types and numbers of result
3087/// values.
3088///
Chris Lattner1e111c72005-09-07 05:37:01 +00003089void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3090 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003091 assert(From != To && "Cannot replace uses of with self");
3092 assert(From->getNumValues() == To->getNumValues() &&
3093 "Cannot use this version of ReplaceAllUsesWith!");
3094 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003095 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003096 return;
3097 }
3098
3099 while (!From->use_empty()) {
3100 // Process users until they are all gone.
3101 SDNode *U = *From->use_begin();
3102
3103 // This node is about to morph, remove its old self from the CSE maps.
3104 RemoveNodeFromCSEMaps(U);
3105
Chris Lattner65113b22005-11-08 22:07:03 +00003106 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3107 I != E; ++I)
3108 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003109 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003110 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003111 To->addUser(U);
3112 }
3113
3114 // Now that we have modified U, add it back to the CSE maps. If it already
3115 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003116 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3117 ReplaceAllUsesWith(U, Existing, Deleted);
3118 // U is now dead.
3119 if (Deleted) Deleted->push_back(U);
3120 DeleteNodeNotInCSEMaps(U);
3121 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003122 }
3123}
3124
Chris Lattner8b52f212005-08-26 18:36:28 +00003125/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3126/// This can cause recursive merging of nodes in the DAG.
3127///
3128/// This version can replace From with any result values. To must match the
3129/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003130void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003131 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003132 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003133 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003134 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003135 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003136 return;
3137 }
3138
3139 while (!From->use_empty()) {
3140 // Process users until they are all gone.
3141 SDNode *U = *From->use_begin();
3142
3143 // This node is about to morph, remove its old self from the CSE maps.
3144 RemoveNodeFromCSEMaps(U);
3145
Chris Lattner65113b22005-11-08 22:07:03 +00003146 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3147 I != E; ++I)
3148 if (I->Val == From) {
3149 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003150 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003151 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003152 ToOp.Val->addUser(U);
3153 }
3154
3155 // Now that we have modified U, add it back to the CSE maps. If it already
3156 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003157 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3158 ReplaceAllUsesWith(U, Existing, Deleted);
3159 // U is now dead.
3160 if (Deleted) Deleted->push_back(U);
3161 DeleteNodeNotInCSEMaps(U);
3162 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003163 }
3164}
3165
Chris Lattner012f2412006-02-17 21:58:01 +00003166/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3167/// uses of other values produced by From.Val alone. The Deleted vector is
3168/// handled the same was as for ReplaceAllUsesWith.
3169void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3170 std::vector<SDNode*> &Deleted) {
3171 assert(From != To && "Cannot replace a value with itself");
3172 // Handle the simple, trivial, case efficiently.
3173 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3174 ReplaceAllUsesWith(From, To, &Deleted);
3175 return;
3176 }
3177
Chris Lattnercf5640b2007-02-04 00:14:31 +00003178 // Get all of the users of From.Val. We want these in a nice,
3179 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3180 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003181
3182 while (!Users.empty()) {
3183 // We know that this user uses some value of From. If it is the right
3184 // value, update it.
3185 SDNode *User = Users.back();
3186 Users.pop_back();
3187
3188 for (SDOperand *Op = User->OperandList,
3189 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
3190 if (*Op == From) {
3191 // Okay, we know this user needs to be updated. Remove its old self
3192 // from the CSE maps.
3193 RemoveNodeFromCSEMaps(User);
3194
3195 // Update all operands that match "From".
3196 for (; Op != E; ++Op) {
3197 if (*Op == From) {
3198 From.Val->removeUser(User);
3199 *Op = To;
3200 To.Val->addUser(User);
3201 }
3202 }
3203
3204 // Now that we have modified User, add it back to the CSE maps. If it
3205 // already exists there, recursively merge the results together.
3206 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
3207 unsigned NumDeleted = Deleted.size();
3208 ReplaceAllUsesWith(User, Existing, &Deleted);
3209
3210 // User is now dead.
3211 Deleted.push_back(User);
3212 DeleteNodeNotInCSEMaps(User);
3213
3214 // We have to be careful here, because ReplaceAllUsesWith could have
3215 // deleted a user of From, which means there may be dangling pointers
3216 // in the "Users" setvector. Scan over the deleted node pointers and
3217 // remove them from the setvector.
3218 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
3219 Users.remove(Deleted[i]);
3220 }
3221 break; // Exit the operand scanning loop.
3222 }
3223 }
3224 }
3225}
3226
Chris Lattner7b2880c2005-08-24 22:44:39 +00003227
Evan Chenge6f35d82006-08-01 08:20:41 +00003228/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3229/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003230unsigned SelectionDAG::AssignNodeIds() {
3231 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003232 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3233 SDNode *N = I;
3234 N->setNodeId(Id++);
3235 }
3236 return Id;
3237}
3238
Evan Chenge6f35d82006-08-01 08:20:41 +00003239/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003240/// based on their topological order. It returns the maximum id and a vector
3241/// of the SDNodes* in assigned order by reference.
3242unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003243 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003244 std::vector<unsigned> InDegree(DAGSize);
3245 std::vector<SDNode*> Sources;
3246
3247 // Use a two pass approach to avoid using a std::map which is slow.
3248 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003249 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3250 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003251 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003252 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003253 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003254 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003255 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003256 }
3257
Evan Cheng99157a02006-08-07 22:13:29 +00003258 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003259 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003260 SDNode *N = Sources.back();
3261 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003262 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003263 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3264 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003265 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003266 if (Degree == 0)
3267 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003268 }
3269 }
3270
Evan Chengc384d6c2006-08-02 22:00:34 +00003271 // Second pass, assign the actual topological order as node ids.
3272 Id = 0;
3273 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3274 TI != TE; ++TI)
3275 (*TI)->setNodeId(Id++);
3276
3277 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003278}
3279
3280
Evan Cheng091cba12006-07-27 06:39:06 +00003281
Jim Laskey58b968b2005-08-17 20:08:02 +00003282//===----------------------------------------------------------------------===//
3283// SDNode Class
3284//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003285
Chris Lattner917d2c92006-07-19 00:00:37 +00003286// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003287void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003288void UnarySDNode::ANCHOR() {}
3289void BinarySDNode::ANCHOR() {}
3290void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003291void HandleSDNode::ANCHOR() {}
3292void StringSDNode::ANCHOR() {}
3293void ConstantSDNode::ANCHOR() {}
3294void ConstantFPSDNode::ANCHOR() {}
3295void GlobalAddressSDNode::ANCHOR() {}
3296void FrameIndexSDNode::ANCHOR() {}
3297void JumpTableSDNode::ANCHOR() {}
3298void ConstantPoolSDNode::ANCHOR() {}
3299void BasicBlockSDNode::ANCHOR() {}
3300void SrcValueSDNode::ANCHOR() {}
3301void RegisterSDNode::ANCHOR() {}
3302void ExternalSymbolSDNode::ANCHOR() {}
3303void CondCodeSDNode::ANCHOR() {}
3304void VTSDNode::ANCHOR() {}
3305void LoadSDNode::ANCHOR() {}
3306void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003307
Chris Lattner48b85922007-02-04 02:41:42 +00003308HandleSDNode::~HandleSDNode() {
3309 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003310 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003311}
3312
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003313GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3314 MVT::ValueType VT, int o)
3315 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003316 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003317 // Thread Local
3318 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3319 // Non Thread Local
3320 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3321 getSDVTList(VT)), Offset(o) {
3322 TheGlobal = const_cast<GlobalValue*>(GA);
3323}
Chris Lattner48b85922007-02-04 02:41:42 +00003324
Jim Laskey583bd472006-10-27 23:46:08 +00003325/// Profile - Gather unique data for the node.
3326///
3327void SDNode::Profile(FoldingSetNodeID &ID) {
3328 AddNodeIDNode(ID, this);
3329}
3330
Chris Lattnera3255112005-11-08 23:30:28 +00003331/// getValueTypeList - Return a pointer to the specified value type.
3332///
3333MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3334 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3335 VTs[VT] = VT;
3336 return &VTs[VT];
3337}
Chris Lattnera5682852006-08-07 23:03:03 +00003338
Chris Lattner5c884562005-01-12 18:37:47 +00003339/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3340/// indicated value. This method ignores uses of other values defined by this
3341/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003342bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003343 assert(Value < getNumValues() && "Bad value!");
3344
3345 // If there is only one value, this is easy.
3346 if (getNumValues() == 1)
3347 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003348 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003349
Evan Cheng4ee62112006-02-05 06:29:23 +00003350 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003351
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003352 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003353
Chris Lattnerf83482d2006-08-16 20:59:32 +00003354 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003355 SDNode *User = *UI;
3356 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003357 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003358 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3359 if (User->getOperand(i) == TheValue) {
3360 if (NUses == 0)
3361 return false; // too many uses
3362 --NUses;
3363 }
3364 }
3365
3366 // Found exactly the right number of uses?
3367 return NUses == 0;
3368}
3369
3370
Evan Cheng33d55952007-08-02 05:29:38 +00003371/// hasAnyUseOfValue - Return true if there are any use of the indicated
3372/// value. This method ignores uses of other values defined by this operation.
3373bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3374 assert(Value < getNumValues() && "Bad value!");
3375
3376 if (use_size() == 0) return false;
3377
3378 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3379
3380 SmallPtrSet<SDNode*, 32> UsersHandled;
3381
3382 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3383 SDNode *User = *UI;
3384 if (User->getNumOperands() == 1 ||
3385 UsersHandled.insert(User)) // First time we've seen this?
3386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3387 if (User->getOperand(i) == TheValue) {
3388 return true;
3389 }
3390 }
3391
3392 return false;
3393}
3394
3395
Evan Chenge6e97e62006-11-03 07:31:32 +00003396/// isOnlyUse - Return true if this node is the only use of N.
3397///
Evan Cheng4ee62112006-02-05 06:29:23 +00003398bool SDNode::isOnlyUse(SDNode *N) const {
3399 bool Seen = false;
3400 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3401 SDNode *User = *I;
3402 if (User == this)
3403 Seen = true;
3404 else
3405 return false;
3406 }
3407
3408 return Seen;
3409}
3410
Evan Chenge6e97e62006-11-03 07:31:32 +00003411/// isOperand - Return true if this node is an operand of N.
3412///
Evan Chengbfa284f2006-03-03 06:42:32 +00003413bool SDOperand::isOperand(SDNode *N) const {
3414 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3415 if (*this == N->getOperand(i))
3416 return true;
3417 return false;
3418}
3419
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003420bool SDNode::isOperand(SDNode *N) const {
3421 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3422 if (this == N->OperandList[i].Val)
3423 return true;
3424 return false;
3425}
Evan Cheng4ee62112006-02-05 06:29:23 +00003426
Evan Chengc5fc57d2006-11-03 03:05:24 +00003427static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003428 SmallPtrSet<SDNode *, 32> &Visited) {
3429 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003430 return;
3431
3432 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3433 SDNode *Op = N->getOperand(i).Val;
3434 if (Op == P) {
3435 found = true;
3436 return;
3437 }
3438 findPredecessor(Op, P, found, Visited);
3439 }
3440}
3441
Evan Chenge6e97e62006-11-03 07:31:32 +00003442/// isPredecessor - Return true if this node is a predecessor of N. This node
3443/// is either an operand of N or it can be reached by recursively traversing
3444/// up the operands.
3445/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003446bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003447 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003448 bool found = false;
3449 findPredecessor(N, this, found, Visited);
3450 return found;
3451}
3452
Evan Chengc5484282006-10-04 00:56:09 +00003453uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3454 assert(Num < NumOperands && "Invalid child # of SDNode!");
3455 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3456}
3457
Reid Spencer577cc322007-04-01 07:32:19 +00003458std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003459 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003460 default:
3461 if (getOpcode() < ISD::BUILTIN_OP_END)
3462 return "<<Unknown DAG Node>>";
3463 else {
Evan Cheng72261582005-12-20 06:22:03 +00003464 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003465 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003466 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3467 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003468
Evan Cheng72261582005-12-20 06:22:03 +00003469 TargetLowering &TLI = G->getTargetLoweringInfo();
3470 const char *Name =
3471 TLI.getTargetNodeName(getOpcode());
3472 if (Name) return Name;
3473 }
3474
3475 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003476 }
3477
Andrew Lenharth95762122005-03-31 21:24:06 +00003478 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003479 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003480 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003481 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003482 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003483 case ISD::AssertSext: return "AssertSext";
3484 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003485
3486 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003487 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003488 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003489 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003490
3491 case ISD::Constant: return "Constant";
3492 case ISD::ConstantFP: return "ConstantFP";
3493 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003494 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003495 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003496 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003497 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003498 case ISD::RETURNADDR: return "RETURNADDR";
3499 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003500 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003501 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3502 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003503 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003504 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003505 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003506 case ISD::INTRINSIC_WO_CHAIN: {
3507 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3508 return Intrinsic::getName((Intrinsic::ID)IID);
3509 }
3510 case ISD::INTRINSIC_VOID:
3511 case ISD::INTRINSIC_W_CHAIN: {
3512 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003513 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003514 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003515
Chris Lattnerb2827b02006-03-19 00:52:58 +00003516 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003517 case ISD::TargetConstant: return "TargetConstant";
3518 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003519 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003520 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003521 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003522 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003523 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003524 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003525
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003526 case ISD::CopyToReg: return "CopyToReg";
3527 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003528 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003529 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003530 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003531 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003532 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003533 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003534 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003535
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003536 // Unary operators
3537 case ISD::FABS: return "fabs";
3538 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003539 case ISD::FSQRT: return "fsqrt";
3540 case ISD::FSIN: return "fsin";
3541 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003542 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003543
3544 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003545 case ISD::ADD: return "add";
3546 case ISD::SUB: return "sub";
3547 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003548 case ISD::MULHU: return "mulhu";
3549 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003550 case ISD::SDIV: return "sdiv";
3551 case ISD::UDIV: return "udiv";
3552 case ISD::SREM: return "srem";
3553 case ISD::UREM: return "urem";
3554 case ISD::AND: return "and";
3555 case ISD::OR: return "or";
3556 case ISD::XOR: return "xor";
3557 case ISD::SHL: return "shl";
3558 case ISD::SRA: return "sra";
3559 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003560 case ISD::ROTL: return "rotl";
3561 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003562 case ISD::FADD: return "fadd";
3563 case ISD::FSUB: return "fsub";
3564 case ISD::FMUL: return "fmul";
3565 case ISD::FDIV: return "fdiv";
3566 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003567 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003568
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003569 case ISD::SETCC: return "setcc";
3570 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003571 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003572 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003573 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003574 case ISD::CONCAT_VECTORS: return "concat_vectors";
3575 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003576 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003577 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003578 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003579 case ISD::ADDC: return "addc";
3580 case ISD::ADDE: return "adde";
3581 case ISD::SUBC: return "subc";
3582 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003583 case ISD::SHL_PARTS: return "shl_parts";
3584 case ISD::SRA_PARTS: return "sra_parts";
3585 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003586
3587 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3588 case ISD::INSERT_SUBREG: return "insert_subreg";
3589
Chris Lattner7f644642005-04-28 21:44:03 +00003590 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003591 case ISD::SIGN_EXTEND: return "sign_extend";
3592 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003593 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003594 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003595 case ISD::TRUNCATE: return "truncate";
3596 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00003597 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003598 case ISD::FP_EXTEND: return "fp_extend";
3599
3600 case ISD::SINT_TO_FP: return "sint_to_fp";
3601 case ISD::UINT_TO_FP: return "uint_to_fp";
3602 case ISD::FP_TO_SINT: return "fp_to_sint";
3603 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003604 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003605
3606 // Control flow instructions
3607 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003608 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003609 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003610 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003611 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003612 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003613 case ISD::CALLSEQ_START: return "callseq_start";
3614 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003615
3616 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003617 case ISD::LOAD: return "load";
3618 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003619 case ISD::VAARG: return "vaarg";
3620 case ISD::VACOPY: return "vacopy";
3621 case ISD::VAEND: return "vaend";
3622 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003623 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003624 case ISD::EXTRACT_ELEMENT: return "extract_element";
3625 case ISD::BUILD_PAIR: return "build_pair";
3626 case ISD::STACKSAVE: return "stacksave";
3627 case ISD::STACKRESTORE: return "stackrestore";
3628
3629 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003630 case ISD::MEMSET: return "memset";
3631 case ISD::MEMCPY: return "memcpy";
3632 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003633
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003634 // Bit manipulation
3635 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003636 case ISD::CTPOP: return "ctpop";
3637 case ISD::CTTZ: return "cttz";
3638 case ISD::CTLZ: return "ctlz";
3639
Chris Lattner36ce6912005-11-29 06:21:05 +00003640 // Debug info
3641 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003642 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003643
Duncan Sands36397f52007-07-27 12:58:54 +00003644 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003645 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003646
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003647 case ISD::CONDCODE:
3648 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003649 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003650 case ISD::SETOEQ: return "setoeq";
3651 case ISD::SETOGT: return "setogt";
3652 case ISD::SETOGE: return "setoge";
3653 case ISD::SETOLT: return "setolt";
3654 case ISD::SETOLE: return "setole";
3655 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003656
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003657 case ISD::SETO: return "seto";
3658 case ISD::SETUO: return "setuo";
3659 case ISD::SETUEQ: return "setue";
3660 case ISD::SETUGT: return "setugt";
3661 case ISD::SETUGE: return "setuge";
3662 case ISD::SETULT: return "setult";
3663 case ISD::SETULE: return "setule";
3664 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003665
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003666 case ISD::SETEQ: return "seteq";
3667 case ISD::SETGT: return "setgt";
3668 case ISD::SETGE: return "setge";
3669 case ISD::SETLT: return "setlt";
3670 case ISD::SETLE: return "setle";
3671 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003672 }
3673 }
3674}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003675
Evan Cheng144d8f02006-11-09 17:55:04 +00003676const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003677 switch (AM) {
3678 default:
3679 return "";
3680 case ISD::PRE_INC:
3681 return "<pre-inc>";
3682 case ISD::PRE_DEC:
3683 return "<pre-dec>";
3684 case ISD::POST_INC:
3685 return "<post-inc>";
3686 case ISD::POST_DEC:
3687 return "<post-dec>";
3688 }
3689}
3690
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003691void SDNode::dump() const { dump(0); }
3692void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003693 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003694
3695 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003696 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003697 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003698 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003699 else
Bill Wendling832171c2006-12-07 20:04:42 +00003700 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003701 }
Bill Wendling832171c2006-12-07 20:04:42 +00003702 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003703
Bill Wendling832171c2006-12-07 20:04:42 +00003704 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003705 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003706 if (i) cerr << ", ";
3707 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003708 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003709 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003710 }
3711
3712 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003713 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003714 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003715 cerr << "<" << (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle ?
3716 CSDN->getValueAPF().convertToFloat() :
3717 CSDN->getValueAPF().convertToDouble()) << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003718 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003719 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003720 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003721 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003722 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003723 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003724 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003725 else
Bill Wendling832171c2006-12-07 20:04:42 +00003726 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003727 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003728 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003729 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003730 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003731 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003732 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003733 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003734 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003735 else
Bill Wendling832171c2006-12-07 20:04:42 +00003736 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003737 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003738 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003739 else
Bill Wendling832171c2006-12-07 20:04:42 +00003740 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003741 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003742 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003743 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3744 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003745 cerr << LBB->getName() << " ";
3746 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003747 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003748 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003749 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003750 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003751 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003752 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003753 } else if (const ExternalSymbolSDNode *ES =
3754 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003755 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003756 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3757 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003758 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003759 else
Bill Wendling832171c2006-12-07 20:04:42 +00003760 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003761 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003762 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003763 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3764 bool doExt = true;
3765 switch (LD->getExtensionType()) {
3766 default: doExt = false; break;
3767 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003768 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003769 break;
3770 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003771 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003772 break;
3773 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003774 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003775 break;
3776 }
3777 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003778 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003779
Evan Cheng144d8f02006-11-09 17:55:04 +00003780 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003781 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003782 cerr << " " << AM;
Evan Cheng2caccca2006-10-17 21:14:32 +00003783 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3784 if (ST->isTruncatingStore())
Bill Wendling832171c2006-12-07 20:04:42 +00003785 cerr << " <trunc "
3786 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00003787
Evan Cheng144d8f02006-11-09 17:55:04 +00003788 const char *AM = getIndexedModeName(ST->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003789 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003790 cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003791 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003792}
3793
Chris Lattnerde202b32005-11-09 23:47:37 +00003794static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003795 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3796 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003797 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003798 else
Bill Wendling832171c2006-12-07 20:04:42 +00003799 cerr << "\n" << std::string(indent+2, ' ')
3800 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003801
Chris Lattnerea946cd2005-01-09 20:38:33 +00003802
Bill Wendling832171c2006-12-07 20:04:42 +00003803 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003804 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003805}
3806
Chris Lattnerc3aae252005-01-07 07:46:32 +00003807void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003808 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003809 std::vector<const SDNode*> Nodes;
3810 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3811 I != E; ++I)
3812 Nodes.push_back(I);
3813
Chris Lattner49d24712005-01-09 20:26:36 +00003814 std::sort(Nodes.begin(), Nodes.end());
3815
3816 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003817 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003818 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003819 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003820
Jim Laskey26f7fa72006-10-17 19:33:52 +00003821 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003822
Bill Wendling832171c2006-12-07 20:04:42 +00003823 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003824}
3825
Evan Chengd6594ae2006-09-12 21:00:35 +00003826const Type *ConstantPoolSDNode::getType() const {
3827 if (isMachineConstantPoolEntry())
3828 return Val.MachineCPVal->getType();
3829 return Val.ConstVal->getType();
3830}