blob: 9e4d72418eeb0a6b9e7e6820ab009f00284299a3 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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 Lattner37ce9df2007-10-15 17:47:20 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000025#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000026#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000027#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000030#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000031#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000032#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000033#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000034#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattner0b3e5252006-08-15 19:11:05 +000039/// makeVTList - Return an instance of the SDVTList struct initialized with the
40/// specified members.
41static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
42 SDVTList Res = {VTs, NumVTs};
43 return Res;
44}
45
Jim Laskey58b968b2005-08-17 20:08:02 +000046//===----------------------------------------------------------------------===//
47// ConstantFPSDNode Class
48//===----------------------------------------------------------------------===//
49
50/// isExactlyValue - We don't rely on operator== working on double values, as
51/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
52/// As such, this method can be used to do an exact bit-for-bit comparison of
53/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000054bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000055 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000056}
57
Dale Johannesenf04afdb2007-08-30 00:23:21 +000058bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
59 const APFloat& Val) {
60 // convert modifies in place, so make a copy.
61 APFloat Val2 = APFloat(Val);
62 switch (VT) {
63 default:
64 return false; // These can't be represented as floating point!
65
66 // FIXME rounding mode needs to be more flexible
67 case MVT::f32:
68 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
69 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
70 APFloat::opOK;
71 case MVT::f64:
72 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
73 &Val2.getSemantics() == &APFloat::IEEEdouble ||
74 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
75 APFloat::opOK;
76 // TODO: Figure out how to test if we can use a shorter type instead!
77 case MVT::f80:
78 case MVT::f128:
79 case MVT::ppcf128:
80 return true;
81 }
82}
83
Jim Laskey58b968b2005-08-17 20:08:02 +000084//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000085// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000086//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000087
Evan Chenga8df1662006-03-27 06:58:47 +000088/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000089/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000090bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000091 // Look through a bit convert.
92 if (N->getOpcode() == ISD::BIT_CONVERT)
93 N = N->getOperand(0).Val;
94
Evan Chenga8df1662006-03-27 06:58:47 +000095 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000096
97 unsigned i = 0, e = N->getNumOperands();
98
99 // Skip over all of the undef values.
100 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
101 ++i;
102
103 // Do not accept an all-undef vector.
104 if (i == e) return false;
105
106 // Do not accept build_vectors that aren't all constants or which have non-~0
107 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000108 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000109 if (isa<ConstantSDNode>(NotZero)) {
110 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
111 return false;
112 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000113 MVT::ValueType VT = NotZero.getValueType();
114 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000115 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
116 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000117 return false;
118 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000119 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
120 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000121 (uint32_t)-1)
122 return false;
123 }
Evan Chenga8df1662006-03-27 06:58:47 +0000124 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000125 return false;
126
127 // Okay, we have at least one ~0 value, check to see if the rest match or are
128 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000129 for (++i; i != e; ++i)
130 if (N->getOperand(i) != NotZero &&
131 N->getOperand(i).getOpcode() != ISD::UNDEF)
132 return false;
133 return true;
134}
135
136
Evan Cheng4a147842006-03-26 09:50:58 +0000137/// isBuildVectorAllZeros - Return true if the specified node is a
138/// BUILD_VECTOR where all of the elements are 0 or undef.
139bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000140 // Look through a bit convert.
141 if (N->getOpcode() == ISD::BIT_CONVERT)
142 N = N->getOperand(0).Val;
143
Evan Cheng4a147842006-03-26 09:50:58 +0000144 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000145
146 unsigned i = 0, e = N->getNumOperands();
147
148 // Skip over all of the undef values.
149 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
150 ++i;
151
152 // Do not accept an all-undef vector.
153 if (i == e) return false;
154
155 // Do not accept build_vectors that aren't all constants or which have non-~0
156 // elements.
157 SDOperand Zero = N->getOperand(i);
158 if (isa<ConstantSDNode>(Zero)) {
159 if (!cast<ConstantSDNode>(Zero)->isNullValue())
160 return false;
161 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000162 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000163 return false;
164 } else
165 return false;
166
167 // Okay, we have at least one ~0 value, check to see if the rest match or are
168 // undefs.
169 for (++i; i != e; ++i)
170 if (N->getOperand(i) != Zero &&
171 N->getOperand(i).getOpcode() != ISD::UNDEF)
172 return false;
173 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000174}
175
Evan Chengbb81d972008-01-31 09:59:15 +0000176/// isDebugLabel - Return true if the specified node represents a debug
177/// label (i.e. ISD::LABEL or TargetInstrInfo::LANEL node and third operand
178/// is 0).
179bool ISD::isDebugLabel(const SDNode *N) {
180 SDOperand Zero;
181 if (N->getOpcode() == ISD::LABEL)
182 Zero = N->getOperand(2);
183 else if (N->isTargetOpcode() &&
184 N->getTargetOpcode() == TargetInstrInfo::LABEL)
185 // Chain moved to last operand.
186 Zero = N->getOperand(1);
187 else
188 return false;
189 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
190}
191
Chris Lattnerc3aae252005-01-07 07:46:32 +0000192/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
193/// when given the operation for (X op Y).
194ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
195 // To perform this operation, we just need to swap the L and G bits of the
196 // operation.
197 unsigned OldL = (Operation >> 2) & 1;
198 unsigned OldG = (Operation >> 1) & 1;
199 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
200 (OldL << 1) | // New G bit
201 (OldG << 2)); // New L bit.
202}
203
204/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
205/// 'op' is a valid SetCC operation.
206ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
207 unsigned Operation = Op;
208 if (isInteger)
209 Operation ^= 7; // Flip L, G, E bits, but not U.
210 else
211 Operation ^= 15; // Flip all of the condition bits.
212 if (Operation > ISD::SETTRUE2)
213 Operation &= ~8; // Don't let N and U bits get set.
214 return ISD::CondCode(Operation);
215}
216
217
218/// isSignedOp - For an integer comparison, return 1 if the comparison is a
219/// signed operation and 2 if the result is an unsigned comparison. Return zero
220/// if the operation does not depend on the sign of the input (setne and seteq).
221static int isSignedOp(ISD::CondCode Opcode) {
222 switch (Opcode) {
223 default: assert(0 && "Illegal integer setcc operation!");
224 case ISD::SETEQ:
225 case ISD::SETNE: return 0;
226 case ISD::SETLT:
227 case ISD::SETLE:
228 case ISD::SETGT:
229 case ISD::SETGE: return 1;
230 case ISD::SETULT:
231 case ISD::SETULE:
232 case ISD::SETUGT:
233 case ISD::SETUGE: return 2;
234 }
235}
236
237/// getSetCCOrOperation - Return the result of a logical OR between different
238/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
239/// returns SETCC_INVALID if it is not possible to represent the resultant
240/// comparison.
241ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
242 bool isInteger) {
243 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
244 // Cannot fold a signed integer setcc with an unsigned integer setcc.
245 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000246
Chris Lattnerc3aae252005-01-07 07:46:32 +0000247 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000248
Chris Lattnerc3aae252005-01-07 07:46:32 +0000249 // If the N and U bits get set then the resultant comparison DOES suddenly
250 // care about orderedness, and is true when ordered.
251 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000252 Op &= ~16; // Clear the U bit if the N bit is set.
253
254 // Canonicalize illegal integer setcc's.
255 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
256 Op = ISD::SETNE;
257
Chris Lattnerc3aae252005-01-07 07:46:32 +0000258 return ISD::CondCode(Op);
259}
260
261/// getSetCCAndOperation - Return the result of a logical AND between different
262/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
263/// function returns zero if it is not possible to represent the resultant
264/// comparison.
265ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
266 bool isInteger) {
267 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
268 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000269 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000270
271 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000272 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
273
274 // Canonicalize illegal integer setcc's.
275 if (isInteger) {
276 switch (Result) {
277 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000278 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
279 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
280 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
281 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000282 }
283 }
284
285 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000286}
287
Chris Lattnerb48da392005-01-23 04:39:44 +0000288const TargetMachine &SelectionDAG::getTarget() const {
289 return TLI.getTargetMachine();
290}
291
Jim Laskey58b968b2005-08-17 20:08:02 +0000292//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000293// SDNode Profile Support
294//===----------------------------------------------------------------------===//
295
Jim Laskeydef69b92006-10-27 23:52:51 +0000296/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
297///
Jim Laskey583bd472006-10-27 23:46:08 +0000298static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
299 ID.AddInteger(OpC);
300}
301
302/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
303/// solely with their pointer.
304void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
305 ID.AddPointer(VTList.VTs);
306}
307
Jim Laskeydef69b92006-10-27 23:52:51 +0000308/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
309///
Jim Laskey583bd472006-10-27 23:46:08 +0000310static void AddNodeIDOperands(FoldingSetNodeID &ID,
311 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000312 for (; NumOps; --NumOps, ++Ops) {
313 ID.AddPointer(Ops->Val);
314 ID.AddInteger(Ops->ResNo);
315 }
Jim Laskey583bd472006-10-27 23:46:08 +0000316}
317
Jim Laskey583bd472006-10-27 23:46:08 +0000318static void AddNodeIDNode(FoldingSetNodeID &ID,
319 unsigned short OpC, SDVTList VTList,
320 const SDOperand *OpList, unsigned N) {
321 AddNodeIDOpcode(ID, OpC);
322 AddNodeIDValueTypes(ID, VTList);
323 AddNodeIDOperands(ID, OpList, N);
324}
325
Jim Laskeydef69b92006-10-27 23:52:51 +0000326/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
327/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000328static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
329 AddNodeIDOpcode(ID, N->getOpcode());
330 // Add the return value info.
331 AddNodeIDValueTypes(ID, N->getVTList());
332 // Add the operand info.
333 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
334
335 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000336 switch (N->getOpcode()) {
337 default: break; // Normal nodes don't need extra info.
338 case ISD::TargetConstant:
339 case ISD::Constant:
340 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
341 break;
342 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000343 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000344 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000345 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000346 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000347 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000348 case ISD::GlobalAddress:
349 case ISD::TargetGlobalTLSAddress:
350 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000351 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
352 ID.AddPointer(GA->getGlobal());
353 ID.AddInteger(GA->getOffset());
354 break;
355 }
356 case ISD::BasicBlock:
357 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
358 break;
359 case ISD::Register:
360 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
361 break;
Evan Cheng334dc1f2008-01-31 21:00:00 +0000362 case ISD::SRCVALUE: {
363 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
364 ID.AddPointer(SV->getValue());
365 ID.AddInteger(SV->getOffset());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 break;
367 }
368 case ISD::FrameIndex:
369 case ISD::TargetFrameIndex:
370 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
371 break;
372 case ISD::JumpTable:
373 case ISD::TargetJumpTable:
374 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
375 break;
376 case ISD::ConstantPool:
377 case ISD::TargetConstantPool: {
378 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
379 ID.AddInteger(CP->getAlignment());
380 ID.AddInteger(CP->getOffset());
381 if (CP->isMachineConstantPoolEntry())
382 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
383 else
384 ID.AddPointer(CP->getConstVal());
385 break;
386 }
387 case ISD::LOAD: {
388 LoadSDNode *LD = cast<LoadSDNode>(N);
389 ID.AddInteger(LD->getAddressingMode());
390 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000391 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000392 ID.AddInteger(LD->getAlignment());
393 ID.AddInteger(LD->isVolatile());
394 break;
395 }
396 case ISD::STORE: {
397 StoreSDNode *ST = cast<StoreSDNode>(N);
398 ID.AddInteger(ST->getAddressingMode());
399 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000400 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000401 ID.AddInteger(ST->getAlignment());
402 ID.AddInteger(ST->isVolatile());
403 break;
404 }
Jim Laskey583bd472006-10-27 23:46:08 +0000405 }
406}
407
408//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000409// SelectionDAG Class
410//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000411
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000412/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000413/// SelectionDAG.
414void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000415 // Create a dummy node (which is not added to allnodes), that adds a reference
416 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000417 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000418
Chris Lattner190a4182006-08-04 17:45:20 +0000419 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000420
Chris Lattner190a4182006-08-04 17:45:20 +0000421 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000422 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000423 if (I->use_empty())
424 DeadNodes.push_back(I);
425
426 // Process the worklist, deleting the nodes and adding their uses to the
427 // worklist.
428 while (!DeadNodes.empty()) {
429 SDNode *N = DeadNodes.back();
430 DeadNodes.pop_back();
431
432 // Take the node out of the appropriate CSE map.
433 RemoveNodeFromCSEMaps(N);
434
435 // Next, brutally remove the operand list. This is safe to do, as there are
436 // no cycles in the graph.
437 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
438 SDNode *Operand = I->Val;
439 Operand->removeUser(N);
440
441 // Now that we removed this operand, see if there are no uses of it left.
442 if (Operand->use_empty())
443 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000444 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000445 if (N->OperandsNeedDelete)
446 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000447 N->OperandList = 0;
448 N->NumOperands = 0;
449
450 // Finally, remove N itself.
451 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000452 }
453
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000454 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000455 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000456}
457
Evan Cheng130a6472006-10-12 20:34:05 +0000458void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
459 SmallVector<SDNode*, 16> DeadNodes;
460 DeadNodes.push_back(N);
461
462 // Process the worklist, deleting the nodes and adding their uses to the
463 // worklist.
464 while (!DeadNodes.empty()) {
465 SDNode *N = DeadNodes.back();
466 DeadNodes.pop_back();
467
468 // Take the node out of the appropriate CSE map.
469 RemoveNodeFromCSEMaps(N);
470
471 // Next, brutally remove the operand list. This is safe to do, as there are
472 // no cycles in the graph.
473 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
474 SDNode *Operand = I->Val;
475 Operand->removeUser(N);
476
477 // Now that we removed this operand, see if there are no uses of it left.
478 if (Operand->use_empty())
479 DeadNodes.push_back(Operand);
480 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000481 if (N->OperandsNeedDelete)
482 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000483 N->OperandList = 0;
484 N->NumOperands = 0;
485
486 // Finally, remove N itself.
487 Deleted.push_back(N);
488 AllNodes.erase(N);
489 }
490}
491
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000492void SelectionDAG::DeleteNode(SDNode *N) {
493 assert(N->use_empty() && "Cannot delete a node that is not dead!");
494
495 // First take this out of the appropriate CSE map.
496 RemoveNodeFromCSEMaps(N);
497
Chris Lattner1e111c72005-09-07 05:37:01 +0000498 // Finally, remove uses due to operands of this node, remove from the
499 // AllNodes list, and delete the node.
500 DeleteNodeNotInCSEMaps(N);
501}
502
503void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
504
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000505 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000506 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000507
508 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000509 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
510 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000511 if (N->OperandsNeedDelete)
512 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000513 N->OperandList = 0;
514 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000515
516 delete N;
517}
518
Chris Lattner149c58c2005-08-16 18:17:10 +0000519/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
520/// correspond to it. This is useful when we're about to delete or repurpose
521/// the node. We don't want future request for structurally identical nodes
522/// to return N anymore.
523void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000524 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000525 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000526 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000527 case ISD::STRING:
528 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
529 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000530 case ISD::CONDCODE:
531 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
532 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000533 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000534 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
535 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000536 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000537 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000538 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000539 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000540 Erased =
541 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000542 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000543 case ISD::VALUETYPE: {
544 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
545 if (MVT::isExtendedVT(VT)) {
546 Erased = ExtendedValueTypeNodes.erase(VT);
547 } else {
548 Erased = ValueTypeNodes[VT] != 0;
549 ValueTypeNodes[VT] = 0;
550 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000551 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000552 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000553 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000554 // Remove it from the CSE Map.
555 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000556 break;
557 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000558#ifndef NDEBUG
559 // Verify that the node was actually in one of the CSE maps, unless it has a
560 // flag result (which cannot be CSE'd) or is one of the special cases that are
561 // not subject to CSE.
562 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000563 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000564 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000565 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000566 assert(0 && "Node is not in map!");
567 }
568#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000569}
570
Chris Lattner8b8749f2005-08-17 19:00:20 +0000571/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
572/// has been taken out and modified in some way. If the specified node already
573/// exists in the CSE maps, do not modify the maps, but return the existing node
574/// instead. If it doesn't exist, add it and return null.
575///
576SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
577 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000578 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000579 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000580
Chris Lattner9f8cc692005-12-19 22:21:21 +0000581 // Check that remaining values produced are not flags.
582 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
583 if (N->getValueType(i) == MVT::Flag)
584 return 0; // Never CSE anything that produces a flag.
585
Chris Lattnera5682852006-08-07 23:03:03 +0000586 SDNode *New = CSEMap.GetOrInsertNode(N);
587 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000588 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000589}
590
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000591/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
592/// were replaced with those specified. If this node is never memoized,
593/// return null, otherwise return a pointer to the slot it would take. If a
594/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000595SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
596 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000597 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000598 return 0; // Never add these nodes.
599
600 // Check that remaining values produced are not flags.
601 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
602 if (N->getValueType(i) == MVT::Flag)
603 return 0; // Never CSE anything that produces a flag.
604
Chris Lattner63e3f142007-02-04 07:28:00 +0000605 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000606 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000607 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000608 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000609}
610
611/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
612/// were replaced with those specified. If this node is never memoized,
613/// return null, otherwise return a pointer to the slot it would take. If a
614/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000615SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
616 SDOperand Op1, SDOperand Op2,
617 void *&InsertPos) {
618 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
619 return 0; // Never add these nodes.
620
621 // Check that remaining values produced are not flags.
622 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
623 if (N->getValueType(i) == MVT::Flag)
624 return 0; // Never CSE anything that produces a flag.
625
Chris Lattner63e3f142007-02-04 07:28:00 +0000626 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000627 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000628 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000629 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
630}
631
632
633/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
634/// were replaced with those specified. If this node is never memoized,
635/// return null, otherwise return a pointer to the slot it would take. If a
636/// node already exists with these operands, the slot will be non-null.
637SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000638 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000639 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000640 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000641 return 0; // Never add these nodes.
642
643 // Check that remaining values produced are not flags.
644 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
645 if (N->getValueType(i) == MVT::Flag)
646 return 0; // Never CSE anything that produces a flag.
647
Jim Laskey583bd472006-10-27 23:46:08 +0000648 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000649 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000650
Evan Cheng9629aba2006-10-11 01:47:58 +0000651 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
652 ID.AddInteger(LD->getAddressingMode());
653 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000654 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000655 ID.AddInteger(LD->getAlignment());
656 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000657 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
658 ID.AddInteger(ST->getAddressingMode());
659 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000660 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000661 ID.AddInteger(ST->getAlignment());
662 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000663 }
Jim Laskey583bd472006-10-27 23:46:08 +0000664
Chris Lattnera5682852006-08-07 23:03:03 +0000665 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000666}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000667
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000668
Chris Lattner78ec3112003-08-11 14:57:33 +0000669SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000670 while (!AllNodes.empty()) {
671 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000672 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000673 if (N->OperandsNeedDelete)
674 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000675 N->OperandList = 0;
676 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000677 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000678 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000679}
680
Chris Lattner0f2287b2005-04-13 02:38:18 +0000681SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000682 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000683 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000684 return getNode(ISD::AND, Op.getValueType(), Op,
685 getConstant(Imm, Op.getValueType()));
686}
687
Chris Lattner36ce6912005-11-29 06:21:05 +0000688SDOperand SelectionDAG::getString(const std::string &Val) {
689 StringSDNode *&N = StringNodes[Val];
690 if (!N) {
691 N = new StringSDNode(Val);
692 AllNodes.push_back(N);
693 }
694 return SDOperand(N, 0);
695}
696
Chris Lattner61b09412006-08-11 21:01:22 +0000697SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000698 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000699
700 MVT::ValueType EltVT =
701 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000702
Chris Lattner61b09412006-08-11 21:01:22 +0000703 // Mask out any bits that are not valid for this constant.
Dan Gohman89081322007-12-12 22:21:26 +0000704 Val &= MVT::getIntVTBitMask(EltVT);
Chris Lattner61b09412006-08-11 21:01:22 +0000705
706 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000707 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000708 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000709 ID.AddInteger(Val);
710 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000711 SDNode *N = NULL;
712 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
713 if (!MVT::isVector(VT))
714 return SDOperand(N, 0);
715 if (!N) {
716 N = new ConstantSDNode(isT, Val, EltVT);
717 CSEMap.InsertNode(N, IP);
718 AllNodes.push_back(N);
719 }
720
721 SDOperand Result(N, 0);
722 if (MVT::isVector(VT)) {
723 SmallVector<SDOperand, 8> Ops;
724 Ops.assign(MVT::getVectorNumElements(VT), Result);
725 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
726 }
727 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000728}
729
Chris Lattner0bd48932008-01-17 07:00:52 +0000730SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
731 return getConstant(Val, TLI.getPointerTy(), isTarget);
732}
733
734
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000735SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000736 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000737 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000738
Dan Gohman7f321562007-06-25 16:23:39 +0000739 MVT::ValueType EltVT =
740 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000741
Chris Lattnerd8658612005-02-17 20:17:32 +0000742 // Do the map lookup using the actual bit pattern for the floating point
743 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
744 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000745 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000746 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000747 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000748 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000749 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000750 SDNode *N = NULL;
751 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
752 if (!MVT::isVector(VT))
753 return SDOperand(N, 0);
754 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000755 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000756 CSEMap.InsertNode(N, IP);
757 AllNodes.push_back(N);
758 }
759
Dan Gohman7f321562007-06-25 16:23:39 +0000760 SDOperand Result(N, 0);
761 if (MVT::isVector(VT)) {
762 SmallVector<SDOperand, 8> Ops;
763 Ops.assign(MVT::getVectorNumElements(VT), Result);
764 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
765 }
766 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000767}
768
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000769SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
770 bool isTarget) {
771 MVT::ValueType EltVT =
772 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
773 if (EltVT==MVT::f32)
774 return getConstantFP(APFloat((float)Val), VT, isTarget);
775 else
776 return getConstantFP(APFloat(Val), VT, isTarget);
777}
778
Chris Lattnerc3aae252005-01-07 07:46:32 +0000779SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000780 MVT::ValueType VT, int Offset,
781 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000782 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
783 unsigned Opc;
784 if (GVar && GVar->isThreadLocal())
785 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
786 else
787 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000788 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000789 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000790 ID.AddPointer(GV);
791 ID.AddInteger(Offset);
792 void *IP = 0;
793 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
794 return SDOperand(E, 0);
795 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
796 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000797 AllNodes.push_back(N);
798 return SDOperand(N, 0);
799}
800
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000801SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
802 bool isTarget) {
803 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000804 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000805 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000806 ID.AddInteger(FI);
807 void *IP = 0;
808 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
809 return SDOperand(E, 0);
810 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
811 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000812 AllNodes.push_back(N);
813 return SDOperand(N, 0);
814}
815
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000816SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
817 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000818 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000819 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000820 ID.AddInteger(JTI);
821 void *IP = 0;
822 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
823 return SDOperand(E, 0);
824 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
825 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000826 AllNodes.push_back(N);
827 return SDOperand(N, 0);
828}
829
Evan Chengb8973bd2006-01-31 22:23:14 +0000830SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000831 unsigned Alignment, int Offset,
832 bool isTarget) {
833 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000834 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000835 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000836 ID.AddInteger(Alignment);
837 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000838 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000839 void *IP = 0;
840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
841 return SDOperand(E, 0);
842 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
843 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000844 AllNodes.push_back(N);
845 return SDOperand(N, 0);
846}
847
Chris Lattnerc3aae252005-01-07 07:46:32 +0000848
Evan Chengd6594ae2006-09-12 21:00:35 +0000849SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
850 MVT::ValueType VT,
851 unsigned Alignment, int Offset,
852 bool isTarget) {
853 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000854 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000855 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000856 ID.AddInteger(Alignment);
857 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000858 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000859 void *IP = 0;
860 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
861 return SDOperand(E, 0);
862 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
863 CSEMap.InsertNode(N, IP);
864 AllNodes.push_back(N);
865 return SDOperand(N, 0);
866}
867
868
Chris Lattnerc3aae252005-01-07 07:46:32 +0000869SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000870 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000871 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000872 ID.AddPointer(MBB);
873 void *IP = 0;
874 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
875 return SDOperand(E, 0);
876 SDNode *N = new BasicBlockSDNode(MBB);
877 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000878 AllNodes.push_back(N);
879 return SDOperand(N, 0);
880}
881
Chris Lattner15e4b012005-07-10 00:07:11 +0000882SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000883 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000884 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000885
Duncan Sandsf411b832007-10-17 13:49:58 +0000886 SDNode *&N = MVT::isExtendedVT(VT) ?
887 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
888
889 if (N) return SDOperand(N, 0);
890 N = new VTSDNode(VT);
891 AllNodes.push_back(N);
892 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000893}
894
Chris Lattnerc3aae252005-01-07 07:46:32 +0000895SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
896 SDNode *&N = ExternalSymbols[Sym];
897 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000898 N = new ExternalSymbolSDNode(false, Sym, VT);
899 AllNodes.push_back(N);
900 return SDOperand(N, 0);
901}
902
Chris Lattner809ec112006-01-28 10:09:25 +0000903SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
904 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000905 SDNode *&N = TargetExternalSymbols[Sym];
906 if (N) return SDOperand(N, 0);
907 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000908 AllNodes.push_back(N);
909 return SDOperand(N, 0);
910}
911
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000912SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
913 if ((unsigned)Cond >= CondCodeNodes.size())
914 CondCodeNodes.resize(Cond+1);
915
Chris Lattner079a27a2005-08-09 20:40:02 +0000916 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000917 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000918 AllNodes.push_back(CondCodeNodes[Cond]);
919 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000920 return SDOperand(CondCodeNodes[Cond], 0);
921}
922
Chris Lattner0fdd7682005-08-30 22:38:38 +0000923SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000924 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000925 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000926 ID.AddInteger(RegNo);
927 void *IP = 0;
928 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
929 return SDOperand(E, 0);
930 SDNode *N = new RegisterSDNode(RegNo, VT);
931 CSEMap.InsertNode(N, IP);
932 AllNodes.push_back(N);
933 return SDOperand(N, 0);
934}
935
Evan Cheng334dc1f2008-01-31 21:00:00 +0000936SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Chris Lattner61b09412006-08-11 21:01:22 +0000937 assert((!V || isa<PointerType>(V->getType())) &&
938 "SrcValue is not a pointer?");
939
Jim Laskey583bd472006-10-27 23:46:08 +0000940 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000941 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000942 ID.AddPointer(V);
Evan Cheng334dc1f2008-01-31 21:00:00 +0000943 ID.AddInteger(Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000944 void *IP = 0;
945 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
946 return SDOperand(E, 0);
Evan Cheng334dc1f2008-01-31 21:00:00 +0000947 SDNode *N = new SrcValueSDNode(V, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000948 CSEMap.InsertNode(N, IP);
949 AllNodes.push_back(N);
950 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000951}
952
Chris Lattner37ce9df2007-10-15 17:47:20 +0000953/// CreateStackTemporary - Create a stack temporary, suitable for holding the
954/// specified value type.
955SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
956 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
957 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
958 const Type *Ty = MVT::getTypeForValueType(VT);
959 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
960 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
961 return getFrameIndex(FrameIdx, TLI.getPointerTy());
962}
963
964
Chris Lattner51dabfb2006-10-14 00:41:01 +0000965SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
966 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967 // These setcc operations always fold.
968 switch (Cond) {
969 default: break;
970 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000971 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000972 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000973 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000974
975 case ISD::SETOEQ:
976 case ISD::SETOGT:
977 case ISD::SETOGE:
978 case ISD::SETOLT:
979 case ISD::SETOLE:
980 case ISD::SETONE:
981 case ISD::SETO:
982 case ISD::SETUO:
983 case ISD::SETUEQ:
984 case ISD::SETUNE:
985 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
986 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000988
Chris Lattner67255a12005-04-07 18:14:58 +0000989 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
990 uint64_t C2 = N2C->getValue();
991 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
992 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000993
Chris Lattnerc3aae252005-01-07 07:46:32 +0000994 // Sign extend the operands if required
995 if (ISD::isSignedIntSetCC(Cond)) {
996 C1 = N1C->getSignExtended();
997 C2 = N2C->getSignExtended();
998 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000999
Chris Lattnerc3aae252005-01-07 07:46:32 +00001000 switch (Cond) {
1001 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001002 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1003 case ISD::SETNE: return getConstant(C1 != C2, VT);
1004 case ISD::SETULT: return getConstant(C1 < C2, VT);
1005 case ISD::SETUGT: return getConstant(C1 > C2, VT);
1006 case ISD::SETULE: return getConstant(C1 <= C2, VT);
1007 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
1008 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
1009 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
1010 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
1011 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001012 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 }
Chris Lattner67255a12005-04-07 18:14:58 +00001014 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001015 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1016 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001017 // No compile time operations on this type yet.
1018 if (N1C->getValueType(0) == MVT::ppcf128)
1019 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001020
1021 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001022 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001023 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001024 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1025 return getNode(ISD::UNDEF, VT);
1026 // fall through
1027 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1028 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1029 return getNode(ISD::UNDEF, VT);
1030 // fall through
1031 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001032 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001033 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1034 return getNode(ISD::UNDEF, VT);
1035 // fall through
1036 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1037 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1038 return getNode(ISD::UNDEF, VT);
1039 // fall through
1040 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1041 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1042 return getNode(ISD::UNDEF, VT);
1043 // fall through
1044 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001045 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001046 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1047 return getNode(ISD::UNDEF, VT);
1048 // fall through
1049 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001050 R==APFloat::cmpEqual, VT);
1051 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1052 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1053 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1054 R==APFloat::cmpEqual, VT);
1055 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1056 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1057 R==APFloat::cmpLessThan, VT);
1058 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1059 R==APFloat::cmpUnordered, VT);
1060 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1061 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062 }
1063 } else {
1064 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001065 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001067
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001068 // Could not fold it.
1069 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070}
1071
Dan Gohmanea859be2007-06-22 14:59:07 +00001072/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1073/// this predicate to simplify operations downstream. Mask is known to be zero
1074/// for bits that V cannot have.
1075bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1076 unsigned Depth) const {
1077 // The masks are not wide enough to represent this type! Should use APInt.
1078 if (Op.getValueType() == MVT::i128)
1079 return false;
1080
1081 uint64_t KnownZero, KnownOne;
1082 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1083 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1084 return (KnownZero & Mask) == Mask;
1085}
1086
1087/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1088/// known to be either zero or one and return them in the KnownZero/KnownOne
1089/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1090/// processing.
1091void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1092 uint64_t &KnownZero, uint64_t &KnownOne,
1093 unsigned Depth) const {
1094 KnownZero = KnownOne = 0; // Don't know anything.
1095 if (Depth == 6 || Mask == 0)
1096 return; // Limit search depth.
1097
1098 // The masks are not wide enough to represent this type! Should use APInt.
1099 if (Op.getValueType() == MVT::i128)
1100 return;
1101
1102 uint64_t KnownZero2, KnownOne2;
1103
1104 switch (Op.getOpcode()) {
1105 case ISD::Constant:
1106 // We know all of the bits for a constant!
1107 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1108 KnownZero = ~KnownOne & Mask;
1109 return;
1110 case ISD::AND:
1111 // If either the LHS or the RHS are Zero, the result is zero.
1112 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1113 Mask &= ~KnownZero;
1114 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1115 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1116 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1117
1118 // Output known-1 bits are only known if set in both the LHS & RHS.
1119 KnownOne &= KnownOne2;
1120 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1121 KnownZero |= KnownZero2;
1122 return;
1123 case ISD::OR:
1124 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1125 Mask &= ~KnownOne;
1126 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1127 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1128 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1129
1130 // Output known-0 bits are only known if clear in both the LHS & RHS.
1131 KnownZero &= KnownZero2;
1132 // Output known-1 are known to be set if set in either the LHS | RHS.
1133 KnownOne |= KnownOne2;
1134 return;
1135 case ISD::XOR: {
1136 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1137 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1138 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1139 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1140
1141 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1142 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1143 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1144 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1145 KnownZero = KnownZeroOut;
1146 return;
1147 }
1148 case ISD::SELECT:
1149 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1150 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1151 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1152 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1153
1154 // Only known if known in both the LHS and RHS.
1155 KnownOne &= KnownOne2;
1156 KnownZero &= KnownZero2;
1157 return;
1158 case ISD::SELECT_CC:
1159 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1160 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1161 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1162 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1163
1164 // Only known if known in both the LHS and RHS.
1165 KnownOne &= KnownOne2;
1166 KnownZero &= KnownZero2;
1167 return;
1168 case ISD::SETCC:
1169 // If we know the result of a setcc has the top bits zero, use this info.
1170 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1171 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1172 return;
1173 case ISD::SHL:
1174 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1175 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1176 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1177 KnownZero, KnownOne, Depth+1);
1178 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1179 KnownZero <<= SA->getValue();
1180 KnownOne <<= SA->getValue();
1181 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1182 }
1183 return;
1184 case ISD::SRL:
1185 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1186 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1187 MVT::ValueType VT = Op.getValueType();
1188 unsigned ShAmt = SA->getValue();
1189
1190 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1191 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1192 KnownZero, KnownOne, Depth+1);
1193 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1194 KnownZero &= TypeMask;
1195 KnownOne &= TypeMask;
1196 KnownZero >>= ShAmt;
1197 KnownOne >>= ShAmt;
1198
1199 uint64_t HighBits = (1ULL << ShAmt)-1;
1200 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1201 KnownZero |= HighBits; // High bits known zero.
1202 }
1203 return;
1204 case ISD::SRA:
1205 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1206 MVT::ValueType VT = Op.getValueType();
1207 unsigned ShAmt = SA->getValue();
1208
1209 // Compute the new bits that are at the top now.
1210 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1211
1212 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1213 // If any of the demanded bits are produced by the sign extension, we also
1214 // demand the input sign bit.
1215 uint64_t HighBits = (1ULL << ShAmt)-1;
1216 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1217 if (HighBits & Mask)
1218 InDemandedMask |= MVT::getIntVTSignBit(VT);
1219
1220 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1221 Depth+1);
1222 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1223 KnownZero &= TypeMask;
1224 KnownOne &= TypeMask;
1225 KnownZero >>= ShAmt;
1226 KnownOne >>= ShAmt;
1227
1228 // Handle the sign bits.
1229 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1230 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1231
1232 if (KnownZero & SignBit) {
1233 KnownZero |= HighBits; // New bits are known zero.
1234 } else if (KnownOne & SignBit) {
1235 KnownOne |= HighBits; // New bits are known one.
1236 }
1237 }
1238 return;
1239 case ISD::SIGN_EXTEND_INREG: {
1240 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1241
1242 // Sign extension. Compute the demanded bits in the result that are not
1243 // present in the input.
1244 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1245
1246 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1247 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1248
1249 // If the sign extended bits are demanded, we know that the sign
1250 // bit is demanded.
1251 if (NewBits)
1252 InputDemandedBits |= InSignBit;
1253
1254 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1255 KnownZero, KnownOne, Depth+1);
1256 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1257
1258 // If the sign bit of the input is known set or clear, then we know the
1259 // top bits of the result.
1260 if (KnownZero & InSignBit) { // Input sign bit known clear
1261 KnownZero |= NewBits;
1262 KnownOne &= ~NewBits;
1263 } else if (KnownOne & InSignBit) { // Input sign bit known set
1264 KnownOne |= NewBits;
1265 KnownZero &= ~NewBits;
1266 } else { // Input sign bit unknown
1267 KnownZero &= ~NewBits;
1268 KnownOne &= ~NewBits;
1269 }
1270 return;
1271 }
1272 case ISD::CTTZ:
1273 case ISD::CTLZ:
1274 case ISD::CTPOP: {
1275 MVT::ValueType VT = Op.getValueType();
1276 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1277 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1278 KnownOne = 0;
1279 return;
1280 }
1281 case ISD::LOAD: {
1282 if (ISD::isZEXTLoad(Op.Val)) {
1283 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001284 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmanea859be2007-06-22 14:59:07 +00001285 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1286 }
1287 return;
1288 }
1289 case ISD::ZERO_EXTEND: {
1290 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1291 uint64_t NewBits = (~InMask) & Mask;
1292 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1293 KnownOne, Depth+1);
1294 KnownZero |= NewBits & Mask;
1295 KnownOne &= ~NewBits;
1296 return;
1297 }
1298 case ISD::SIGN_EXTEND: {
1299 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1300 unsigned InBits = MVT::getSizeInBits(InVT);
1301 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1302 uint64_t InSignBit = 1ULL << (InBits-1);
1303 uint64_t NewBits = (~InMask) & Mask;
1304 uint64_t InDemandedBits = Mask & InMask;
1305
1306 // If any of the sign extended bits are demanded, we know that the sign
1307 // bit is demanded.
1308 if (NewBits & Mask)
1309 InDemandedBits |= InSignBit;
1310
1311 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1312 KnownOne, Depth+1);
1313 // If the sign bit is known zero or one, the top bits match.
1314 if (KnownZero & InSignBit) {
1315 KnownZero |= NewBits;
1316 KnownOne &= ~NewBits;
1317 } else if (KnownOne & InSignBit) {
1318 KnownOne |= NewBits;
1319 KnownZero &= ~NewBits;
1320 } else { // Otherwise, top bits aren't known.
1321 KnownOne &= ~NewBits;
1322 KnownZero &= ~NewBits;
1323 }
1324 return;
1325 }
1326 case ISD::ANY_EXTEND: {
1327 MVT::ValueType VT = Op.getOperand(0).getValueType();
1328 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1329 KnownZero, KnownOne, Depth+1);
1330 return;
1331 }
1332 case ISD::TRUNCATE: {
1333 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1334 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1335 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1336 KnownZero &= OutMask;
1337 KnownOne &= OutMask;
1338 break;
1339 }
1340 case ISD::AssertZext: {
1341 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1342 uint64_t InMask = MVT::getIntVTBitMask(VT);
1343 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1344 KnownOne, Depth+1);
1345 KnownZero |= (~InMask) & Mask;
1346 return;
1347 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001348 case ISD::FGETSIGN:
1349 // All bits are zero except the low bit.
1350 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1351 return;
1352
Dan Gohmanea859be2007-06-22 14:59:07 +00001353 case ISD::ADD: {
1354 // If either the LHS or the RHS are Zero, the result is zero.
1355 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1356 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1357 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1358 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1359
1360 // Output known-0 bits are known if clear or set in both the low clear bits
1361 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1362 // low 3 bits clear.
1363 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1364 CountTrailingZeros_64(~KnownZero2));
1365
1366 KnownZero = (1ULL << KnownZeroOut) - 1;
1367 KnownOne = 0;
1368 return;
1369 }
1370 case ISD::SUB: {
1371 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1372 if (!CLHS) return;
1373
1374 // We know that the top bits of C-X are clear if X contains less bits
1375 // than C (i.e. no wrap-around can happen). For example, 20-X is
1376 // positive if we can prove that X is >= 0 and < 16.
1377 MVT::ValueType VT = CLHS->getValueType(0);
1378 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1379 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1380 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1381 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1382 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1383
1384 // If all of the MaskV bits are known to be zero, then we know the output
1385 // top bits are zero, because we now know that the output is from [0-C].
1386 if ((KnownZero & MaskV) == MaskV) {
1387 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1388 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1389 KnownOne = 0; // No one bits known.
1390 } else {
1391 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1392 }
1393 }
1394 return;
1395 }
1396 default:
1397 // Allow the target to implement this method for its nodes.
1398 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1399 case ISD::INTRINSIC_WO_CHAIN:
1400 case ISD::INTRINSIC_W_CHAIN:
1401 case ISD::INTRINSIC_VOID:
1402 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1403 }
1404 return;
1405 }
1406}
1407
1408/// ComputeNumSignBits - Return the number of times the sign bit of the
1409/// register is replicated into the other bits. We know that at least 1 bit
1410/// is always equal to the sign bit (itself), but other cases can give us
1411/// information. For example, immediately after an "SRA X, 2", we know that
1412/// the top 3 bits are all equal to each other, so we return 3.
1413unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1414 MVT::ValueType VT = Op.getValueType();
1415 assert(MVT::isInteger(VT) && "Invalid VT!");
1416 unsigned VTBits = MVT::getSizeInBits(VT);
1417 unsigned Tmp, Tmp2;
1418
1419 if (Depth == 6)
1420 return 1; // Limit search depth.
1421
1422 switch (Op.getOpcode()) {
1423 default: break;
1424 case ISD::AssertSext:
1425 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1426 return VTBits-Tmp+1;
1427 case ISD::AssertZext:
1428 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1429 return VTBits-Tmp;
1430
1431 case ISD::Constant: {
1432 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1433 // If negative, invert the bits, then look at it.
1434 if (Val & MVT::getIntVTSignBit(VT))
1435 Val = ~Val;
1436
1437 // Shift the bits so they are the leading bits in the int64_t.
1438 Val <<= 64-VTBits;
1439
1440 // Return # leading zeros. We use 'min' here in case Val was zero before
1441 // shifting. We don't want to return '64' as for an i32 "0".
1442 return std::min(VTBits, CountLeadingZeros_64(Val));
1443 }
1444
1445 case ISD::SIGN_EXTEND:
1446 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1447 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1448
1449 case ISD::SIGN_EXTEND_INREG:
1450 // Max of the input and what this extends.
1451 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1452 Tmp = VTBits-Tmp+1;
1453
1454 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1455 return std::max(Tmp, Tmp2);
1456
1457 case ISD::SRA:
1458 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1459 // SRA X, C -> adds C sign bits.
1460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1461 Tmp += C->getValue();
1462 if (Tmp > VTBits) Tmp = VTBits;
1463 }
1464 return Tmp;
1465 case ISD::SHL:
1466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1467 // shl destroys sign bits.
1468 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1469 if (C->getValue() >= VTBits || // Bad shift.
1470 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1471 return Tmp - C->getValue();
1472 }
1473 break;
1474 case ISD::AND:
1475 case ISD::OR:
1476 case ISD::XOR: // NOT is handled here.
1477 // Logical binary ops preserve the number of sign bits.
1478 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1479 if (Tmp == 1) return 1; // Early out.
1480 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1481 return std::min(Tmp, Tmp2);
1482
1483 case ISD::SELECT:
1484 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1485 if (Tmp == 1) return 1; // Early out.
1486 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1487 return std::min(Tmp, Tmp2);
1488
1489 case ISD::SETCC:
1490 // If setcc returns 0/-1, all bits are sign bits.
1491 if (TLI.getSetCCResultContents() ==
1492 TargetLowering::ZeroOrNegativeOneSetCCResult)
1493 return VTBits;
1494 break;
1495 case ISD::ROTL:
1496 case ISD::ROTR:
1497 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1498 unsigned RotAmt = C->getValue() & (VTBits-1);
1499
1500 // Handle rotate right by N like a rotate left by 32-N.
1501 if (Op.getOpcode() == ISD::ROTR)
1502 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1503
1504 // If we aren't rotating out all of the known-in sign bits, return the
1505 // number that are left. This handles rotl(sext(x), 1) for example.
1506 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1507 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1508 }
1509 break;
1510 case ISD::ADD:
1511 // Add can have at most one carry bit. Thus we know that the output
1512 // is, at worst, one more bit than the inputs.
1513 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1514 if (Tmp == 1) return 1; // Early out.
1515
1516 // Special case decrementing a value (ADD X, -1):
1517 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1518 if (CRHS->isAllOnesValue()) {
1519 uint64_t KnownZero, KnownOne;
1520 uint64_t Mask = MVT::getIntVTBitMask(VT);
1521 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1522
1523 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1524 // sign bits set.
1525 if ((KnownZero|1) == Mask)
1526 return VTBits;
1527
1528 // If we are subtracting one from a positive number, there is no carry
1529 // out of the result.
1530 if (KnownZero & MVT::getIntVTSignBit(VT))
1531 return Tmp;
1532 }
1533
1534 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1535 if (Tmp2 == 1) return 1;
1536 return std::min(Tmp, Tmp2)-1;
1537 break;
1538
1539 case ISD::SUB:
1540 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1541 if (Tmp2 == 1) return 1;
1542
1543 // Handle NEG.
1544 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1545 if (CLHS->getValue() == 0) {
1546 uint64_t KnownZero, KnownOne;
1547 uint64_t Mask = MVT::getIntVTBitMask(VT);
1548 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1549 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1550 // sign bits set.
1551 if ((KnownZero|1) == Mask)
1552 return VTBits;
1553
1554 // If the input is known to be positive (the sign bit is known clear),
1555 // the output of the NEG has the same number of sign bits as the input.
1556 if (KnownZero & MVT::getIntVTSignBit(VT))
1557 return Tmp2;
1558
1559 // Otherwise, we treat this like a SUB.
1560 }
1561
1562 // Sub can have at most one carry bit. Thus we know that the output
1563 // is, at worst, one more bit than the inputs.
1564 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1565 if (Tmp == 1) return 1; // Early out.
1566 return std::min(Tmp, Tmp2)-1;
1567 break;
1568 case ISD::TRUNCATE:
1569 // FIXME: it's tricky to do anything useful for this, but it is an important
1570 // case for targets like X86.
1571 break;
1572 }
1573
1574 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1575 if (Op.getOpcode() == ISD::LOAD) {
1576 LoadSDNode *LD = cast<LoadSDNode>(Op);
1577 unsigned ExtType = LD->getExtensionType();
1578 switch (ExtType) {
1579 default: break;
1580 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001581 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001582 return VTBits-Tmp+1;
1583 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001584 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001585 return VTBits-Tmp;
1586 }
1587 }
1588
1589 // Allow the target to implement this method for its nodes.
1590 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1591 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1592 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1593 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1594 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1595 if (NumBits > 1) return NumBits;
1596 }
1597
1598 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1599 // use this information.
1600 uint64_t KnownZero, KnownOne;
1601 uint64_t Mask = MVT::getIntVTBitMask(VT);
1602 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1603
1604 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1605 if (KnownZero & SignBit) { // SignBit is 0
1606 Mask = KnownZero;
1607 } else if (KnownOne & SignBit) { // SignBit is 1;
1608 Mask = KnownOne;
1609 } else {
1610 // Nothing known.
1611 return 1;
1612 }
1613
1614 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1615 // the number of identical bits in the top of the input value.
1616 Mask ^= ~0ULL;
1617 Mask <<= 64-VTBits;
1618 // Return # leading zeros. We use 'min' here in case Val was zero before
1619 // shifting. We don't want to return '64' as for an i32 "0".
1620 return std::min(VTBits, CountLeadingZeros_64(Mask));
1621}
1622
Chris Lattner51dabfb2006-10-14 00:41:01 +00001623
Evan Chenga844bde2008-02-02 04:07:54 +00001624bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1625 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1626 if (!GA) return false;
1627 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1628 if (!GV) return false;
1629 MachineModuleInfo *MMI = getMachineModuleInfo();
1630 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1631}
1632
1633
Chris Lattnerc3aae252005-01-07 07:46:32 +00001634/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001635///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001636SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001637 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001638 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001639 void *IP = 0;
1640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1641 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001642 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001643 CSEMap.InsertNode(N, IP);
1644
1645 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001646 return SDOperand(N, 0);
1647}
1648
Chris Lattnerc3aae252005-01-07 07:46:32 +00001649SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1650 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001651 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001652 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001653 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1654 uint64_t Val = C->getValue();
1655 switch (Opcode) {
1656 default: break;
1657 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001658 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001659 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1660 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001661 case ISD::UINT_TO_FP:
1662 case ISD::SINT_TO_FP: {
1663 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001664 // No compile time operations on this type.
1665 if (VT==MVT::ppcf128)
1666 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00001667 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001668 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001669 MVT::getSizeInBits(Operand.getValueType()),
1670 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001671 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001672 return getConstantFP(apf, VT);
1673 }
Chris Lattner94683772005-12-23 05:30:37 +00001674 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001675 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001676 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001677 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001678 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001679 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001680 case ISD::BSWAP:
1681 switch(VT) {
1682 default: assert(0 && "Invalid bswap!"); break;
1683 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1684 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1685 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1686 }
1687 break;
1688 case ISD::CTPOP:
1689 switch(VT) {
1690 default: assert(0 && "Invalid ctpop!"); break;
1691 case MVT::i1: return getConstant(Val != 0, VT);
1692 case MVT::i8:
1693 Tmp1 = (unsigned)Val & 0xFF;
1694 return getConstant(CountPopulation_32(Tmp1), VT);
1695 case MVT::i16:
1696 Tmp1 = (unsigned)Val & 0xFFFF;
1697 return getConstant(CountPopulation_32(Tmp1), VT);
1698 case MVT::i32:
1699 return getConstant(CountPopulation_32((unsigned)Val), VT);
1700 case MVT::i64:
1701 return getConstant(CountPopulation_64(Val), VT);
1702 }
1703 case ISD::CTLZ:
1704 switch(VT) {
1705 default: assert(0 && "Invalid ctlz!"); break;
1706 case MVT::i1: return getConstant(Val == 0, VT);
1707 case MVT::i8:
1708 Tmp1 = (unsigned)Val & 0xFF;
1709 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1710 case MVT::i16:
1711 Tmp1 = (unsigned)Val & 0xFFFF;
1712 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1713 case MVT::i32:
1714 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1715 case MVT::i64:
1716 return getConstant(CountLeadingZeros_64(Val), VT);
1717 }
1718 case ISD::CTTZ:
1719 switch(VT) {
1720 default: assert(0 && "Invalid cttz!"); break;
1721 case MVT::i1: return getConstant(Val == 0, VT);
1722 case MVT::i8:
1723 Tmp1 = (unsigned)Val | 0x100;
1724 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1725 case MVT::i16:
1726 Tmp1 = (unsigned)Val | 0x10000;
1727 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1728 case MVT::i32:
1729 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1730 case MVT::i64:
1731 return getConstant(CountTrailingZeros_64(Val), VT);
1732 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001733 }
1734 }
1735
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001736 // Constant fold unary operations with a floating point constant operand.
1737 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1738 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001739 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001740 switch (Opcode) {
1741 case ISD::FNEG:
1742 V.changeSign();
1743 return getConstantFP(V, VT);
1744 case ISD::FABS:
1745 V.clearSign();
1746 return getConstantFP(V, VT);
1747 case ISD::FP_ROUND:
1748 case ISD::FP_EXTEND:
1749 // This can return overflow, underflow, or inexact; we don't care.
1750 // FIXME need to be more flexible about rounding mode.
1751 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1752 VT==MVT::f64 ? APFloat::IEEEdouble :
1753 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1754 VT==MVT::f128 ? APFloat::IEEEquad :
1755 APFloat::Bogus,
1756 APFloat::rmNearestTiesToEven);
1757 return getConstantFP(V, VT);
1758 case ISD::FP_TO_SINT:
1759 case ISD::FP_TO_UINT: {
1760 integerPart x;
1761 assert(integerPartWidth >= 64);
1762 // FIXME need to be more flexible about rounding mode.
1763 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1764 Opcode==ISD::FP_TO_SINT,
1765 APFloat::rmTowardZero);
1766 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1767 break;
1768 return getConstant(x, VT);
1769 }
1770 case ISD::BIT_CONVERT:
1771 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1772 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1773 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1774 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001775 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001776 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001777 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001778 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001779
1780 unsigned OpOpcode = Operand.Val->getOpcode();
1781 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001782 case ISD::TokenFactor:
1783 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001784 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001785 case ISD::FP_EXTEND:
1786 assert(MVT::isFloatingPoint(VT) &&
1787 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001788 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001789 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00001790 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001791 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1792 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001793 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001794 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1795 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001796 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1797 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1798 break;
1799 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001800 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1801 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001802 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001803 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1804 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001805 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001806 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001807 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001808 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001809 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1810 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001811 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001812 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1813 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001814 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1815 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1816 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1817 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001818 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001819 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1820 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001821 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001822 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1823 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001824 if (OpOpcode == ISD::TRUNCATE)
1825 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001826 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1827 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001828 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001829 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1830 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001831 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001832 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1833 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001834 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1835 else
1836 return Operand.Val->getOperand(0);
1837 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001838 break;
Chris Lattner35481892005-12-23 00:16:34 +00001839 case ISD::BIT_CONVERT:
1840 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001841 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001842 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001843 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001844 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1845 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001846 if (OpOpcode == ISD::UNDEF)
1847 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001848 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001849 case ISD::SCALAR_TO_VECTOR:
1850 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001851 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001852 "Illegal SCALAR_TO_VECTOR node!");
1853 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001854 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001855 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1856 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001857 Operand.Val->getOperand(0));
1858 if (OpOpcode == ISD::FNEG) // --X -> X
1859 return Operand.Val->getOperand(0);
1860 break;
1861 case ISD::FABS:
1862 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1863 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1864 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001865 }
1866
Chris Lattner43247a12005-08-25 19:12:10 +00001867 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001868 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001869 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001870 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001871 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001872 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001873 void *IP = 0;
1874 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1875 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001876 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001877 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001878 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001879 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001880 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001881 AllNodes.push_back(N);
1882 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001883}
1884
Chris Lattner36019aa2005-04-18 03:48:41 +00001885
1886
Chris Lattnerc3aae252005-01-07 07:46:32 +00001887SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1888 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001889 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1890 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001891 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001892 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001893 case ISD::TokenFactor:
1894 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1895 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001896 // Fold trivial token factors.
1897 if (N1.getOpcode() == ISD::EntryToken) return N2;
1898 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001899 break;
Chris Lattner76365122005-01-16 02:23:22 +00001900 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001901 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1902 N1.getValueType() == VT && "Binary operator types must match!");
1903 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1904 // worth handling here.
1905 if (N2C && N2C->getValue() == 0)
1906 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001907 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1908 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001909 break;
Chris Lattner76365122005-01-16 02:23:22 +00001910 case ISD::OR:
1911 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001912 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1913 N1.getValueType() == VT && "Binary operator types must match!");
1914 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1915 // worth handling here.
1916 if (N2C && N2C->getValue() == 0)
1917 return N1;
1918 break;
Chris Lattner76365122005-01-16 02:23:22 +00001919 case ISD::UDIV:
1920 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001921 case ISD::MULHU:
1922 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001923 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1924 // fall through
1925 case ISD::ADD:
1926 case ISD::SUB:
1927 case ISD::MUL:
1928 case ISD::SDIV:
1929 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001930 case ISD::FADD:
1931 case ISD::FSUB:
1932 case ISD::FMUL:
1933 case ISD::FDIV:
1934 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001935 assert(N1.getValueType() == N2.getValueType() &&
1936 N1.getValueType() == VT && "Binary operator types must match!");
1937 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001938 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1939 assert(N1.getValueType() == VT &&
1940 MVT::isFloatingPoint(N1.getValueType()) &&
1941 MVT::isFloatingPoint(N2.getValueType()) &&
1942 "Invalid FCOPYSIGN!");
1943 break;
Chris Lattner76365122005-01-16 02:23:22 +00001944 case ISD::SHL:
1945 case ISD::SRA:
1946 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001947 case ISD::ROTL:
1948 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001949 assert(VT == N1.getValueType() &&
1950 "Shift operators return type must be the same as their first arg");
1951 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001952 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001953 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001954 case ISD::FP_ROUND_INREG: {
1955 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1956 assert(VT == N1.getValueType() && "Not an inreg round!");
1957 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1958 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001959 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1960 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001961 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00001962 break;
1963 }
Chris Lattner0bd48932008-01-17 07:00:52 +00001964 case ISD::FP_ROUND:
1965 assert(MVT::isFloatingPoint(VT) &&
1966 MVT::isFloatingPoint(N1.getValueType()) &&
1967 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
1968 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001969 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00001970 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00001971 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001972 case ISD::AssertZext: {
1973 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1974 assert(VT == N1.getValueType() && "Not an inreg extend!");
1975 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1976 "Cannot *_EXTEND_INREG FP types");
1977 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1978 "Not extending!");
1979 break;
1980 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001981 case ISD::SIGN_EXTEND_INREG: {
1982 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1983 assert(VT == N1.getValueType() && "Not an inreg extend!");
1984 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1985 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001986 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1987 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001988 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001989
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001990 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001991 int64_t Val = N1C->getValue();
1992 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1993 Val <<= 64-FromBits;
1994 Val >>= 64-FromBits;
1995 return getConstant(Val, VT);
1996 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001997 break;
1998 }
1999 case ISD::EXTRACT_VECTOR_ELT:
2000 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2001
2002 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2003 // expanding copies of large vectors from registers.
2004 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2005 N1.getNumOperands() > 0) {
2006 unsigned Factor =
2007 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2008 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2009 N1.getOperand(N2C->getValue() / Factor),
2010 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2011 }
2012
2013 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2014 // expanding large vector constants.
2015 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2016 return N1.getOperand(N2C->getValue());
2017
2018 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2019 // operations are lowered to scalars.
2020 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2021 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2022 if (IEC == N2C)
2023 return N1.getOperand(1);
2024 else
2025 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2026 }
2027 break;
2028 case ISD::EXTRACT_ELEMENT:
2029 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002030
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002031 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2032 // 64-bit integers into 32-bit parts. Instead of building the extract of
2033 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2034 if (N1.getOpcode() == ISD::BUILD_PAIR)
2035 return N1.getOperand(N2C->getValue());
2036
2037 // EXTRACT_ELEMENT of a constant int is also very common.
2038 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2039 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2040 return getConstant(C->getValue() >> Shift, VT);
2041 }
2042 break;
2043 }
2044
2045 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002046 if (N2C) {
2047 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
2048 switch (Opcode) {
2049 case ISD::ADD: return getConstant(C1 + C2, VT);
2050 case ISD::SUB: return getConstant(C1 - C2, VT);
2051 case ISD::MUL: return getConstant(C1 * C2, VT);
2052 case ISD::UDIV:
2053 if (C2) return getConstant(C1 / C2, VT);
2054 break;
2055 case ISD::UREM :
2056 if (C2) return getConstant(C1 % C2, VT);
2057 break;
2058 case ISD::SDIV :
2059 if (C2) return getConstant(N1C->getSignExtended() /
2060 N2C->getSignExtended(), VT);
2061 break;
2062 case ISD::SREM :
2063 if (C2) return getConstant(N1C->getSignExtended() %
2064 N2C->getSignExtended(), VT);
2065 break;
2066 case ISD::AND : return getConstant(C1 & C2, VT);
2067 case ISD::OR : return getConstant(C1 | C2, VT);
2068 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002069 case ISD::SHL : return getConstant(C1 << C2, VT);
2070 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00002071 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00002072 case ISD::ROTL :
2073 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
2074 VT);
2075 case ISD::ROTR :
2076 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
2077 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002078 default: break;
2079 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002080 } else { // Cannonicalize constant to RHS if commutative
2081 if (isCommutativeBinOp(Opcode)) {
2082 std::swap(N1C, N2C);
2083 std::swap(N1, N2);
2084 }
2085 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002086 }
2087
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002088 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002089 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2090 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002091 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002092 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2093 // Cannonicalize constant to RHS if commutative
2094 std::swap(N1CFP, N2CFP);
2095 std::swap(N1, N2);
2096 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002097 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2098 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002099 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002100 case ISD::FADD:
2101 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002102 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002103 return getConstantFP(V1, VT);
2104 break;
2105 case ISD::FSUB:
2106 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2107 if (s!=APFloat::opInvalidOp)
2108 return getConstantFP(V1, VT);
2109 break;
2110 case ISD::FMUL:
2111 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2112 if (s!=APFloat::opInvalidOp)
2113 return getConstantFP(V1, VT);
2114 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002115 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002116 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2117 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2118 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002119 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002120 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002121 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2122 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2123 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002124 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002125 case ISD::FCOPYSIGN:
2126 V1.copySign(V2);
2127 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002128 default: break;
2129 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002130 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002131 }
Chris Lattner62b57722006-04-20 05:39:12 +00002132
2133 // Canonicalize an UNDEF to the RHS, even over a constant.
2134 if (N1.getOpcode() == ISD::UNDEF) {
2135 if (isCommutativeBinOp(Opcode)) {
2136 std::swap(N1, N2);
2137 } else {
2138 switch (Opcode) {
2139 case ISD::FP_ROUND_INREG:
2140 case ISD::SIGN_EXTEND_INREG:
2141 case ISD::SUB:
2142 case ISD::FSUB:
2143 case ISD::FDIV:
2144 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002145 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002146 return N1; // fold op(undef, arg2) -> undef
2147 case ISD::UDIV:
2148 case ISD::SDIV:
2149 case ISD::UREM:
2150 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002151 case ISD::SRL:
2152 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002153 if (!MVT::isVector(VT))
2154 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2155 // For vectors, we can't easily build an all zero vector, just return
2156 // the LHS.
2157 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002158 }
2159 }
2160 }
2161
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002162 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002163 if (N2.getOpcode() == ISD::UNDEF) {
2164 switch (Opcode) {
2165 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002166 case ISD::ADDC:
2167 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002168 case ISD::SUB:
2169 case ISD::FADD:
2170 case ISD::FSUB:
2171 case ISD::FMUL:
2172 case ISD::FDIV:
2173 case ISD::FREM:
2174 case ISD::UDIV:
2175 case ISD::SDIV:
2176 case ISD::UREM:
2177 case ISD::SREM:
2178 case ISD::XOR:
2179 return N2; // fold op(arg1, undef) -> undef
2180 case ISD::MUL:
2181 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002182 case ISD::SRL:
2183 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002184 if (!MVT::isVector(VT))
2185 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2186 // For vectors, we can't easily build an all zero vector, just return
2187 // the LHS.
2188 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002189 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002190 if (!MVT::isVector(VT))
2191 return getConstant(MVT::getIntVTBitMask(VT), VT);
2192 // For vectors, we can't easily build an all one vector, just return
2193 // the LHS.
2194 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002195 case ISD::SRA:
2196 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002197 }
2198 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002199
Chris Lattner27e9b412005-05-11 18:57:39 +00002200 // Memoize this node if possible.
2201 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002202 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002203 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002204 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002205 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002206 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
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 BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002211 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002212 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002213 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002214 }
2215
Chris Lattnerc3aae252005-01-07 07:46:32 +00002216 AllNodes.push_back(N);
2217 return SDOperand(N, 0);
2218}
2219
Chris Lattnerc3aae252005-01-07 07:46:32 +00002220SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2221 SDOperand N1, SDOperand N2, SDOperand N3) {
2222 // Perform various simplifications.
2223 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2224 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002225 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002226 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002227 // Use FoldSetCC to simplify SETCC's.
2228 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002229 if (Simp.Val) return Simp;
2230 break;
2231 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002232 case ISD::SELECT:
2233 if (N1C)
2234 if (N1C->getValue())
2235 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002236 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002237 return N3; // select false, X, Y -> Y
2238
2239 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002240 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002241 case ISD::BRCOND:
2242 if (N2C)
2243 if (N2C->getValue()) // Unconditional branch
2244 return getNode(ISD::BR, MVT::Other, N1, N3);
2245 else
2246 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002247 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002248 case ISD::VECTOR_SHUFFLE:
2249 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2250 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2251 N3.getOpcode() == ISD::BUILD_VECTOR &&
2252 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2253 "Illegal VECTOR_SHUFFLE node!");
2254 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002255 case ISD::BIT_CONVERT:
2256 // Fold bit_convert nodes from a type to themselves.
2257 if (N1.getValueType() == VT)
2258 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002259 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002260 }
2261
Chris Lattner43247a12005-08-25 19:12:10 +00002262 // Memoize node if it doesn't produce a flag.
2263 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002264 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002265 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002266 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002267 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002268 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002269 void *IP = 0;
2270 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2271 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002272 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002273 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002274 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002275 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002276 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002277 AllNodes.push_back(N);
2278 return SDOperand(N, 0);
2279}
2280
2281SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002282 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002283 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002284 SDOperand Ops[] = { N1, N2, N3, N4 };
2285 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002286}
2287
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002288SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2289 SDOperand N1, SDOperand N2, SDOperand N3,
2290 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002291 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2292 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002293}
2294
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002295SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2296 SDOperand Src, SDOperand Size,
2297 SDOperand Align,
2298 SDOperand AlwaysInline) {
2299 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2300 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2301}
2302
2303SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2304 SDOperand Src, SDOperand Size,
2305 SDOperand Align,
2306 SDOperand AlwaysInline) {
2307 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2308 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2309}
2310
2311SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2312 SDOperand Src, SDOperand Size,
2313 SDOperand Align,
2314 SDOperand AlwaysInline) {
2315 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2316 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2317}
2318
Evan Cheng7038daf2005-12-10 00:37:58 +00002319SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2320 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002321 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002322 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002323 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2324 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002325 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002326 Ty = MVT::getTypeForValueType(VT);
2327 } else if (SV) {
2328 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2329 assert(PT && "Value for load must be a pointer");
2330 Ty = PT->getElementType();
2331 }
2332 assert(Ty && "Could not get type information for load");
2333 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2334 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002335 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002336 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002337 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002338 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002339 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002340 ID.AddInteger(ISD::UNINDEXED);
2341 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002342 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002343 ID.AddInteger(Alignment);
2344 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002345 void *IP = 0;
2346 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2347 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002348 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002349 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2350 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002351 CSEMap.InsertNode(N, IP);
2352 AllNodes.push_back(N);
2353 return SDOperand(N, 0);
2354}
2355
2356SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002357 SDOperand Chain, SDOperand Ptr,
2358 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002359 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002360 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002361 // If they are asking for an extending load from/to the same thing, return a
2362 // normal load.
2363 if (VT == EVT)
Duncan Sands5d868b12007-10-19 13:05:40 +00002364 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00002365
2366 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002367 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002368 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002369 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2370 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002371 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2372 "Cannot sign/zero extend a FP/Vector load!");
2373 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2374 "Cannot convert from FP to Int or Int -> FP!");
2375
Dan Gohman575e2f42007-06-04 15:49:41 +00002376 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2377 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002378 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002379 Ty = MVT::getTypeForValueType(VT);
2380 } else if (SV) {
2381 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2382 assert(PT && "Value for load must be a pointer");
2383 Ty = PT->getElementType();
2384 }
2385 assert(Ty && "Could not get type information for load");
2386 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2387 }
Evan Cheng466685d2006-10-09 20:57:25 +00002388 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002389 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002390 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002391 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002392 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002393 ID.AddInteger(ISD::UNINDEXED);
2394 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002395 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002396 ID.AddInteger(Alignment);
2397 ID.AddInteger(isVolatile);
2398 void *IP = 0;
2399 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2400 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002401 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002402 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002403 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002404 AllNodes.push_back(N);
2405 return SDOperand(N, 0);
2406}
2407
Evan Cheng144d8f02006-11-09 17:55:04 +00002408SDOperand
2409SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2410 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002411 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002412 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2413 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002414 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002415 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002416 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002417 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002418 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002419 ID.AddInteger(AM);
2420 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002421 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002422 ID.AddInteger(LD->getAlignment());
2423 ID.AddInteger(LD->isVolatile());
2424 void *IP = 0;
2425 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2426 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002427 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002428 LD->getExtensionType(), LD->getMemoryVT(),
Evan Cheng2caccca2006-10-17 21:14:32 +00002429 LD->getSrcValue(), LD->getSrcValueOffset(),
2430 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002431 CSEMap.InsertNode(N, IP);
2432 AllNodes.push_back(N);
2433 return SDOperand(N, 0);
2434}
2435
Jeff Cohend41b30d2006-11-05 19:31:28 +00002436SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002437 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002438 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002439 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002440
Dan Gohman575e2f42007-06-04 15:49:41 +00002441 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2442 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002443 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002444 Ty = MVT::getTypeForValueType(VT);
2445 } else if (SV) {
2446 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2447 assert(PT && "Value for store must be a pointer");
2448 Ty = PT->getElementType();
2449 }
2450 assert(Ty && "Could not get type information for store");
2451 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2452 }
Evan Chengad071e12006-10-05 22:57:11 +00002453 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002454 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002455 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002456 FoldingSetNodeID ID;
2457 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002458 ID.AddInteger(ISD::UNINDEXED);
2459 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002460 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002461 ID.AddInteger(Alignment);
2462 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002463 void *IP = 0;
2464 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2465 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002466 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002467 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002468 CSEMap.InsertNode(N, IP);
2469 AllNodes.push_back(N);
2470 return SDOperand(N, 0);
2471}
2472
Jeff Cohend41b30d2006-11-05 19:31:28 +00002473SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002474 SDOperand Ptr, const Value *SV,
2475 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002476 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002477 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002478
2479 if (VT == SVT)
2480 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002481
Duncan Sandsaf47b112007-10-16 09:56:48 +00002482 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2483 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002484 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2485 "Can't do FP-INT conversion!");
2486
Dan Gohman575e2f42007-06-04 15:49:41 +00002487 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2488 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002489 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002490 Ty = MVT::getTypeForValueType(VT);
2491 } else if (SV) {
2492 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2493 assert(PT && "Value for store must be a pointer");
2494 Ty = PT->getElementType();
2495 }
2496 assert(Ty && "Could not get type information for store");
2497 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2498 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002499 SDVTList VTs = getVTList(MVT::Other);
2500 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002501 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002502 FoldingSetNodeID ID;
2503 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002504 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002505 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002506 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002507 ID.AddInteger(Alignment);
2508 ID.AddInteger(isVolatile);
2509 void *IP = 0;
2510 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2511 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002512 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002513 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002514 CSEMap.InsertNode(N, IP);
2515 AllNodes.push_back(N);
2516 return SDOperand(N, 0);
2517}
2518
Evan Cheng144d8f02006-11-09 17:55:04 +00002519SDOperand
2520SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2521 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002522 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2523 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2524 "Store is already a indexed store!");
2525 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2526 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2527 FoldingSetNodeID ID;
2528 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2529 ID.AddInteger(AM);
2530 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002531 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002532 ID.AddInteger(ST->getAlignment());
2533 ID.AddInteger(ST->isVolatile());
2534 void *IP = 0;
2535 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2536 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002537 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002538 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002539 ST->getSrcValue(), ST->getSrcValueOffset(),
2540 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002541 CSEMap.InsertNode(N, IP);
2542 AllNodes.push_back(N);
2543 return SDOperand(N, 0);
2544}
2545
Nate Begemanacc398c2006-01-25 18:21:52 +00002546SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2547 SDOperand Chain, SDOperand Ptr,
2548 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002549 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002550 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002551}
2552
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002553SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002554 const SDOperand *Ops, unsigned NumOps) {
2555 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002556 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002557 case 1: return getNode(Opcode, VT, Ops[0]);
2558 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2559 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002560 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002561 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002562
Chris Lattneref847df2005-04-09 03:27:28 +00002563 switch (Opcode) {
2564 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002565 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002566 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002567 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2568 "LHS and RHS of condition must have same type!");
2569 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2570 "True and False arms of SelectCC must have same type!");
2571 assert(Ops[2].getValueType() == VT &&
2572 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002573 break;
2574 }
2575 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002576 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002577 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2578 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002579 break;
2580 }
Chris Lattneref847df2005-04-09 03:27:28 +00002581 }
2582
Chris Lattner385328c2005-05-14 07:42:29 +00002583 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002584 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002585 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002586 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002587 FoldingSetNodeID ID;
2588 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002589 void *IP = 0;
2590 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2591 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002592 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002593 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002594 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002595 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002596 }
Chris Lattneref847df2005-04-09 03:27:28 +00002597 AllNodes.push_back(N);
2598 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002599}
2600
Chris Lattner89c34632005-05-14 06:20:26 +00002601SDOperand SelectionDAG::getNode(unsigned Opcode,
2602 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002603 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002604 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2605 Ops, NumOps);
2606}
2607
2608SDOperand SelectionDAG::getNode(unsigned Opcode,
2609 const MVT::ValueType *VTs, unsigned NumVTs,
2610 const SDOperand *Ops, unsigned NumOps) {
2611 if (NumVTs == 1)
2612 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002613 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2614}
2615
2616SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2617 const SDOperand *Ops, unsigned NumOps) {
2618 if (VTList.NumVTs == 1)
2619 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002620
Chris Lattner5f056bf2005-07-10 01:55:33 +00002621 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002622 // FIXME: figure out how to safely handle things like
2623 // int foo(int x) { return 1 << (x & 255); }
2624 // int bar() { return foo(256); }
2625#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002626 case ISD::SRA_PARTS:
2627 case ISD::SRL_PARTS:
2628 case ISD::SHL_PARTS:
2629 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002630 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002631 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2632 else if (N3.getOpcode() == ISD::AND)
2633 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2634 // If the and is only masking out bits that cannot effect the shift,
2635 // eliminate the and.
2636 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2637 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2638 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2639 }
2640 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002641#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002642 }
Chris Lattner89c34632005-05-14 06:20:26 +00002643
Chris Lattner43247a12005-08-25 19:12:10 +00002644 // Memoize the node unless it returns a flag.
2645 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002646 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002647 FoldingSetNodeID ID;
2648 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002649 void *IP = 0;
2650 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2651 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002652 if (NumOps == 1)
2653 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2654 else if (NumOps == 2)
2655 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2656 else if (NumOps == 3)
2657 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2658 else
2659 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002660 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002661 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002662 if (NumOps == 1)
2663 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2664 else if (NumOps == 2)
2665 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2666 else if (NumOps == 3)
2667 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2668 else
2669 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002670 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002671 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002672 return SDOperand(N, 0);
2673}
2674
Dan Gohman08ce9762007-10-08 15:49:58 +00002675SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2676 return getNode(Opcode, VTList, 0, 0);
2677}
2678
2679SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2680 SDOperand N1) {
2681 SDOperand Ops[] = { N1 };
2682 return getNode(Opcode, VTList, Ops, 1);
2683}
2684
2685SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2686 SDOperand N1, SDOperand N2) {
2687 SDOperand Ops[] = { N1, N2 };
2688 return getNode(Opcode, VTList, Ops, 2);
2689}
2690
2691SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2692 SDOperand N1, SDOperand N2, SDOperand N3) {
2693 SDOperand Ops[] = { N1, N2, N3 };
2694 return getNode(Opcode, VTList, Ops, 3);
2695}
2696
2697SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2698 SDOperand N1, SDOperand N2, SDOperand N3,
2699 SDOperand N4) {
2700 SDOperand Ops[] = { N1, N2, N3, N4 };
2701 return getNode(Opcode, VTList, Ops, 4);
2702}
2703
2704SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2705 SDOperand N1, SDOperand N2, SDOperand N3,
2706 SDOperand N4, SDOperand N5) {
2707 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2708 return getNode(Opcode, VTList, Ops, 5);
2709}
2710
Chris Lattner70046e92006-08-15 17:46:01 +00002711SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002712 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002713}
2714
Chris Lattner70046e92006-08-15 17:46:01 +00002715SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002716 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2717 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002718 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002719 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002720 }
2721 std::vector<MVT::ValueType> V;
2722 V.push_back(VT1);
2723 V.push_back(VT2);
2724 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002725 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002726}
Chris Lattner70046e92006-08-15 17:46:01 +00002727SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2728 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002729 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002730 E = VTList.end(); I != E; ++I) {
2731 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2732 (*I)[2] == VT3)
2733 return makeVTList(&(*I)[0], 3);
2734 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002735 std::vector<MVT::ValueType> V;
2736 V.push_back(VT1);
2737 V.push_back(VT2);
2738 V.push_back(VT3);
2739 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002740 return makeVTList(&(*VTList.begin())[0], 3);
2741}
2742
2743SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2744 switch (NumVTs) {
2745 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002746 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002747 case 2: return getVTList(VTs[0], VTs[1]);
2748 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2749 default: break;
2750 }
2751
2752 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2753 E = VTList.end(); I != E; ++I) {
2754 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2755
2756 bool NoMatch = false;
2757 for (unsigned i = 2; i != NumVTs; ++i)
2758 if (VTs[i] != (*I)[i]) {
2759 NoMatch = true;
2760 break;
2761 }
2762 if (!NoMatch)
2763 return makeVTList(&*I->begin(), NumVTs);
2764 }
2765
2766 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2767 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002768}
2769
2770
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002771/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2772/// specified operands. If the resultant node already exists in the DAG,
2773/// this does not modify the specified node, instead it returns the node that
2774/// already exists. If the resultant node does not exist in the DAG, the
2775/// input node is returned. As a degenerate case, if you specify the same
2776/// input operands as the node already has, the input node is returned.
2777SDOperand SelectionDAG::
2778UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2779 SDNode *N = InN.Val;
2780 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2781
2782 // Check to see if there is no change.
2783 if (Op == N->getOperand(0)) return InN;
2784
2785 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002786 void *InsertPos = 0;
2787 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2788 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002789
2790 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002791 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002792 RemoveNodeFromCSEMaps(N);
2793
2794 // Now we update the operands.
2795 N->OperandList[0].Val->removeUser(N);
2796 Op.Val->addUser(N);
2797 N->OperandList[0] = Op;
2798
2799 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002800 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002801 return InN;
2802}
2803
2804SDOperand SelectionDAG::
2805UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2806 SDNode *N = InN.Val;
2807 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2808
2809 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002810 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2811 return InN; // No operands changed, just return the input node.
2812
2813 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002814 void *InsertPos = 0;
2815 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2816 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002817
2818 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002819 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002820 RemoveNodeFromCSEMaps(N);
2821
2822 // Now we update the operands.
2823 if (N->OperandList[0] != Op1) {
2824 N->OperandList[0].Val->removeUser(N);
2825 Op1.Val->addUser(N);
2826 N->OperandList[0] = Op1;
2827 }
2828 if (N->OperandList[1] != Op2) {
2829 N->OperandList[1].Val->removeUser(N);
2830 Op2.Val->addUser(N);
2831 N->OperandList[1] = Op2;
2832 }
2833
2834 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002835 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002836 return InN;
2837}
2838
2839SDOperand SelectionDAG::
2840UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002841 SDOperand Ops[] = { Op1, Op2, Op3 };
2842 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002843}
2844
2845SDOperand SelectionDAG::
2846UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2847 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002848 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2849 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002850}
2851
2852SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002853UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2854 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002855 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2856 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002857}
2858
2859
2860SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002861UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002862 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002863 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002864 "Update with wrong number of operands");
2865
2866 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002867 bool AnyChange = false;
2868 for (unsigned i = 0; i != NumOps; ++i) {
2869 if (Ops[i] != N->getOperand(i)) {
2870 AnyChange = true;
2871 break;
2872 }
2873 }
2874
2875 // No operands changed, just return the input node.
2876 if (!AnyChange) return InN;
2877
2878 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002879 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002880 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002881 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002882
2883 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002884 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002885 RemoveNodeFromCSEMaps(N);
2886
2887 // Now we update the operands.
2888 for (unsigned i = 0; i != NumOps; ++i) {
2889 if (N->OperandList[i] != Ops[i]) {
2890 N->OperandList[i].Val->removeUser(N);
2891 Ops[i].Val->addUser(N);
2892 N->OperandList[i] = Ops[i];
2893 }
2894 }
2895
2896 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002897 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002898 return InN;
2899}
2900
2901
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002902/// MorphNodeTo - This frees the operands of the current node, resets the
2903/// opcode, types, and operands to the specified value. This should only be
2904/// used by the SelectionDAG class.
2905void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2906 const SDOperand *Ops, unsigned NumOps) {
2907 NodeType = Opc;
2908 ValueList = L.VTs;
2909 NumValues = L.NumVTs;
2910
2911 // Clear the operands list, updating used nodes to remove this from their
2912 // use list.
2913 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2914 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002915
2916 // If NumOps is larger than the # of operands we currently have, reallocate
2917 // the operand list.
2918 if (NumOps > NumOperands) {
2919 if (OperandsNeedDelete)
2920 delete [] OperandList;
2921 OperandList = new SDOperand[NumOps];
2922 OperandsNeedDelete = true;
2923 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002924
2925 // Assign the new operands.
2926 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002927
2928 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2929 OperandList[i] = Ops[i];
2930 SDNode *N = OperandList[i].Val;
2931 N->Uses.push_back(this);
2932 }
2933}
Chris Lattner149c58c2005-08-16 18:17:10 +00002934
2935/// SelectNodeTo - These are used for target selectors to *mutate* the
2936/// specified node to have the specified return type, Target opcode, and
2937/// operands. Note that target opcodes are stored as
2938/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002939///
2940/// Note that SelectNodeTo returns the resultant node. If there is already a
2941/// node of the specified opcode and operands, it returns that node instead of
2942/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002943SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2944 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002945 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002946 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002947 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002948 void *IP = 0;
2949 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002950 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002951
Chris Lattner7651fa42005-08-24 23:00:29 +00002952 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002953
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002954 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002955
Chris Lattner4a283e92006-08-11 18:38:11 +00002956 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002957 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002958}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002959
Evan Cheng95514ba2006-08-26 08:00:10 +00002960SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2961 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002962 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002963 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002964 SDOperand Ops[] = { Op1 };
2965
Jim Laskey583bd472006-10-27 23:46:08 +00002966 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002967 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002968 void *IP = 0;
2969 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002970 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002971
Chris Lattner149c58c2005-08-16 18:17:10 +00002972 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002973 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002974 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002975 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002976}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002977
Evan Cheng95514ba2006-08-26 08:00:10 +00002978SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2979 MVT::ValueType VT, SDOperand Op1,
2980 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002981 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002982 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002983 SDOperand Ops[] = { Op1, Op2 };
2984
Jim Laskey583bd472006-10-27 23:46:08 +00002985 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002986 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002987 void *IP = 0;
2988 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002989 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002990
Chris Lattner149c58c2005-08-16 18:17:10 +00002991 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002992
Chris Lattner63e3f142007-02-04 07:28:00 +00002993 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002994
Chris Lattnera5682852006-08-07 23:03:03 +00002995 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002996 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002997}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002998
Evan Cheng95514ba2006-08-26 08:00:10 +00002999SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3000 MVT::ValueType VT, SDOperand Op1,
3001 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003002 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003003 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003004 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003005 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003006 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003007 void *IP = 0;
3008 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003009 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003010
Chris Lattner149c58c2005-08-16 18:17:10 +00003011 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003012
Chris Lattner63e3f142007-02-04 07:28:00 +00003013 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003014
Chris Lattnera5682852006-08-07 23:03:03 +00003015 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003016 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003017}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003018
Evan Cheng95514ba2006-08-26 08:00:10 +00003019SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003020 MVT::ValueType VT, const SDOperand *Ops,
3021 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003022 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003023 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003024 FoldingSetNodeID ID;
3025 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003026 void *IP = 0;
3027 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003028 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003029
Chris Lattner6b09a292005-08-21 18:49:33 +00003030 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003031 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003032
Chris Lattnera5682852006-08-07 23:03:03 +00003033 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003034 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003035}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003036
Evan Cheng95514ba2006-08-26 08:00:10 +00003037SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3038 MVT::ValueType VT1, MVT::ValueType VT2,
3039 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003040 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003041 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003042 SDOperand Ops[] = { Op1, Op2 };
3043 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003044 void *IP = 0;
3045 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003046 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003047
Chris Lattner0fb094f2005-11-19 01:44:53 +00003048 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003049 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003050 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003051 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003052}
3053
Evan Cheng95514ba2006-08-26 08:00:10 +00003054SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3055 MVT::ValueType VT1, MVT::ValueType VT2,
3056 SDOperand Op1, SDOperand Op2,
3057 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003058 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003059 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003060 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003061 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003062 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003063 void *IP = 0;
3064 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003065 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003066
Chris Lattner0fb094f2005-11-19 01:44:53 +00003067 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003068
Chris Lattner63e3f142007-02-04 07:28:00 +00003069 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003070 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003071 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003072}
3073
Chris Lattner0fb094f2005-11-19 01:44:53 +00003074
Evan Cheng6ae46c42006-02-09 07:15:23 +00003075/// getTargetNode - These are used for target selectors to create a new node
3076/// with specified return type(s), target opcode, and operands.
3077///
3078/// Note that getTargetNode returns the resultant node. If there is already a
3079/// node of the specified opcode and operands, it returns that node instead of
3080/// the current one.
3081SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3082 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3083}
3084SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3085 SDOperand Op1) {
3086 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3087}
3088SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3089 SDOperand Op1, SDOperand Op2) {
3090 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3091}
3092SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003093 SDOperand Op1, SDOperand Op2,
3094 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003095 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3096}
3097SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003098 const SDOperand *Ops, unsigned NumOps) {
3099 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003100}
3101SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003102 MVT::ValueType VT2) {
3103 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3104 SDOperand Op;
3105 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3106}
3107SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003108 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003109 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003110 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003111}
3112SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003113 MVT::ValueType VT2, SDOperand Op1,
3114 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003115 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003116 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003117 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003118}
3119SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003120 MVT::ValueType VT2, SDOperand Op1,
3121 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003122 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003123 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003124 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003125}
Evan Cheng694481e2006-08-27 08:08:54 +00003126SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3127 MVT::ValueType VT2,
3128 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003129 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003130 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003131}
3132SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3133 MVT::ValueType VT2, MVT::ValueType VT3,
3134 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003135 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003136 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003137 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003138}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003139SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3140 MVT::ValueType VT2, MVT::ValueType VT3,
3141 SDOperand Op1, SDOperand Op2,
3142 SDOperand Op3) {
3143 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3144 SDOperand Ops[] = { Op1, Op2, Op3 };
3145 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3146}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003147SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003148 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003149 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003150 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3151 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003152}
Evan Cheng05e69c12007-09-12 23:39:49 +00003153SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3154 MVT::ValueType VT2, MVT::ValueType VT3,
3155 MVT::ValueType VT4,
3156 const SDOperand *Ops, unsigned NumOps) {
3157 std::vector<MVT::ValueType> VTList;
3158 VTList.push_back(VT1);
3159 VTList.push_back(VT2);
3160 VTList.push_back(VT3);
3161 VTList.push_back(VT4);
3162 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3163 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3164}
Evan Cheng39305cf2007-10-05 01:10:49 +00003165SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3166 std::vector<MVT::ValueType> &ResultTys,
3167 const SDOperand *Ops, unsigned NumOps) {
3168 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3169 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3170 Ops, NumOps).Val;
3171}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003172
Evan Cheng99157a02006-08-07 22:13:29 +00003173/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003174/// This can cause recursive merging of nodes in the DAG.
3175///
Chris Lattner11d049c2008-02-03 03:35:22 +00003176/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003177///
Chris Lattner11d049c2008-02-03 03:35:22 +00003178void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003179 std::vector<SDNode*> *Deleted) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003180 SDNode *From = FromN.Val;
3181 // FIXME: This works around a dag isel emitter bug.
3182 if (From->getNumValues() == 1 && FromN.ResNo != 0)
3183 return; // FIXME: THIS IS BOGUS
3184
3185 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003186 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003187 assert(From != To.Val && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003188
Chris Lattner8b8749f2005-08-17 19:00:20 +00003189 while (!From->use_empty()) {
3190 // Process users until they are all gone.
3191 SDNode *U = *From->use_begin();
3192
3193 // This node is about to morph, remove its old self from the CSE maps.
3194 RemoveNodeFromCSEMaps(U);
3195
Chris Lattner65113b22005-11-08 22:07:03 +00003196 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3197 I != E; ++I)
3198 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003199 From->removeUser(U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003200 *I = To;
3201 To.Val->addUser(U);
Chris Lattner8b52f212005-08-26 18:36:28 +00003202 }
3203
3204 // Now that we have modified U, add it back to the CSE maps. If it already
3205 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003206 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3207 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003208 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003209 if (Deleted) Deleted->push_back(U);
3210 DeleteNodeNotInCSEMaps(U);
3211 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003212 }
3213}
3214
3215/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3216/// This can cause recursive merging of nodes in the DAG.
3217///
3218/// This version assumes From/To have matching types and numbers of result
3219/// values.
3220///
Chris Lattner1e111c72005-09-07 05:37:01 +00003221void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3222 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003223 assert(From != To && "Cannot replace uses of with self");
3224 assert(From->getNumValues() == To->getNumValues() &&
3225 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003226 if (From->getNumValues() == 1) // If possible, use the faster version.
3227 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003228
3229 while (!From->use_empty()) {
3230 // Process users until they are all gone.
3231 SDNode *U = *From->use_begin();
3232
3233 // This node is about to morph, remove its old self from the CSE maps.
3234 RemoveNodeFromCSEMaps(U);
3235
Chris Lattner65113b22005-11-08 22:07:03 +00003236 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3237 I != E; ++I)
3238 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003239 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003240 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003241 To->addUser(U);
3242 }
3243
3244 // Now that we have modified U, add it back to the CSE maps. If it already
3245 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003246 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3247 ReplaceAllUsesWith(U, Existing, Deleted);
3248 // U is now dead.
3249 if (Deleted) Deleted->push_back(U);
3250 DeleteNodeNotInCSEMaps(U);
3251 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003252 }
3253}
3254
Chris Lattner8b52f212005-08-26 18:36:28 +00003255/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3256/// This can cause recursive merging of nodes in the DAG.
3257///
3258/// This version can replace From with any result values. To must match the
3259/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003260void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003261 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003262 std::vector<SDNode*> *Deleted) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003263 if (From->getNumValues() == 1) // Handle the simple case efficiently.
3264 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003265
3266 while (!From->use_empty()) {
3267 // Process users until they are all gone.
3268 SDNode *U = *From->use_begin();
3269
3270 // This node is about to morph, remove its old self from the CSE maps.
3271 RemoveNodeFromCSEMaps(U);
3272
Chris Lattner65113b22005-11-08 22:07:03 +00003273 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3274 I != E; ++I)
3275 if (I->Val == From) {
3276 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003277 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003278 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003279 ToOp.Val->addUser(U);
3280 }
3281
3282 // Now that we have modified U, add it back to the CSE maps. If it already
3283 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003284 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3285 ReplaceAllUsesWith(U, Existing, Deleted);
3286 // U is now dead.
3287 if (Deleted) Deleted->push_back(U);
3288 DeleteNodeNotInCSEMaps(U);
3289 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003290 }
3291}
3292
Chris Lattner012f2412006-02-17 21:58:01 +00003293/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3294/// uses of other values produced by From.Val alone. The Deleted vector is
3295/// handled the same was as for ReplaceAllUsesWith.
3296void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003297 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003298 assert(From != To && "Cannot replace a value with itself");
3299 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003300 if (From.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003301 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003302 return;
3303 }
3304
Chris Lattnercf5640b2007-02-04 00:14:31 +00003305 // Get all of the users of From.Val. We want these in a nice,
3306 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3307 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003308
Chris Lattner01d029b2007-10-15 06:10:22 +00003309 std::vector<SDNode*> LocalDeletionVector;
3310
3311 // Pick a deletion vector to use. If the user specified one, use theirs,
3312 // otherwise use a local one.
3313 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003314 while (!Users.empty()) {
3315 // We know that this user uses some value of From. If it is the right
3316 // value, update it.
3317 SDNode *User = Users.back();
3318 Users.pop_back();
3319
Chris Lattner01d029b2007-10-15 06:10:22 +00003320 // Scan for an operand that matches From.
3321 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3322 for (; Op != E; ++Op)
3323 if (*Op == From) break;
3324
3325 // If there are no matches, the user must use some other result of From.
3326 if (Op == E) continue;
3327
3328 // Okay, we know this user needs to be updated. Remove its old self
3329 // from the CSE maps.
3330 RemoveNodeFromCSEMaps(User);
3331
3332 // Update all operands that match "From".
3333 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003334 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003335 From.Val->removeUser(User);
3336 *Op = To;
3337 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003338 }
3339 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003340
3341 // Now that we have modified User, add it back to the CSE maps. If it
3342 // already exists there, recursively merge the results together.
3343 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3344 if (!Existing) continue; // Continue on to next user.
3345
3346 // If there was already an existing matching node, use ReplaceAllUsesWith
3347 // to replace the dead one with the existing one. However, this can cause
3348 // recursive merging of other unrelated nodes down the line. The merging
3349 // can cause deletion of nodes that used the old value. In this case,
3350 // we have to be certain to remove them from the Users set.
3351 unsigned NumDeleted = DeleteVector->size();
3352 ReplaceAllUsesWith(User, Existing, DeleteVector);
3353
3354 // User is now dead.
3355 DeleteVector->push_back(User);
3356 DeleteNodeNotInCSEMaps(User);
3357
3358 // We have to be careful here, because ReplaceAllUsesWith could have
3359 // deleted a user of From, which means there may be dangling pointers
3360 // in the "Users" setvector. Scan over the deleted node pointers and
3361 // remove them from the setvector.
3362 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3363 Users.remove((*DeleteVector)[i]);
3364
3365 // If the user doesn't need the set of deleted elements, don't retain them
3366 // to the next loop iteration.
3367 if (Deleted == 0)
3368 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003369 }
3370}
3371
Chris Lattner7b2880c2005-08-24 22:44:39 +00003372
Evan Chenge6f35d82006-08-01 08:20:41 +00003373/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3374/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003375unsigned SelectionDAG::AssignNodeIds() {
3376 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003377 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3378 SDNode *N = I;
3379 N->setNodeId(Id++);
3380 }
3381 return Id;
3382}
3383
Evan Chenge6f35d82006-08-01 08:20:41 +00003384/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003385/// based on their topological order. It returns the maximum id and a vector
3386/// of the SDNodes* in assigned order by reference.
3387unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003388 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003389 std::vector<unsigned> InDegree(DAGSize);
3390 std::vector<SDNode*> Sources;
3391
3392 // Use a two pass approach to avoid using a std::map which is slow.
3393 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003394 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3395 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003396 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003397 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003398 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003399 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003400 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003401 }
3402
Evan Cheng99157a02006-08-07 22:13:29 +00003403 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003404 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003405 SDNode *N = Sources.back();
3406 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003407 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003408 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3409 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003410 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003411 if (Degree == 0)
3412 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003413 }
3414 }
3415
Evan Chengc384d6c2006-08-02 22:00:34 +00003416 // Second pass, assign the actual topological order as node ids.
3417 Id = 0;
3418 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3419 TI != TE; ++TI)
3420 (*TI)->setNodeId(Id++);
3421
3422 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003423}
3424
3425
Evan Cheng091cba12006-07-27 06:39:06 +00003426
Jim Laskey58b968b2005-08-17 20:08:02 +00003427//===----------------------------------------------------------------------===//
3428// SDNode Class
3429//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003430
Chris Lattner917d2c92006-07-19 00:00:37 +00003431// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003432void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003433void UnarySDNode::ANCHOR() {}
3434void BinarySDNode::ANCHOR() {}
3435void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003436void HandleSDNode::ANCHOR() {}
3437void StringSDNode::ANCHOR() {}
3438void ConstantSDNode::ANCHOR() {}
3439void ConstantFPSDNode::ANCHOR() {}
3440void GlobalAddressSDNode::ANCHOR() {}
3441void FrameIndexSDNode::ANCHOR() {}
3442void JumpTableSDNode::ANCHOR() {}
3443void ConstantPoolSDNode::ANCHOR() {}
3444void BasicBlockSDNode::ANCHOR() {}
3445void SrcValueSDNode::ANCHOR() {}
3446void RegisterSDNode::ANCHOR() {}
3447void ExternalSymbolSDNode::ANCHOR() {}
3448void CondCodeSDNode::ANCHOR() {}
3449void VTSDNode::ANCHOR() {}
3450void LoadSDNode::ANCHOR() {}
3451void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003452
Chris Lattner48b85922007-02-04 02:41:42 +00003453HandleSDNode::~HandleSDNode() {
3454 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003455 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003456}
3457
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003458GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3459 MVT::ValueType VT, int o)
3460 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003461 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003462 // Thread Local
3463 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3464 // Non Thread Local
3465 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3466 getSDVTList(VT)), Offset(o) {
3467 TheGlobal = const_cast<GlobalValue*>(GA);
3468}
Chris Lattner48b85922007-02-04 02:41:42 +00003469
Jim Laskey583bd472006-10-27 23:46:08 +00003470/// Profile - Gather unique data for the node.
3471///
3472void SDNode::Profile(FoldingSetNodeID &ID) {
3473 AddNodeIDNode(ID, this);
3474}
3475
Chris Lattnera3255112005-11-08 23:30:28 +00003476/// getValueTypeList - Return a pointer to the specified value type.
3477///
3478MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003479 if (MVT::isExtendedVT(VT)) {
3480 static std::set<MVT::ValueType> EVTs;
3481 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3482 } else {
3483 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3484 VTs[VT] = VT;
3485 return &VTs[VT];
3486 }
Chris Lattnera3255112005-11-08 23:30:28 +00003487}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003488
Chris Lattner5c884562005-01-12 18:37:47 +00003489/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3490/// indicated value. This method ignores uses of other values defined by this
3491/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003492bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003493 assert(Value < getNumValues() && "Bad value!");
3494
3495 // If there is only one value, this is easy.
3496 if (getNumValues() == 1)
3497 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003498 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003499
Evan Cheng4ee62112006-02-05 06:29:23 +00003500 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003501
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003502 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003503
Chris Lattnerf83482d2006-08-16 20:59:32 +00003504 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003505 SDNode *User = *UI;
3506 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003507 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003508 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3509 if (User->getOperand(i) == TheValue) {
3510 if (NUses == 0)
3511 return false; // too many uses
3512 --NUses;
3513 }
3514 }
3515
3516 // Found exactly the right number of uses?
3517 return NUses == 0;
3518}
3519
3520
Evan Cheng33d55952007-08-02 05:29:38 +00003521/// hasAnyUseOfValue - Return true if there are any use of the indicated
3522/// value. This method ignores uses of other values defined by this operation.
3523bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3524 assert(Value < getNumValues() && "Bad value!");
3525
Dan Gohman30359592008-01-29 13:02:09 +00003526 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003527
3528 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3529
3530 SmallPtrSet<SDNode*, 32> UsersHandled;
3531
3532 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3533 SDNode *User = *UI;
3534 if (User->getNumOperands() == 1 ||
3535 UsersHandled.insert(User)) // First time we've seen this?
3536 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3537 if (User->getOperand(i) == TheValue) {
3538 return true;
3539 }
3540 }
3541
3542 return false;
3543}
3544
3545
Evan Chenge6e97e62006-11-03 07:31:32 +00003546/// isOnlyUse - Return true if this node is the only use of N.
3547///
Evan Cheng4ee62112006-02-05 06:29:23 +00003548bool SDNode::isOnlyUse(SDNode *N) const {
3549 bool Seen = false;
3550 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3551 SDNode *User = *I;
3552 if (User == this)
3553 Seen = true;
3554 else
3555 return false;
3556 }
3557
3558 return Seen;
3559}
3560
Evan Chenge6e97e62006-11-03 07:31:32 +00003561/// isOperand - Return true if this node is an operand of N.
3562///
Evan Chengbfa284f2006-03-03 06:42:32 +00003563bool SDOperand::isOperand(SDNode *N) const {
3564 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3565 if (*this == N->getOperand(i))
3566 return true;
3567 return false;
3568}
3569
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003570bool SDNode::isOperand(SDNode *N) const {
3571 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3572 if (this == N->OperandList[i].Val)
3573 return true;
3574 return false;
3575}
Evan Cheng4ee62112006-02-05 06:29:23 +00003576
Chris Lattner572dee72008-01-16 05:49:24 +00003577/// reachesChainWithoutSideEffects - Return true if this operand (which must
3578/// be a chain) reaches the specified operand without crossing any
3579/// side-effecting instructions. In practice, this looks through token
3580/// factors and non-volatile loads. In order to remain efficient, this only
3581/// looks a couple of nodes in, it does not do an exhaustive search.
3582bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3583 unsigned Depth) const {
3584 if (*this == Dest) return true;
3585
3586 // Don't search too deeply, we just want to be able to see through
3587 // TokenFactor's etc.
3588 if (Depth == 0) return false;
3589
3590 // If this is a token factor, all inputs to the TF happen in parallel. If any
3591 // of the operands of the TF reach dest, then we can do the xform.
3592 if (getOpcode() == ISD::TokenFactor) {
3593 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3594 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3595 return true;
3596 return false;
3597 }
3598
3599 // Loads don't have side effects, look through them.
3600 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3601 if (!Ld->isVolatile())
3602 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3603 }
3604 return false;
3605}
3606
3607
Evan Chengc5fc57d2006-11-03 03:05:24 +00003608static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003609 SmallPtrSet<SDNode *, 32> &Visited) {
3610 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003611 return;
3612
3613 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3614 SDNode *Op = N->getOperand(i).Val;
3615 if (Op == P) {
3616 found = true;
3617 return;
3618 }
3619 findPredecessor(Op, P, found, Visited);
3620 }
3621}
3622
Evan Chenge6e97e62006-11-03 07:31:32 +00003623/// isPredecessor - Return true if this node is a predecessor of N. This node
3624/// is either an operand of N or it can be reached by recursively traversing
3625/// up the operands.
3626/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003627bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003628 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003629 bool found = false;
3630 findPredecessor(N, this, found, Visited);
3631 return found;
3632}
3633
Evan Chengc5484282006-10-04 00:56:09 +00003634uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3635 assert(Num < NumOperands && "Invalid child # of SDNode!");
3636 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3637}
3638
Reid Spencer577cc322007-04-01 07:32:19 +00003639std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003640 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003641 default:
3642 if (getOpcode() < ISD::BUILTIN_OP_END)
3643 return "<<Unknown DAG Node>>";
3644 else {
Evan Cheng72261582005-12-20 06:22:03 +00003645 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003646 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003647 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003648 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003649
Evan Cheng72261582005-12-20 06:22:03 +00003650 TargetLowering &TLI = G->getTargetLoweringInfo();
3651 const char *Name =
3652 TLI.getTargetNodeName(getOpcode());
3653 if (Name) return Name;
3654 }
3655
3656 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003657 }
3658
Andrew Lenharth95762122005-03-31 21:24:06 +00003659 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003660 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003661 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003662 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003663 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003664 case ISD::AssertSext: return "AssertSext";
3665 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003666
3667 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003668 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003669 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003670 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003671
3672 case ISD::Constant: return "Constant";
3673 case ISD::ConstantFP: return "ConstantFP";
3674 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003675 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003676 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003677 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003678 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003679 case ISD::RETURNADDR: return "RETURNADDR";
3680 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003681 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003682 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3683 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003684 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003685 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003686 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003687 case ISD::INTRINSIC_WO_CHAIN: {
3688 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3689 return Intrinsic::getName((Intrinsic::ID)IID);
3690 }
3691 case ISD::INTRINSIC_VOID:
3692 case ISD::INTRINSIC_W_CHAIN: {
3693 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003694 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003695 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003696
Chris Lattnerb2827b02006-03-19 00:52:58 +00003697 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003698 case ISD::TargetConstant: return "TargetConstant";
3699 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003700 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003701 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003702 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003703 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003704 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003705 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003706
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003707 case ISD::CopyToReg: return "CopyToReg";
3708 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003709 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003710 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003711 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003712 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00003713 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003714 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003715 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003716 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003717
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003718 // Unary operators
3719 case ISD::FABS: return "fabs";
3720 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003721 case ISD::FSQRT: return "fsqrt";
3722 case ISD::FSIN: return "fsin";
3723 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003724 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003725 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003726
3727 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003728 case ISD::ADD: return "add";
3729 case ISD::SUB: return "sub";
3730 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003731 case ISD::MULHU: return "mulhu";
3732 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003733 case ISD::SDIV: return "sdiv";
3734 case ISD::UDIV: return "udiv";
3735 case ISD::SREM: return "srem";
3736 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003737 case ISD::SMUL_LOHI: return "smul_lohi";
3738 case ISD::UMUL_LOHI: return "umul_lohi";
3739 case ISD::SDIVREM: return "sdivrem";
3740 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003741 case ISD::AND: return "and";
3742 case ISD::OR: return "or";
3743 case ISD::XOR: return "xor";
3744 case ISD::SHL: return "shl";
3745 case ISD::SRA: return "sra";
3746 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003747 case ISD::ROTL: return "rotl";
3748 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003749 case ISD::FADD: return "fadd";
3750 case ISD::FSUB: return "fsub";
3751 case ISD::FMUL: return "fmul";
3752 case ISD::FDIV: return "fdiv";
3753 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003754 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003755 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003756
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003757 case ISD::SETCC: return "setcc";
3758 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003759 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003760 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003761 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003762 case ISD::CONCAT_VECTORS: return "concat_vectors";
3763 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003764 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003765 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003766 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003767 case ISD::ADDC: return "addc";
3768 case ISD::ADDE: return "adde";
3769 case ISD::SUBC: return "subc";
3770 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003771 case ISD::SHL_PARTS: return "shl_parts";
3772 case ISD::SRA_PARTS: return "sra_parts";
3773 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003774
3775 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3776 case ISD::INSERT_SUBREG: return "insert_subreg";
3777
Chris Lattner7f644642005-04-28 21:44:03 +00003778 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003779 case ISD::SIGN_EXTEND: return "sign_extend";
3780 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003781 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003782 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003783 case ISD::TRUNCATE: return "truncate";
3784 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003785 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003786 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003787 case ISD::FP_EXTEND: return "fp_extend";
3788
3789 case ISD::SINT_TO_FP: return "sint_to_fp";
3790 case ISD::UINT_TO_FP: return "uint_to_fp";
3791 case ISD::FP_TO_SINT: return "fp_to_sint";
3792 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003793 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003794
3795 // Control flow instructions
3796 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003797 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003798 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003799 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003800 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003801 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003802 case ISD::CALLSEQ_START: return "callseq_start";
3803 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003804
3805 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003806 case ISD::LOAD: return "load";
3807 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003808 case ISD::VAARG: return "vaarg";
3809 case ISD::VACOPY: return "vacopy";
3810 case ISD::VAEND: return "vaend";
3811 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003812 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003813 case ISD::EXTRACT_ELEMENT: return "extract_element";
3814 case ISD::BUILD_PAIR: return "build_pair";
3815 case ISD::STACKSAVE: return "stacksave";
3816 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003817 case ISD::TRAP: return "trap";
3818
Chris Lattner5a67afc2006-01-13 02:39:42 +00003819 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003820 case ISD::MEMSET: return "memset";
3821 case ISD::MEMCPY: return "memcpy";
3822 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003823
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003824 // Bit manipulation
3825 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003826 case ISD::CTPOP: return "ctpop";
3827 case ISD::CTTZ: return "cttz";
3828 case ISD::CTLZ: return "ctlz";
3829
Chris Lattner36ce6912005-11-29 06:21:05 +00003830 // Debug info
3831 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003832 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003833
Duncan Sands36397f52007-07-27 12:58:54 +00003834 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003835 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003836
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003837 case ISD::CONDCODE:
3838 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003839 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003840 case ISD::SETOEQ: return "setoeq";
3841 case ISD::SETOGT: return "setogt";
3842 case ISD::SETOGE: return "setoge";
3843 case ISD::SETOLT: return "setolt";
3844 case ISD::SETOLE: return "setole";
3845 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003846
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003847 case ISD::SETO: return "seto";
3848 case ISD::SETUO: return "setuo";
3849 case ISD::SETUEQ: return "setue";
3850 case ISD::SETUGT: return "setugt";
3851 case ISD::SETUGE: return "setuge";
3852 case ISD::SETULT: return "setult";
3853 case ISD::SETULE: return "setule";
3854 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003855
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003856 case ISD::SETEQ: return "seteq";
3857 case ISD::SETGT: return "setgt";
3858 case ISD::SETGE: return "setge";
3859 case ISD::SETLT: return "setlt";
3860 case ISD::SETLE: return "setle";
3861 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003862 }
3863 }
3864}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003865
Evan Cheng144d8f02006-11-09 17:55:04 +00003866const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003867 switch (AM) {
3868 default:
3869 return "";
3870 case ISD::PRE_INC:
3871 return "<pre-inc>";
3872 case ISD::PRE_DEC:
3873 return "<pre-dec>";
3874 case ISD::POST_INC:
3875 return "<post-inc>";
3876 case ISD::POST_DEC:
3877 return "<post-dec>";
3878 }
3879}
3880
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003881void SDNode::dump() const { dump(0); }
3882void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003883 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003884
3885 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003886 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003887 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003888 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003889 else
Bill Wendling832171c2006-12-07 20:04:42 +00003890 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003891 }
Bill Wendling832171c2006-12-07 20:04:42 +00003892 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003893
Bill Wendling832171c2006-12-07 20:04:42 +00003894 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003895 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003896 if (i) cerr << ", ";
3897 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003898 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003899 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003900 }
3901
Evan Chengce254432007-12-11 02:08:35 +00003902 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3903 SDNode *Mask = getOperand(2).Val;
3904 cerr << "<";
3905 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3906 if (i) cerr << ",";
3907 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3908 cerr << "u";
3909 else
3910 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3911 }
3912 cerr << ">";
3913 }
3914
Chris Lattnerc3aae252005-01-07 07:46:32 +00003915 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003916 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003917 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003918 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3919 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3920 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3921 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3922 else {
3923 cerr << "<APFloat(";
3924 CSDN->getValueAPF().convertToAPInt().dump();
3925 cerr << ")>";
3926 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003927 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003928 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003929 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003930 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003931 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003932 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003933 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003934 else
Bill Wendling832171c2006-12-07 20:04:42 +00003935 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003936 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003937 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003938 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003939 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003940 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003941 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003942 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003943 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003944 else
Bill Wendling832171c2006-12-07 20:04:42 +00003945 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003946 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003947 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003948 else
Bill Wendling832171c2006-12-07 20:04:42 +00003949 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003950 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003951 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003952 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3953 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003954 cerr << LBB->getName() << " ";
3955 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003956 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003957 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003958 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003959 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003960 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003961 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003962 } else if (const ExternalSymbolSDNode *ES =
3963 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003964 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003965 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3966 if (M->getValue())
Evan Cheng334dc1f2008-01-31 21:00:00 +00003967 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003968 else
Evan Cheng334dc1f2008-01-31 21:00:00 +00003969 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003970 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003971 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003972 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00003973 const Value *SrcValue = LD->getSrcValue();
3974 int SrcOffset = LD->getSrcValueOffset();
3975 cerr << " <";
3976 if (SrcValue)
3977 cerr << SrcValue;
3978 else
3979 cerr << "null";
3980 cerr << ":" << SrcOffset << ">";
3981
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003982 bool doExt = true;
3983 switch (LD->getExtensionType()) {
3984 default: doExt = false; break;
3985 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003986 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003987 break;
3988 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003989 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003990 break;
3991 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003992 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003993 break;
3994 }
3995 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003996 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003997
Evan Cheng144d8f02006-11-09 17:55:04 +00003998 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003999 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004000 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004001 if (LD->isVolatile())
4002 cerr << " <volatile>";
4003 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004004 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004005 const Value *SrcValue = ST->getSrcValue();
4006 int SrcOffset = ST->getSrcValueOffset();
4007 cerr << " <";
4008 if (SrcValue)
4009 cerr << SrcValue;
4010 else
4011 cerr << "null";
4012 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004013
4014 if (ST->isTruncatingStore())
4015 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004016 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004017
4018 const char *AM = getIndexedModeName(ST->getAddressingMode());
4019 if (*AM)
4020 cerr << " " << AM;
4021 if (ST->isVolatile())
4022 cerr << " <volatile>";
4023 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004024 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004025}
4026
Chris Lattnerde202b32005-11-09 23:47:37 +00004027static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004028 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4029 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004030 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004031 else
Bill Wendling832171c2006-12-07 20:04:42 +00004032 cerr << "\n" << std::string(indent+2, ' ')
4033 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004034
Chris Lattnerea946cd2005-01-09 20:38:33 +00004035
Bill Wendling832171c2006-12-07 20:04:42 +00004036 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004037 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004038}
4039
Chris Lattnerc3aae252005-01-07 07:46:32 +00004040void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004041 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004042 std::vector<const SDNode*> Nodes;
4043 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4044 I != E; ++I)
4045 Nodes.push_back(I);
4046
Chris Lattner49d24712005-01-09 20:26:36 +00004047 std::sort(Nodes.begin(), Nodes.end());
4048
4049 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004050 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004051 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004052 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004053
Jim Laskey26f7fa72006-10-17 19:33:52 +00004054 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004055
Bill Wendling832171c2006-12-07 20:04:42 +00004056 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004057}
4058
Evan Chengd6594ae2006-09-12 21:00:35 +00004059const Type *ConstantPoolSDNode::getType() const {
4060 if (isMachineConstantPoolEntry())
4061 return Val.MachineCPVal->getType();
4062 return Val.ConstVal->getType();
4063}