blob: d295a607777ebe0c58defb2666ede2d5736ca05f [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;
Chris Lattner15e4b012005-07-10 00:07:11 +0000530 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000531 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000532 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
533 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000534 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000535 // Remove it from the CSE Map.
536 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000537 break;
538 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000539#ifndef NDEBUG
540 // Verify that the node was actually in one of the CSE maps, unless it has a
541 // flag result (which cannot be CSE'd) or is one of the special cases that are
542 // not subject to CSE.
543 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000544 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000545 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000546 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000547 assert(0 && "Node is not in map!");
548 }
549#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000550}
551
Chris Lattner8b8749f2005-08-17 19:00:20 +0000552/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
553/// has been taken out and modified in some way. If the specified node already
554/// exists in the CSE maps, do not modify the maps, but return the existing node
555/// instead. If it doesn't exist, add it and return null.
556///
557SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
558 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000559 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000560 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000561
Chris Lattner9f8cc692005-12-19 22:21:21 +0000562 // Check that remaining values produced are not flags.
563 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
564 if (N->getValueType(i) == MVT::Flag)
565 return 0; // Never CSE anything that produces a flag.
566
Chris Lattnera5682852006-08-07 23:03:03 +0000567 SDNode *New = CSEMap.GetOrInsertNode(N);
568 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000569 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000570}
571
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000572/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
573/// were replaced with those specified. If this node is never memoized,
574/// return null, otherwise return a pointer to the slot it would take. If a
575/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000576SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
577 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000578 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000579 return 0; // Never add these nodes.
580
581 // Check that remaining values produced are not flags.
582 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
583 if (N->getValueType(i) == MVT::Flag)
584 return 0; // Never CSE anything that produces a flag.
585
Chris Lattner63e3f142007-02-04 07:28:00 +0000586 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000587 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000588 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000589 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000590}
591
592/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
593/// were replaced with those specified. If this node is never memoized,
594/// return null, otherwise return a pointer to the slot it would take. If a
595/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000596SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
597 SDOperand Op1, SDOperand Op2,
598 void *&InsertPos) {
599 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
600 return 0; // Never add these nodes.
601
602 // Check that remaining values produced are not flags.
603 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
604 if (N->getValueType(i) == MVT::Flag)
605 return 0; // Never CSE anything that produces a flag.
606
Chris Lattner63e3f142007-02-04 07:28:00 +0000607 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000608 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000609 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000610 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
611}
612
613
614/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
615/// were replaced with those specified. If this node is never memoized,
616/// return null, otherwise return a pointer to the slot it would take. If a
617/// node already exists with these operands, the slot will be non-null.
618SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000619 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000620 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000621 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000622 return 0; // Never add these nodes.
623
624 // Check that remaining values produced are not flags.
625 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
626 if (N->getValueType(i) == MVT::Flag)
627 return 0; // Never CSE anything that produces a flag.
628
Jim Laskey583bd472006-10-27 23:46:08 +0000629 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000630 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000631
Evan Cheng9629aba2006-10-11 01:47:58 +0000632 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
633 ID.AddInteger(LD->getAddressingMode());
634 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000635 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000636 ID.AddPointer(LD->getSrcValue());
637 ID.AddInteger(LD->getSrcValueOffset());
638 ID.AddInteger(LD->getAlignment());
639 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000640 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
641 ID.AddInteger(ST->getAddressingMode());
642 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000643 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000644 ID.AddPointer(ST->getSrcValue());
645 ID.AddInteger(ST->getSrcValueOffset());
646 ID.AddInteger(ST->getAlignment());
647 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000648 }
Jim Laskey583bd472006-10-27 23:46:08 +0000649
Chris Lattnera5682852006-08-07 23:03:03 +0000650 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000651}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000652
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000653
Chris Lattner78ec3112003-08-11 14:57:33 +0000654SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000655 while (!AllNodes.empty()) {
656 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000657 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000658 if (N->OperandsNeedDelete)
659 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000660 N->OperandList = 0;
661 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000662 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000663 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000664}
665
Chris Lattner0f2287b2005-04-13 02:38:18 +0000666SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000667 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000668 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000669 return getNode(ISD::AND, Op.getValueType(), Op,
670 getConstant(Imm, Op.getValueType()));
671}
672
Chris Lattner36ce6912005-11-29 06:21:05 +0000673SDOperand SelectionDAG::getString(const std::string &Val) {
674 StringSDNode *&N = StringNodes[Val];
675 if (!N) {
676 N = new StringSDNode(Val);
677 AllNodes.push_back(N);
678 }
679 return SDOperand(N, 0);
680}
681
Chris Lattner61b09412006-08-11 21:01:22 +0000682SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000683 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000684 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000685
Chris Lattner61b09412006-08-11 21:01:22 +0000686 // Mask out any bits that are not valid for this constant.
687 Val &= MVT::getIntVTBitMask(VT);
688
689 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000690 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000691 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000692 ID.AddInteger(Val);
693 void *IP = 0;
694 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
695 return SDOperand(E, 0);
696 SDNode *N = new ConstantSDNode(isT, Val, VT);
697 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000698 AllNodes.push_back(N);
699 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000700}
701
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000702SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000703 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000704 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000705
Dan Gohman7f321562007-06-25 16:23:39 +0000706 MVT::ValueType EltVT =
707 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000708
Chris Lattnerd8658612005-02-17 20:17:32 +0000709 // Do the map lookup using the actual bit pattern for the floating point
710 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
711 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000712 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000713 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000714 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000715 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000716 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000717 SDNode *N = NULL;
718 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
719 if (!MVT::isVector(VT))
720 return SDOperand(N, 0);
721 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000722 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000723 CSEMap.InsertNode(N, IP);
724 AllNodes.push_back(N);
725 }
726
Dan Gohman7f321562007-06-25 16:23:39 +0000727 SDOperand Result(N, 0);
728 if (MVT::isVector(VT)) {
729 SmallVector<SDOperand, 8> Ops;
730 Ops.assign(MVT::getVectorNumElements(VT), Result);
731 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
732 }
733 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000734}
735
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000736SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
737 bool isTarget) {
738 MVT::ValueType EltVT =
739 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
740 if (EltVT==MVT::f32)
741 return getConstantFP(APFloat((float)Val), VT, isTarget);
742 else
743 return getConstantFP(APFloat(Val), VT, isTarget);
744}
745
Chris Lattnerc3aae252005-01-07 07:46:32 +0000746SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000747 MVT::ValueType VT, int Offset,
748 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000749 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
750 unsigned Opc;
751 if (GVar && GVar->isThreadLocal())
752 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
753 else
754 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000755 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000756 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000757 ID.AddPointer(GV);
758 ID.AddInteger(Offset);
759 void *IP = 0;
760 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
761 return SDOperand(E, 0);
762 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
763 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000764 AllNodes.push_back(N);
765 return SDOperand(N, 0);
766}
767
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000768SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
769 bool isTarget) {
770 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000771 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000772 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000773 ID.AddInteger(FI);
774 void *IP = 0;
775 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
776 return SDOperand(E, 0);
777 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
778 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000779 AllNodes.push_back(N);
780 return SDOperand(N, 0);
781}
782
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000783SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
784 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000785 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000786 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000787 ID.AddInteger(JTI);
788 void *IP = 0;
789 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
790 return SDOperand(E, 0);
791 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
792 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000793 AllNodes.push_back(N);
794 return SDOperand(N, 0);
795}
796
Evan Chengb8973bd2006-01-31 22:23:14 +0000797SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000798 unsigned Alignment, int Offset,
799 bool isTarget) {
800 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000801 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000802 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000803 ID.AddInteger(Alignment);
804 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000805 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000806 void *IP = 0;
807 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
808 return SDOperand(E, 0);
809 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
810 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000811 AllNodes.push_back(N);
812 return SDOperand(N, 0);
813}
814
Chris Lattnerc3aae252005-01-07 07:46:32 +0000815
Evan Chengd6594ae2006-09-12 21:00:35 +0000816SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
817 MVT::ValueType VT,
818 unsigned Alignment, int Offset,
819 bool isTarget) {
820 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000821 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000822 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000823 ID.AddInteger(Alignment);
824 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000825 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000826 void *IP = 0;
827 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
828 return SDOperand(E, 0);
829 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
830 CSEMap.InsertNode(N, IP);
831 AllNodes.push_back(N);
832 return SDOperand(N, 0);
833}
834
835
Chris Lattnerc3aae252005-01-07 07:46:32 +0000836SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000837 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000838 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000839 ID.AddPointer(MBB);
840 void *IP = 0;
841 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
842 return SDOperand(E, 0);
843 SDNode *N = new BasicBlockSDNode(MBB);
844 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000845 AllNodes.push_back(N);
846 return SDOperand(N, 0);
847}
848
Chris Lattner15e4b012005-07-10 00:07:11 +0000849SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +0000850 assert(!MVT::isExtendedVT(VT) && "Expecting a simple value type!");
Chris Lattner15e4b012005-07-10 00:07:11 +0000851 if ((unsigned)VT >= ValueTypeNodes.size())
852 ValueTypeNodes.resize(VT+1);
853 if (ValueTypeNodes[VT] == 0) {
854 ValueTypeNodes[VT] = new VTSDNode(VT);
855 AllNodes.push_back(ValueTypeNodes[VT]);
856 }
857
858 return SDOperand(ValueTypeNodes[VT], 0);
859}
860
Chris Lattnerc3aae252005-01-07 07:46:32 +0000861SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
862 SDNode *&N = ExternalSymbols[Sym];
863 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000864 N = new ExternalSymbolSDNode(false, Sym, VT);
865 AllNodes.push_back(N);
866 return SDOperand(N, 0);
867}
868
Chris Lattner809ec112006-01-28 10:09:25 +0000869SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
870 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000871 SDNode *&N = TargetExternalSymbols[Sym];
872 if (N) return SDOperand(N, 0);
873 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000874 AllNodes.push_back(N);
875 return SDOperand(N, 0);
876}
877
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000878SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
879 if ((unsigned)Cond >= CondCodeNodes.size())
880 CondCodeNodes.resize(Cond+1);
881
Chris Lattner079a27a2005-08-09 20:40:02 +0000882 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000883 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000884 AllNodes.push_back(CondCodeNodes[Cond]);
885 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000886 return SDOperand(CondCodeNodes[Cond], 0);
887}
888
Chris Lattner0fdd7682005-08-30 22:38:38 +0000889SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000890 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000891 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000892 ID.AddInteger(RegNo);
893 void *IP = 0;
894 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
895 return SDOperand(E, 0);
896 SDNode *N = new RegisterSDNode(RegNo, VT);
897 CSEMap.InsertNode(N, IP);
898 AllNodes.push_back(N);
899 return SDOperand(N, 0);
900}
901
902SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
903 assert((!V || isa<PointerType>(V->getType())) &&
904 "SrcValue is not a pointer?");
905
Jim Laskey583bd472006-10-27 23:46:08 +0000906 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000907 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000908 ID.AddPointer(V);
909 ID.AddInteger(Offset);
910 void *IP = 0;
911 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
912 return SDOperand(E, 0);
913 SDNode *N = new SrcValueSDNode(V, Offset);
914 CSEMap.InsertNode(N, IP);
915 AllNodes.push_back(N);
916 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000917}
918
Chris Lattner37ce9df2007-10-15 17:47:20 +0000919/// CreateStackTemporary - Create a stack temporary, suitable for holding the
920/// specified value type.
921SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
922 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
923 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
924 const Type *Ty = MVT::getTypeForValueType(VT);
925 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
926 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
927 return getFrameIndex(FrameIdx, TLI.getPointerTy());
928}
929
930
Chris Lattner51dabfb2006-10-14 00:41:01 +0000931SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
932 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 // These setcc operations always fold.
934 switch (Cond) {
935 default: break;
936 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000937 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000938 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000939 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000940
941 case ISD::SETOEQ:
942 case ISD::SETOGT:
943 case ISD::SETOGE:
944 case ISD::SETOLT:
945 case ISD::SETOLE:
946 case ISD::SETONE:
947 case ISD::SETO:
948 case ISD::SETUO:
949 case ISD::SETUEQ:
950 case ISD::SETUNE:
951 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
952 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000953 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000954
Chris Lattner67255a12005-04-07 18:14:58 +0000955 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
956 uint64_t C2 = N2C->getValue();
957 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
958 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000959
Chris Lattnerc3aae252005-01-07 07:46:32 +0000960 // Sign extend the operands if required
961 if (ISD::isSignedIntSetCC(Cond)) {
962 C1 = N1C->getSignExtended();
963 C2 = N2C->getSignExtended();
964 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000965
Chris Lattnerc3aae252005-01-07 07:46:32 +0000966 switch (Cond) {
967 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000968 case ISD::SETEQ: return getConstant(C1 == C2, VT);
969 case ISD::SETNE: return getConstant(C1 != C2, VT);
970 case ISD::SETULT: return getConstant(C1 < C2, VT);
971 case ISD::SETUGT: return getConstant(C1 > C2, VT);
972 case ISD::SETULE: return getConstant(C1 <= C2, VT);
973 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
974 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
975 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
976 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
977 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000978 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000979 }
Chris Lattner67255a12005-04-07 18:14:58 +0000980 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000981 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
982 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +0000983 // No compile time operations on this type yet.
984 if (N1C->getValueType(0) == MVT::ppcf128)
985 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +0000986
987 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +0000988 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000989 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +0000990 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
991 return getNode(ISD::UNDEF, VT);
992 // fall through
993 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
994 case ISD::SETNE: if (R==APFloat::cmpUnordered)
995 return getNode(ISD::UNDEF, VT);
996 // fall through
997 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000998 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +0000999 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1000 return getNode(ISD::UNDEF, VT);
1001 // fall through
1002 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1003 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1004 return getNode(ISD::UNDEF, VT);
1005 // fall through
1006 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1007 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1008 return getNode(ISD::UNDEF, VT);
1009 // fall through
1010 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001011 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001012 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1013 return getNode(ISD::UNDEF, VT);
1014 // fall through
1015 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001016 R==APFloat::cmpEqual, VT);
1017 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1018 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1019 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1020 R==APFloat::cmpEqual, VT);
1021 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1022 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1023 R==APFloat::cmpLessThan, VT);
1024 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1025 R==APFloat::cmpUnordered, VT);
1026 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1027 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001028 }
1029 } else {
1030 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001031 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001032 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001033
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001034 // Could not fold it.
1035 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001036}
1037
Dan Gohmanea859be2007-06-22 14:59:07 +00001038/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1039/// this predicate to simplify operations downstream. Mask is known to be zero
1040/// for bits that V cannot have.
1041bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1042 unsigned Depth) const {
1043 // The masks are not wide enough to represent this type! Should use APInt.
1044 if (Op.getValueType() == MVT::i128)
1045 return false;
1046
1047 uint64_t KnownZero, KnownOne;
1048 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1049 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1050 return (KnownZero & Mask) == Mask;
1051}
1052
1053/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1054/// known to be either zero or one and return them in the KnownZero/KnownOne
1055/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1056/// processing.
1057void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1058 uint64_t &KnownZero, uint64_t &KnownOne,
1059 unsigned Depth) const {
1060 KnownZero = KnownOne = 0; // Don't know anything.
1061 if (Depth == 6 || Mask == 0)
1062 return; // Limit search depth.
1063
1064 // The masks are not wide enough to represent this type! Should use APInt.
1065 if (Op.getValueType() == MVT::i128)
1066 return;
1067
1068 uint64_t KnownZero2, KnownOne2;
1069
1070 switch (Op.getOpcode()) {
1071 case ISD::Constant:
1072 // We know all of the bits for a constant!
1073 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1074 KnownZero = ~KnownOne & Mask;
1075 return;
1076 case ISD::AND:
1077 // If either the LHS or the RHS are Zero, the result is zero.
1078 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1079 Mask &= ~KnownZero;
1080 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1081 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1082 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1083
1084 // Output known-1 bits are only known if set in both the LHS & RHS.
1085 KnownOne &= KnownOne2;
1086 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1087 KnownZero |= KnownZero2;
1088 return;
1089 case ISD::OR:
1090 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1091 Mask &= ~KnownOne;
1092 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1093 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1094 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1095
1096 // Output known-0 bits are only known if clear in both the LHS & RHS.
1097 KnownZero &= KnownZero2;
1098 // Output known-1 are known to be set if set in either the LHS | RHS.
1099 KnownOne |= KnownOne2;
1100 return;
1101 case ISD::XOR: {
1102 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1103 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1104 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1105 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1106
1107 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1108 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1109 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1110 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1111 KnownZero = KnownZeroOut;
1112 return;
1113 }
1114 case ISD::SELECT:
1115 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1116 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1117 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1118 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1119
1120 // Only known if known in both the LHS and RHS.
1121 KnownOne &= KnownOne2;
1122 KnownZero &= KnownZero2;
1123 return;
1124 case ISD::SELECT_CC:
1125 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1126 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1127 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1128 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1129
1130 // Only known if known in both the LHS and RHS.
1131 KnownOne &= KnownOne2;
1132 KnownZero &= KnownZero2;
1133 return;
1134 case ISD::SETCC:
1135 // If we know the result of a setcc has the top bits zero, use this info.
1136 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1137 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1138 return;
1139 case ISD::SHL:
1140 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1141 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1142 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1143 KnownZero, KnownOne, Depth+1);
1144 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1145 KnownZero <<= SA->getValue();
1146 KnownOne <<= SA->getValue();
1147 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1148 }
1149 return;
1150 case ISD::SRL:
1151 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1152 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1153 MVT::ValueType VT = Op.getValueType();
1154 unsigned ShAmt = SA->getValue();
1155
1156 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1157 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1158 KnownZero, KnownOne, Depth+1);
1159 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1160 KnownZero &= TypeMask;
1161 KnownOne &= TypeMask;
1162 KnownZero >>= ShAmt;
1163 KnownOne >>= ShAmt;
1164
1165 uint64_t HighBits = (1ULL << ShAmt)-1;
1166 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1167 KnownZero |= HighBits; // High bits known zero.
1168 }
1169 return;
1170 case ISD::SRA:
1171 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1172 MVT::ValueType VT = Op.getValueType();
1173 unsigned ShAmt = SA->getValue();
1174
1175 // Compute the new bits that are at the top now.
1176 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1177
1178 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1179 // If any of the demanded bits are produced by the sign extension, we also
1180 // demand the input sign bit.
1181 uint64_t HighBits = (1ULL << ShAmt)-1;
1182 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1183 if (HighBits & Mask)
1184 InDemandedMask |= MVT::getIntVTSignBit(VT);
1185
1186 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1187 Depth+1);
1188 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1189 KnownZero &= TypeMask;
1190 KnownOne &= TypeMask;
1191 KnownZero >>= ShAmt;
1192 KnownOne >>= ShAmt;
1193
1194 // Handle the sign bits.
1195 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1196 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1197
1198 if (KnownZero & SignBit) {
1199 KnownZero |= HighBits; // New bits are known zero.
1200 } else if (KnownOne & SignBit) {
1201 KnownOne |= HighBits; // New bits are known one.
1202 }
1203 }
1204 return;
1205 case ISD::SIGN_EXTEND_INREG: {
1206 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1207
1208 // Sign extension. Compute the demanded bits in the result that are not
1209 // present in the input.
1210 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1211
1212 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1213 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1214
1215 // If the sign extended bits are demanded, we know that the sign
1216 // bit is demanded.
1217 if (NewBits)
1218 InputDemandedBits |= InSignBit;
1219
1220 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1221 KnownZero, KnownOne, Depth+1);
1222 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1223
1224 // If the sign bit of the input is known set or clear, then we know the
1225 // top bits of the result.
1226 if (KnownZero & InSignBit) { // Input sign bit known clear
1227 KnownZero |= NewBits;
1228 KnownOne &= ~NewBits;
1229 } else if (KnownOne & InSignBit) { // Input sign bit known set
1230 KnownOne |= NewBits;
1231 KnownZero &= ~NewBits;
1232 } else { // Input sign bit unknown
1233 KnownZero &= ~NewBits;
1234 KnownOne &= ~NewBits;
1235 }
1236 return;
1237 }
1238 case ISD::CTTZ:
1239 case ISD::CTLZ:
1240 case ISD::CTPOP: {
1241 MVT::ValueType VT = Op.getValueType();
1242 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1243 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1244 KnownOne = 0;
1245 return;
1246 }
1247 case ISD::LOAD: {
1248 if (ISD::isZEXTLoad(Op.Val)) {
1249 LoadSDNode *LD = cast<LoadSDNode>(Op);
1250 MVT::ValueType VT = LD->getLoadedVT();
1251 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1252 }
1253 return;
1254 }
1255 case ISD::ZERO_EXTEND: {
1256 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1257 uint64_t NewBits = (~InMask) & Mask;
1258 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1259 KnownOne, Depth+1);
1260 KnownZero |= NewBits & Mask;
1261 KnownOne &= ~NewBits;
1262 return;
1263 }
1264 case ISD::SIGN_EXTEND: {
1265 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1266 unsigned InBits = MVT::getSizeInBits(InVT);
1267 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1268 uint64_t InSignBit = 1ULL << (InBits-1);
1269 uint64_t NewBits = (~InMask) & Mask;
1270 uint64_t InDemandedBits = Mask & InMask;
1271
1272 // If any of the sign extended bits are demanded, we know that the sign
1273 // bit is demanded.
1274 if (NewBits & Mask)
1275 InDemandedBits |= InSignBit;
1276
1277 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1278 KnownOne, Depth+1);
1279 // If the sign bit is known zero or one, the top bits match.
1280 if (KnownZero & InSignBit) {
1281 KnownZero |= NewBits;
1282 KnownOne &= ~NewBits;
1283 } else if (KnownOne & InSignBit) {
1284 KnownOne |= NewBits;
1285 KnownZero &= ~NewBits;
1286 } else { // Otherwise, top bits aren't known.
1287 KnownOne &= ~NewBits;
1288 KnownZero &= ~NewBits;
1289 }
1290 return;
1291 }
1292 case ISD::ANY_EXTEND: {
1293 MVT::ValueType VT = Op.getOperand(0).getValueType();
1294 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1295 KnownZero, KnownOne, Depth+1);
1296 return;
1297 }
1298 case ISD::TRUNCATE: {
1299 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1300 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1301 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1302 KnownZero &= OutMask;
1303 KnownOne &= OutMask;
1304 break;
1305 }
1306 case ISD::AssertZext: {
1307 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1308 uint64_t InMask = MVT::getIntVTBitMask(VT);
1309 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1310 KnownOne, Depth+1);
1311 KnownZero |= (~InMask) & Mask;
1312 return;
1313 }
1314 case ISD::ADD: {
1315 // If either the LHS or the RHS are Zero, the result is zero.
1316 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1317 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1318 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1319 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1320
1321 // Output known-0 bits are known if clear or set in both the low clear bits
1322 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1323 // low 3 bits clear.
1324 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1325 CountTrailingZeros_64(~KnownZero2));
1326
1327 KnownZero = (1ULL << KnownZeroOut) - 1;
1328 KnownOne = 0;
1329 return;
1330 }
1331 case ISD::SUB: {
1332 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1333 if (!CLHS) return;
1334
1335 // We know that the top bits of C-X are clear if X contains less bits
1336 // than C (i.e. no wrap-around can happen). For example, 20-X is
1337 // positive if we can prove that X is >= 0 and < 16.
1338 MVT::ValueType VT = CLHS->getValueType(0);
1339 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1340 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1341 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1342 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1343 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1344
1345 // If all of the MaskV bits are known to be zero, then we know the output
1346 // top bits are zero, because we now know that the output is from [0-C].
1347 if ((KnownZero & MaskV) == MaskV) {
1348 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1349 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1350 KnownOne = 0; // No one bits known.
1351 } else {
1352 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1353 }
1354 }
1355 return;
1356 }
1357 default:
1358 // Allow the target to implement this method for its nodes.
1359 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1360 case ISD::INTRINSIC_WO_CHAIN:
1361 case ISD::INTRINSIC_W_CHAIN:
1362 case ISD::INTRINSIC_VOID:
1363 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1364 }
1365 return;
1366 }
1367}
1368
1369/// ComputeNumSignBits - Return the number of times the sign bit of the
1370/// register is replicated into the other bits. We know that at least 1 bit
1371/// is always equal to the sign bit (itself), but other cases can give us
1372/// information. For example, immediately after an "SRA X, 2", we know that
1373/// the top 3 bits are all equal to each other, so we return 3.
1374unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1375 MVT::ValueType VT = Op.getValueType();
1376 assert(MVT::isInteger(VT) && "Invalid VT!");
1377 unsigned VTBits = MVT::getSizeInBits(VT);
1378 unsigned Tmp, Tmp2;
1379
1380 if (Depth == 6)
1381 return 1; // Limit search depth.
1382
1383 switch (Op.getOpcode()) {
1384 default: break;
1385 case ISD::AssertSext:
1386 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1387 return VTBits-Tmp+1;
1388 case ISD::AssertZext:
1389 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1390 return VTBits-Tmp;
1391
1392 case ISD::Constant: {
1393 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1394 // If negative, invert the bits, then look at it.
1395 if (Val & MVT::getIntVTSignBit(VT))
1396 Val = ~Val;
1397
1398 // Shift the bits so they are the leading bits in the int64_t.
1399 Val <<= 64-VTBits;
1400
1401 // Return # leading zeros. We use 'min' here in case Val was zero before
1402 // shifting. We don't want to return '64' as for an i32 "0".
1403 return std::min(VTBits, CountLeadingZeros_64(Val));
1404 }
1405
1406 case ISD::SIGN_EXTEND:
1407 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1408 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1409
1410 case ISD::SIGN_EXTEND_INREG:
1411 // Max of the input and what this extends.
1412 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1413 Tmp = VTBits-Tmp+1;
1414
1415 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1416 return std::max(Tmp, Tmp2);
1417
1418 case ISD::SRA:
1419 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1420 // SRA X, C -> adds C sign bits.
1421 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1422 Tmp += C->getValue();
1423 if (Tmp > VTBits) Tmp = VTBits;
1424 }
1425 return Tmp;
1426 case ISD::SHL:
1427 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1428 // shl destroys sign bits.
1429 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1430 if (C->getValue() >= VTBits || // Bad shift.
1431 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1432 return Tmp - C->getValue();
1433 }
1434 break;
1435 case ISD::AND:
1436 case ISD::OR:
1437 case ISD::XOR: // NOT is handled here.
1438 // Logical binary ops preserve the number of sign bits.
1439 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1440 if (Tmp == 1) return 1; // Early out.
1441 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1442 return std::min(Tmp, Tmp2);
1443
1444 case ISD::SELECT:
1445 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1446 if (Tmp == 1) return 1; // Early out.
1447 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1448 return std::min(Tmp, Tmp2);
1449
1450 case ISD::SETCC:
1451 // If setcc returns 0/-1, all bits are sign bits.
1452 if (TLI.getSetCCResultContents() ==
1453 TargetLowering::ZeroOrNegativeOneSetCCResult)
1454 return VTBits;
1455 break;
1456 case ISD::ROTL:
1457 case ISD::ROTR:
1458 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1459 unsigned RotAmt = C->getValue() & (VTBits-1);
1460
1461 // Handle rotate right by N like a rotate left by 32-N.
1462 if (Op.getOpcode() == ISD::ROTR)
1463 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1464
1465 // If we aren't rotating out all of the known-in sign bits, return the
1466 // number that are left. This handles rotl(sext(x), 1) for example.
1467 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1468 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1469 }
1470 break;
1471 case ISD::ADD:
1472 // Add can have at most one carry bit. Thus we know that the output
1473 // is, at worst, one more bit than the inputs.
1474 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1475 if (Tmp == 1) return 1; // Early out.
1476
1477 // Special case decrementing a value (ADD X, -1):
1478 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1479 if (CRHS->isAllOnesValue()) {
1480 uint64_t KnownZero, KnownOne;
1481 uint64_t Mask = MVT::getIntVTBitMask(VT);
1482 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1483
1484 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1485 // sign bits set.
1486 if ((KnownZero|1) == Mask)
1487 return VTBits;
1488
1489 // If we are subtracting one from a positive number, there is no carry
1490 // out of the result.
1491 if (KnownZero & MVT::getIntVTSignBit(VT))
1492 return Tmp;
1493 }
1494
1495 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1496 if (Tmp2 == 1) return 1;
1497 return std::min(Tmp, Tmp2)-1;
1498 break;
1499
1500 case ISD::SUB:
1501 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1502 if (Tmp2 == 1) return 1;
1503
1504 // Handle NEG.
1505 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1506 if (CLHS->getValue() == 0) {
1507 uint64_t KnownZero, KnownOne;
1508 uint64_t Mask = MVT::getIntVTBitMask(VT);
1509 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1510 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1511 // sign bits set.
1512 if ((KnownZero|1) == Mask)
1513 return VTBits;
1514
1515 // If the input is known to be positive (the sign bit is known clear),
1516 // the output of the NEG has the same number of sign bits as the input.
1517 if (KnownZero & MVT::getIntVTSignBit(VT))
1518 return Tmp2;
1519
1520 // Otherwise, we treat this like a SUB.
1521 }
1522
1523 // Sub can have at most one carry bit. Thus we know that the output
1524 // is, at worst, one more bit than the inputs.
1525 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1526 if (Tmp == 1) return 1; // Early out.
1527 return std::min(Tmp, Tmp2)-1;
1528 break;
1529 case ISD::TRUNCATE:
1530 // FIXME: it's tricky to do anything useful for this, but it is an important
1531 // case for targets like X86.
1532 break;
1533 }
1534
1535 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1536 if (Op.getOpcode() == ISD::LOAD) {
1537 LoadSDNode *LD = cast<LoadSDNode>(Op);
1538 unsigned ExtType = LD->getExtensionType();
1539 switch (ExtType) {
1540 default: break;
1541 case ISD::SEXTLOAD: // '17' bits known
1542 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1543 return VTBits-Tmp+1;
1544 case ISD::ZEXTLOAD: // '16' bits known
1545 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1546 return VTBits-Tmp;
1547 }
1548 }
1549
1550 // Allow the target to implement this method for its nodes.
1551 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1552 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1553 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1554 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1555 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1556 if (NumBits > 1) return NumBits;
1557 }
1558
1559 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1560 // use this information.
1561 uint64_t KnownZero, KnownOne;
1562 uint64_t Mask = MVT::getIntVTBitMask(VT);
1563 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1564
1565 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1566 if (KnownZero & SignBit) { // SignBit is 0
1567 Mask = KnownZero;
1568 } else if (KnownOne & SignBit) { // SignBit is 1;
1569 Mask = KnownOne;
1570 } else {
1571 // Nothing known.
1572 return 1;
1573 }
1574
1575 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1576 // the number of identical bits in the top of the input value.
1577 Mask ^= ~0ULL;
1578 Mask <<= 64-VTBits;
1579 // Return # leading zeros. We use 'min' here in case Val was zero before
1580 // shifting. We don't want to return '64' as for an i32 "0".
1581 return std::min(VTBits, CountLeadingZeros_64(Mask));
1582}
1583
Chris Lattner51dabfb2006-10-14 00:41:01 +00001584
Chris Lattnerc3aae252005-01-07 07:46:32 +00001585/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001586///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001587SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001588 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001589 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001590 void *IP = 0;
1591 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1592 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001593 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001594 CSEMap.InsertNode(N, IP);
1595
1596 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001597 return SDOperand(N, 0);
1598}
1599
Chris Lattnerc3aae252005-01-07 07:46:32 +00001600SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1601 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001602 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001603 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001604 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1605 uint64_t Val = C->getValue();
1606 switch (Opcode) {
1607 default: break;
1608 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001609 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001610 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1611 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001612 case ISD::UINT_TO_FP:
1613 case ISD::SINT_TO_FP: {
1614 const uint64_t zero[] = {0, 0};
1615 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001616 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001617 MVT::getSizeInBits(Operand.getValueType()),
1618 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001619 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001620 return getConstantFP(apf, VT);
1621 }
Chris Lattner94683772005-12-23 05:30:37 +00001622 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001623 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001624 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001625 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001626 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001627 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001628 case ISD::BSWAP:
1629 switch(VT) {
1630 default: assert(0 && "Invalid bswap!"); break;
1631 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1632 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1633 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1634 }
1635 break;
1636 case ISD::CTPOP:
1637 switch(VT) {
1638 default: assert(0 && "Invalid ctpop!"); break;
1639 case MVT::i1: return getConstant(Val != 0, VT);
1640 case MVT::i8:
1641 Tmp1 = (unsigned)Val & 0xFF;
1642 return getConstant(CountPopulation_32(Tmp1), VT);
1643 case MVT::i16:
1644 Tmp1 = (unsigned)Val & 0xFFFF;
1645 return getConstant(CountPopulation_32(Tmp1), VT);
1646 case MVT::i32:
1647 return getConstant(CountPopulation_32((unsigned)Val), VT);
1648 case MVT::i64:
1649 return getConstant(CountPopulation_64(Val), VT);
1650 }
1651 case ISD::CTLZ:
1652 switch(VT) {
1653 default: assert(0 && "Invalid ctlz!"); break;
1654 case MVT::i1: return getConstant(Val == 0, VT);
1655 case MVT::i8:
1656 Tmp1 = (unsigned)Val & 0xFF;
1657 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1658 case MVT::i16:
1659 Tmp1 = (unsigned)Val & 0xFFFF;
1660 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1661 case MVT::i32:
1662 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1663 case MVT::i64:
1664 return getConstant(CountLeadingZeros_64(Val), VT);
1665 }
1666 case ISD::CTTZ:
1667 switch(VT) {
1668 default: assert(0 && "Invalid cttz!"); break;
1669 case MVT::i1: return getConstant(Val == 0, VT);
1670 case MVT::i8:
1671 Tmp1 = (unsigned)Val | 0x100;
1672 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1673 case MVT::i16:
1674 Tmp1 = (unsigned)Val | 0x10000;
1675 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1676 case MVT::i32:
1677 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1678 case MVT::i64:
1679 return getConstant(CountTrailingZeros_64(Val), VT);
1680 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001681 }
1682 }
1683
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001684 // Constant fold unary operations with a floating point constant operand.
1685 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1686 APFloat V = C->getValueAPF(); // make copy
Chris Lattnerc3aae252005-01-07 07:46:32 +00001687 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001688 case ISD::FNEG:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001689 V.changeSign();
1690 return getConstantFP(V, VT);
Chris Lattner94683772005-12-23 05:30:37 +00001691 case ISD::FABS:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001692 V.clearSign();
1693 return getConstantFP(V, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001694 case ISD::FP_ROUND:
1695 case ISD::FP_EXTEND:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001696 // This can return overflow, underflow, or inexact; we don't care.
1697 // FIXME need to be more flexible about rounding mode.
1698 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
Dale Johannesen73328d12007-09-19 23:55:34 +00001699 VT==MVT::f64 ? APFloat::IEEEdouble :
1700 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1701 VT==MVT::f128 ? APFloat::IEEEquad :
1702 APFloat::Bogus,
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001703 APFloat::rmNearestTiesToEven);
1704 return getConstantFP(V, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001705 case ISD::FP_TO_SINT:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001706 case ISD::FP_TO_UINT: {
1707 integerPart x;
1708 assert(integerPartWidth >= 64);
1709 // FIXME need to be more flexible about rounding mode.
1710 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1711 Opcode==ISD::FP_TO_SINT,
1712 APFloat::rmTowardZero);
1713 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1714 break;
1715 return getConstant(x, VT);
1716 }
Chris Lattner94683772005-12-23 05:30:37 +00001717 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001718 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001719 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001720 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001721 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001722 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001723 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001724 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001725
1726 unsigned OpOpcode = Operand.Val->getOpcode();
1727 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001728 case ISD::TokenFactor:
1729 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001730 case ISD::FP_ROUND:
1731 case ISD::FP_EXTEND:
1732 assert(MVT::isFloatingPoint(VT) &&
1733 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1734 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001735 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001736 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1737 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001738 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001739 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1740 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001741 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1742 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1743 break;
1744 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001745 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1746 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001747 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001748 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1749 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001750 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001751 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001752 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001753 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001754 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1755 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001756 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001757 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1758 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001759 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1760 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1761 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1762 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001763 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001764 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1765 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001766 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001767 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1768 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001769 if (OpOpcode == ISD::TRUNCATE)
1770 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001771 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1772 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001773 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001774 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1775 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001776 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001777 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1778 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001779 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1780 else
1781 return Operand.Val->getOperand(0);
1782 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001783 break;
Chris Lattner35481892005-12-23 00:16:34 +00001784 case ISD::BIT_CONVERT:
1785 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001786 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001787 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001788 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001789 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1790 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001791 if (OpOpcode == ISD::UNDEF)
1792 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001793 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001794 case ISD::SCALAR_TO_VECTOR:
1795 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001796 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001797 "Illegal SCALAR_TO_VECTOR node!");
1798 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001799 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001800 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1801 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001802 Operand.Val->getOperand(0));
1803 if (OpOpcode == ISD::FNEG) // --X -> X
1804 return Operand.Val->getOperand(0);
1805 break;
1806 case ISD::FABS:
1807 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1808 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1809 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001810 }
1811
Chris Lattner43247a12005-08-25 19:12:10 +00001812 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001813 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001814 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001815 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001816 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001817 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001818 void *IP = 0;
1819 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1820 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001821 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001822 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001823 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001824 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001825 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001826 AllNodes.push_back(N);
1827 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001828}
1829
Chris Lattner36019aa2005-04-18 03:48:41 +00001830
1831
Chris Lattnerc3aae252005-01-07 07:46:32 +00001832SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1833 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001834#ifndef NDEBUG
1835 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001836 case ISD::TokenFactor:
1837 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1838 N2.getValueType() == MVT::Other && "Invalid token factor!");
1839 break;
Chris Lattner76365122005-01-16 02:23:22 +00001840 case ISD::AND:
1841 case ISD::OR:
1842 case ISD::XOR:
1843 case ISD::UDIV:
1844 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001845 case ISD::MULHU:
1846 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001847 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1848 // fall through
1849 case ISD::ADD:
1850 case ISD::SUB:
1851 case ISD::MUL:
1852 case ISD::SDIV:
1853 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001854 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1855 // fall through.
1856 case ISD::FADD:
1857 case ISD::FSUB:
1858 case ISD::FMUL:
1859 case ISD::FDIV:
1860 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001861 assert(N1.getValueType() == N2.getValueType() &&
1862 N1.getValueType() == VT && "Binary operator types must match!");
1863 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001864 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1865 assert(N1.getValueType() == VT &&
1866 MVT::isFloatingPoint(N1.getValueType()) &&
1867 MVT::isFloatingPoint(N2.getValueType()) &&
1868 "Invalid FCOPYSIGN!");
1869 break;
Chris Lattner76365122005-01-16 02:23:22 +00001870 case ISD::SHL:
1871 case ISD::SRA:
1872 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001873 case ISD::ROTL:
1874 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001875 assert(VT == N1.getValueType() &&
1876 "Shift operators return type must be the same as their first arg");
1877 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001878 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001879 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001880 case ISD::FP_ROUND_INREG: {
1881 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1882 assert(VT == N1.getValueType() && "Not an inreg round!");
1883 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1884 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001885 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1886 "Not rounding down!");
Chris Lattner15e4b012005-07-10 00:07:11 +00001887 break;
1888 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001889 case ISD::AssertSext:
1890 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001891 case ISD::SIGN_EXTEND_INREG: {
1892 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1893 assert(VT == N1.getValueType() && "Not an inreg extend!");
1894 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1895 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001896 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1897 "Not extending!");
Chris Lattner15e4b012005-07-10 00:07:11 +00001898 }
1899
Chris Lattner76365122005-01-16 02:23:22 +00001900 default: break;
1901 }
1902#endif
1903
Chris Lattnerc3aae252005-01-07 07:46:32 +00001904 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1905 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1906 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001907 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1908 int64_t Val = N1C->getValue();
1909 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1910 Val <<= 64-FromBits;
1911 Val >>= 64-FromBits;
1912 return getConstant(Val, VT);
1913 }
1914
Chris Lattnerc3aae252005-01-07 07:46:32 +00001915 if (N2C) {
1916 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1917 switch (Opcode) {
1918 case ISD::ADD: return getConstant(C1 + C2, VT);
1919 case ISD::SUB: return getConstant(C1 - C2, VT);
1920 case ISD::MUL: return getConstant(C1 * C2, VT);
1921 case ISD::UDIV:
1922 if (C2) return getConstant(C1 / C2, VT);
1923 break;
1924 case ISD::UREM :
1925 if (C2) return getConstant(C1 % C2, VT);
1926 break;
1927 case ISD::SDIV :
1928 if (C2) return getConstant(N1C->getSignExtended() /
1929 N2C->getSignExtended(), VT);
1930 break;
1931 case ISD::SREM :
1932 if (C2) return getConstant(N1C->getSignExtended() %
1933 N2C->getSignExtended(), VT);
1934 break;
1935 case ISD::AND : return getConstant(C1 & C2, VT);
1936 case ISD::OR : return getConstant(C1 | C2, VT);
1937 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001938 case ISD::SHL : return getConstant(C1 << C2, VT);
1939 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001940 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001941 case ISD::ROTL :
1942 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1943 VT);
1944 case ISD::ROTR :
1945 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1946 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001947 default: break;
1948 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001949 } else { // Cannonicalize constant to RHS if commutative
1950 if (isCommutativeBinOp(Opcode)) {
1951 std::swap(N1C, N2C);
1952 std::swap(N1, N2);
1953 }
1954 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001955 }
1956
1957 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1958 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001959 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001960 if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001961 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1962 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001963 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001964 case ISD::FADD:
1965 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1966 if (s!=APFloat::opInvalidOp)
1967 return getConstantFP(V1, VT);
1968 break;
1969 case ISD::FSUB:
1970 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1971 if (s!=APFloat::opInvalidOp)
1972 return getConstantFP(V1, VT);
1973 break;
1974 case ISD::FMUL:
1975 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1976 if (s!=APFloat::opInvalidOp)
1977 return getConstantFP(V1, VT);
1978 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001979 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001980 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1981 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1982 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001983 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001984 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001985 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1986 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1987 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001988 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001989 case ISD::FCOPYSIGN:
1990 V1.copySign(V2);
1991 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001992 default: break;
1993 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001994 } else { // Cannonicalize constant to RHS if commutative
1995 if (isCommutativeBinOp(Opcode)) {
1996 std::swap(N1CFP, N2CFP);
1997 std::swap(N1, N2);
1998 }
1999 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002000 }
Chris Lattner62b57722006-04-20 05:39:12 +00002001
2002 // Canonicalize an UNDEF to the RHS, even over a constant.
2003 if (N1.getOpcode() == ISD::UNDEF) {
2004 if (isCommutativeBinOp(Opcode)) {
2005 std::swap(N1, N2);
2006 } else {
2007 switch (Opcode) {
2008 case ISD::FP_ROUND_INREG:
2009 case ISD::SIGN_EXTEND_INREG:
2010 case ISD::SUB:
2011 case ISD::FSUB:
2012 case ISD::FDIV:
2013 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002014 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002015 return N1; // fold op(undef, arg2) -> undef
2016 case ISD::UDIV:
2017 case ISD::SDIV:
2018 case ISD::UREM:
2019 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002020 case ISD::SRL:
2021 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002022 if (!MVT::isVector(VT))
2023 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2024 // For vectors, we can't easily build an all zero vector, just return
2025 // the LHS.
2026 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002027 }
2028 }
2029 }
2030
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002031 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002032 if (N2.getOpcode() == ISD::UNDEF) {
2033 switch (Opcode) {
2034 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002035 case ISD::ADDC:
2036 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002037 case ISD::SUB:
2038 case ISD::FADD:
2039 case ISD::FSUB:
2040 case ISD::FMUL:
2041 case ISD::FDIV:
2042 case ISD::FREM:
2043 case ISD::UDIV:
2044 case ISD::SDIV:
2045 case ISD::UREM:
2046 case ISD::SREM:
2047 case ISD::XOR:
2048 return N2; // fold op(arg1, undef) -> undef
2049 case ISD::MUL:
2050 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002051 case ISD::SRL:
2052 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002053 if (!MVT::isVector(VT))
2054 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2055 // For vectors, we can't easily build an all zero vector, just return
2056 // the LHS.
2057 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002058 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002059 if (!MVT::isVector(VT))
2060 return getConstant(MVT::getIntVTBitMask(VT), VT);
2061 // For vectors, we can't easily build an all one vector, just return
2062 // the LHS.
2063 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002064 case ISD::SRA:
2065 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002066 }
2067 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002068
Chris Lattner51dabfb2006-10-14 00:41:01 +00002069 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002070 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00002071 case ISD::TokenFactor:
2072 // Fold trivial token factors.
2073 if (N1.getOpcode() == ISD::EntryToken) return N2;
2074 if (N2.getOpcode() == ISD::EntryToken) return N1;
2075 break;
2076
Chris Lattner51dabfb2006-10-14 00:41:01 +00002077 case ISD::AND:
2078 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2079 // worth handling here.
2080 if (N2C && N2C->getValue() == 0)
2081 return N2;
2082 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00002083 case ISD::OR:
2084 case ISD::XOR:
2085 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2086 // worth handling here.
2087 if (N2C && N2C->getValue() == 0)
2088 return N1;
2089 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002090 case ISD::FP_ROUND_INREG:
2091 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2092 break;
2093 case ISD::SIGN_EXTEND_INREG: {
2094 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2095 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002096 break;
2097 }
Dan Gohman7f321562007-06-25 16:23:39 +00002098 case ISD::EXTRACT_VECTOR_ELT:
2099 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2100
Dan Gohman743d3a72007-07-10 18:20:44 +00002101 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002102 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002103 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2104 N1.getNumOperands() > 0) {
2105 unsigned Factor =
2106 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2107 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2108 N1.getOperand(N2C->getValue() / Factor),
2109 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002110 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002111
Dan Gohman7f321562007-06-25 16:23:39 +00002112 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2113 // expanding large vector constants.
2114 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2115 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002116
2117 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2118 // operations are lowered to scalars.
2119 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2120 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2121 if (IEC == N2C)
2122 return N1.getOperand(1);
2123 else
2124 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2125 }
Dan Gohman7f321562007-06-25 16:23:39 +00002126 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002127 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002128 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2129
Chris Lattner5c6621c2006-09-19 04:51:23 +00002130 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2131 // 64-bit integers into 32-bit parts. Instead of building the extract of
2132 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002133 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002134 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002135
2136 // EXTRACT_ELEMENT of a constant int is also very common.
2137 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2138 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2139 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002140 }
2141 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002142
Nate Begemaneea805e2005-04-13 21:23:31 +00002143 // FIXME: figure out how to safely handle things like
2144 // int foo(int x) { return 1 << (x & 255); }
2145 // int bar() { return foo(256); }
2146#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002147 case ISD::SHL:
2148 case ISD::SRL:
2149 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002150 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002151 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002152 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002153 else if (N2.getOpcode() == ISD::AND)
2154 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2155 // If the and is only masking out bits that cannot effect the shift,
2156 // eliminate the and.
2157 unsigned NumBits = MVT::getSizeInBits(VT);
2158 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2159 return getNode(Opcode, VT, N1, N2.getOperand(0));
2160 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002161 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002162#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002163 }
2164
Chris Lattner27e9b412005-05-11 18:57:39 +00002165 // Memoize this node if possible.
2166 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002167 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002168 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002169 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002170 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002171 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002172 void *IP = 0;
2173 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2174 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002175 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002176 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002177 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002178 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002179 }
2180
Chris Lattnerc3aae252005-01-07 07:46:32 +00002181 AllNodes.push_back(N);
2182 return SDOperand(N, 0);
2183}
2184
Chris Lattnerc3aae252005-01-07 07:46:32 +00002185SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2186 SDOperand N1, SDOperand N2, SDOperand N3) {
2187 // Perform various simplifications.
2188 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2189 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002190 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002191 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002192 // Use FoldSetCC to simplify SETCC's.
2193 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002194 if (Simp.Val) return Simp;
2195 break;
2196 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002197 case ISD::SELECT:
2198 if (N1C)
2199 if (N1C->getValue())
2200 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002201 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002202 return N3; // select false, X, Y -> Y
2203
2204 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002205 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002206 case ISD::BRCOND:
2207 if (N2C)
2208 if (N2C->getValue()) // Unconditional branch
2209 return getNode(ISD::BR, MVT::Other, N1, N3);
2210 else
2211 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002212 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002213 case ISD::VECTOR_SHUFFLE:
2214 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2215 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2216 N3.getOpcode() == ISD::BUILD_VECTOR &&
2217 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2218 "Illegal VECTOR_SHUFFLE node!");
2219 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002220 case ISD::BIT_CONVERT:
2221 // Fold bit_convert nodes from a type to themselves.
2222 if (N1.getValueType() == VT)
2223 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002224 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002225 }
2226
Chris Lattner43247a12005-08-25 19:12:10 +00002227 // Memoize node if it doesn't produce a flag.
2228 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002229 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002230 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002231 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002232 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002233 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002234 void *IP = 0;
2235 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2236 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002237 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002238 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002239 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002240 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002241 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002242 AllNodes.push_back(N);
2243 return SDOperand(N, 0);
2244}
2245
2246SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002247 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002248 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002249 SDOperand Ops[] = { N1, N2, N3, N4 };
2250 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002251}
2252
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002253SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2254 SDOperand N1, SDOperand N2, SDOperand N3,
2255 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002256 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2257 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002258}
2259
Evan Cheng7038daf2005-12-10 00:37:58 +00002260SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2261 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002262 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002263 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002264 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2265 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002266 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002267 Ty = MVT::getTypeForValueType(VT);
2268 } else if (SV) {
2269 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2270 assert(PT && "Value for load must be a pointer");
2271 Ty = PT->getElementType();
2272 }
2273 assert(Ty && "Could not get type information for load");
2274 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2275 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002276 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002277 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002278 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002279 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002280 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002281 ID.AddInteger(ISD::UNINDEXED);
2282 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002283 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002284 ID.AddPointer(SV);
2285 ID.AddInteger(SVOffset);
2286 ID.AddInteger(Alignment);
2287 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002288 void *IP = 0;
2289 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2290 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002291 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002292 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2293 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002294 CSEMap.InsertNode(N, IP);
2295 AllNodes.push_back(N);
2296 return SDOperand(N, 0);
2297}
2298
2299SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002300 SDOperand Chain, SDOperand Ptr,
2301 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002302 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002303 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002304 // If they are asking for an extending load from/to the same thing, return a
2305 // normal load.
2306 if (VT == EVT)
2307 ExtType = ISD::NON_EXTLOAD;
2308
2309 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002310 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002311 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002312 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2313 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002314 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2315 "Cannot sign/zero extend a FP/Vector load!");
2316 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2317 "Cannot convert from FP to Int or Int -> FP!");
2318
Dan Gohman575e2f42007-06-04 15:49:41 +00002319 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2320 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002321 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002322 Ty = MVT::getTypeForValueType(VT);
2323 } else if (SV) {
2324 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2325 assert(PT && "Value for load must be a pointer");
2326 Ty = PT->getElementType();
2327 }
2328 assert(Ty && "Could not get type information for load");
2329 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2330 }
Evan Cheng466685d2006-10-09 20:57:25 +00002331 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002332 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002333 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002334 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002335 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002336 ID.AddInteger(ISD::UNINDEXED);
2337 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002338 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002339 ID.AddPointer(SV);
2340 ID.AddInteger(SVOffset);
2341 ID.AddInteger(Alignment);
2342 ID.AddInteger(isVolatile);
2343 void *IP = 0;
2344 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2345 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002346 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002347 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002348 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002349 AllNodes.push_back(N);
2350 return SDOperand(N, 0);
2351}
2352
Evan Cheng144d8f02006-11-09 17:55:04 +00002353SDOperand
2354SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2355 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002356 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002357 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2358 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002359 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002360 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002361 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002362 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002363 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002364 ID.AddInteger(AM);
2365 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002366 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002367 ID.AddPointer(LD->getSrcValue());
2368 ID.AddInteger(LD->getSrcValueOffset());
2369 ID.AddInteger(LD->getAlignment());
2370 ID.AddInteger(LD->isVolatile());
2371 void *IP = 0;
2372 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2373 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002374 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002375 LD->getExtensionType(), LD->getLoadedVT(),
2376 LD->getSrcValue(), LD->getSrcValueOffset(),
2377 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002378 CSEMap.InsertNode(N, IP);
2379 AllNodes.push_back(N);
2380 return SDOperand(N, 0);
2381}
2382
Jeff Cohend41b30d2006-11-05 19:31:28 +00002383SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002384 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002385 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002386 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002387
Dan Gohman575e2f42007-06-04 15:49:41 +00002388 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2389 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002390 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002391 Ty = MVT::getTypeForValueType(VT);
2392 } else if (SV) {
2393 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2394 assert(PT && "Value for store must be a pointer");
2395 Ty = PT->getElementType();
2396 }
2397 assert(Ty && "Could not get type information for store");
2398 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2399 }
Evan Chengad071e12006-10-05 22:57:11 +00002400 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002401 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002402 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002403 FoldingSetNodeID ID;
2404 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002405 ID.AddInteger(ISD::UNINDEXED);
2406 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002407 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002408 ID.AddPointer(SV);
2409 ID.AddInteger(SVOffset);
2410 ID.AddInteger(Alignment);
2411 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002412 void *IP = 0;
2413 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2414 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002415 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002416 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002417 CSEMap.InsertNode(N, IP);
2418 AllNodes.push_back(N);
2419 return SDOperand(N, 0);
2420}
2421
Jeff Cohend41b30d2006-11-05 19:31:28 +00002422SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002423 SDOperand Ptr, const Value *SV,
2424 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002425 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002426 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002427 bool isTrunc = VT != SVT;
2428
Duncan Sandsaf47b112007-10-16 09:56:48 +00002429 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2430 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002431 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2432 "Can't do FP-INT conversion!");
2433
Dan Gohman575e2f42007-06-04 15:49:41 +00002434 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2435 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002436 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002437 Ty = MVT::getTypeForValueType(VT);
2438 } else if (SV) {
2439 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2440 assert(PT && "Value for store must be a pointer");
2441 Ty = PT->getElementType();
2442 }
2443 assert(Ty && "Could not get type information for store");
2444 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2445 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002446 SDVTList VTs = getVTList(MVT::Other);
2447 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002448 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002449 FoldingSetNodeID ID;
2450 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002451 ID.AddInteger(ISD::UNINDEXED);
2452 ID.AddInteger(isTrunc);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002453 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002454 ID.AddPointer(SV);
2455 ID.AddInteger(SVOffset);
2456 ID.AddInteger(Alignment);
2457 ID.AddInteger(isVolatile);
2458 void *IP = 0;
2459 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2460 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002461 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002462 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002463 CSEMap.InsertNode(N, IP);
2464 AllNodes.push_back(N);
2465 return SDOperand(N, 0);
2466}
2467
Evan Cheng144d8f02006-11-09 17:55:04 +00002468SDOperand
2469SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2470 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002471 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2472 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2473 "Store is already a indexed store!");
2474 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2475 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2476 FoldingSetNodeID ID;
2477 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2478 ID.AddInteger(AM);
2479 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002480 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002481 ID.AddPointer(ST->getSrcValue());
2482 ID.AddInteger(ST->getSrcValueOffset());
2483 ID.AddInteger(ST->getAlignment());
2484 ID.AddInteger(ST->isVolatile());
2485 void *IP = 0;
2486 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2487 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002488 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002489 ST->isTruncatingStore(), ST->getStoredVT(),
2490 ST->getSrcValue(), ST->getSrcValueOffset(),
2491 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002492 CSEMap.InsertNode(N, IP);
2493 AllNodes.push_back(N);
2494 return SDOperand(N, 0);
2495}
2496
Nate Begemanacc398c2006-01-25 18:21:52 +00002497SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2498 SDOperand Chain, SDOperand Ptr,
2499 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002500 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002501 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002502}
2503
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002504SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002505 const SDOperand *Ops, unsigned NumOps) {
2506 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002507 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002508 case 1: return getNode(Opcode, VT, Ops[0]);
2509 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2510 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002511 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002512 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002513
Chris Lattneref847df2005-04-09 03:27:28 +00002514 switch (Opcode) {
2515 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002516 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002517 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002518 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2519 "LHS and RHS of condition must have same type!");
2520 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2521 "True and False arms of SelectCC must have same type!");
2522 assert(Ops[2].getValueType() == VT &&
2523 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002524 break;
2525 }
2526 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002527 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002528 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2529 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002530 break;
2531 }
Chris Lattneref847df2005-04-09 03:27:28 +00002532 }
2533
Chris Lattner385328c2005-05-14 07:42:29 +00002534 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002535 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002536 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002537 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002538 FoldingSetNodeID ID;
2539 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002540 void *IP = 0;
2541 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2542 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002543 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002544 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002545 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002546 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002547 }
Chris Lattneref847df2005-04-09 03:27:28 +00002548 AllNodes.push_back(N);
2549 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002550}
2551
Chris Lattner89c34632005-05-14 06:20:26 +00002552SDOperand SelectionDAG::getNode(unsigned Opcode,
2553 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002554 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002555 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2556 Ops, NumOps);
2557}
2558
2559SDOperand SelectionDAG::getNode(unsigned Opcode,
2560 const MVT::ValueType *VTs, unsigned NumVTs,
2561 const SDOperand *Ops, unsigned NumOps) {
2562 if (NumVTs == 1)
2563 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002564 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2565}
2566
2567SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2568 const SDOperand *Ops, unsigned NumOps) {
2569 if (VTList.NumVTs == 1)
2570 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002571
Chris Lattner5f056bf2005-07-10 01:55:33 +00002572 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002573 // FIXME: figure out how to safely handle things like
2574 // int foo(int x) { return 1 << (x & 255); }
2575 // int bar() { return foo(256); }
2576#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002577 case ISD::SRA_PARTS:
2578 case ISD::SRL_PARTS:
2579 case ISD::SHL_PARTS:
2580 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002581 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002582 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2583 else if (N3.getOpcode() == ISD::AND)
2584 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2585 // If the and is only masking out bits that cannot effect the shift,
2586 // eliminate the and.
2587 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2588 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2589 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2590 }
2591 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002592#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002593 }
Chris Lattner89c34632005-05-14 06:20:26 +00002594
Chris Lattner43247a12005-08-25 19:12:10 +00002595 // Memoize the node unless it returns a flag.
2596 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002597 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002598 FoldingSetNodeID ID;
2599 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002600 void *IP = 0;
2601 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2602 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002603 if (NumOps == 1)
2604 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2605 else if (NumOps == 2)
2606 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2607 else if (NumOps == 3)
2608 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2609 else
2610 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002611 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002612 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002613 if (NumOps == 1)
2614 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2615 else if (NumOps == 2)
2616 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2617 else if (NumOps == 3)
2618 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2619 else
2620 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002621 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002622 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002623 return SDOperand(N, 0);
2624}
2625
Dan Gohman08ce9762007-10-08 15:49:58 +00002626SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2627 return getNode(Opcode, VTList, 0, 0);
2628}
2629
2630SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2631 SDOperand N1) {
2632 SDOperand Ops[] = { N1 };
2633 return getNode(Opcode, VTList, Ops, 1);
2634}
2635
2636SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2637 SDOperand N1, SDOperand N2) {
2638 SDOperand Ops[] = { N1, N2 };
2639 return getNode(Opcode, VTList, Ops, 2);
2640}
2641
2642SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2643 SDOperand N1, SDOperand N2, SDOperand N3) {
2644 SDOperand Ops[] = { N1, N2, N3 };
2645 return getNode(Opcode, VTList, Ops, 3);
2646}
2647
2648SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2649 SDOperand N1, SDOperand N2, SDOperand N3,
2650 SDOperand N4) {
2651 SDOperand Ops[] = { N1, N2, N3, N4 };
2652 return getNode(Opcode, VTList, Ops, 4);
2653}
2654
2655SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2656 SDOperand N1, SDOperand N2, SDOperand N3,
2657 SDOperand N4, SDOperand N5) {
2658 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2659 return getNode(Opcode, VTList, Ops, 5);
2660}
2661
Chris Lattner70046e92006-08-15 17:46:01 +00002662SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002663 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002664}
2665
Chris Lattner70046e92006-08-15 17:46:01 +00002666SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002667 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2668 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002669 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002670 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002671 }
2672 std::vector<MVT::ValueType> V;
2673 V.push_back(VT1);
2674 V.push_back(VT2);
2675 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002676 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002677}
Chris Lattner70046e92006-08-15 17:46:01 +00002678SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2679 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002680 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002681 E = VTList.end(); I != E; ++I) {
2682 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2683 (*I)[2] == VT3)
2684 return makeVTList(&(*I)[0], 3);
2685 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002686 std::vector<MVT::ValueType> V;
2687 V.push_back(VT1);
2688 V.push_back(VT2);
2689 V.push_back(VT3);
2690 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002691 return makeVTList(&(*VTList.begin())[0], 3);
2692}
2693
2694SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2695 switch (NumVTs) {
2696 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002697 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002698 case 2: return getVTList(VTs[0], VTs[1]);
2699 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2700 default: break;
2701 }
2702
2703 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2704 E = VTList.end(); I != E; ++I) {
2705 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2706
2707 bool NoMatch = false;
2708 for (unsigned i = 2; i != NumVTs; ++i)
2709 if (VTs[i] != (*I)[i]) {
2710 NoMatch = true;
2711 break;
2712 }
2713 if (!NoMatch)
2714 return makeVTList(&*I->begin(), NumVTs);
2715 }
2716
2717 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2718 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002719}
2720
2721
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002722/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2723/// specified operands. If the resultant node already exists in the DAG,
2724/// this does not modify the specified node, instead it returns the node that
2725/// already exists. If the resultant node does not exist in the DAG, the
2726/// input node is returned. As a degenerate case, if you specify the same
2727/// input operands as the node already has, the input node is returned.
2728SDOperand SelectionDAG::
2729UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2730 SDNode *N = InN.Val;
2731 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2732
2733 // Check to see if there is no change.
2734 if (Op == N->getOperand(0)) return InN;
2735
2736 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002737 void *InsertPos = 0;
2738 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2739 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002740
2741 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002742 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002743 RemoveNodeFromCSEMaps(N);
2744
2745 // Now we update the operands.
2746 N->OperandList[0].Val->removeUser(N);
2747 Op.Val->addUser(N);
2748 N->OperandList[0] = Op;
2749
2750 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002751 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002752 return InN;
2753}
2754
2755SDOperand SelectionDAG::
2756UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2757 SDNode *N = InN.Val;
2758 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2759
2760 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002761 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2762 return InN; // No operands changed, just return the input node.
2763
2764 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002765 void *InsertPos = 0;
2766 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2767 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002768
2769 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002770 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002771 RemoveNodeFromCSEMaps(N);
2772
2773 // Now we update the operands.
2774 if (N->OperandList[0] != Op1) {
2775 N->OperandList[0].Val->removeUser(N);
2776 Op1.Val->addUser(N);
2777 N->OperandList[0] = Op1;
2778 }
2779 if (N->OperandList[1] != Op2) {
2780 N->OperandList[1].Val->removeUser(N);
2781 Op2.Val->addUser(N);
2782 N->OperandList[1] = Op2;
2783 }
2784
2785 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002786 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002787 return InN;
2788}
2789
2790SDOperand SelectionDAG::
2791UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002792 SDOperand Ops[] = { Op1, Op2, Op3 };
2793 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002794}
2795
2796SDOperand SelectionDAG::
2797UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2798 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002799 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2800 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002801}
2802
2803SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002804UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2805 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002806 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2807 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002808}
2809
2810
2811SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002812UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002813 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002814 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002815 "Update with wrong number of operands");
2816
2817 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002818 bool AnyChange = false;
2819 for (unsigned i = 0; i != NumOps; ++i) {
2820 if (Ops[i] != N->getOperand(i)) {
2821 AnyChange = true;
2822 break;
2823 }
2824 }
2825
2826 // No operands changed, just return the input node.
2827 if (!AnyChange) return InN;
2828
2829 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002830 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002831 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002832 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002833
2834 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002835 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002836 RemoveNodeFromCSEMaps(N);
2837
2838 // Now we update the operands.
2839 for (unsigned i = 0; i != NumOps; ++i) {
2840 if (N->OperandList[i] != Ops[i]) {
2841 N->OperandList[i].Val->removeUser(N);
2842 Ops[i].Val->addUser(N);
2843 N->OperandList[i] = Ops[i];
2844 }
2845 }
2846
2847 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002848 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002849 return InN;
2850}
2851
2852
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002853/// MorphNodeTo - This frees the operands of the current node, resets the
2854/// opcode, types, and operands to the specified value. This should only be
2855/// used by the SelectionDAG class.
2856void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2857 const SDOperand *Ops, unsigned NumOps) {
2858 NodeType = Opc;
2859 ValueList = L.VTs;
2860 NumValues = L.NumVTs;
2861
2862 // Clear the operands list, updating used nodes to remove this from their
2863 // use list.
2864 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2865 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002866
2867 // If NumOps is larger than the # of operands we currently have, reallocate
2868 // the operand list.
2869 if (NumOps > NumOperands) {
2870 if (OperandsNeedDelete)
2871 delete [] OperandList;
2872 OperandList = new SDOperand[NumOps];
2873 OperandsNeedDelete = true;
2874 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002875
2876 // Assign the new operands.
2877 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002878
2879 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2880 OperandList[i] = Ops[i];
2881 SDNode *N = OperandList[i].Val;
2882 N->Uses.push_back(this);
2883 }
2884}
Chris Lattner149c58c2005-08-16 18:17:10 +00002885
2886/// SelectNodeTo - These are used for target selectors to *mutate* the
2887/// specified node to have the specified return type, Target opcode, and
2888/// operands. Note that target opcodes are stored as
2889/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002890///
2891/// Note that SelectNodeTo returns the resultant node. If there is already a
2892/// node of the specified opcode and operands, it returns that node instead of
2893/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002894SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2895 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002896 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002897 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002898 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002899 void *IP = 0;
2900 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002901 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002902
Chris Lattner7651fa42005-08-24 23:00:29 +00002903 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002904
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002905 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002906
Chris Lattner4a283e92006-08-11 18:38:11 +00002907 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002908 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002909}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002910
Evan Cheng95514ba2006-08-26 08:00:10 +00002911SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2912 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002913 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002914 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002915 SDOperand Ops[] = { Op1 };
2916
Jim Laskey583bd472006-10-27 23:46:08 +00002917 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002918 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002919 void *IP = 0;
2920 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002921 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002922
Chris Lattner149c58c2005-08-16 18:17:10 +00002923 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002924 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002925 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002926 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002927}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002928
Evan Cheng95514ba2006-08-26 08:00:10 +00002929SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2930 MVT::ValueType VT, SDOperand Op1,
2931 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002932 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002933 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002934 SDOperand Ops[] = { Op1, Op2 };
2935
Jim Laskey583bd472006-10-27 23:46:08 +00002936 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002937 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002938 void *IP = 0;
2939 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002940 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002941
Chris Lattner149c58c2005-08-16 18:17:10 +00002942 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002943
Chris Lattner63e3f142007-02-04 07:28:00 +00002944 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002945
Chris Lattnera5682852006-08-07 23:03:03 +00002946 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002947 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002948}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002949
Evan Cheng95514ba2006-08-26 08:00:10 +00002950SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2951 MVT::ValueType VT, SDOperand Op1,
2952 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002953 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002954 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002955 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002956 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002957 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002958 void *IP = 0;
2959 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002960 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002961
Chris Lattner149c58c2005-08-16 18:17:10 +00002962 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002963
Chris Lattner63e3f142007-02-04 07:28:00 +00002964 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002965
Chris Lattnera5682852006-08-07 23:03:03 +00002966 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002967 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002968}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002969
Evan Cheng95514ba2006-08-26 08:00:10 +00002970SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002971 MVT::ValueType VT, const SDOperand *Ops,
2972 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002973 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002974 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002975 FoldingSetNodeID ID;
2976 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002977 void *IP = 0;
2978 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002979 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002980
Chris Lattner6b09a292005-08-21 18:49:33 +00002981 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002982 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002983
Chris Lattnera5682852006-08-07 23:03:03 +00002984 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002985 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002986}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002987
Evan Cheng95514ba2006-08-26 08:00:10 +00002988SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2989 MVT::ValueType VT1, MVT::ValueType VT2,
2990 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002991 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002992 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002993 SDOperand Ops[] = { Op1, Op2 };
2994 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002995 void *IP = 0;
2996 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002997 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002998
Chris Lattner0fb094f2005-11-19 01:44:53 +00002999 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003000 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003001 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003002 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003003}
3004
Evan Cheng95514ba2006-08-26 08:00:10 +00003005SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3006 MVT::ValueType VT1, MVT::ValueType VT2,
3007 SDOperand Op1, SDOperand Op2,
3008 SDOperand Op3) {
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(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003011 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003012 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003013 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003014 void *IP = 0;
3015 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003016 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003017
Chris Lattner0fb094f2005-11-19 01:44:53 +00003018 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003019
Chris Lattner63e3f142007-02-04 07:28:00 +00003020 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003021 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003022 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003023}
3024
Chris Lattner0fb094f2005-11-19 01:44:53 +00003025
Evan Cheng6ae46c42006-02-09 07:15:23 +00003026/// getTargetNode - These are used for target selectors to create a new node
3027/// with specified return type(s), target opcode, and operands.
3028///
3029/// Note that getTargetNode returns the resultant node. If there is already a
3030/// node of the specified opcode and operands, it returns that node instead of
3031/// the current one.
3032SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3033 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3034}
3035SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3036 SDOperand Op1) {
3037 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3038}
3039SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3040 SDOperand Op1, SDOperand Op2) {
3041 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3042}
3043SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003044 SDOperand Op1, SDOperand Op2,
3045 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003046 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3047}
3048SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003049 const SDOperand *Ops, unsigned NumOps) {
3050 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003051}
3052SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003053 MVT::ValueType VT2) {
3054 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3055 SDOperand Op;
3056 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3057}
3058SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003059 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003060 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003061 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003062}
3063SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003064 MVT::ValueType VT2, SDOperand Op1,
3065 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003066 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003067 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003068 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003069}
3070SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003071 MVT::ValueType VT2, SDOperand Op1,
3072 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003073 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003074 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003075 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003076}
Evan Cheng694481e2006-08-27 08:08:54 +00003077SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3078 MVT::ValueType VT2,
3079 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003080 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003081 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003082}
3083SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3084 MVT::ValueType VT2, MVT::ValueType VT3,
3085 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003086 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003087 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003088 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003089}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003090SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3091 MVT::ValueType VT2, MVT::ValueType VT3,
3092 SDOperand Op1, SDOperand Op2,
3093 SDOperand Op3) {
3094 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3095 SDOperand Ops[] = { Op1, Op2, Op3 };
3096 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3097}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003098SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003099 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003100 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003101 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3102 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003103}
Evan Cheng05e69c12007-09-12 23:39:49 +00003104SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3105 MVT::ValueType VT2, MVT::ValueType VT3,
3106 MVT::ValueType VT4,
3107 const SDOperand *Ops, unsigned NumOps) {
3108 std::vector<MVT::ValueType> VTList;
3109 VTList.push_back(VT1);
3110 VTList.push_back(VT2);
3111 VTList.push_back(VT3);
3112 VTList.push_back(VT4);
3113 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3114 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3115}
Evan Cheng39305cf2007-10-05 01:10:49 +00003116SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3117 std::vector<MVT::ValueType> &ResultTys,
3118 const SDOperand *Ops, unsigned NumOps) {
3119 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3120 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3121 Ops, NumOps).Val;
3122}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003123
Evan Cheng99157a02006-08-07 22:13:29 +00003124/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003125/// This can cause recursive merging of nodes in the DAG.
3126///
Chris Lattner8b52f212005-08-26 18:36:28 +00003127/// This version assumes From/To have a single result value.
3128///
Chris Lattner1e111c72005-09-07 05:37:01 +00003129void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3130 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003131 SDNode *From = FromN.Val, *To = ToN.Val;
3132 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3133 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003134 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003135
Chris Lattner8b8749f2005-08-17 19:00:20 +00003136 while (!From->use_empty()) {
3137 // Process users until they are all gone.
3138 SDNode *U = *From->use_begin();
3139
3140 // This node is about to morph, remove its old self from the CSE maps.
3141 RemoveNodeFromCSEMaps(U);
3142
Chris Lattner65113b22005-11-08 22:07:03 +00003143 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3144 I != E; ++I)
3145 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003146 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003147 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003148 To->addUser(U);
3149 }
3150
3151 // Now that we have modified U, add it back to the CSE maps. If it already
3152 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003153 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3154 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003155 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003156 if (Deleted) Deleted->push_back(U);
3157 DeleteNodeNotInCSEMaps(U);
3158 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003159 }
3160}
3161
3162/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3163/// This can cause recursive merging of nodes in the DAG.
3164///
3165/// This version assumes From/To have matching types and numbers of result
3166/// values.
3167///
Chris Lattner1e111c72005-09-07 05:37:01 +00003168void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3169 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003170 assert(From != To && "Cannot replace uses of with self");
3171 assert(From->getNumValues() == To->getNumValues() &&
3172 "Cannot use this version of ReplaceAllUsesWith!");
3173 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003174 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003175 return;
3176 }
3177
3178 while (!From->use_empty()) {
3179 // Process users until they are all gone.
3180 SDNode *U = *From->use_begin();
3181
3182 // This node is about to morph, remove its old self from the CSE maps.
3183 RemoveNodeFromCSEMaps(U);
3184
Chris Lattner65113b22005-11-08 22:07:03 +00003185 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3186 I != E; ++I)
3187 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003188 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003189 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003190 To->addUser(U);
3191 }
3192
3193 // Now that we have modified U, add it back to the CSE maps. If it already
3194 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003195 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3196 ReplaceAllUsesWith(U, Existing, Deleted);
3197 // U is now dead.
3198 if (Deleted) Deleted->push_back(U);
3199 DeleteNodeNotInCSEMaps(U);
3200 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003201 }
3202}
3203
Chris Lattner8b52f212005-08-26 18:36:28 +00003204/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3205/// This can cause recursive merging of nodes in the DAG.
3206///
3207/// This version can replace From with any result values. To must match the
3208/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003209void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003210 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003211 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003212 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003213 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003214 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003215 return;
3216 }
3217
3218 while (!From->use_empty()) {
3219 // Process users until they are all gone.
3220 SDNode *U = *From->use_begin();
3221
3222 // This node is about to morph, remove its old self from the CSE maps.
3223 RemoveNodeFromCSEMaps(U);
3224
Chris Lattner65113b22005-11-08 22:07:03 +00003225 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3226 I != E; ++I)
3227 if (I->Val == From) {
3228 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003229 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003230 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003231 ToOp.Val->addUser(U);
3232 }
3233
3234 // Now that we have modified U, add it back to the CSE maps. If it already
3235 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003236 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3237 ReplaceAllUsesWith(U, Existing, Deleted);
3238 // U is now dead.
3239 if (Deleted) Deleted->push_back(U);
3240 DeleteNodeNotInCSEMaps(U);
3241 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003242 }
3243}
3244
Chris Lattner012f2412006-02-17 21:58:01 +00003245/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3246/// uses of other values produced by From.Val alone. The Deleted vector is
3247/// handled the same was as for ReplaceAllUsesWith.
3248void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003249 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003250 assert(From != To && "Cannot replace a value with itself");
3251 // Handle the simple, trivial, case efficiently.
3252 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003253 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003254 return;
3255 }
3256
Chris Lattnercf5640b2007-02-04 00:14:31 +00003257 // Get all of the users of From.Val. We want these in a nice,
3258 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3259 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003260
Chris Lattner01d029b2007-10-15 06:10:22 +00003261 std::vector<SDNode*> LocalDeletionVector;
3262
3263 // Pick a deletion vector to use. If the user specified one, use theirs,
3264 // otherwise use a local one.
3265 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003266 while (!Users.empty()) {
3267 // We know that this user uses some value of From. If it is the right
3268 // value, update it.
3269 SDNode *User = Users.back();
3270 Users.pop_back();
3271
Chris Lattner01d029b2007-10-15 06:10:22 +00003272 // Scan for an operand that matches From.
3273 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3274 for (; Op != E; ++Op)
3275 if (*Op == From) break;
3276
3277 // If there are no matches, the user must use some other result of From.
3278 if (Op == E) continue;
3279
3280 // Okay, we know this user needs to be updated. Remove its old self
3281 // from the CSE maps.
3282 RemoveNodeFromCSEMaps(User);
3283
3284 // Update all operands that match "From".
3285 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003286 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003287 From.Val->removeUser(User);
3288 *Op = To;
3289 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003290 }
3291 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003292
3293 // Now that we have modified User, add it back to the CSE maps. If it
3294 // already exists there, recursively merge the results together.
3295 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3296 if (!Existing) continue; // Continue on to next user.
3297
3298 // If there was already an existing matching node, use ReplaceAllUsesWith
3299 // to replace the dead one with the existing one. However, this can cause
3300 // recursive merging of other unrelated nodes down the line. The merging
3301 // can cause deletion of nodes that used the old value. In this case,
3302 // we have to be certain to remove them from the Users set.
3303 unsigned NumDeleted = DeleteVector->size();
3304 ReplaceAllUsesWith(User, Existing, DeleteVector);
3305
3306 // User is now dead.
3307 DeleteVector->push_back(User);
3308 DeleteNodeNotInCSEMaps(User);
3309
3310 // We have to be careful here, because ReplaceAllUsesWith could have
3311 // deleted a user of From, which means there may be dangling pointers
3312 // in the "Users" setvector. Scan over the deleted node pointers and
3313 // remove them from the setvector.
3314 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3315 Users.remove((*DeleteVector)[i]);
3316
3317 // If the user doesn't need the set of deleted elements, don't retain them
3318 // to the next loop iteration.
3319 if (Deleted == 0)
3320 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003321 }
3322}
3323
Chris Lattner7b2880c2005-08-24 22:44:39 +00003324
Evan Chenge6f35d82006-08-01 08:20:41 +00003325/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3326/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003327unsigned SelectionDAG::AssignNodeIds() {
3328 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003329 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3330 SDNode *N = I;
3331 N->setNodeId(Id++);
3332 }
3333 return Id;
3334}
3335
Evan Chenge6f35d82006-08-01 08:20:41 +00003336/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003337/// based on their topological order. It returns the maximum id and a vector
3338/// of the SDNodes* in assigned order by reference.
3339unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003340 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003341 std::vector<unsigned> InDegree(DAGSize);
3342 std::vector<SDNode*> Sources;
3343
3344 // Use a two pass approach to avoid using a std::map which is slow.
3345 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003346 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3347 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003348 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003349 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003350 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003351 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003352 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003353 }
3354
Evan Cheng99157a02006-08-07 22:13:29 +00003355 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003356 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003357 SDNode *N = Sources.back();
3358 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003359 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003360 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3361 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003362 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003363 if (Degree == 0)
3364 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003365 }
3366 }
3367
Evan Chengc384d6c2006-08-02 22:00:34 +00003368 // Second pass, assign the actual topological order as node ids.
3369 Id = 0;
3370 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3371 TI != TE; ++TI)
3372 (*TI)->setNodeId(Id++);
3373
3374 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003375}
3376
3377
Evan Cheng091cba12006-07-27 06:39:06 +00003378
Jim Laskey58b968b2005-08-17 20:08:02 +00003379//===----------------------------------------------------------------------===//
3380// SDNode Class
3381//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003382
Chris Lattner917d2c92006-07-19 00:00:37 +00003383// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003384void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003385void UnarySDNode::ANCHOR() {}
3386void BinarySDNode::ANCHOR() {}
3387void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003388void HandleSDNode::ANCHOR() {}
3389void StringSDNode::ANCHOR() {}
3390void ConstantSDNode::ANCHOR() {}
3391void ConstantFPSDNode::ANCHOR() {}
3392void GlobalAddressSDNode::ANCHOR() {}
3393void FrameIndexSDNode::ANCHOR() {}
3394void JumpTableSDNode::ANCHOR() {}
3395void ConstantPoolSDNode::ANCHOR() {}
3396void BasicBlockSDNode::ANCHOR() {}
3397void SrcValueSDNode::ANCHOR() {}
3398void RegisterSDNode::ANCHOR() {}
3399void ExternalSymbolSDNode::ANCHOR() {}
3400void CondCodeSDNode::ANCHOR() {}
3401void VTSDNode::ANCHOR() {}
3402void LoadSDNode::ANCHOR() {}
3403void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003404
Chris Lattner48b85922007-02-04 02:41:42 +00003405HandleSDNode::~HandleSDNode() {
3406 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003407 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003408}
3409
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003410GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3411 MVT::ValueType VT, int o)
3412 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003413 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003414 // Thread Local
3415 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3416 // Non Thread Local
3417 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3418 getSDVTList(VT)), Offset(o) {
3419 TheGlobal = const_cast<GlobalValue*>(GA);
3420}
Chris Lattner48b85922007-02-04 02:41:42 +00003421
Jim Laskey583bd472006-10-27 23:46:08 +00003422/// Profile - Gather unique data for the node.
3423///
3424void SDNode::Profile(FoldingSetNodeID &ID) {
3425 AddNodeIDNode(ID, this);
3426}
3427
Chris Lattnera3255112005-11-08 23:30:28 +00003428/// getValueTypeList - Return a pointer to the specified value type.
3429///
3430MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003431 if (MVT::isExtendedVT(VT)) {
3432 static std::set<MVT::ValueType> EVTs;
3433 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3434 } else {
3435 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3436 VTs[VT] = VT;
3437 return &VTs[VT];
3438 }
Chris Lattnera3255112005-11-08 23:30:28 +00003439}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003440
Chris Lattner5c884562005-01-12 18:37:47 +00003441/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3442/// indicated value. This method ignores uses of other values defined by this
3443/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003444bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003445 assert(Value < getNumValues() && "Bad value!");
3446
3447 // If there is only one value, this is easy.
3448 if (getNumValues() == 1)
3449 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003450 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003451
Evan Cheng4ee62112006-02-05 06:29:23 +00003452 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003453
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003454 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003455
Chris Lattnerf83482d2006-08-16 20:59:32 +00003456 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003457 SDNode *User = *UI;
3458 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003459 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003460 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3461 if (User->getOperand(i) == TheValue) {
3462 if (NUses == 0)
3463 return false; // too many uses
3464 --NUses;
3465 }
3466 }
3467
3468 // Found exactly the right number of uses?
3469 return NUses == 0;
3470}
3471
3472
Evan Cheng33d55952007-08-02 05:29:38 +00003473/// hasAnyUseOfValue - Return true if there are any use of the indicated
3474/// value. This method ignores uses of other values defined by this operation.
3475bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3476 assert(Value < getNumValues() && "Bad value!");
3477
3478 if (use_size() == 0) return false;
3479
3480 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3481
3482 SmallPtrSet<SDNode*, 32> UsersHandled;
3483
3484 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3485 SDNode *User = *UI;
3486 if (User->getNumOperands() == 1 ||
3487 UsersHandled.insert(User)) // First time we've seen this?
3488 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3489 if (User->getOperand(i) == TheValue) {
3490 return true;
3491 }
3492 }
3493
3494 return false;
3495}
3496
3497
Evan Chenge6e97e62006-11-03 07:31:32 +00003498/// isOnlyUse - Return true if this node is the only use of N.
3499///
Evan Cheng4ee62112006-02-05 06:29:23 +00003500bool SDNode::isOnlyUse(SDNode *N) const {
3501 bool Seen = false;
3502 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3503 SDNode *User = *I;
3504 if (User == this)
3505 Seen = true;
3506 else
3507 return false;
3508 }
3509
3510 return Seen;
3511}
3512
Evan Chenge6e97e62006-11-03 07:31:32 +00003513/// isOperand - Return true if this node is an operand of N.
3514///
Evan Chengbfa284f2006-03-03 06:42:32 +00003515bool SDOperand::isOperand(SDNode *N) const {
3516 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3517 if (*this == N->getOperand(i))
3518 return true;
3519 return false;
3520}
3521
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003522bool SDNode::isOperand(SDNode *N) const {
3523 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3524 if (this == N->OperandList[i].Val)
3525 return true;
3526 return false;
3527}
Evan Cheng4ee62112006-02-05 06:29:23 +00003528
Evan Chengc5fc57d2006-11-03 03:05:24 +00003529static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003530 SmallPtrSet<SDNode *, 32> &Visited) {
3531 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003532 return;
3533
3534 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3535 SDNode *Op = N->getOperand(i).Val;
3536 if (Op == P) {
3537 found = true;
3538 return;
3539 }
3540 findPredecessor(Op, P, found, Visited);
3541 }
3542}
3543
Evan Chenge6e97e62006-11-03 07:31:32 +00003544/// isPredecessor - Return true if this node is a predecessor of N. This node
3545/// is either an operand of N or it can be reached by recursively traversing
3546/// up the operands.
3547/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003548bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003549 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003550 bool found = false;
3551 findPredecessor(N, this, found, Visited);
3552 return found;
3553}
3554
Evan Chengc5484282006-10-04 00:56:09 +00003555uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3556 assert(Num < NumOperands && "Invalid child # of SDNode!");
3557 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3558}
3559
Reid Spencer577cc322007-04-01 07:32:19 +00003560std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003561 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003562 default:
3563 if (getOpcode() < ISD::BUILTIN_OP_END)
3564 return "<<Unknown DAG Node>>";
3565 else {
Evan Cheng72261582005-12-20 06:22:03 +00003566 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003567 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003568 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3569 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003570
Evan Cheng72261582005-12-20 06:22:03 +00003571 TargetLowering &TLI = G->getTargetLoweringInfo();
3572 const char *Name =
3573 TLI.getTargetNodeName(getOpcode());
3574 if (Name) return Name;
3575 }
3576
3577 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003578 }
3579
Andrew Lenharth95762122005-03-31 21:24:06 +00003580 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003581 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003582 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003583 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003584 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003585 case ISD::AssertSext: return "AssertSext";
3586 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003587
3588 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003589 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003590 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003591 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003592
3593 case ISD::Constant: return "Constant";
3594 case ISD::ConstantFP: return "ConstantFP";
3595 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003596 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003597 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003598 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003599 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003600 case ISD::RETURNADDR: return "RETURNADDR";
3601 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003602 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003603 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3604 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003605 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003606 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003607 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003608 case ISD::INTRINSIC_WO_CHAIN: {
3609 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3610 return Intrinsic::getName((Intrinsic::ID)IID);
3611 }
3612 case ISD::INTRINSIC_VOID:
3613 case ISD::INTRINSIC_W_CHAIN: {
3614 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003615 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003616 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003617
Chris Lattnerb2827b02006-03-19 00:52:58 +00003618 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003619 case ISD::TargetConstant: return "TargetConstant";
3620 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003621 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003622 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003623 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003624 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003625 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003626 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003627
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003628 case ISD::CopyToReg: return "CopyToReg";
3629 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003630 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003631 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003632 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003633 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003634 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003635 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003636 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003637
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003638 // Unary operators
3639 case ISD::FABS: return "fabs";
3640 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003641 case ISD::FSQRT: return "fsqrt";
3642 case ISD::FSIN: return "fsin";
3643 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003644 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003645 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003646
3647 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003648 case ISD::ADD: return "add";
3649 case ISD::SUB: return "sub";
3650 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003651 case ISD::MULHU: return "mulhu";
3652 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003653 case ISD::SDIV: return "sdiv";
3654 case ISD::UDIV: return "udiv";
3655 case ISD::SREM: return "srem";
3656 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003657 case ISD::SMUL_LOHI: return "smul_lohi";
3658 case ISD::UMUL_LOHI: return "umul_lohi";
3659 case ISD::SDIVREM: return "sdivrem";
3660 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003661 case ISD::AND: return "and";
3662 case ISD::OR: return "or";
3663 case ISD::XOR: return "xor";
3664 case ISD::SHL: return "shl";
3665 case ISD::SRA: return "sra";
3666 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003667 case ISD::ROTL: return "rotl";
3668 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003669 case ISD::FADD: return "fadd";
3670 case ISD::FSUB: return "fsub";
3671 case ISD::FMUL: return "fmul";
3672 case ISD::FDIV: return "fdiv";
3673 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003674 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003675
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003676 case ISD::SETCC: return "setcc";
3677 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003678 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003679 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003680 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003681 case ISD::CONCAT_VECTORS: return "concat_vectors";
3682 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003683 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003684 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003685 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003686 case ISD::ADDC: return "addc";
3687 case ISD::ADDE: return "adde";
3688 case ISD::SUBC: return "subc";
3689 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003690 case ISD::SHL_PARTS: return "shl_parts";
3691 case ISD::SRA_PARTS: return "sra_parts";
3692 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003693
3694 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3695 case ISD::INSERT_SUBREG: return "insert_subreg";
3696
Chris Lattner7f644642005-04-28 21:44:03 +00003697 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003698 case ISD::SIGN_EXTEND: return "sign_extend";
3699 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003700 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003701 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003702 case ISD::TRUNCATE: return "truncate";
3703 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00003704 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003705 case ISD::FP_EXTEND: return "fp_extend";
3706
3707 case ISD::SINT_TO_FP: return "sint_to_fp";
3708 case ISD::UINT_TO_FP: return "uint_to_fp";
3709 case ISD::FP_TO_SINT: return "fp_to_sint";
3710 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003711 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003712
3713 // Control flow instructions
3714 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003715 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003716 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003717 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003718 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003719 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003720 case ISD::CALLSEQ_START: return "callseq_start";
3721 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003722
3723 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003724 case ISD::LOAD: return "load";
3725 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003726 case ISD::VAARG: return "vaarg";
3727 case ISD::VACOPY: return "vacopy";
3728 case ISD::VAEND: return "vaend";
3729 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003730 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003731 case ISD::EXTRACT_ELEMENT: return "extract_element";
3732 case ISD::BUILD_PAIR: return "build_pair";
3733 case ISD::STACKSAVE: return "stacksave";
3734 case ISD::STACKRESTORE: return "stackrestore";
3735
3736 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003737 case ISD::MEMSET: return "memset";
3738 case ISD::MEMCPY: return "memcpy";
3739 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003740
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003741 // Bit manipulation
3742 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003743 case ISD::CTPOP: return "ctpop";
3744 case ISD::CTTZ: return "cttz";
3745 case ISD::CTLZ: return "ctlz";
3746
Chris Lattner36ce6912005-11-29 06:21:05 +00003747 // Debug info
3748 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003749 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003750
Duncan Sands36397f52007-07-27 12:58:54 +00003751 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003752 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003753
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003754 case ISD::CONDCODE:
3755 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003756 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003757 case ISD::SETOEQ: return "setoeq";
3758 case ISD::SETOGT: return "setogt";
3759 case ISD::SETOGE: return "setoge";
3760 case ISD::SETOLT: return "setolt";
3761 case ISD::SETOLE: return "setole";
3762 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003763
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003764 case ISD::SETO: return "seto";
3765 case ISD::SETUO: return "setuo";
3766 case ISD::SETUEQ: return "setue";
3767 case ISD::SETUGT: return "setugt";
3768 case ISD::SETUGE: return "setuge";
3769 case ISD::SETULT: return "setult";
3770 case ISD::SETULE: return "setule";
3771 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003772
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003773 case ISD::SETEQ: return "seteq";
3774 case ISD::SETGT: return "setgt";
3775 case ISD::SETGE: return "setge";
3776 case ISD::SETLT: return "setlt";
3777 case ISD::SETLE: return "setle";
3778 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003779 }
3780 }
3781}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003782
Evan Cheng144d8f02006-11-09 17:55:04 +00003783const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003784 switch (AM) {
3785 default:
3786 return "";
3787 case ISD::PRE_INC:
3788 return "<pre-inc>";
3789 case ISD::PRE_DEC:
3790 return "<pre-dec>";
3791 case ISD::POST_INC:
3792 return "<post-inc>";
3793 case ISD::POST_DEC:
3794 return "<post-dec>";
3795 }
3796}
3797
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003798void SDNode::dump() const { dump(0); }
3799void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003800 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003801
3802 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003803 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003804 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003805 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003806 else
Bill Wendling832171c2006-12-07 20:04:42 +00003807 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003808 }
Bill Wendling832171c2006-12-07 20:04:42 +00003809 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003810
Bill Wendling832171c2006-12-07 20:04:42 +00003811 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003812 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003813 if (i) cerr << ", ";
3814 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003815 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003816 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003817 }
3818
3819 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003820 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003821 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003822 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3823 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3824 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3825 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3826 else {
3827 cerr << "<APFloat(";
3828 CSDN->getValueAPF().convertToAPInt().dump();
3829 cerr << ")>";
3830 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003831 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003832 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003833 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003834 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003835 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003836 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003837 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003838 else
Bill Wendling832171c2006-12-07 20:04:42 +00003839 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003840 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003841 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003842 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003843 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003844 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003845 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003846 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003847 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003848 else
Bill Wendling832171c2006-12-07 20:04:42 +00003849 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003850 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003851 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003852 else
Bill Wendling832171c2006-12-07 20:04:42 +00003853 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003854 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003855 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003856 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3857 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003858 cerr << LBB->getName() << " ";
3859 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003860 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003861 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003862 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003863 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003864 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003865 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003866 } else if (const ExternalSymbolSDNode *ES =
3867 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003868 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003869 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3870 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003871 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003872 else
Bill Wendling832171c2006-12-07 20:04:42 +00003873 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003874 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003875 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003876 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3877 bool doExt = true;
3878 switch (LD->getExtensionType()) {
3879 default: doExt = false; break;
3880 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003881 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003882 break;
3883 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003884 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003885 break;
3886 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003887 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003888 break;
3889 }
3890 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003891 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003892
Evan Cheng144d8f02006-11-09 17:55:04 +00003893 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003894 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003895 cerr << " " << AM;
Evan Cheng2caccca2006-10-17 21:14:32 +00003896 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3897 if (ST->isTruncatingStore())
Bill Wendling832171c2006-12-07 20:04:42 +00003898 cerr << " <trunc "
3899 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00003900
Evan Cheng144d8f02006-11-09 17:55:04 +00003901 const char *AM = getIndexedModeName(ST->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003902 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003903 cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003904 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003905}
3906
Chris Lattnerde202b32005-11-09 23:47:37 +00003907static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003908 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3909 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003910 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003911 else
Bill Wendling832171c2006-12-07 20:04:42 +00003912 cerr << "\n" << std::string(indent+2, ' ')
3913 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003914
Chris Lattnerea946cd2005-01-09 20:38:33 +00003915
Bill Wendling832171c2006-12-07 20:04:42 +00003916 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003917 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003918}
3919
Chris Lattnerc3aae252005-01-07 07:46:32 +00003920void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003921 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003922 std::vector<const SDNode*> Nodes;
3923 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3924 I != E; ++I)
3925 Nodes.push_back(I);
3926
Chris Lattner49d24712005-01-09 20:26:36 +00003927 std::sort(Nodes.begin(), Nodes.end());
3928
3929 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003930 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003931 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003932 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003933
Jim Laskey26f7fa72006-10-17 19:33:52 +00003934 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003935
Bill Wendling832171c2006-12-07 20:04:42 +00003936 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003937}
3938
Evan Chengd6594ae2006-09-12 21:00:35 +00003939const Type *ConstantPoolSDNode::getType() const {
3940 if (isMachineConstantPoolEntry())
3941 return Val.MachineCPVal->getType();
3942 return Val.ConstVal->getType();
3943}