blob: 09e8b579a0fd3438ec21ead607b44f1f5e35799a [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000023#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000024#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000025#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000026#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000029#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000030#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000031#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000032#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000033#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000034#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000035#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner0b3e5252006-08-15 19:11:05 +000038/// makeVTList - Return an instance of the SDVTList struct initialized with the
39/// specified members.
40static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
41 SDVTList Res = {VTs, NumVTs};
42 return Res;
43}
44
Jim Laskey58b968b2005-08-17 20:08:02 +000045//===----------------------------------------------------------------------===//
46// ConstantFPSDNode Class
47//===----------------------------------------------------------------------===//
48
49/// isExactlyValue - We don't rely on operator== working on double values, as
50/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
51/// As such, this method can be used to do an exact bit-for-bit comparison of
52/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000053bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000054 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000055}
56
Dale Johannesenf04afdb2007-08-30 00:23:21 +000057bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
58 const APFloat& Val) {
59 // convert modifies in place, so make a copy.
60 APFloat Val2 = APFloat(Val);
61 switch (VT) {
62 default:
63 return false; // These can't be represented as floating point!
64
65 // FIXME rounding mode needs to be more flexible
66 case MVT::f32:
67 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
68 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
69 APFloat::opOK;
70 case MVT::f64:
71 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
72 &Val2.getSemantics() == &APFloat::IEEEdouble ||
73 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
74 APFloat::opOK;
75 // TODO: Figure out how to test if we can use a shorter type instead!
76 case MVT::f80:
77 case MVT::f128:
78 case MVT::ppcf128:
79 return true;
80 }
81}
82
Jim Laskey58b968b2005-08-17 20:08:02 +000083//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000084// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000085//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000086
Evan Chenga8df1662006-03-27 06:58:47 +000087/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000088/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000089bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000090 // Look through a bit convert.
91 if (N->getOpcode() == ISD::BIT_CONVERT)
92 N = N->getOperand(0).Val;
93
Evan Chenga8df1662006-03-27 06:58:47 +000094 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000095
96 unsigned i = 0, e = N->getNumOperands();
97
98 // Skip over all of the undef values.
99 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
100 ++i;
101
102 // Do not accept an all-undef vector.
103 if (i == e) return false;
104
105 // Do not accept build_vectors that aren't all constants or which have non-~0
106 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000107 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000108 if (isa<ConstantSDNode>(NotZero)) {
109 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
110 return false;
111 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000112 MVT::ValueType VT = NotZero.getValueType();
113 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000114 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
115 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000116 return false;
117 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000118 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
119 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000120 (uint32_t)-1)
121 return false;
122 }
Evan Chenga8df1662006-03-27 06:58:47 +0000123 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000124 return false;
125
126 // Okay, we have at least one ~0 value, check to see if the rest match or are
127 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000128 for (++i; i != e; ++i)
129 if (N->getOperand(i) != NotZero &&
130 N->getOperand(i).getOpcode() != ISD::UNDEF)
131 return false;
132 return true;
133}
134
135
Evan Cheng4a147842006-03-26 09:50:58 +0000136/// isBuildVectorAllZeros - Return true if the specified node is a
137/// BUILD_VECTOR where all of the elements are 0 or undef.
138bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000139 // Look through a bit convert.
140 if (N->getOpcode() == ISD::BIT_CONVERT)
141 N = N->getOperand(0).Val;
142
Evan Cheng4a147842006-03-26 09:50:58 +0000143 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000144
145 unsigned i = 0, e = N->getNumOperands();
146
147 // Skip over all of the undef values.
148 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
149 ++i;
150
151 // Do not accept an all-undef vector.
152 if (i == e) return false;
153
154 // Do not accept build_vectors that aren't all constants or which have non-~0
155 // elements.
156 SDOperand Zero = N->getOperand(i);
157 if (isa<ConstantSDNode>(Zero)) {
158 if (!cast<ConstantSDNode>(Zero)->isNullValue())
159 return false;
160 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000161 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000162 return false;
163 } else
164 return false;
165
166 // Okay, we have at least one ~0 value, check to see if the rest match or are
167 // undefs.
168 for (++i; i != e; ++i)
169 if (N->getOperand(i) != Zero &&
170 N->getOperand(i).getOpcode() != ISD::UNDEF)
171 return false;
172 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000173}
174
Chris Lattnerc3aae252005-01-07 07:46:32 +0000175/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
176/// when given the operation for (X op Y).
177ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
178 // To perform this operation, we just need to swap the L and G bits of the
179 // operation.
180 unsigned OldL = (Operation >> 2) & 1;
181 unsigned OldG = (Operation >> 1) & 1;
182 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
183 (OldL << 1) | // New G bit
184 (OldG << 2)); // New L bit.
185}
186
187/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
188/// 'op' is a valid SetCC operation.
189ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
190 unsigned Operation = Op;
191 if (isInteger)
192 Operation ^= 7; // Flip L, G, E bits, but not U.
193 else
194 Operation ^= 15; // Flip all of the condition bits.
195 if (Operation > ISD::SETTRUE2)
196 Operation &= ~8; // Don't let N and U bits get set.
197 return ISD::CondCode(Operation);
198}
199
200
201/// isSignedOp - For an integer comparison, return 1 if the comparison is a
202/// signed operation and 2 if the result is an unsigned comparison. Return zero
203/// if the operation does not depend on the sign of the input (setne and seteq).
204static int isSignedOp(ISD::CondCode Opcode) {
205 switch (Opcode) {
206 default: assert(0 && "Illegal integer setcc operation!");
207 case ISD::SETEQ:
208 case ISD::SETNE: return 0;
209 case ISD::SETLT:
210 case ISD::SETLE:
211 case ISD::SETGT:
212 case ISD::SETGE: return 1;
213 case ISD::SETULT:
214 case ISD::SETULE:
215 case ISD::SETUGT:
216 case ISD::SETUGE: return 2;
217 }
218}
219
220/// getSetCCOrOperation - Return the result of a logical OR between different
221/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
222/// returns SETCC_INVALID if it is not possible to represent the resultant
223/// comparison.
224ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
225 bool isInteger) {
226 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
227 // Cannot fold a signed integer setcc with an unsigned integer setcc.
228 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000229
Chris Lattnerc3aae252005-01-07 07:46:32 +0000230 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000231
Chris Lattnerc3aae252005-01-07 07:46:32 +0000232 // If the N and U bits get set then the resultant comparison DOES suddenly
233 // care about orderedness, and is true when ordered.
234 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000235 Op &= ~16; // Clear the U bit if the N bit is set.
236
237 // Canonicalize illegal integer setcc's.
238 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
239 Op = ISD::SETNE;
240
Chris Lattnerc3aae252005-01-07 07:46:32 +0000241 return ISD::CondCode(Op);
242}
243
244/// getSetCCAndOperation - Return the result of a logical AND between different
245/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
246/// function returns zero if it is not possible to represent the resultant
247/// comparison.
248ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
249 bool isInteger) {
250 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
251 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000252 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000253
254 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000255 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
256
257 // Canonicalize illegal integer setcc's.
258 if (isInteger) {
259 switch (Result) {
260 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000261 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
262 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
263 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
264 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000265 }
266 }
267
268 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000269}
270
Chris Lattnerb48da392005-01-23 04:39:44 +0000271const TargetMachine &SelectionDAG::getTarget() const {
272 return TLI.getTargetMachine();
273}
274
Jim Laskey58b968b2005-08-17 20:08:02 +0000275//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000276// SDNode Profile Support
277//===----------------------------------------------------------------------===//
278
Jim Laskeydef69b92006-10-27 23:52:51 +0000279/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
280///
Jim Laskey583bd472006-10-27 23:46:08 +0000281static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
282 ID.AddInteger(OpC);
283}
284
285/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
286/// solely with their pointer.
287void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
288 ID.AddPointer(VTList.VTs);
289}
290
Jim Laskeydef69b92006-10-27 23:52:51 +0000291/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
292///
Jim Laskey583bd472006-10-27 23:46:08 +0000293static void AddNodeIDOperands(FoldingSetNodeID &ID,
294 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000295 for (; NumOps; --NumOps, ++Ops) {
296 ID.AddPointer(Ops->Val);
297 ID.AddInteger(Ops->ResNo);
298 }
Jim Laskey583bd472006-10-27 23:46:08 +0000299}
300
Jim Laskey583bd472006-10-27 23:46:08 +0000301static void AddNodeIDNode(FoldingSetNodeID &ID,
302 unsigned short OpC, SDVTList VTList,
303 const SDOperand *OpList, unsigned N) {
304 AddNodeIDOpcode(ID, OpC);
305 AddNodeIDValueTypes(ID, VTList);
306 AddNodeIDOperands(ID, OpList, N);
307}
308
Jim Laskeydef69b92006-10-27 23:52:51 +0000309/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
310/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000311static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
312 AddNodeIDOpcode(ID, N->getOpcode());
313 // Add the return value info.
314 AddNodeIDValueTypes(ID, N->getVTList());
315 // Add the operand info.
316 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
317
318 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000319 switch (N->getOpcode()) {
320 default: break; // Normal nodes don't need extra info.
321 case ISD::TargetConstant:
322 case ISD::Constant:
323 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
324 break;
325 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000326 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000327 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000328 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000329 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000330 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000331 case ISD::GlobalAddress:
332 case ISD::TargetGlobalTLSAddress:
333 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000334 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
335 ID.AddPointer(GA->getGlobal());
336 ID.AddInteger(GA->getOffset());
337 break;
338 }
339 case ISD::BasicBlock:
340 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
341 break;
342 case ISD::Register:
343 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
344 break;
345 case ISD::SRCVALUE: {
346 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
347 ID.AddPointer(SV->getValue());
348 ID.AddInteger(SV->getOffset());
349 break;
350 }
351 case ISD::FrameIndex:
352 case ISD::TargetFrameIndex:
353 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
354 break;
355 case ISD::JumpTable:
356 case ISD::TargetJumpTable:
357 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
358 break;
359 case ISD::ConstantPool:
360 case ISD::TargetConstantPool: {
361 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
362 ID.AddInteger(CP->getAlignment());
363 ID.AddInteger(CP->getOffset());
364 if (CP->isMachineConstantPoolEntry())
365 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
366 else
367 ID.AddPointer(CP->getConstVal());
368 break;
369 }
370 case ISD::LOAD: {
371 LoadSDNode *LD = cast<LoadSDNode>(N);
372 ID.AddInteger(LD->getAddressingMode());
373 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000374 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 ID.AddPointer(LD->getSrcValue());
376 ID.AddInteger(LD->getSrcValueOffset());
377 ID.AddInteger(LD->getAlignment());
378 ID.AddInteger(LD->isVolatile());
379 break;
380 }
381 case ISD::STORE: {
382 StoreSDNode *ST = cast<StoreSDNode>(N);
383 ID.AddInteger(ST->getAddressingMode());
384 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000385 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000386 ID.AddPointer(ST->getSrcValue());
387 ID.AddInteger(ST->getSrcValueOffset());
388 ID.AddInteger(ST->getAlignment());
389 ID.AddInteger(ST->isVolatile());
390 break;
391 }
Jim Laskey583bd472006-10-27 23:46:08 +0000392 }
393}
394
395//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000396// SelectionDAG Class
397//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000398
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000399/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000400/// SelectionDAG.
401void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000402 // Create a dummy node (which is not added to allnodes), that adds a reference
403 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000404 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000405
Chris Lattner190a4182006-08-04 17:45:20 +0000406 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000407
Chris Lattner190a4182006-08-04 17:45:20 +0000408 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000409 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000410 if (I->use_empty())
411 DeadNodes.push_back(I);
412
413 // Process the worklist, deleting the nodes and adding their uses to the
414 // worklist.
415 while (!DeadNodes.empty()) {
416 SDNode *N = DeadNodes.back();
417 DeadNodes.pop_back();
418
419 // Take the node out of the appropriate CSE map.
420 RemoveNodeFromCSEMaps(N);
421
422 // Next, brutally remove the operand list. This is safe to do, as there are
423 // no cycles in the graph.
424 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
425 SDNode *Operand = I->Val;
426 Operand->removeUser(N);
427
428 // Now that we removed this operand, see if there are no uses of it left.
429 if (Operand->use_empty())
430 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000431 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000432 if (N->OperandsNeedDelete)
433 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000434 N->OperandList = 0;
435 N->NumOperands = 0;
436
437 // Finally, remove N itself.
438 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000439 }
440
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000441 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000442 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000443}
444
Evan Cheng130a6472006-10-12 20:34:05 +0000445void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
446 SmallVector<SDNode*, 16> DeadNodes;
447 DeadNodes.push_back(N);
448
449 // Process the worklist, deleting the nodes and adding their uses to the
450 // worklist.
451 while (!DeadNodes.empty()) {
452 SDNode *N = DeadNodes.back();
453 DeadNodes.pop_back();
454
455 // Take the node out of the appropriate CSE map.
456 RemoveNodeFromCSEMaps(N);
457
458 // Next, brutally remove the operand list. This is safe to do, as there are
459 // no cycles in the graph.
460 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
461 SDNode *Operand = I->Val;
462 Operand->removeUser(N);
463
464 // Now that we removed this operand, see if there are no uses of it left.
465 if (Operand->use_empty())
466 DeadNodes.push_back(Operand);
467 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000468 if (N->OperandsNeedDelete)
469 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000470 N->OperandList = 0;
471 N->NumOperands = 0;
472
473 // Finally, remove N itself.
474 Deleted.push_back(N);
475 AllNodes.erase(N);
476 }
477}
478
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000479void SelectionDAG::DeleteNode(SDNode *N) {
480 assert(N->use_empty() && "Cannot delete a node that is not dead!");
481
482 // First take this out of the appropriate CSE map.
483 RemoveNodeFromCSEMaps(N);
484
Chris Lattner1e111c72005-09-07 05:37:01 +0000485 // Finally, remove uses due to operands of this node, remove from the
486 // AllNodes list, and delete the node.
487 DeleteNodeNotInCSEMaps(N);
488}
489
490void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
491
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000492 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000493 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000494
495 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000496 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
497 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000498 if (N->OperandsNeedDelete)
499 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000500 N->OperandList = 0;
501 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000502
503 delete N;
504}
505
Chris Lattner149c58c2005-08-16 18:17:10 +0000506/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
507/// correspond to it. This is useful when we're about to delete or repurpose
508/// the node. We don't want future request for structurally identical nodes
509/// to return N anymore.
510void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000511 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000512 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000513 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000514 case ISD::STRING:
515 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
516 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000517 case ISD::CONDCODE:
518 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
519 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000520 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000521 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
522 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000523 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000524 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000525 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000526 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000527 Erased =
528 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000529 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000530 case ISD::VALUETYPE: {
531 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
532 if (MVT::isExtendedVT(VT)) {
533 Erased = ExtendedValueTypeNodes.erase(VT);
534 } else {
535 Erased = ValueTypeNodes[VT] != 0;
536 ValueTypeNodes[VT] = 0;
537 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000538 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000539 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000540 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000541 // Remove it from the CSE Map.
542 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000543 break;
544 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000545#ifndef NDEBUG
546 // Verify that the node was actually in one of the CSE maps, unless it has a
547 // flag result (which cannot be CSE'd) or is one of the special cases that are
548 // not subject to CSE.
549 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000550 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000551 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000552 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000553 assert(0 && "Node is not in map!");
554 }
555#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000556}
557
Chris Lattner8b8749f2005-08-17 19:00:20 +0000558/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
559/// has been taken out and modified in some way. If the specified node already
560/// exists in the CSE maps, do not modify the maps, but return the existing node
561/// instead. If it doesn't exist, add it and return null.
562///
563SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
564 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000565 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000566 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000567
Chris Lattner9f8cc692005-12-19 22:21:21 +0000568 // Check that remaining values produced are not flags.
569 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
570 if (N->getValueType(i) == MVT::Flag)
571 return 0; // Never CSE anything that produces a flag.
572
Chris Lattnera5682852006-08-07 23:03:03 +0000573 SDNode *New = CSEMap.GetOrInsertNode(N);
574 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000575 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000576}
577
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000578/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
579/// were replaced with those specified. If this node is never memoized,
580/// return null, otherwise return a pointer to the slot it would take. If a
581/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000582SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
583 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000584 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000585 return 0; // Never add these nodes.
586
587 // Check that remaining values produced are not flags.
588 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
589 if (N->getValueType(i) == MVT::Flag)
590 return 0; // Never CSE anything that produces a flag.
591
Chris Lattner63e3f142007-02-04 07:28:00 +0000592 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000593 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000594 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000595 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000596}
597
598/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
599/// were replaced with those specified. If this node is never memoized,
600/// return null, otherwise return a pointer to the slot it would take. If a
601/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000602SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
603 SDOperand Op1, SDOperand Op2,
604 void *&InsertPos) {
605 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
606 return 0; // Never add these nodes.
607
608 // Check that remaining values produced are not flags.
609 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
610 if (N->getValueType(i) == MVT::Flag)
611 return 0; // Never CSE anything that produces a flag.
612
Chris Lattner63e3f142007-02-04 07:28:00 +0000613 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000614 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000615 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000616 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
617}
618
619
620/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
621/// were replaced with those specified. If this node is never memoized,
622/// return null, otherwise return a pointer to the slot it would take. If a
623/// node already exists with these operands, the slot will be non-null.
624SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000625 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000626 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000627 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000628 return 0; // Never add these nodes.
629
630 // Check that remaining values produced are not flags.
631 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
632 if (N->getValueType(i) == MVT::Flag)
633 return 0; // Never CSE anything that produces a flag.
634
Jim Laskey583bd472006-10-27 23:46:08 +0000635 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000636 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000637
Evan Cheng9629aba2006-10-11 01:47:58 +0000638 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
639 ID.AddInteger(LD->getAddressingMode());
640 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000641 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000642 ID.AddPointer(LD->getSrcValue());
643 ID.AddInteger(LD->getSrcValueOffset());
644 ID.AddInteger(LD->getAlignment());
645 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000646 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
647 ID.AddInteger(ST->getAddressingMode());
648 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000649 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000650 ID.AddPointer(ST->getSrcValue());
651 ID.AddInteger(ST->getSrcValueOffset());
652 ID.AddInteger(ST->getAlignment());
653 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000654 }
Jim Laskey583bd472006-10-27 23:46:08 +0000655
Chris Lattnera5682852006-08-07 23:03:03 +0000656 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000657}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000658
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000659
Chris Lattner78ec3112003-08-11 14:57:33 +0000660SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000661 while (!AllNodes.empty()) {
662 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000663 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000664 if (N->OperandsNeedDelete)
665 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000666 N->OperandList = 0;
667 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000668 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000669 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000670}
671
Chris Lattner0f2287b2005-04-13 02:38:18 +0000672SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000673 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000674 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000675 return getNode(ISD::AND, Op.getValueType(), Op,
676 getConstant(Imm, Op.getValueType()));
677}
678
Chris Lattner36ce6912005-11-29 06:21:05 +0000679SDOperand SelectionDAG::getString(const std::string &Val) {
680 StringSDNode *&N = StringNodes[Val];
681 if (!N) {
682 N = new StringSDNode(Val);
683 AllNodes.push_back(N);
684 }
685 return SDOperand(N, 0);
686}
687
Chris Lattner61b09412006-08-11 21:01:22 +0000688SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000689 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000690 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000691
Chris Lattner61b09412006-08-11 21:01:22 +0000692 // Mask out any bits that are not valid for this constant.
693 Val &= MVT::getIntVTBitMask(VT);
694
695 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000696 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000697 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000698 ID.AddInteger(Val);
699 void *IP = 0;
700 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
701 return SDOperand(E, 0);
702 SDNode *N = new ConstantSDNode(isT, Val, VT);
703 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000704 AllNodes.push_back(N);
705 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000706}
707
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000708SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000709 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000710 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000711
Dan Gohman7f321562007-06-25 16:23:39 +0000712 MVT::ValueType EltVT =
713 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000714
Chris Lattnerd8658612005-02-17 20:17:32 +0000715 // Do the map lookup using the actual bit pattern for the floating point
716 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
717 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000718 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000719 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000720 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000721 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000722 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000723 SDNode *N = NULL;
724 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
725 if (!MVT::isVector(VT))
726 return SDOperand(N, 0);
727 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000728 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000729 CSEMap.InsertNode(N, IP);
730 AllNodes.push_back(N);
731 }
732
Dan Gohman7f321562007-06-25 16:23:39 +0000733 SDOperand Result(N, 0);
734 if (MVT::isVector(VT)) {
735 SmallVector<SDOperand, 8> Ops;
736 Ops.assign(MVT::getVectorNumElements(VT), Result);
737 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
738 }
739 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000740}
741
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000742SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
743 bool isTarget) {
744 MVT::ValueType EltVT =
745 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
746 if (EltVT==MVT::f32)
747 return getConstantFP(APFloat((float)Val), VT, isTarget);
748 else
749 return getConstantFP(APFloat(Val), VT, isTarget);
750}
751
Chris Lattnerc3aae252005-01-07 07:46:32 +0000752SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000753 MVT::ValueType VT, int Offset,
754 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000755 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
756 unsigned Opc;
757 if (GVar && GVar->isThreadLocal())
758 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
759 else
760 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000761 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000762 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000763 ID.AddPointer(GV);
764 ID.AddInteger(Offset);
765 void *IP = 0;
766 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
767 return SDOperand(E, 0);
768 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
769 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000770 AllNodes.push_back(N);
771 return SDOperand(N, 0);
772}
773
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000774SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
775 bool isTarget) {
776 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000777 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000778 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000779 ID.AddInteger(FI);
780 void *IP = 0;
781 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
782 return SDOperand(E, 0);
783 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
784 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000785 AllNodes.push_back(N);
786 return SDOperand(N, 0);
787}
788
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000789SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
790 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000791 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000792 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000793 ID.AddInteger(JTI);
794 void *IP = 0;
795 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
796 return SDOperand(E, 0);
797 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
798 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000799 AllNodes.push_back(N);
800 return SDOperand(N, 0);
801}
802
Evan Chengb8973bd2006-01-31 22:23:14 +0000803SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000804 unsigned Alignment, int Offset,
805 bool isTarget) {
806 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000807 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000808 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000809 ID.AddInteger(Alignment);
810 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000811 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000812 void *IP = 0;
813 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
814 return SDOperand(E, 0);
815 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
816 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000817 AllNodes.push_back(N);
818 return SDOperand(N, 0);
819}
820
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821
Evan Chengd6594ae2006-09-12 21:00:35 +0000822SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
823 MVT::ValueType VT,
824 unsigned Alignment, int Offset,
825 bool isTarget) {
826 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000827 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000828 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000829 ID.AddInteger(Alignment);
830 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000831 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000832 void *IP = 0;
833 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
834 return SDOperand(E, 0);
835 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
836 CSEMap.InsertNode(N, IP);
837 AllNodes.push_back(N);
838 return SDOperand(N, 0);
839}
840
841
Chris Lattnerc3aae252005-01-07 07:46:32 +0000842SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000843 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000844 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000845 ID.AddPointer(MBB);
846 void *IP = 0;
847 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
848 return SDOperand(E, 0);
849 SDNode *N = new BasicBlockSDNode(MBB);
850 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000851 AllNodes.push_back(N);
852 return SDOperand(N, 0);
853}
854
Chris Lattner15e4b012005-07-10 00:07:11 +0000855SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000856 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000857 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000858
Duncan Sandsf411b832007-10-17 13:49:58 +0000859 SDNode *&N = MVT::isExtendedVT(VT) ?
860 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
861
862 if (N) return SDOperand(N, 0);
863 N = new VTSDNode(VT);
864 AllNodes.push_back(N);
865 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000866}
867
Chris Lattnerc3aae252005-01-07 07:46:32 +0000868SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
869 SDNode *&N = ExternalSymbols[Sym];
870 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000871 N = new ExternalSymbolSDNode(false, Sym, VT);
872 AllNodes.push_back(N);
873 return SDOperand(N, 0);
874}
875
Chris Lattner809ec112006-01-28 10:09:25 +0000876SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
877 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000878 SDNode *&N = TargetExternalSymbols[Sym];
879 if (N) return SDOperand(N, 0);
880 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000881 AllNodes.push_back(N);
882 return SDOperand(N, 0);
883}
884
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000885SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
886 if ((unsigned)Cond >= CondCodeNodes.size())
887 CondCodeNodes.resize(Cond+1);
888
Chris Lattner079a27a2005-08-09 20:40:02 +0000889 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000890 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000891 AllNodes.push_back(CondCodeNodes[Cond]);
892 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000893 return SDOperand(CondCodeNodes[Cond], 0);
894}
895
Chris Lattner0fdd7682005-08-30 22:38:38 +0000896SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000897 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000898 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000899 ID.AddInteger(RegNo);
900 void *IP = 0;
901 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
902 return SDOperand(E, 0);
903 SDNode *N = new RegisterSDNode(RegNo, VT);
904 CSEMap.InsertNode(N, IP);
905 AllNodes.push_back(N);
906 return SDOperand(N, 0);
907}
908
909SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
910 assert((!V || isa<PointerType>(V->getType())) &&
911 "SrcValue is not a pointer?");
912
Jim Laskey583bd472006-10-27 23:46:08 +0000913 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000914 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000915 ID.AddPointer(V);
916 ID.AddInteger(Offset);
917 void *IP = 0;
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new SrcValueSDNode(V, Offset);
921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000924}
925
Chris Lattner37ce9df2007-10-15 17:47:20 +0000926/// CreateStackTemporary - Create a stack temporary, suitable for holding the
927/// specified value type.
928SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
929 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
930 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
931 const Type *Ty = MVT::getTypeForValueType(VT);
932 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
933 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
934 return getFrameIndex(FrameIdx, TLI.getPointerTy());
935}
936
937
Chris Lattner51dabfb2006-10-14 00:41:01 +0000938SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
939 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000940 // These setcc operations always fold.
941 switch (Cond) {
942 default: break;
943 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000944 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000946 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000947
948 case ISD::SETOEQ:
949 case ISD::SETOGT:
950 case ISD::SETOGE:
951 case ISD::SETOLT:
952 case ISD::SETOLE:
953 case ISD::SETONE:
954 case ISD::SETO:
955 case ISD::SETUO:
956 case ISD::SETUEQ:
957 case ISD::SETUNE:
958 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
959 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000960 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000961
Chris Lattner67255a12005-04-07 18:14:58 +0000962 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
963 uint64_t C2 = N2C->getValue();
964 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
965 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000966
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967 // Sign extend the operands if required
968 if (ISD::isSignedIntSetCC(Cond)) {
969 C1 = N1C->getSignExtended();
970 C2 = N2C->getSignExtended();
971 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000972
Chris Lattnerc3aae252005-01-07 07:46:32 +0000973 switch (Cond) {
974 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000975 case ISD::SETEQ: return getConstant(C1 == C2, VT);
976 case ISD::SETNE: return getConstant(C1 != C2, VT);
977 case ISD::SETULT: return getConstant(C1 < C2, VT);
978 case ISD::SETUGT: return getConstant(C1 > C2, VT);
979 case ISD::SETULE: return getConstant(C1 <= C2, VT);
980 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
981 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
982 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
983 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
984 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000985 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986 }
Chris Lattner67255a12005-04-07 18:14:58 +0000987 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000988 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
989 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +0000990 // No compile time operations on this type yet.
991 if (N1C->getValueType(0) == MVT::ppcf128)
992 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +0000993
994 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +0000995 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000996 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +0000997 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
998 return getNode(ISD::UNDEF, VT);
999 // fall through
1000 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1001 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1002 return getNode(ISD::UNDEF, VT);
1003 // fall through
1004 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001005 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001006 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1007 return getNode(ISD::UNDEF, VT);
1008 // fall through
1009 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1010 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1011 return getNode(ISD::UNDEF, VT);
1012 // fall through
1013 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1014 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1015 return getNode(ISD::UNDEF, VT);
1016 // fall through
1017 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001018 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001019 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1020 return getNode(ISD::UNDEF, VT);
1021 // fall through
1022 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001023 R==APFloat::cmpEqual, VT);
1024 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1025 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1026 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1027 R==APFloat::cmpEqual, VT);
1028 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1029 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1030 R==APFloat::cmpLessThan, VT);
1031 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1032 R==APFloat::cmpUnordered, VT);
1033 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1034 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001035 }
1036 } else {
1037 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001038 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001039 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001040
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001041 // Could not fold it.
1042 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001043}
1044
Dan Gohmanea859be2007-06-22 14:59:07 +00001045/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1046/// this predicate to simplify operations downstream. Mask is known to be zero
1047/// for bits that V cannot have.
1048bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1049 unsigned Depth) const {
1050 // The masks are not wide enough to represent this type! Should use APInt.
1051 if (Op.getValueType() == MVT::i128)
1052 return false;
1053
1054 uint64_t KnownZero, KnownOne;
1055 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1056 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1057 return (KnownZero & Mask) == Mask;
1058}
1059
1060/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1061/// known to be either zero or one and return them in the KnownZero/KnownOne
1062/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1063/// processing.
1064void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1065 uint64_t &KnownZero, uint64_t &KnownOne,
1066 unsigned Depth) const {
1067 KnownZero = KnownOne = 0; // Don't know anything.
1068 if (Depth == 6 || Mask == 0)
1069 return; // Limit search depth.
1070
1071 // The masks are not wide enough to represent this type! Should use APInt.
1072 if (Op.getValueType() == MVT::i128)
1073 return;
1074
1075 uint64_t KnownZero2, KnownOne2;
1076
1077 switch (Op.getOpcode()) {
1078 case ISD::Constant:
1079 // We know all of the bits for a constant!
1080 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1081 KnownZero = ~KnownOne & Mask;
1082 return;
1083 case ISD::AND:
1084 // If either the LHS or the RHS are Zero, the result is zero.
1085 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1086 Mask &= ~KnownZero;
1087 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1088 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1089 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1090
1091 // Output known-1 bits are only known if set in both the LHS & RHS.
1092 KnownOne &= KnownOne2;
1093 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1094 KnownZero |= KnownZero2;
1095 return;
1096 case ISD::OR:
1097 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1098 Mask &= ~KnownOne;
1099 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1100 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1101 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1102
1103 // Output known-0 bits are only known if clear in both the LHS & RHS.
1104 KnownZero &= KnownZero2;
1105 // Output known-1 are known to be set if set in either the LHS | RHS.
1106 KnownOne |= KnownOne2;
1107 return;
1108 case ISD::XOR: {
1109 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1110 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1111 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1112 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1113
1114 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1115 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1116 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1117 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1118 KnownZero = KnownZeroOut;
1119 return;
1120 }
1121 case ISD::SELECT:
1122 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1123 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1124 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1125 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1126
1127 // Only known if known in both the LHS and RHS.
1128 KnownOne &= KnownOne2;
1129 KnownZero &= KnownZero2;
1130 return;
1131 case ISD::SELECT_CC:
1132 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1133 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1134 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1135 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1136
1137 // Only known if known in both the LHS and RHS.
1138 KnownOne &= KnownOne2;
1139 KnownZero &= KnownZero2;
1140 return;
1141 case ISD::SETCC:
1142 // If we know the result of a setcc has the top bits zero, use this info.
1143 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1144 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1145 return;
1146 case ISD::SHL:
1147 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1148 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1149 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1150 KnownZero, KnownOne, Depth+1);
1151 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1152 KnownZero <<= SA->getValue();
1153 KnownOne <<= SA->getValue();
1154 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1155 }
1156 return;
1157 case ISD::SRL:
1158 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1159 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1160 MVT::ValueType VT = Op.getValueType();
1161 unsigned ShAmt = SA->getValue();
1162
1163 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1164 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1165 KnownZero, KnownOne, Depth+1);
1166 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1167 KnownZero &= TypeMask;
1168 KnownOne &= TypeMask;
1169 KnownZero >>= ShAmt;
1170 KnownOne >>= ShAmt;
1171
1172 uint64_t HighBits = (1ULL << ShAmt)-1;
1173 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1174 KnownZero |= HighBits; // High bits known zero.
1175 }
1176 return;
1177 case ISD::SRA:
1178 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1179 MVT::ValueType VT = Op.getValueType();
1180 unsigned ShAmt = SA->getValue();
1181
1182 // Compute the new bits that are at the top now.
1183 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1184
1185 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1186 // If any of the demanded bits are produced by the sign extension, we also
1187 // demand the input sign bit.
1188 uint64_t HighBits = (1ULL << ShAmt)-1;
1189 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1190 if (HighBits & Mask)
1191 InDemandedMask |= MVT::getIntVTSignBit(VT);
1192
1193 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1194 Depth+1);
1195 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1196 KnownZero &= TypeMask;
1197 KnownOne &= TypeMask;
1198 KnownZero >>= ShAmt;
1199 KnownOne >>= ShAmt;
1200
1201 // Handle the sign bits.
1202 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1203 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1204
1205 if (KnownZero & SignBit) {
1206 KnownZero |= HighBits; // New bits are known zero.
1207 } else if (KnownOne & SignBit) {
1208 KnownOne |= HighBits; // New bits are known one.
1209 }
1210 }
1211 return;
1212 case ISD::SIGN_EXTEND_INREG: {
1213 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1214
1215 // Sign extension. Compute the demanded bits in the result that are not
1216 // present in the input.
1217 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1218
1219 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1220 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1221
1222 // If the sign extended bits are demanded, we know that the sign
1223 // bit is demanded.
1224 if (NewBits)
1225 InputDemandedBits |= InSignBit;
1226
1227 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1228 KnownZero, KnownOne, Depth+1);
1229 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1230
1231 // If the sign bit of the input is known set or clear, then we know the
1232 // top bits of the result.
1233 if (KnownZero & InSignBit) { // Input sign bit known clear
1234 KnownZero |= NewBits;
1235 KnownOne &= ~NewBits;
1236 } else if (KnownOne & InSignBit) { // Input sign bit known set
1237 KnownOne |= NewBits;
1238 KnownZero &= ~NewBits;
1239 } else { // Input sign bit unknown
1240 KnownZero &= ~NewBits;
1241 KnownOne &= ~NewBits;
1242 }
1243 return;
1244 }
1245 case ISD::CTTZ:
1246 case ISD::CTLZ:
1247 case ISD::CTPOP: {
1248 MVT::ValueType VT = Op.getValueType();
1249 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1250 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1251 KnownOne = 0;
1252 return;
1253 }
1254 case ISD::LOAD: {
1255 if (ISD::isZEXTLoad(Op.Val)) {
1256 LoadSDNode *LD = cast<LoadSDNode>(Op);
1257 MVT::ValueType VT = LD->getLoadedVT();
1258 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1259 }
1260 return;
1261 }
1262 case ISD::ZERO_EXTEND: {
1263 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1264 uint64_t NewBits = (~InMask) & Mask;
1265 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1266 KnownOne, Depth+1);
1267 KnownZero |= NewBits & Mask;
1268 KnownOne &= ~NewBits;
1269 return;
1270 }
1271 case ISD::SIGN_EXTEND: {
1272 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1273 unsigned InBits = MVT::getSizeInBits(InVT);
1274 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1275 uint64_t InSignBit = 1ULL << (InBits-1);
1276 uint64_t NewBits = (~InMask) & Mask;
1277 uint64_t InDemandedBits = Mask & InMask;
1278
1279 // If any of the sign extended bits are demanded, we know that the sign
1280 // bit is demanded.
1281 if (NewBits & Mask)
1282 InDemandedBits |= InSignBit;
1283
1284 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1285 KnownOne, Depth+1);
1286 // If the sign bit is known zero or one, the top bits match.
1287 if (KnownZero & InSignBit) {
1288 KnownZero |= NewBits;
1289 KnownOne &= ~NewBits;
1290 } else if (KnownOne & InSignBit) {
1291 KnownOne |= NewBits;
1292 KnownZero &= ~NewBits;
1293 } else { // Otherwise, top bits aren't known.
1294 KnownOne &= ~NewBits;
1295 KnownZero &= ~NewBits;
1296 }
1297 return;
1298 }
1299 case ISD::ANY_EXTEND: {
1300 MVT::ValueType VT = Op.getOperand(0).getValueType();
1301 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1302 KnownZero, KnownOne, Depth+1);
1303 return;
1304 }
1305 case ISD::TRUNCATE: {
1306 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1307 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1308 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1309 KnownZero &= OutMask;
1310 KnownOne &= OutMask;
1311 break;
1312 }
1313 case ISD::AssertZext: {
1314 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1315 uint64_t InMask = MVT::getIntVTBitMask(VT);
1316 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1317 KnownOne, Depth+1);
1318 KnownZero |= (~InMask) & Mask;
1319 return;
1320 }
1321 case ISD::ADD: {
1322 // If either the LHS or the RHS are Zero, the result is zero.
1323 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1324 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1325 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1326 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1327
1328 // Output known-0 bits are known if clear or set in both the low clear bits
1329 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1330 // low 3 bits clear.
1331 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1332 CountTrailingZeros_64(~KnownZero2));
1333
1334 KnownZero = (1ULL << KnownZeroOut) - 1;
1335 KnownOne = 0;
1336 return;
1337 }
1338 case ISD::SUB: {
1339 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1340 if (!CLHS) return;
1341
1342 // We know that the top bits of C-X are clear if X contains less bits
1343 // than C (i.e. no wrap-around can happen). For example, 20-X is
1344 // positive if we can prove that X is >= 0 and < 16.
1345 MVT::ValueType VT = CLHS->getValueType(0);
1346 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1347 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1348 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1349 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1350 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1351
1352 // If all of the MaskV bits are known to be zero, then we know the output
1353 // top bits are zero, because we now know that the output is from [0-C].
1354 if ((KnownZero & MaskV) == MaskV) {
1355 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1356 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1357 KnownOne = 0; // No one bits known.
1358 } else {
1359 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1360 }
1361 }
1362 return;
1363 }
1364 default:
1365 // Allow the target to implement this method for its nodes.
1366 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1367 case ISD::INTRINSIC_WO_CHAIN:
1368 case ISD::INTRINSIC_W_CHAIN:
1369 case ISD::INTRINSIC_VOID:
1370 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1371 }
1372 return;
1373 }
1374}
1375
1376/// ComputeNumSignBits - Return the number of times the sign bit of the
1377/// register is replicated into the other bits. We know that at least 1 bit
1378/// is always equal to the sign bit (itself), but other cases can give us
1379/// information. For example, immediately after an "SRA X, 2", we know that
1380/// the top 3 bits are all equal to each other, so we return 3.
1381unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1382 MVT::ValueType VT = Op.getValueType();
1383 assert(MVT::isInteger(VT) && "Invalid VT!");
1384 unsigned VTBits = MVT::getSizeInBits(VT);
1385 unsigned Tmp, Tmp2;
1386
1387 if (Depth == 6)
1388 return 1; // Limit search depth.
1389
1390 switch (Op.getOpcode()) {
1391 default: break;
1392 case ISD::AssertSext:
1393 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1394 return VTBits-Tmp+1;
1395 case ISD::AssertZext:
1396 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1397 return VTBits-Tmp;
1398
1399 case ISD::Constant: {
1400 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1401 // If negative, invert the bits, then look at it.
1402 if (Val & MVT::getIntVTSignBit(VT))
1403 Val = ~Val;
1404
1405 // Shift the bits so they are the leading bits in the int64_t.
1406 Val <<= 64-VTBits;
1407
1408 // Return # leading zeros. We use 'min' here in case Val was zero before
1409 // shifting. We don't want to return '64' as for an i32 "0".
1410 return std::min(VTBits, CountLeadingZeros_64(Val));
1411 }
1412
1413 case ISD::SIGN_EXTEND:
1414 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1415 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1416
1417 case ISD::SIGN_EXTEND_INREG:
1418 // Max of the input and what this extends.
1419 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1420 Tmp = VTBits-Tmp+1;
1421
1422 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1423 return std::max(Tmp, Tmp2);
1424
1425 case ISD::SRA:
1426 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1427 // SRA X, C -> adds C sign bits.
1428 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1429 Tmp += C->getValue();
1430 if (Tmp > VTBits) Tmp = VTBits;
1431 }
1432 return Tmp;
1433 case ISD::SHL:
1434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1435 // shl destroys sign bits.
1436 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1437 if (C->getValue() >= VTBits || // Bad shift.
1438 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1439 return Tmp - C->getValue();
1440 }
1441 break;
1442 case ISD::AND:
1443 case ISD::OR:
1444 case ISD::XOR: // NOT is handled here.
1445 // Logical binary ops preserve the number of sign bits.
1446 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1447 if (Tmp == 1) return 1; // Early out.
1448 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1449 return std::min(Tmp, Tmp2);
1450
1451 case ISD::SELECT:
1452 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1453 if (Tmp == 1) return 1; // Early out.
1454 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1455 return std::min(Tmp, Tmp2);
1456
1457 case ISD::SETCC:
1458 // If setcc returns 0/-1, all bits are sign bits.
1459 if (TLI.getSetCCResultContents() ==
1460 TargetLowering::ZeroOrNegativeOneSetCCResult)
1461 return VTBits;
1462 break;
1463 case ISD::ROTL:
1464 case ISD::ROTR:
1465 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1466 unsigned RotAmt = C->getValue() & (VTBits-1);
1467
1468 // Handle rotate right by N like a rotate left by 32-N.
1469 if (Op.getOpcode() == ISD::ROTR)
1470 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1471
1472 // If we aren't rotating out all of the known-in sign bits, return the
1473 // number that are left. This handles rotl(sext(x), 1) for example.
1474 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1475 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1476 }
1477 break;
1478 case ISD::ADD:
1479 // Add can have at most one carry bit. Thus we know that the output
1480 // is, at worst, one more bit than the inputs.
1481 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1482 if (Tmp == 1) return 1; // Early out.
1483
1484 // Special case decrementing a value (ADD X, -1):
1485 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1486 if (CRHS->isAllOnesValue()) {
1487 uint64_t KnownZero, KnownOne;
1488 uint64_t Mask = MVT::getIntVTBitMask(VT);
1489 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1490
1491 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1492 // sign bits set.
1493 if ((KnownZero|1) == Mask)
1494 return VTBits;
1495
1496 // If we are subtracting one from a positive number, there is no carry
1497 // out of the result.
1498 if (KnownZero & MVT::getIntVTSignBit(VT))
1499 return Tmp;
1500 }
1501
1502 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1503 if (Tmp2 == 1) return 1;
1504 return std::min(Tmp, Tmp2)-1;
1505 break;
1506
1507 case ISD::SUB:
1508 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1509 if (Tmp2 == 1) return 1;
1510
1511 // Handle NEG.
1512 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1513 if (CLHS->getValue() == 0) {
1514 uint64_t KnownZero, KnownOne;
1515 uint64_t Mask = MVT::getIntVTBitMask(VT);
1516 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1517 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1518 // sign bits set.
1519 if ((KnownZero|1) == Mask)
1520 return VTBits;
1521
1522 // If the input is known to be positive (the sign bit is known clear),
1523 // the output of the NEG has the same number of sign bits as the input.
1524 if (KnownZero & MVT::getIntVTSignBit(VT))
1525 return Tmp2;
1526
1527 // Otherwise, we treat this like a SUB.
1528 }
1529
1530 // Sub can have at most one carry bit. Thus we know that the output
1531 // is, at worst, one more bit than the inputs.
1532 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1533 if (Tmp == 1) return 1; // Early out.
1534 return std::min(Tmp, Tmp2)-1;
1535 break;
1536 case ISD::TRUNCATE:
1537 // FIXME: it's tricky to do anything useful for this, but it is an important
1538 // case for targets like X86.
1539 break;
1540 }
1541
1542 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1543 if (Op.getOpcode() == ISD::LOAD) {
1544 LoadSDNode *LD = cast<LoadSDNode>(Op);
1545 unsigned ExtType = LD->getExtensionType();
1546 switch (ExtType) {
1547 default: break;
1548 case ISD::SEXTLOAD: // '17' bits known
1549 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1550 return VTBits-Tmp+1;
1551 case ISD::ZEXTLOAD: // '16' bits known
1552 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1553 return VTBits-Tmp;
1554 }
1555 }
1556
1557 // Allow the target to implement this method for its nodes.
1558 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1559 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1560 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1561 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1562 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1563 if (NumBits > 1) return NumBits;
1564 }
1565
1566 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1567 // use this information.
1568 uint64_t KnownZero, KnownOne;
1569 uint64_t Mask = MVT::getIntVTBitMask(VT);
1570 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1571
1572 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1573 if (KnownZero & SignBit) { // SignBit is 0
1574 Mask = KnownZero;
1575 } else if (KnownOne & SignBit) { // SignBit is 1;
1576 Mask = KnownOne;
1577 } else {
1578 // Nothing known.
1579 return 1;
1580 }
1581
1582 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1583 // the number of identical bits in the top of the input value.
1584 Mask ^= ~0ULL;
1585 Mask <<= 64-VTBits;
1586 // Return # leading zeros. We use 'min' here in case Val was zero before
1587 // shifting. We don't want to return '64' as for an i32 "0".
1588 return std::min(VTBits, CountLeadingZeros_64(Mask));
1589}
1590
Chris Lattner51dabfb2006-10-14 00:41:01 +00001591
Chris Lattnerc3aae252005-01-07 07:46:32 +00001592/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001593///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001594SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001595 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001596 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001597 void *IP = 0;
1598 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1599 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001600 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001601 CSEMap.InsertNode(N, IP);
1602
1603 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001604 return SDOperand(N, 0);
1605}
1606
Chris Lattnerc3aae252005-01-07 07:46:32 +00001607SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1608 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001609 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001610 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001611 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1612 uint64_t Val = C->getValue();
1613 switch (Opcode) {
1614 default: break;
1615 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001616 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001617 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1618 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001619 case ISD::UINT_TO_FP:
1620 case ISD::SINT_TO_FP: {
1621 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001622 // No compile time operations on this type.
1623 if (VT==MVT::ppcf128)
1624 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00001625 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001626 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001627 MVT::getSizeInBits(Operand.getValueType()),
1628 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001629 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001630 return getConstantFP(apf, VT);
1631 }
Chris Lattner94683772005-12-23 05:30:37 +00001632 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001633 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001634 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001635 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001636 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001637 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001638 case ISD::BSWAP:
1639 switch(VT) {
1640 default: assert(0 && "Invalid bswap!"); break;
1641 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1642 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1643 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1644 }
1645 break;
1646 case ISD::CTPOP:
1647 switch(VT) {
1648 default: assert(0 && "Invalid ctpop!"); break;
1649 case MVT::i1: return getConstant(Val != 0, VT);
1650 case MVT::i8:
1651 Tmp1 = (unsigned)Val & 0xFF;
1652 return getConstant(CountPopulation_32(Tmp1), VT);
1653 case MVT::i16:
1654 Tmp1 = (unsigned)Val & 0xFFFF;
1655 return getConstant(CountPopulation_32(Tmp1), VT);
1656 case MVT::i32:
1657 return getConstant(CountPopulation_32((unsigned)Val), VT);
1658 case MVT::i64:
1659 return getConstant(CountPopulation_64(Val), VT);
1660 }
1661 case ISD::CTLZ:
1662 switch(VT) {
1663 default: assert(0 && "Invalid ctlz!"); break;
1664 case MVT::i1: return getConstant(Val == 0, VT);
1665 case MVT::i8:
1666 Tmp1 = (unsigned)Val & 0xFF;
1667 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1668 case MVT::i16:
1669 Tmp1 = (unsigned)Val & 0xFFFF;
1670 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1671 case MVT::i32:
1672 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1673 case MVT::i64:
1674 return getConstant(CountLeadingZeros_64(Val), VT);
1675 }
1676 case ISD::CTTZ:
1677 switch(VT) {
1678 default: assert(0 && "Invalid cttz!"); break;
1679 case MVT::i1: return getConstant(Val == 0, VT);
1680 case MVT::i8:
1681 Tmp1 = (unsigned)Val | 0x100;
1682 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1683 case MVT::i16:
1684 Tmp1 = (unsigned)Val | 0x10000;
1685 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1686 case MVT::i32:
1687 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1688 case MVT::i64:
1689 return getConstant(CountTrailingZeros_64(Val), VT);
1690 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001691 }
1692 }
1693
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001694 // Constant fold unary operations with a floating point constant operand.
1695 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1696 APFloat V = C->getValueAPF(); // make copy
Dale Johannesendb44bf82007-10-16 23:38:29 +00001697 if (VT!=MVT::ppcf128 && Operand.getValueType()!=MVT::ppcf128) {
1698 switch (Opcode) {
1699 case ISD::FNEG:
1700 V.changeSign();
1701 return getConstantFP(V, VT);
1702 case ISD::FABS:
1703 V.clearSign();
1704 return getConstantFP(V, VT);
1705 case ISD::FP_ROUND:
1706 case ISD::FP_EXTEND:
1707 // This can return overflow, underflow, or inexact; we don't care.
1708 // FIXME need to be more flexible about rounding mode.
1709 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1710 VT==MVT::f64 ? APFloat::IEEEdouble :
1711 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1712 VT==MVT::f128 ? APFloat::IEEEquad :
1713 APFloat::Bogus,
1714 APFloat::rmNearestTiesToEven);
1715 return getConstantFP(V, VT);
1716 case ISD::FP_TO_SINT:
1717 case ISD::FP_TO_UINT: {
1718 integerPart x;
1719 assert(integerPartWidth >= 64);
1720 // FIXME need to be more flexible about rounding mode.
1721 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1722 Opcode==ISD::FP_TO_SINT,
1723 APFloat::rmTowardZero);
1724 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1725 break;
1726 return getConstant(x, VT);
1727 }
1728 case ISD::BIT_CONVERT:
1729 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1730 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1731 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1732 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001733 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001734 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001735 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001736 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001737
1738 unsigned OpOpcode = Operand.Val->getOpcode();
1739 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001740 case ISD::TokenFactor:
1741 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001742 case ISD::FP_ROUND:
1743 case ISD::FP_EXTEND:
1744 assert(MVT::isFloatingPoint(VT) &&
1745 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1746 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001747 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001748 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1749 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001750 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001751 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1752 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001753 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1754 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1755 break;
1756 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001757 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1758 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001759 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001760 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1761 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001762 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001763 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001764 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001765 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001766 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1767 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001768 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001769 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1770 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001771 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1772 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1773 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1774 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001775 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001776 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1777 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001778 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001779 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1780 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001781 if (OpOpcode == ISD::TRUNCATE)
1782 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001783 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1784 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001785 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001786 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1787 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001788 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001789 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1790 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001791 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1792 else
1793 return Operand.Val->getOperand(0);
1794 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001795 break;
Chris Lattner35481892005-12-23 00:16:34 +00001796 case ISD::BIT_CONVERT:
1797 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001798 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001799 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001800 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001801 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1802 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001803 if (OpOpcode == ISD::UNDEF)
1804 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001805 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001806 case ISD::SCALAR_TO_VECTOR:
1807 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001808 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001809 "Illegal SCALAR_TO_VECTOR node!");
1810 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001811 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001812 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1813 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001814 Operand.Val->getOperand(0));
1815 if (OpOpcode == ISD::FNEG) // --X -> X
1816 return Operand.Val->getOperand(0);
1817 break;
1818 case ISD::FABS:
1819 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1820 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1821 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001822 }
1823
Chris Lattner43247a12005-08-25 19:12:10 +00001824 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001825 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001826 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001827 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001828 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001829 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001830 void *IP = 0;
1831 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1832 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001833 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001834 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001835 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001836 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001837 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001838 AllNodes.push_back(N);
1839 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001840}
1841
Chris Lattner36019aa2005-04-18 03:48:41 +00001842
1843
Chris Lattnerc3aae252005-01-07 07:46:32 +00001844SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1845 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001846#ifndef NDEBUG
1847 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001848 case ISD::TokenFactor:
1849 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1850 N2.getValueType() == MVT::Other && "Invalid token factor!");
1851 break;
Chris Lattner76365122005-01-16 02:23:22 +00001852 case ISD::AND:
1853 case ISD::OR:
1854 case ISD::XOR:
1855 case ISD::UDIV:
1856 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001857 case ISD::MULHU:
1858 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001859 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1860 // fall through
1861 case ISD::ADD:
1862 case ISD::SUB:
1863 case ISD::MUL:
1864 case ISD::SDIV:
1865 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001866 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1867 // fall through.
1868 case ISD::FADD:
1869 case ISD::FSUB:
1870 case ISD::FMUL:
1871 case ISD::FDIV:
1872 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001873 assert(N1.getValueType() == N2.getValueType() &&
1874 N1.getValueType() == VT && "Binary operator types must match!");
1875 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001876 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1877 assert(N1.getValueType() == VT &&
1878 MVT::isFloatingPoint(N1.getValueType()) &&
1879 MVT::isFloatingPoint(N2.getValueType()) &&
1880 "Invalid FCOPYSIGN!");
1881 break;
Chris Lattner76365122005-01-16 02:23:22 +00001882 case ISD::SHL:
1883 case ISD::SRA:
1884 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001885 case ISD::ROTL:
1886 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001887 assert(VT == N1.getValueType() &&
1888 "Shift operators return type must be the same as their first arg");
1889 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001890 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001891 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001892 case ISD::FP_ROUND_INREG: {
1893 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1894 assert(VT == N1.getValueType() && "Not an inreg round!");
1895 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1896 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001897 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1898 "Not rounding down!");
Chris Lattner15e4b012005-07-10 00:07:11 +00001899 break;
1900 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001901 case ISD::AssertSext:
1902 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001903 case ISD::SIGN_EXTEND_INREG: {
1904 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1905 assert(VT == N1.getValueType() && "Not an inreg extend!");
1906 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1907 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001908 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1909 "Not extending!");
Chris Lattner15e4b012005-07-10 00:07:11 +00001910 }
1911
Chris Lattner76365122005-01-16 02:23:22 +00001912 default: break;
1913 }
1914#endif
1915
Chris Lattnerc3aae252005-01-07 07:46:32 +00001916 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1917 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1918 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001919 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1920 int64_t Val = N1C->getValue();
1921 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1922 Val <<= 64-FromBits;
1923 Val >>= 64-FromBits;
1924 return getConstant(Val, VT);
1925 }
1926
Chris Lattnerc3aae252005-01-07 07:46:32 +00001927 if (N2C) {
1928 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1929 switch (Opcode) {
1930 case ISD::ADD: return getConstant(C1 + C2, VT);
1931 case ISD::SUB: return getConstant(C1 - C2, VT);
1932 case ISD::MUL: return getConstant(C1 * C2, VT);
1933 case ISD::UDIV:
1934 if (C2) return getConstant(C1 / C2, VT);
1935 break;
1936 case ISD::UREM :
1937 if (C2) return getConstant(C1 % C2, VT);
1938 break;
1939 case ISD::SDIV :
1940 if (C2) return getConstant(N1C->getSignExtended() /
1941 N2C->getSignExtended(), VT);
1942 break;
1943 case ISD::SREM :
1944 if (C2) return getConstant(N1C->getSignExtended() %
1945 N2C->getSignExtended(), VT);
1946 break;
1947 case ISD::AND : return getConstant(C1 & C2, VT);
1948 case ISD::OR : return getConstant(C1 | C2, VT);
1949 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001950 case ISD::SHL : return getConstant(C1 << C2, VT);
1951 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001952 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001953 case ISD::ROTL :
1954 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1955 VT);
1956 case ISD::ROTR :
1957 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1958 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001959 default: break;
1960 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001961 } else { // Cannonicalize constant to RHS if commutative
1962 if (isCommutativeBinOp(Opcode)) {
1963 std::swap(N1C, N2C);
1964 std::swap(N1, N2);
1965 }
1966 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001967 }
1968
1969 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1970 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001971 if (N1CFP) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001972 if (N2CFP && VT!=MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001973 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1974 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001975 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001976 case ISD::FADD:
1977 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1978 if (s!=APFloat::opInvalidOp)
1979 return getConstantFP(V1, VT);
1980 break;
1981 case ISD::FSUB:
1982 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1983 if (s!=APFloat::opInvalidOp)
1984 return getConstantFP(V1, VT);
1985 break;
1986 case ISD::FMUL:
1987 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1988 if (s!=APFloat::opInvalidOp)
1989 return getConstantFP(V1, VT);
1990 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001991 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001992 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1993 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1994 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001995 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001996 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001997 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1998 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1999 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002000 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002001 case ISD::FCOPYSIGN:
2002 V1.copySign(V2);
2003 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002004 default: break;
2005 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002006 } else { // Cannonicalize constant to RHS if commutative
2007 if (isCommutativeBinOp(Opcode)) {
2008 std::swap(N1CFP, N2CFP);
2009 std::swap(N1, N2);
2010 }
2011 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002012 }
Chris Lattner62b57722006-04-20 05:39:12 +00002013
2014 // Canonicalize an UNDEF to the RHS, even over a constant.
2015 if (N1.getOpcode() == ISD::UNDEF) {
2016 if (isCommutativeBinOp(Opcode)) {
2017 std::swap(N1, N2);
2018 } else {
2019 switch (Opcode) {
2020 case ISD::FP_ROUND_INREG:
2021 case ISD::SIGN_EXTEND_INREG:
2022 case ISD::SUB:
2023 case ISD::FSUB:
2024 case ISD::FDIV:
2025 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002026 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002027 return N1; // fold op(undef, arg2) -> undef
2028 case ISD::UDIV:
2029 case ISD::SDIV:
2030 case ISD::UREM:
2031 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002032 case ISD::SRL:
2033 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002034 if (!MVT::isVector(VT))
2035 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2036 // For vectors, we can't easily build an all zero vector, just return
2037 // the LHS.
2038 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002039 }
2040 }
2041 }
2042
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002043 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002044 if (N2.getOpcode() == ISD::UNDEF) {
2045 switch (Opcode) {
2046 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002047 case ISD::ADDC:
2048 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002049 case ISD::SUB:
2050 case ISD::FADD:
2051 case ISD::FSUB:
2052 case ISD::FMUL:
2053 case ISD::FDIV:
2054 case ISD::FREM:
2055 case ISD::UDIV:
2056 case ISD::SDIV:
2057 case ISD::UREM:
2058 case ISD::SREM:
2059 case ISD::XOR:
2060 return N2; // fold op(arg1, undef) -> undef
2061 case ISD::MUL:
2062 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002063 case ISD::SRL:
2064 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002065 if (!MVT::isVector(VT))
2066 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2067 // For vectors, we can't easily build an all zero vector, just return
2068 // the LHS.
2069 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002070 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002071 if (!MVT::isVector(VT))
2072 return getConstant(MVT::getIntVTBitMask(VT), VT);
2073 // For vectors, we can't easily build an all one vector, just return
2074 // the LHS.
2075 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002076 case ISD::SRA:
2077 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002078 }
2079 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002080
Chris Lattner51dabfb2006-10-14 00:41:01 +00002081 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002082 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00002083 case ISD::TokenFactor:
2084 // Fold trivial token factors.
2085 if (N1.getOpcode() == ISD::EntryToken) return N2;
2086 if (N2.getOpcode() == ISD::EntryToken) return N1;
2087 break;
2088
Chris Lattner51dabfb2006-10-14 00:41:01 +00002089 case ISD::AND:
2090 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2091 // worth handling here.
2092 if (N2C && N2C->getValue() == 0)
2093 return N2;
2094 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00002095 case ISD::OR:
2096 case ISD::XOR:
2097 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2098 // worth handling here.
2099 if (N2C && N2C->getValue() == 0)
2100 return N1;
2101 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002102 case ISD::FP_ROUND_INREG:
2103 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2104 break;
2105 case ISD::SIGN_EXTEND_INREG: {
2106 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2107 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002108 break;
2109 }
Dan Gohman7f321562007-06-25 16:23:39 +00002110 case ISD::EXTRACT_VECTOR_ELT:
2111 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2112
Dan Gohman743d3a72007-07-10 18:20:44 +00002113 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002114 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002115 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2116 N1.getNumOperands() > 0) {
2117 unsigned Factor =
2118 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2119 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2120 N1.getOperand(N2C->getValue() / Factor),
2121 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002122 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002123
Dan Gohman7f321562007-06-25 16:23:39 +00002124 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2125 // expanding large vector constants.
2126 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2127 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002128
2129 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2130 // operations are lowered to scalars.
2131 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2132 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2133 if (IEC == N2C)
2134 return N1.getOperand(1);
2135 else
2136 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2137 }
Dan Gohman7f321562007-06-25 16:23:39 +00002138 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002139 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002140 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2141
Chris Lattner5c6621c2006-09-19 04:51:23 +00002142 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2143 // 64-bit integers into 32-bit parts. Instead of building the extract of
2144 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002145 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002146 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002147
2148 // EXTRACT_ELEMENT of a constant int is also very common.
2149 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2150 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2151 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002152 }
2153 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002154
Nate Begemaneea805e2005-04-13 21:23:31 +00002155 // FIXME: figure out how to safely handle things like
2156 // int foo(int x) { return 1 << (x & 255); }
2157 // int bar() { return foo(256); }
2158#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002159 case ISD::SHL:
2160 case ISD::SRL:
2161 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002162 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002163 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002164 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002165 else if (N2.getOpcode() == ISD::AND)
2166 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2167 // If the and is only masking out bits that cannot effect the shift,
2168 // eliminate the and.
2169 unsigned NumBits = MVT::getSizeInBits(VT);
2170 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2171 return getNode(Opcode, VT, N1, N2.getOperand(0));
2172 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002173 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002174#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002175 }
2176
Chris Lattner27e9b412005-05-11 18:57:39 +00002177 // Memoize this node if possible.
2178 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002179 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002180 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002181 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002182 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002183 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002184 void *IP = 0;
2185 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2186 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002187 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002188 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002189 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002190 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002191 }
2192
Chris Lattnerc3aae252005-01-07 07:46:32 +00002193 AllNodes.push_back(N);
2194 return SDOperand(N, 0);
2195}
2196
Chris Lattnerc3aae252005-01-07 07:46:32 +00002197SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2198 SDOperand N1, SDOperand N2, SDOperand N3) {
2199 // Perform various simplifications.
2200 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2201 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002202 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002203 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002204 // Use FoldSetCC to simplify SETCC's.
2205 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002206 if (Simp.Val) return Simp;
2207 break;
2208 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002209 case ISD::SELECT:
2210 if (N1C)
2211 if (N1C->getValue())
2212 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002213 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002214 return N3; // select false, X, Y -> Y
2215
2216 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002217 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002218 case ISD::BRCOND:
2219 if (N2C)
2220 if (N2C->getValue()) // Unconditional branch
2221 return getNode(ISD::BR, MVT::Other, N1, N3);
2222 else
2223 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002224 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002225 case ISD::VECTOR_SHUFFLE:
2226 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2227 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2228 N3.getOpcode() == ISD::BUILD_VECTOR &&
2229 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2230 "Illegal VECTOR_SHUFFLE node!");
2231 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002232 case ISD::BIT_CONVERT:
2233 // Fold bit_convert nodes from a type to themselves.
2234 if (N1.getValueType() == VT)
2235 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002236 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002237 }
2238
Chris Lattner43247a12005-08-25 19:12:10 +00002239 // Memoize node if it doesn't produce a flag.
2240 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002241 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002242 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002243 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002244 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002245 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002246 void *IP = 0;
2247 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2248 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002249 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002250 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002251 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002252 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002253 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002254 AllNodes.push_back(N);
2255 return SDOperand(N, 0);
2256}
2257
2258SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002259 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002260 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002261 SDOperand Ops[] = { N1, N2, N3, N4 };
2262 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002263}
2264
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002265SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2266 SDOperand N1, SDOperand N2, SDOperand N3,
2267 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002268 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2269 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002270}
2271
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002272SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2273 SDOperand Src, SDOperand Size,
2274 SDOperand Align,
2275 SDOperand AlwaysInline) {
2276 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2277 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2278}
2279
2280SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2281 SDOperand Src, SDOperand Size,
2282 SDOperand Align,
2283 SDOperand AlwaysInline) {
2284 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2285 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2286}
2287
2288SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2289 SDOperand Src, SDOperand Size,
2290 SDOperand Align,
2291 SDOperand AlwaysInline) {
2292 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2293 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2294}
2295
Evan Cheng7038daf2005-12-10 00:37:58 +00002296SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2297 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002298 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002299 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002300 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2301 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002302 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002303 Ty = MVT::getTypeForValueType(VT);
2304 } else if (SV) {
2305 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2306 assert(PT && "Value for load must be a pointer");
2307 Ty = PT->getElementType();
2308 }
2309 assert(Ty && "Could not get type information for load");
2310 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2311 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002312 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002313 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002314 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002315 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002316 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002317 ID.AddInteger(ISD::UNINDEXED);
2318 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002319 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002320 ID.AddPointer(SV);
2321 ID.AddInteger(SVOffset);
2322 ID.AddInteger(Alignment);
2323 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002324 void *IP = 0;
2325 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2326 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002327 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002328 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2329 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002330 CSEMap.InsertNode(N, IP);
2331 AllNodes.push_back(N);
2332 return SDOperand(N, 0);
2333}
2334
2335SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002336 SDOperand Chain, SDOperand Ptr,
2337 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002338 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002339 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002340 // If they are asking for an extending load from/to the same thing, return a
2341 // normal load.
2342 if (VT == EVT)
2343 ExtType = ISD::NON_EXTLOAD;
2344
2345 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002346 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002347 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002348 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2349 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002350 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2351 "Cannot sign/zero extend a FP/Vector load!");
2352 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2353 "Cannot convert from FP to Int or Int -> FP!");
2354
Dan Gohman575e2f42007-06-04 15:49:41 +00002355 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2356 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002357 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002358 Ty = MVT::getTypeForValueType(VT);
2359 } else if (SV) {
2360 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2361 assert(PT && "Value for load must be a pointer");
2362 Ty = PT->getElementType();
2363 }
2364 assert(Ty && "Could not get type information for load");
2365 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2366 }
Evan Cheng466685d2006-10-09 20:57:25 +00002367 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002368 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002369 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002370 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002371 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002372 ID.AddInteger(ISD::UNINDEXED);
2373 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002374 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002375 ID.AddPointer(SV);
2376 ID.AddInteger(SVOffset);
2377 ID.AddInteger(Alignment);
2378 ID.AddInteger(isVolatile);
2379 void *IP = 0;
2380 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2381 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002382 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002383 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002384 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002385 AllNodes.push_back(N);
2386 return SDOperand(N, 0);
2387}
2388
Evan Cheng144d8f02006-11-09 17:55:04 +00002389SDOperand
2390SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2391 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002392 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002393 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2394 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002395 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002396 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002397 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002398 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002399 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002400 ID.AddInteger(AM);
2401 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002402 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002403 ID.AddPointer(LD->getSrcValue());
2404 ID.AddInteger(LD->getSrcValueOffset());
2405 ID.AddInteger(LD->getAlignment());
2406 ID.AddInteger(LD->isVolatile());
2407 void *IP = 0;
2408 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2409 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002410 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002411 LD->getExtensionType(), LD->getLoadedVT(),
2412 LD->getSrcValue(), LD->getSrcValueOffset(),
2413 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002414 CSEMap.InsertNode(N, IP);
2415 AllNodes.push_back(N);
2416 return SDOperand(N, 0);
2417}
2418
Jeff Cohend41b30d2006-11-05 19:31:28 +00002419SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002420 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002421 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002422 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002423
Dan Gohman575e2f42007-06-04 15:49:41 +00002424 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2425 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002426 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002427 Ty = MVT::getTypeForValueType(VT);
2428 } else if (SV) {
2429 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2430 assert(PT && "Value for store must be a pointer");
2431 Ty = PT->getElementType();
2432 }
2433 assert(Ty && "Could not get type information for store");
2434 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2435 }
Evan Chengad071e12006-10-05 22:57:11 +00002436 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002437 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002438 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002439 FoldingSetNodeID ID;
2440 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002441 ID.AddInteger(ISD::UNINDEXED);
2442 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002443 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002444 ID.AddPointer(SV);
2445 ID.AddInteger(SVOffset);
2446 ID.AddInteger(Alignment);
2447 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002448 void *IP = 0;
2449 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2450 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002451 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002452 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002453 CSEMap.InsertNode(N, IP);
2454 AllNodes.push_back(N);
2455 return SDOperand(N, 0);
2456}
2457
Jeff Cohend41b30d2006-11-05 19:31:28 +00002458SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002459 SDOperand Ptr, const Value *SV,
2460 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002461 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002462 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002463 bool isTrunc = VT != SVT;
2464
Duncan Sandsaf47b112007-10-16 09:56:48 +00002465 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2466 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002467 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2468 "Can't do FP-INT conversion!");
2469
Dan Gohman575e2f42007-06-04 15:49:41 +00002470 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2471 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002472 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002473 Ty = MVT::getTypeForValueType(VT);
2474 } else if (SV) {
2475 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2476 assert(PT && "Value for store must be a pointer");
2477 Ty = PT->getElementType();
2478 }
2479 assert(Ty && "Could not get type information for store");
2480 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2481 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002482 SDVTList VTs = getVTList(MVT::Other);
2483 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002484 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002485 FoldingSetNodeID ID;
2486 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002487 ID.AddInteger(ISD::UNINDEXED);
2488 ID.AddInteger(isTrunc);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002489 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002490 ID.AddPointer(SV);
2491 ID.AddInteger(SVOffset);
2492 ID.AddInteger(Alignment);
2493 ID.AddInteger(isVolatile);
2494 void *IP = 0;
2495 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2496 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002497 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002498 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002499 CSEMap.InsertNode(N, IP);
2500 AllNodes.push_back(N);
2501 return SDOperand(N, 0);
2502}
2503
Evan Cheng144d8f02006-11-09 17:55:04 +00002504SDOperand
2505SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2506 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002507 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2508 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2509 "Store is already a indexed store!");
2510 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2511 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2512 FoldingSetNodeID ID;
2513 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2514 ID.AddInteger(AM);
2515 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002516 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002517 ID.AddPointer(ST->getSrcValue());
2518 ID.AddInteger(ST->getSrcValueOffset());
2519 ID.AddInteger(ST->getAlignment());
2520 ID.AddInteger(ST->isVolatile());
2521 void *IP = 0;
2522 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2523 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002524 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002525 ST->isTruncatingStore(), ST->getStoredVT(),
2526 ST->getSrcValue(), ST->getSrcValueOffset(),
2527 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002528 CSEMap.InsertNode(N, IP);
2529 AllNodes.push_back(N);
2530 return SDOperand(N, 0);
2531}
2532
Nate Begemanacc398c2006-01-25 18:21:52 +00002533SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2534 SDOperand Chain, SDOperand Ptr,
2535 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002536 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002537 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002538}
2539
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002540SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002541 const SDOperand *Ops, unsigned NumOps) {
2542 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002543 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002544 case 1: return getNode(Opcode, VT, Ops[0]);
2545 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2546 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002547 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002548 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002549
Chris Lattneref847df2005-04-09 03:27:28 +00002550 switch (Opcode) {
2551 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002552 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002553 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002554 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2555 "LHS and RHS of condition must have same type!");
2556 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2557 "True and False arms of SelectCC must have same type!");
2558 assert(Ops[2].getValueType() == VT &&
2559 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002560 break;
2561 }
2562 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002563 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002564 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2565 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002566 break;
2567 }
Chris Lattneref847df2005-04-09 03:27:28 +00002568 }
2569
Chris Lattner385328c2005-05-14 07:42:29 +00002570 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002571 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002572 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002573 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002574 FoldingSetNodeID ID;
2575 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002576 void *IP = 0;
2577 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2578 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002579 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002580 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002581 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002582 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002583 }
Chris Lattneref847df2005-04-09 03:27:28 +00002584 AllNodes.push_back(N);
2585 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002586}
2587
Chris Lattner89c34632005-05-14 06:20:26 +00002588SDOperand SelectionDAG::getNode(unsigned Opcode,
2589 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002590 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002591 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2592 Ops, NumOps);
2593}
2594
2595SDOperand SelectionDAG::getNode(unsigned Opcode,
2596 const MVT::ValueType *VTs, unsigned NumVTs,
2597 const SDOperand *Ops, unsigned NumOps) {
2598 if (NumVTs == 1)
2599 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002600 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2601}
2602
2603SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2604 const SDOperand *Ops, unsigned NumOps) {
2605 if (VTList.NumVTs == 1)
2606 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002607
Chris Lattner5f056bf2005-07-10 01:55:33 +00002608 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002609 // FIXME: figure out how to safely handle things like
2610 // int foo(int x) { return 1 << (x & 255); }
2611 // int bar() { return foo(256); }
2612#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002613 case ISD::SRA_PARTS:
2614 case ISD::SRL_PARTS:
2615 case ISD::SHL_PARTS:
2616 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002617 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002618 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2619 else if (N3.getOpcode() == ISD::AND)
2620 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2621 // If the and is only masking out bits that cannot effect the shift,
2622 // eliminate the and.
2623 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2624 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2625 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2626 }
2627 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002628#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002629 }
Chris Lattner89c34632005-05-14 06:20:26 +00002630
Chris Lattner43247a12005-08-25 19:12:10 +00002631 // Memoize the node unless it returns a flag.
2632 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002633 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002634 FoldingSetNodeID ID;
2635 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002636 void *IP = 0;
2637 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2638 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002639 if (NumOps == 1)
2640 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2641 else if (NumOps == 2)
2642 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2643 else if (NumOps == 3)
2644 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2645 else
2646 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002647 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002648 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002649 if (NumOps == 1)
2650 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2651 else if (NumOps == 2)
2652 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2653 else if (NumOps == 3)
2654 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2655 else
2656 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002657 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002658 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002659 return SDOperand(N, 0);
2660}
2661
Dan Gohman08ce9762007-10-08 15:49:58 +00002662SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2663 return getNode(Opcode, VTList, 0, 0);
2664}
2665
2666SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2667 SDOperand N1) {
2668 SDOperand Ops[] = { N1 };
2669 return getNode(Opcode, VTList, Ops, 1);
2670}
2671
2672SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2673 SDOperand N1, SDOperand N2) {
2674 SDOperand Ops[] = { N1, N2 };
2675 return getNode(Opcode, VTList, Ops, 2);
2676}
2677
2678SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2679 SDOperand N1, SDOperand N2, SDOperand N3) {
2680 SDOperand Ops[] = { N1, N2, N3 };
2681 return getNode(Opcode, VTList, Ops, 3);
2682}
2683
2684SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2685 SDOperand N1, SDOperand N2, SDOperand N3,
2686 SDOperand N4) {
2687 SDOperand Ops[] = { N1, N2, N3, N4 };
2688 return getNode(Opcode, VTList, Ops, 4);
2689}
2690
2691SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2692 SDOperand N1, SDOperand N2, SDOperand N3,
2693 SDOperand N4, SDOperand N5) {
2694 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2695 return getNode(Opcode, VTList, Ops, 5);
2696}
2697
Chris Lattner70046e92006-08-15 17:46:01 +00002698SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002699 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002700}
2701
Chris Lattner70046e92006-08-15 17:46:01 +00002702SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002703 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2704 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002705 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002706 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002707 }
2708 std::vector<MVT::ValueType> V;
2709 V.push_back(VT1);
2710 V.push_back(VT2);
2711 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002712 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002713}
Chris Lattner70046e92006-08-15 17:46:01 +00002714SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2715 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002716 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002717 E = VTList.end(); I != E; ++I) {
2718 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2719 (*I)[2] == VT3)
2720 return makeVTList(&(*I)[0], 3);
2721 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002722 std::vector<MVT::ValueType> V;
2723 V.push_back(VT1);
2724 V.push_back(VT2);
2725 V.push_back(VT3);
2726 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002727 return makeVTList(&(*VTList.begin())[0], 3);
2728}
2729
2730SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2731 switch (NumVTs) {
2732 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002733 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002734 case 2: return getVTList(VTs[0], VTs[1]);
2735 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2736 default: break;
2737 }
2738
2739 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2740 E = VTList.end(); I != E; ++I) {
2741 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2742
2743 bool NoMatch = false;
2744 for (unsigned i = 2; i != NumVTs; ++i)
2745 if (VTs[i] != (*I)[i]) {
2746 NoMatch = true;
2747 break;
2748 }
2749 if (!NoMatch)
2750 return makeVTList(&*I->begin(), NumVTs);
2751 }
2752
2753 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2754 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002755}
2756
2757
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002758/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2759/// specified operands. If the resultant node already exists in the DAG,
2760/// this does not modify the specified node, instead it returns the node that
2761/// already exists. If the resultant node does not exist in the DAG, the
2762/// input node is returned. As a degenerate case, if you specify the same
2763/// input operands as the node already has, the input node is returned.
2764SDOperand SelectionDAG::
2765UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2766 SDNode *N = InN.Val;
2767 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2768
2769 // Check to see if there is no change.
2770 if (Op == N->getOperand(0)) return InN;
2771
2772 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002773 void *InsertPos = 0;
2774 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2775 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002776
2777 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002778 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002779 RemoveNodeFromCSEMaps(N);
2780
2781 // Now we update the operands.
2782 N->OperandList[0].Val->removeUser(N);
2783 Op.Val->addUser(N);
2784 N->OperandList[0] = Op;
2785
2786 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002787 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002788 return InN;
2789}
2790
2791SDOperand SelectionDAG::
2792UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2793 SDNode *N = InN.Val;
2794 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2795
2796 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002797 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2798 return InN; // No operands changed, just return the input node.
2799
2800 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002801 void *InsertPos = 0;
2802 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2803 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002804
2805 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002806 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002807 RemoveNodeFromCSEMaps(N);
2808
2809 // Now we update the operands.
2810 if (N->OperandList[0] != Op1) {
2811 N->OperandList[0].Val->removeUser(N);
2812 Op1.Val->addUser(N);
2813 N->OperandList[0] = Op1;
2814 }
2815 if (N->OperandList[1] != Op2) {
2816 N->OperandList[1].Val->removeUser(N);
2817 Op2.Val->addUser(N);
2818 N->OperandList[1] = Op2;
2819 }
2820
2821 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002822 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002823 return InN;
2824}
2825
2826SDOperand SelectionDAG::
2827UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002828 SDOperand Ops[] = { Op1, Op2, Op3 };
2829 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002830}
2831
2832SDOperand SelectionDAG::
2833UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2834 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002835 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2836 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002837}
2838
2839SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002840UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2841 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002842 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2843 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002844}
2845
2846
2847SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002848UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002849 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002850 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002851 "Update with wrong number of operands");
2852
2853 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002854 bool AnyChange = false;
2855 for (unsigned i = 0; i != NumOps; ++i) {
2856 if (Ops[i] != N->getOperand(i)) {
2857 AnyChange = true;
2858 break;
2859 }
2860 }
2861
2862 // No operands changed, just return the input node.
2863 if (!AnyChange) return InN;
2864
2865 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002866 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002867 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002868 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002869
2870 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002871 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002872 RemoveNodeFromCSEMaps(N);
2873
2874 // Now we update the operands.
2875 for (unsigned i = 0; i != NumOps; ++i) {
2876 if (N->OperandList[i] != Ops[i]) {
2877 N->OperandList[i].Val->removeUser(N);
2878 Ops[i].Val->addUser(N);
2879 N->OperandList[i] = Ops[i];
2880 }
2881 }
2882
2883 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002884 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002885 return InN;
2886}
2887
2888
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002889/// MorphNodeTo - This frees the operands of the current node, resets the
2890/// opcode, types, and operands to the specified value. This should only be
2891/// used by the SelectionDAG class.
2892void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2893 const SDOperand *Ops, unsigned NumOps) {
2894 NodeType = Opc;
2895 ValueList = L.VTs;
2896 NumValues = L.NumVTs;
2897
2898 // Clear the operands list, updating used nodes to remove this from their
2899 // use list.
2900 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2901 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002902
2903 // If NumOps is larger than the # of operands we currently have, reallocate
2904 // the operand list.
2905 if (NumOps > NumOperands) {
2906 if (OperandsNeedDelete)
2907 delete [] OperandList;
2908 OperandList = new SDOperand[NumOps];
2909 OperandsNeedDelete = true;
2910 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002911
2912 // Assign the new operands.
2913 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002914
2915 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2916 OperandList[i] = Ops[i];
2917 SDNode *N = OperandList[i].Val;
2918 N->Uses.push_back(this);
2919 }
2920}
Chris Lattner149c58c2005-08-16 18:17:10 +00002921
2922/// SelectNodeTo - These are used for target selectors to *mutate* the
2923/// specified node to have the specified return type, Target opcode, and
2924/// operands. Note that target opcodes are stored as
2925/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002926///
2927/// Note that SelectNodeTo returns the resultant node. If there is already a
2928/// node of the specified opcode and operands, it returns that node instead of
2929/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002930SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2931 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002932 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002933 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002934 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002935 void *IP = 0;
2936 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002937 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002938
Chris Lattner7651fa42005-08-24 23:00:29 +00002939 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002940
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002941 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002942
Chris Lattner4a283e92006-08-11 18:38:11 +00002943 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002944 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002945}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002946
Evan Cheng95514ba2006-08-26 08:00:10 +00002947SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2948 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002949 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002950 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002951 SDOperand Ops[] = { Op1 };
2952
Jim Laskey583bd472006-10-27 23:46:08 +00002953 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002954 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002955 void *IP = 0;
2956 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002957 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002958
Chris Lattner149c58c2005-08-16 18:17:10 +00002959 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002960 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002961 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002962 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002963}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002964
Evan Cheng95514ba2006-08-26 08:00:10 +00002965SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2966 MVT::ValueType VT, SDOperand Op1,
2967 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002968 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002969 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002970 SDOperand Ops[] = { Op1, Op2 };
2971
Jim Laskey583bd472006-10-27 23:46:08 +00002972 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002973 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002974 void *IP = 0;
2975 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002976 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002977
Chris Lattner149c58c2005-08-16 18:17:10 +00002978 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002979
Chris Lattner63e3f142007-02-04 07:28:00 +00002980 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002981
Chris Lattnera5682852006-08-07 23:03:03 +00002982 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002983 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002984}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002985
Evan Cheng95514ba2006-08-26 08:00:10 +00002986SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2987 MVT::ValueType VT, SDOperand Op1,
2988 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002989 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002990 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002991 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002992 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002993 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002994 void *IP = 0;
2995 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002996 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002997
Chris Lattner149c58c2005-08-16 18:17:10 +00002998 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002999
Chris Lattner63e3f142007-02-04 07:28:00 +00003000 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003001
Chris Lattnera5682852006-08-07 23:03:03 +00003002 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003003 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003004}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003005
Evan Cheng95514ba2006-08-26 08:00:10 +00003006SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003007 MVT::ValueType VT, const SDOperand *Ops,
3008 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003009 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003010 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003011 FoldingSetNodeID ID;
3012 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003013 void *IP = 0;
3014 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003015 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003016
Chris Lattner6b09a292005-08-21 18:49:33 +00003017 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003018 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003019
Chris Lattnera5682852006-08-07 23:03:03 +00003020 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003021 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003022}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003023
Evan Cheng95514ba2006-08-26 08:00:10 +00003024SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3025 MVT::ValueType VT1, MVT::ValueType VT2,
3026 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003027 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003028 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003029 SDOperand Ops[] = { Op1, Op2 };
3030 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003031 void *IP = 0;
3032 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003033 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003034
Chris Lattner0fb094f2005-11-19 01:44:53 +00003035 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003036 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003037 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003038 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003039}
3040
Evan Cheng95514ba2006-08-26 08:00:10 +00003041SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3042 MVT::ValueType VT1, MVT::ValueType VT2,
3043 SDOperand Op1, SDOperand Op2,
3044 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003045 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003046 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003047 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003048 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003049 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003050 void *IP = 0;
3051 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003052 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003053
Chris Lattner0fb094f2005-11-19 01:44:53 +00003054 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003055
Chris Lattner63e3f142007-02-04 07:28:00 +00003056 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003057 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003058 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003059}
3060
Chris Lattner0fb094f2005-11-19 01:44:53 +00003061
Evan Cheng6ae46c42006-02-09 07:15:23 +00003062/// getTargetNode - These are used for target selectors to create a new node
3063/// with specified return type(s), target opcode, and operands.
3064///
3065/// Note that getTargetNode returns the resultant node. If there is already a
3066/// node of the specified opcode and operands, it returns that node instead of
3067/// the current one.
3068SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3069 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3070}
3071SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3072 SDOperand Op1) {
3073 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3074}
3075SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3076 SDOperand Op1, SDOperand Op2) {
3077 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3078}
3079SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003080 SDOperand Op1, SDOperand Op2,
3081 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003082 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3083}
3084SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003085 const SDOperand *Ops, unsigned NumOps) {
3086 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003087}
3088SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003089 MVT::ValueType VT2) {
3090 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3091 SDOperand Op;
3092 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3093}
3094SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003095 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003096 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003097 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003098}
3099SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003100 MVT::ValueType VT2, SDOperand Op1,
3101 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003102 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003103 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003104 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003105}
3106SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003107 MVT::ValueType VT2, SDOperand Op1,
3108 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003109 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003110 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003111 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003112}
Evan Cheng694481e2006-08-27 08:08:54 +00003113SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3114 MVT::ValueType VT2,
3115 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003116 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003117 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003118}
3119SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3120 MVT::ValueType VT2, MVT::ValueType VT3,
3121 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003122 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003123 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003124 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003125}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003126SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3127 MVT::ValueType VT2, MVT::ValueType VT3,
3128 SDOperand Op1, SDOperand Op2,
3129 SDOperand Op3) {
3130 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3131 SDOperand Ops[] = { Op1, Op2, Op3 };
3132 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3133}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003134SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003135 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003136 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003137 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3138 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003139}
Evan Cheng05e69c12007-09-12 23:39:49 +00003140SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3141 MVT::ValueType VT2, MVT::ValueType VT3,
3142 MVT::ValueType VT4,
3143 const SDOperand *Ops, unsigned NumOps) {
3144 std::vector<MVT::ValueType> VTList;
3145 VTList.push_back(VT1);
3146 VTList.push_back(VT2);
3147 VTList.push_back(VT3);
3148 VTList.push_back(VT4);
3149 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3150 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3151}
Evan Cheng39305cf2007-10-05 01:10:49 +00003152SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3153 std::vector<MVT::ValueType> &ResultTys,
3154 const SDOperand *Ops, unsigned NumOps) {
3155 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3156 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3157 Ops, NumOps).Val;
3158}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003159
Evan Cheng99157a02006-08-07 22:13:29 +00003160/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003161/// This can cause recursive merging of nodes in the DAG.
3162///
Chris Lattner8b52f212005-08-26 18:36:28 +00003163/// This version assumes From/To have a single result value.
3164///
Chris Lattner1e111c72005-09-07 05:37:01 +00003165void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3166 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003167 SDNode *From = FromN.Val, *To = ToN.Val;
3168 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3169 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003170 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003171
Chris Lattner8b8749f2005-08-17 19:00:20 +00003172 while (!From->use_empty()) {
3173 // Process users until they are all gone.
3174 SDNode *U = *From->use_begin();
3175
3176 // This node is about to morph, remove its old self from the CSE maps.
3177 RemoveNodeFromCSEMaps(U);
3178
Chris Lattner65113b22005-11-08 22:07:03 +00003179 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3180 I != E; ++I)
3181 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003182 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003183 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003184 To->addUser(U);
3185 }
3186
3187 // Now that we have modified U, add it back to the CSE maps. If it already
3188 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003189 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3190 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003191 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003192 if (Deleted) Deleted->push_back(U);
3193 DeleteNodeNotInCSEMaps(U);
3194 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003195 }
3196}
3197
3198/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3199/// This can cause recursive merging of nodes in the DAG.
3200///
3201/// This version assumes From/To have matching types and numbers of result
3202/// values.
3203///
Chris Lattner1e111c72005-09-07 05:37:01 +00003204void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3205 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003206 assert(From != To && "Cannot replace uses of with self");
3207 assert(From->getNumValues() == To->getNumValues() &&
3208 "Cannot use this version of ReplaceAllUsesWith!");
3209 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003210 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003211 return;
3212 }
3213
3214 while (!From->use_empty()) {
3215 // Process users until they are all gone.
3216 SDNode *U = *From->use_begin();
3217
3218 // This node is about to morph, remove its old self from the CSE maps.
3219 RemoveNodeFromCSEMaps(U);
3220
Chris Lattner65113b22005-11-08 22:07:03 +00003221 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3222 I != E; ++I)
3223 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003224 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003225 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003226 To->addUser(U);
3227 }
3228
3229 // Now that we have modified U, add it back to the CSE maps. If it already
3230 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003231 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3232 ReplaceAllUsesWith(U, Existing, Deleted);
3233 // U is now dead.
3234 if (Deleted) Deleted->push_back(U);
3235 DeleteNodeNotInCSEMaps(U);
3236 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003237 }
3238}
3239
Chris Lattner8b52f212005-08-26 18:36:28 +00003240/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3241/// This can cause recursive merging of nodes in the DAG.
3242///
3243/// This version can replace From with any result values. To must match the
3244/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003245void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003246 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003247 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003248 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003249 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003250 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003251 return;
3252 }
3253
3254 while (!From->use_empty()) {
3255 // Process users until they are all gone.
3256 SDNode *U = *From->use_begin();
3257
3258 // This node is about to morph, remove its old self from the CSE maps.
3259 RemoveNodeFromCSEMaps(U);
3260
Chris Lattner65113b22005-11-08 22:07:03 +00003261 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3262 I != E; ++I)
3263 if (I->Val == From) {
3264 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003265 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003266 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003267 ToOp.Val->addUser(U);
3268 }
3269
3270 // Now that we have modified U, add it back to the CSE maps. If it already
3271 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003272 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3273 ReplaceAllUsesWith(U, Existing, Deleted);
3274 // U is now dead.
3275 if (Deleted) Deleted->push_back(U);
3276 DeleteNodeNotInCSEMaps(U);
3277 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003278 }
3279}
3280
Chris Lattner012f2412006-02-17 21:58:01 +00003281/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3282/// uses of other values produced by From.Val alone. The Deleted vector is
3283/// handled the same was as for ReplaceAllUsesWith.
3284void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003285 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003286 assert(From != To && "Cannot replace a value with itself");
3287 // Handle the simple, trivial, case efficiently.
3288 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003289 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003290 return;
3291 }
3292
Chris Lattnercf5640b2007-02-04 00:14:31 +00003293 // Get all of the users of From.Val. We want these in a nice,
3294 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3295 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003296
Chris Lattner01d029b2007-10-15 06:10:22 +00003297 std::vector<SDNode*> LocalDeletionVector;
3298
3299 // Pick a deletion vector to use. If the user specified one, use theirs,
3300 // otherwise use a local one.
3301 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003302 while (!Users.empty()) {
3303 // We know that this user uses some value of From. If it is the right
3304 // value, update it.
3305 SDNode *User = Users.back();
3306 Users.pop_back();
3307
Chris Lattner01d029b2007-10-15 06:10:22 +00003308 // Scan for an operand that matches From.
3309 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3310 for (; Op != E; ++Op)
3311 if (*Op == From) break;
3312
3313 // If there are no matches, the user must use some other result of From.
3314 if (Op == E) continue;
3315
3316 // Okay, we know this user needs to be updated. Remove its old self
3317 // from the CSE maps.
3318 RemoveNodeFromCSEMaps(User);
3319
3320 // Update all operands that match "From".
3321 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003322 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003323 From.Val->removeUser(User);
3324 *Op = To;
3325 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003326 }
3327 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003328
3329 // Now that we have modified User, add it back to the CSE maps. If it
3330 // already exists there, recursively merge the results together.
3331 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3332 if (!Existing) continue; // Continue on to next user.
3333
3334 // If there was already an existing matching node, use ReplaceAllUsesWith
3335 // to replace the dead one with the existing one. However, this can cause
3336 // recursive merging of other unrelated nodes down the line. The merging
3337 // can cause deletion of nodes that used the old value. In this case,
3338 // we have to be certain to remove them from the Users set.
3339 unsigned NumDeleted = DeleteVector->size();
3340 ReplaceAllUsesWith(User, Existing, DeleteVector);
3341
3342 // User is now dead.
3343 DeleteVector->push_back(User);
3344 DeleteNodeNotInCSEMaps(User);
3345
3346 // We have to be careful here, because ReplaceAllUsesWith could have
3347 // deleted a user of From, which means there may be dangling pointers
3348 // in the "Users" setvector. Scan over the deleted node pointers and
3349 // remove them from the setvector.
3350 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3351 Users.remove((*DeleteVector)[i]);
3352
3353 // If the user doesn't need the set of deleted elements, don't retain them
3354 // to the next loop iteration.
3355 if (Deleted == 0)
3356 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003357 }
3358}
3359
Chris Lattner7b2880c2005-08-24 22:44:39 +00003360
Evan Chenge6f35d82006-08-01 08:20:41 +00003361/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3362/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003363unsigned SelectionDAG::AssignNodeIds() {
3364 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003365 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3366 SDNode *N = I;
3367 N->setNodeId(Id++);
3368 }
3369 return Id;
3370}
3371
Evan Chenge6f35d82006-08-01 08:20:41 +00003372/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003373/// based on their topological order. It returns the maximum id and a vector
3374/// of the SDNodes* in assigned order by reference.
3375unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003376 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003377 std::vector<unsigned> InDegree(DAGSize);
3378 std::vector<SDNode*> Sources;
3379
3380 // Use a two pass approach to avoid using a std::map which is slow.
3381 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003382 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3383 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003384 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003385 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003386 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003387 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003388 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003389 }
3390
Evan Cheng99157a02006-08-07 22:13:29 +00003391 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003392 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003393 SDNode *N = Sources.back();
3394 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003395 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003396 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3397 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003398 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003399 if (Degree == 0)
3400 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003401 }
3402 }
3403
Evan Chengc384d6c2006-08-02 22:00:34 +00003404 // Second pass, assign the actual topological order as node ids.
3405 Id = 0;
3406 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3407 TI != TE; ++TI)
3408 (*TI)->setNodeId(Id++);
3409
3410 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003411}
3412
3413
Evan Cheng091cba12006-07-27 06:39:06 +00003414
Jim Laskey58b968b2005-08-17 20:08:02 +00003415//===----------------------------------------------------------------------===//
3416// SDNode Class
3417//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003418
Chris Lattner917d2c92006-07-19 00:00:37 +00003419// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003420void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003421void UnarySDNode::ANCHOR() {}
3422void BinarySDNode::ANCHOR() {}
3423void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003424void HandleSDNode::ANCHOR() {}
3425void StringSDNode::ANCHOR() {}
3426void ConstantSDNode::ANCHOR() {}
3427void ConstantFPSDNode::ANCHOR() {}
3428void GlobalAddressSDNode::ANCHOR() {}
3429void FrameIndexSDNode::ANCHOR() {}
3430void JumpTableSDNode::ANCHOR() {}
3431void ConstantPoolSDNode::ANCHOR() {}
3432void BasicBlockSDNode::ANCHOR() {}
3433void SrcValueSDNode::ANCHOR() {}
3434void RegisterSDNode::ANCHOR() {}
3435void ExternalSymbolSDNode::ANCHOR() {}
3436void CondCodeSDNode::ANCHOR() {}
3437void VTSDNode::ANCHOR() {}
3438void LoadSDNode::ANCHOR() {}
3439void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003440
Chris Lattner48b85922007-02-04 02:41:42 +00003441HandleSDNode::~HandleSDNode() {
3442 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003443 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003444}
3445
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003446GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3447 MVT::ValueType VT, int o)
3448 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003449 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003450 // Thread Local
3451 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3452 // Non Thread Local
3453 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3454 getSDVTList(VT)), Offset(o) {
3455 TheGlobal = const_cast<GlobalValue*>(GA);
3456}
Chris Lattner48b85922007-02-04 02:41:42 +00003457
Jim Laskey583bd472006-10-27 23:46:08 +00003458/// Profile - Gather unique data for the node.
3459///
3460void SDNode::Profile(FoldingSetNodeID &ID) {
3461 AddNodeIDNode(ID, this);
3462}
3463
Chris Lattnera3255112005-11-08 23:30:28 +00003464/// getValueTypeList - Return a pointer to the specified value type.
3465///
3466MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003467 if (MVT::isExtendedVT(VT)) {
3468 static std::set<MVT::ValueType> EVTs;
3469 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3470 } else {
3471 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3472 VTs[VT] = VT;
3473 return &VTs[VT];
3474 }
Chris Lattnera3255112005-11-08 23:30:28 +00003475}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003476
Chris Lattner5c884562005-01-12 18:37:47 +00003477/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3478/// indicated value. This method ignores uses of other values defined by this
3479/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003480bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003481 assert(Value < getNumValues() && "Bad value!");
3482
3483 // If there is only one value, this is easy.
3484 if (getNumValues() == 1)
3485 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003486 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003487
Evan Cheng4ee62112006-02-05 06:29:23 +00003488 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003489
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003490 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003491
Chris Lattnerf83482d2006-08-16 20:59:32 +00003492 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003493 SDNode *User = *UI;
3494 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003495 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003496 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3497 if (User->getOperand(i) == TheValue) {
3498 if (NUses == 0)
3499 return false; // too many uses
3500 --NUses;
3501 }
3502 }
3503
3504 // Found exactly the right number of uses?
3505 return NUses == 0;
3506}
3507
3508
Evan Cheng33d55952007-08-02 05:29:38 +00003509/// hasAnyUseOfValue - Return true if there are any use of the indicated
3510/// value. This method ignores uses of other values defined by this operation.
3511bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3512 assert(Value < getNumValues() && "Bad value!");
3513
3514 if (use_size() == 0) return false;
3515
3516 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3517
3518 SmallPtrSet<SDNode*, 32> UsersHandled;
3519
3520 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3521 SDNode *User = *UI;
3522 if (User->getNumOperands() == 1 ||
3523 UsersHandled.insert(User)) // First time we've seen this?
3524 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3525 if (User->getOperand(i) == TheValue) {
3526 return true;
3527 }
3528 }
3529
3530 return false;
3531}
3532
3533
Evan Chenge6e97e62006-11-03 07:31:32 +00003534/// isOnlyUse - Return true if this node is the only use of N.
3535///
Evan Cheng4ee62112006-02-05 06:29:23 +00003536bool SDNode::isOnlyUse(SDNode *N) const {
3537 bool Seen = false;
3538 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3539 SDNode *User = *I;
3540 if (User == this)
3541 Seen = true;
3542 else
3543 return false;
3544 }
3545
3546 return Seen;
3547}
3548
Evan Chenge6e97e62006-11-03 07:31:32 +00003549/// isOperand - Return true if this node is an operand of N.
3550///
Evan Chengbfa284f2006-03-03 06:42:32 +00003551bool SDOperand::isOperand(SDNode *N) const {
3552 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3553 if (*this == N->getOperand(i))
3554 return true;
3555 return false;
3556}
3557
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003558bool SDNode::isOperand(SDNode *N) const {
3559 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3560 if (this == N->OperandList[i].Val)
3561 return true;
3562 return false;
3563}
Evan Cheng4ee62112006-02-05 06:29:23 +00003564
Evan Chengc5fc57d2006-11-03 03:05:24 +00003565static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003566 SmallPtrSet<SDNode *, 32> &Visited) {
3567 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003568 return;
3569
3570 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3571 SDNode *Op = N->getOperand(i).Val;
3572 if (Op == P) {
3573 found = true;
3574 return;
3575 }
3576 findPredecessor(Op, P, found, Visited);
3577 }
3578}
3579
Evan Chenge6e97e62006-11-03 07:31:32 +00003580/// isPredecessor - Return true if this node is a predecessor of N. This node
3581/// is either an operand of N or it can be reached by recursively traversing
3582/// up the operands.
3583/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003584bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003585 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003586 bool found = false;
3587 findPredecessor(N, this, found, Visited);
3588 return found;
3589}
3590
Evan Chengc5484282006-10-04 00:56:09 +00003591uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3592 assert(Num < NumOperands && "Invalid child # of SDNode!");
3593 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3594}
3595
Reid Spencer577cc322007-04-01 07:32:19 +00003596std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003597 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003598 default:
3599 if (getOpcode() < ISD::BUILTIN_OP_END)
3600 return "<<Unknown DAG Node>>";
3601 else {
Evan Cheng72261582005-12-20 06:22:03 +00003602 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003603 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003604 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3605 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003606
Evan Cheng72261582005-12-20 06:22:03 +00003607 TargetLowering &TLI = G->getTargetLoweringInfo();
3608 const char *Name =
3609 TLI.getTargetNodeName(getOpcode());
3610 if (Name) return Name;
3611 }
3612
3613 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003614 }
3615
Andrew Lenharth95762122005-03-31 21:24:06 +00003616 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003617 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003618 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003619 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003620 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003621 case ISD::AssertSext: return "AssertSext";
3622 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003623
3624 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003625 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003626 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003627 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003628
3629 case ISD::Constant: return "Constant";
3630 case ISD::ConstantFP: return "ConstantFP";
3631 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003632 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003633 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003634 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003635 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003636 case ISD::RETURNADDR: return "RETURNADDR";
3637 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003638 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003639 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3640 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003641 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003642 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003643 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003644 case ISD::INTRINSIC_WO_CHAIN: {
3645 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3646 return Intrinsic::getName((Intrinsic::ID)IID);
3647 }
3648 case ISD::INTRINSIC_VOID:
3649 case ISD::INTRINSIC_W_CHAIN: {
3650 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003651 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003652 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003653
Chris Lattnerb2827b02006-03-19 00:52:58 +00003654 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003655 case ISD::TargetConstant: return "TargetConstant";
3656 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003657 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003658 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003659 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003660 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003661 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003662 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003663
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003664 case ISD::CopyToReg: return "CopyToReg";
3665 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003666 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003667 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003668 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003669 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003670 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003671 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003672 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003673
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003674 // Unary operators
3675 case ISD::FABS: return "fabs";
3676 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003677 case ISD::FSQRT: return "fsqrt";
3678 case ISD::FSIN: return "fsin";
3679 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003680 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003681 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003682
3683 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003684 case ISD::ADD: return "add";
3685 case ISD::SUB: return "sub";
3686 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003687 case ISD::MULHU: return "mulhu";
3688 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003689 case ISD::SDIV: return "sdiv";
3690 case ISD::UDIV: return "udiv";
3691 case ISD::SREM: return "srem";
3692 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003693 case ISD::SMUL_LOHI: return "smul_lohi";
3694 case ISD::UMUL_LOHI: return "umul_lohi";
3695 case ISD::SDIVREM: return "sdivrem";
3696 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003697 case ISD::AND: return "and";
3698 case ISD::OR: return "or";
3699 case ISD::XOR: return "xor";
3700 case ISD::SHL: return "shl";
3701 case ISD::SRA: return "sra";
3702 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003703 case ISD::ROTL: return "rotl";
3704 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003705 case ISD::FADD: return "fadd";
3706 case ISD::FSUB: return "fsub";
3707 case ISD::FMUL: return "fmul";
3708 case ISD::FDIV: return "fdiv";
3709 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003710 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003711
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003712 case ISD::SETCC: return "setcc";
3713 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003714 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003715 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003716 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003717 case ISD::CONCAT_VECTORS: return "concat_vectors";
3718 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003719 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003720 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003721 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003722 case ISD::ADDC: return "addc";
3723 case ISD::ADDE: return "adde";
3724 case ISD::SUBC: return "subc";
3725 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003726 case ISD::SHL_PARTS: return "shl_parts";
3727 case ISD::SRA_PARTS: return "sra_parts";
3728 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003729
3730 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3731 case ISD::INSERT_SUBREG: return "insert_subreg";
3732
Chris Lattner7f644642005-04-28 21:44:03 +00003733 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003734 case ISD::SIGN_EXTEND: return "sign_extend";
3735 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003736 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003737 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003738 case ISD::TRUNCATE: return "truncate";
3739 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00003740 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003741 case ISD::FP_EXTEND: return "fp_extend";
3742
3743 case ISD::SINT_TO_FP: return "sint_to_fp";
3744 case ISD::UINT_TO_FP: return "uint_to_fp";
3745 case ISD::FP_TO_SINT: return "fp_to_sint";
3746 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003747 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003748
3749 // Control flow instructions
3750 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003751 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003752 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003753 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003754 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003755 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003756 case ISD::CALLSEQ_START: return "callseq_start";
3757 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003758
3759 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003760 case ISD::LOAD: return "load";
3761 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003762 case ISD::VAARG: return "vaarg";
3763 case ISD::VACOPY: return "vacopy";
3764 case ISD::VAEND: return "vaend";
3765 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003766 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003767 case ISD::EXTRACT_ELEMENT: return "extract_element";
3768 case ISD::BUILD_PAIR: return "build_pair";
3769 case ISD::STACKSAVE: return "stacksave";
3770 case ISD::STACKRESTORE: return "stackrestore";
3771
3772 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003773 case ISD::MEMSET: return "memset";
3774 case ISD::MEMCPY: return "memcpy";
3775 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003776
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003777 // Bit manipulation
3778 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003779 case ISD::CTPOP: return "ctpop";
3780 case ISD::CTTZ: return "cttz";
3781 case ISD::CTLZ: return "ctlz";
3782
Chris Lattner36ce6912005-11-29 06:21:05 +00003783 // Debug info
3784 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003785 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003786
Duncan Sands36397f52007-07-27 12:58:54 +00003787 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003788 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003789
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003790 case ISD::CONDCODE:
3791 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003792 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003793 case ISD::SETOEQ: return "setoeq";
3794 case ISD::SETOGT: return "setogt";
3795 case ISD::SETOGE: return "setoge";
3796 case ISD::SETOLT: return "setolt";
3797 case ISD::SETOLE: return "setole";
3798 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003799
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003800 case ISD::SETO: return "seto";
3801 case ISD::SETUO: return "setuo";
3802 case ISD::SETUEQ: return "setue";
3803 case ISD::SETUGT: return "setugt";
3804 case ISD::SETUGE: return "setuge";
3805 case ISD::SETULT: return "setult";
3806 case ISD::SETULE: return "setule";
3807 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003808
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003809 case ISD::SETEQ: return "seteq";
3810 case ISD::SETGT: return "setgt";
3811 case ISD::SETGE: return "setge";
3812 case ISD::SETLT: return "setlt";
3813 case ISD::SETLE: return "setle";
3814 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003815 }
3816 }
3817}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003818
Evan Cheng144d8f02006-11-09 17:55:04 +00003819const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003820 switch (AM) {
3821 default:
3822 return "";
3823 case ISD::PRE_INC:
3824 return "<pre-inc>";
3825 case ISD::PRE_DEC:
3826 return "<pre-dec>";
3827 case ISD::POST_INC:
3828 return "<post-inc>";
3829 case ISD::POST_DEC:
3830 return "<post-dec>";
3831 }
3832}
3833
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003834void SDNode::dump() const { dump(0); }
3835void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003836 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003837
3838 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003839 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003840 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003841 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003842 else
Bill Wendling832171c2006-12-07 20:04:42 +00003843 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003844 }
Bill Wendling832171c2006-12-07 20:04:42 +00003845 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003846
Bill Wendling832171c2006-12-07 20:04:42 +00003847 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003848 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003849 if (i) cerr << ", ";
3850 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003851 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003852 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003853 }
3854
3855 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003856 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003857 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003858 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3859 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3860 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3861 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3862 else {
3863 cerr << "<APFloat(";
3864 CSDN->getValueAPF().convertToAPInt().dump();
3865 cerr << ")>";
3866 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003867 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003868 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003869 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003870 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003871 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003872 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003873 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003874 else
Bill Wendling832171c2006-12-07 20:04:42 +00003875 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003876 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003877 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003878 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003879 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003880 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003881 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003882 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003883 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003884 else
Bill Wendling832171c2006-12-07 20:04:42 +00003885 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003886 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003887 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003888 else
Bill Wendling832171c2006-12-07 20:04:42 +00003889 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003890 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003891 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003892 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3893 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003894 cerr << LBB->getName() << " ";
3895 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003896 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003897 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003898 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003899 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003900 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003901 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003902 } else if (const ExternalSymbolSDNode *ES =
3903 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003904 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003905 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3906 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003907 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003908 else
Bill Wendling832171c2006-12-07 20:04:42 +00003909 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003910 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003911 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003912 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3913 bool doExt = true;
3914 switch (LD->getExtensionType()) {
3915 default: doExt = false; break;
3916 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003917 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003918 break;
3919 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003920 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003921 break;
3922 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003923 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003924 break;
3925 }
3926 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003927 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003928
Evan Cheng144d8f02006-11-09 17:55:04 +00003929 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003930 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003931 cerr << " " << AM;
Evan Cheng2caccca2006-10-17 21:14:32 +00003932 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3933 if (ST->isTruncatingStore())
Bill Wendling832171c2006-12-07 20:04:42 +00003934 cerr << " <trunc "
3935 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00003936
Evan Cheng144d8f02006-11-09 17:55:04 +00003937 const char *AM = getIndexedModeName(ST->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003938 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003939 cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003940 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003941}
3942
Chris Lattnerde202b32005-11-09 23:47:37 +00003943static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003944 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3945 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003946 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003947 else
Bill Wendling832171c2006-12-07 20:04:42 +00003948 cerr << "\n" << std::string(indent+2, ' ')
3949 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003950
Chris Lattnerea946cd2005-01-09 20:38:33 +00003951
Bill Wendling832171c2006-12-07 20:04:42 +00003952 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003953 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003954}
3955
Chris Lattnerc3aae252005-01-07 07:46:32 +00003956void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003957 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003958 std::vector<const SDNode*> Nodes;
3959 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3960 I != E; ++I)
3961 Nodes.push_back(I);
3962
Chris Lattner49d24712005-01-09 20:26:36 +00003963 std::sort(Nodes.begin(), Nodes.end());
3964
3965 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003966 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003967 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003968 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003969
Jim Laskey26f7fa72006-10-17 19:33:52 +00003970 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003971
Bill Wendling832171c2006-12-07 20:04:42 +00003972 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003973}
3974
Evan Chengd6594ae2006-09-12 21:00:35 +00003975const Type *ConstantPoolSDNode::getType() const {
3976 if (isMachineConstantPoolEntry())
3977 return Val.MachineCPVal->getType();
3978 return Val.ConstVal->getType();
3979}