blob: eb6e60302d920cfdb5cbad46f2ebfeb409323b49 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000022#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000023#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000024#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000025#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000026#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000028#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000029#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000030#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000031#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000032#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000033#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner0b3e5252006-08-15 19:11:05 +000036/// makeVTList - Return an instance of the SDVTList struct initialized with the
37/// specified members.
38static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
39 SDVTList Res = {VTs, NumVTs};
40 return Res;
41}
42
Jim Laskey58b968b2005-08-17 20:08:02 +000043//===----------------------------------------------------------------------===//
44// ConstantFPSDNode Class
45//===----------------------------------------------------------------------===//
46
47/// isExactlyValue - We don't rely on operator== working on double values, as
48/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
49/// As such, this method can be used to do an exact bit-for-bit comparison of
50/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000051bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000052 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000053}
54
Dale Johannesenf04afdb2007-08-30 00:23:21 +000055bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
56 const APFloat& Val) {
57 // convert modifies in place, so make a copy.
58 APFloat Val2 = APFloat(Val);
59 switch (VT) {
60 default:
61 return false; // These can't be represented as floating point!
62
63 // FIXME rounding mode needs to be more flexible
64 case MVT::f32:
65 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
66 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
67 APFloat::opOK;
68 case MVT::f64:
69 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
70 &Val2.getSemantics() == &APFloat::IEEEdouble ||
71 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
72 APFloat::opOK;
73 // TODO: Figure out how to test if we can use a shorter type instead!
74 case MVT::f80:
75 case MVT::f128:
76 case MVT::ppcf128:
77 return true;
78 }
79}
80
Jim Laskey58b968b2005-08-17 20:08:02 +000081//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000082// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000083//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000084
Evan Chenga8df1662006-03-27 06:58:47 +000085/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000086/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000087bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000088 // Look through a bit convert.
89 if (N->getOpcode() == ISD::BIT_CONVERT)
90 N = N->getOperand(0).Val;
91
Evan Chenga8df1662006-03-27 06:58:47 +000092 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000093
94 unsigned i = 0, e = N->getNumOperands();
95
96 // Skip over all of the undef values.
97 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
98 ++i;
99
100 // Do not accept an all-undef vector.
101 if (i == e) return false;
102
103 // Do not accept build_vectors that aren't all constants or which have non-~0
104 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000105 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000106 if (isa<ConstantSDNode>(NotZero)) {
107 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
108 return false;
109 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000110 MVT::ValueType VT = NotZero.getValueType();
111 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000112 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
113 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000114 return false;
115 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000116 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
117 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000118 (uint32_t)-1)
119 return false;
120 }
Evan Chenga8df1662006-03-27 06:58:47 +0000121 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000122 return false;
123
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
125 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
129 return false;
130 return true;
131}
132
133
Evan Cheng4a147842006-03-26 09:50:58 +0000134/// isBuildVectorAllZeros - Return true if the specified node is a
135/// BUILD_VECTOR where all of the elements are 0 or undef.
136bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
140
Evan Cheng4a147842006-03-26 09:50:58 +0000141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000142
143 unsigned i = 0, e = N->getNumOperands();
144
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
147 ++i;
148
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
151
152 // Do not accept build_vectors that aren't all constants or which have non-~0
153 // elements.
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
157 return false;
158 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000160 return false;
161 } else
162 return false;
163
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
165 // undefs.
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
169 return false;
170 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000171}
172
Chris Lattnerc3aae252005-01-07 07:46:32 +0000173/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
174/// when given the operation for (X op Y).
175ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
176 // To perform this operation, we just need to swap the L and G bits of the
177 // operation.
178 unsigned OldL = (Operation >> 2) & 1;
179 unsigned OldG = (Operation >> 1) & 1;
180 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
181 (OldL << 1) | // New G bit
182 (OldG << 2)); // New L bit.
183}
184
185/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
186/// 'op' is a valid SetCC operation.
187ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
188 unsigned Operation = Op;
189 if (isInteger)
190 Operation ^= 7; // Flip L, G, E bits, but not U.
191 else
192 Operation ^= 15; // Flip all of the condition bits.
193 if (Operation > ISD::SETTRUE2)
194 Operation &= ~8; // Don't let N and U bits get set.
195 return ISD::CondCode(Operation);
196}
197
198
199/// isSignedOp - For an integer comparison, return 1 if the comparison is a
200/// signed operation and 2 if the result is an unsigned comparison. Return zero
201/// if the operation does not depend on the sign of the input (setne and seteq).
202static int isSignedOp(ISD::CondCode Opcode) {
203 switch (Opcode) {
204 default: assert(0 && "Illegal integer setcc operation!");
205 case ISD::SETEQ:
206 case ISD::SETNE: return 0;
207 case ISD::SETLT:
208 case ISD::SETLE:
209 case ISD::SETGT:
210 case ISD::SETGE: return 1;
211 case ISD::SETULT:
212 case ISD::SETULE:
213 case ISD::SETUGT:
214 case ISD::SETUGE: return 2;
215 }
216}
217
218/// getSetCCOrOperation - Return the result of a logical OR between different
219/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
220/// returns SETCC_INVALID if it is not possible to represent the resultant
221/// comparison.
222ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
223 bool isInteger) {
224 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
225 // Cannot fold a signed integer setcc with an unsigned integer setcc.
226 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000227
Chris Lattnerc3aae252005-01-07 07:46:32 +0000228 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000229
Chris Lattnerc3aae252005-01-07 07:46:32 +0000230 // If the N and U bits get set then the resultant comparison DOES suddenly
231 // care about orderedness, and is true when ordered.
232 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000233 Op &= ~16; // Clear the U bit if the N bit is set.
234
235 // Canonicalize illegal integer setcc's.
236 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
237 Op = ISD::SETNE;
238
Chris Lattnerc3aae252005-01-07 07:46:32 +0000239 return ISD::CondCode(Op);
240}
241
242/// getSetCCAndOperation - Return the result of a logical AND between different
243/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
244/// function returns zero if it is not possible to represent the resultant
245/// comparison.
246ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
247 bool isInteger) {
248 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
249 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000250 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000251
252 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000253 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
254
255 // Canonicalize illegal integer setcc's.
256 if (isInteger) {
257 switch (Result) {
258 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000259 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
260 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
261 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
262 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000263 }
264 }
265
266 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000267}
268
Chris Lattnerb48da392005-01-23 04:39:44 +0000269const TargetMachine &SelectionDAG::getTarget() const {
270 return TLI.getTargetMachine();
271}
272
Jim Laskey58b968b2005-08-17 20:08:02 +0000273//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000274// SDNode Profile Support
275//===----------------------------------------------------------------------===//
276
Jim Laskeydef69b92006-10-27 23:52:51 +0000277/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
278///
Jim Laskey583bd472006-10-27 23:46:08 +0000279static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
280 ID.AddInteger(OpC);
281}
282
283/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
284/// solely with their pointer.
285void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
286 ID.AddPointer(VTList.VTs);
287}
288
Jim Laskeydef69b92006-10-27 23:52:51 +0000289/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
290///
Jim Laskey583bd472006-10-27 23:46:08 +0000291static void AddNodeIDOperands(FoldingSetNodeID &ID,
292 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000293 for (; NumOps; --NumOps, ++Ops) {
294 ID.AddPointer(Ops->Val);
295 ID.AddInteger(Ops->ResNo);
296 }
Jim Laskey583bd472006-10-27 23:46:08 +0000297}
298
Jim Laskey583bd472006-10-27 23:46:08 +0000299static void AddNodeIDNode(FoldingSetNodeID &ID,
300 unsigned short OpC, SDVTList VTList,
301 const SDOperand *OpList, unsigned N) {
302 AddNodeIDOpcode(ID, OpC);
303 AddNodeIDValueTypes(ID, VTList);
304 AddNodeIDOperands(ID, OpList, N);
305}
306
Jim Laskeydef69b92006-10-27 23:52:51 +0000307/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
308/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000309static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
310 AddNodeIDOpcode(ID, N->getOpcode());
311 // Add the return value info.
312 AddNodeIDValueTypes(ID, N->getVTList());
313 // Add the operand info.
314 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
315
316 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000317 switch (N->getOpcode()) {
318 default: break; // Normal nodes don't need extra info.
319 case ISD::TargetConstant:
320 case ISD::Constant:
321 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
322 break;
323 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000324 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000325 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000326 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000327 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000328 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000329 case ISD::GlobalAddress:
330 case ISD::TargetGlobalTLSAddress:
331 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000332 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
333 ID.AddPointer(GA->getGlobal());
334 ID.AddInteger(GA->getOffset());
335 break;
336 }
337 case ISD::BasicBlock:
338 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
339 break;
340 case ISD::Register:
341 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
342 break;
343 case ISD::SRCVALUE: {
344 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
345 ID.AddPointer(SV->getValue());
346 ID.AddInteger(SV->getOffset());
347 break;
348 }
349 case ISD::FrameIndex:
350 case ISD::TargetFrameIndex:
351 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
352 break;
353 case ISD::JumpTable:
354 case ISD::TargetJumpTable:
355 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
356 break;
357 case ISD::ConstantPool:
358 case ISD::TargetConstantPool: {
359 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
360 ID.AddInteger(CP->getAlignment());
361 ID.AddInteger(CP->getOffset());
362 if (CP->isMachineConstantPoolEntry())
363 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
364 else
365 ID.AddPointer(CP->getConstVal());
366 break;
367 }
368 case ISD::LOAD: {
369 LoadSDNode *LD = cast<LoadSDNode>(N);
370 ID.AddInteger(LD->getAddressingMode());
371 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000372 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 ID.AddPointer(LD->getSrcValue());
374 ID.AddInteger(LD->getSrcValueOffset());
375 ID.AddInteger(LD->getAlignment());
376 ID.AddInteger(LD->isVolatile());
377 break;
378 }
379 case ISD::STORE: {
380 StoreSDNode *ST = cast<StoreSDNode>(N);
381 ID.AddInteger(ST->getAddressingMode());
382 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000383 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000384 ID.AddPointer(ST->getSrcValue());
385 ID.AddInteger(ST->getSrcValueOffset());
386 ID.AddInteger(ST->getAlignment());
387 ID.AddInteger(ST->isVolatile());
388 break;
389 }
Jim Laskey583bd472006-10-27 23:46:08 +0000390 }
391}
392
393//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000394// SelectionDAG Class
395//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000396
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000397/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000398/// SelectionDAG.
399void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000400 // Create a dummy node (which is not added to allnodes), that adds a reference
401 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000402 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000403
Chris Lattner190a4182006-08-04 17:45:20 +0000404 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000405
Chris Lattner190a4182006-08-04 17:45:20 +0000406 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000407 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000408 if (I->use_empty())
409 DeadNodes.push_back(I);
410
411 // Process the worklist, deleting the nodes and adding their uses to the
412 // worklist.
413 while (!DeadNodes.empty()) {
414 SDNode *N = DeadNodes.back();
415 DeadNodes.pop_back();
416
417 // Take the node out of the appropriate CSE map.
418 RemoveNodeFromCSEMaps(N);
419
420 // Next, brutally remove the operand list. This is safe to do, as there are
421 // no cycles in the graph.
422 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
423 SDNode *Operand = I->Val;
424 Operand->removeUser(N);
425
426 // Now that we removed this operand, see if there are no uses of it left.
427 if (Operand->use_empty())
428 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000429 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000430 if (N->OperandsNeedDelete)
431 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000432 N->OperandList = 0;
433 N->NumOperands = 0;
434
435 // Finally, remove N itself.
436 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000437 }
438
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000439 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000440 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000441}
442
Evan Cheng130a6472006-10-12 20:34:05 +0000443void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
444 SmallVector<SDNode*, 16> DeadNodes;
445 DeadNodes.push_back(N);
446
447 // Process the worklist, deleting the nodes and adding their uses to the
448 // worklist.
449 while (!DeadNodes.empty()) {
450 SDNode *N = DeadNodes.back();
451 DeadNodes.pop_back();
452
453 // Take the node out of the appropriate CSE map.
454 RemoveNodeFromCSEMaps(N);
455
456 // Next, brutally remove the operand list. This is safe to do, as there are
457 // no cycles in the graph.
458 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
459 SDNode *Operand = I->Val;
460 Operand->removeUser(N);
461
462 // Now that we removed this operand, see if there are no uses of it left.
463 if (Operand->use_empty())
464 DeadNodes.push_back(Operand);
465 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000466 if (N->OperandsNeedDelete)
467 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000468 N->OperandList = 0;
469 N->NumOperands = 0;
470
471 // Finally, remove N itself.
472 Deleted.push_back(N);
473 AllNodes.erase(N);
474 }
475}
476
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000477void SelectionDAG::DeleteNode(SDNode *N) {
478 assert(N->use_empty() && "Cannot delete a node that is not dead!");
479
480 // First take this out of the appropriate CSE map.
481 RemoveNodeFromCSEMaps(N);
482
Chris Lattner1e111c72005-09-07 05:37:01 +0000483 // Finally, remove uses due to operands of this node, remove from the
484 // AllNodes list, and delete the node.
485 DeleteNodeNotInCSEMaps(N);
486}
487
488void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
489
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000490 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000491 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000492
493 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000494 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
495 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000496 if (N->OperandsNeedDelete)
497 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000498 N->OperandList = 0;
499 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000500
501 delete N;
502}
503
Chris Lattner149c58c2005-08-16 18:17:10 +0000504/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
505/// correspond to it. This is useful when we're about to delete or repurpose
506/// the node. We don't want future request for structurally identical nodes
507/// to return N anymore.
508void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000509 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000510 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000511 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000512 case ISD::STRING:
513 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
514 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000515 case ISD::CONDCODE:
516 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
517 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000518 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000519 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
520 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000521 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000522 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000523 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000524 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000525 Erased =
526 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000527 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000528 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000529 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000530 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
531 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000532 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000533 // Remove it from the CSE Map.
534 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000535 break;
536 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000537#ifndef NDEBUG
538 // Verify that the node was actually in one of the CSE maps, unless it has a
539 // flag result (which cannot be CSE'd) or is one of the special cases that are
540 // not subject to CSE.
541 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000542 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000543 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000544 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000545 assert(0 && "Node is not in map!");
546 }
547#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000548}
549
Chris Lattner8b8749f2005-08-17 19:00:20 +0000550/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
551/// has been taken out and modified in some way. If the specified node already
552/// exists in the CSE maps, do not modify the maps, but return the existing node
553/// instead. If it doesn't exist, add it and return null.
554///
555SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
556 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000557 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000558 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000559
Chris Lattner9f8cc692005-12-19 22:21:21 +0000560 // Check that remaining values produced are not flags.
561 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
562 if (N->getValueType(i) == MVT::Flag)
563 return 0; // Never CSE anything that produces a flag.
564
Chris Lattnera5682852006-08-07 23:03:03 +0000565 SDNode *New = CSEMap.GetOrInsertNode(N);
566 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000567 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000568}
569
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000570/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
571/// were replaced with those specified. If this node is never memoized,
572/// return null, otherwise return a pointer to the slot it would take. If a
573/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000574SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
575 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000576 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000577 return 0; // Never add these nodes.
578
579 // Check that remaining values produced are not flags.
580 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
581 if (N->getValueType(i) == MVT::Flag)
582 return 0; // Never CSE anything that produces a flag.
583
Chris Lattner63e3f142007-02-04 07:28:00 +0000584 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000585 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000586 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000587 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000588}
589
590/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
591/// were replaced with those specified. If this node is never memoized,
592/// return null, otherwise return a pointer to the slot it would take. If a
593/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000594SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
595 SDOperand Op1, SDOperand Op2,
596 void *&InsertPos) {
597 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
598 return 0; // Never add these nodes.
599
600 // Check that remaining values produced are not flags.
601 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
602 if (N->getValueType(i) == MVT::Flag)
603 return 0; // Never CSE anything that produces a flag.
604
Chris Lattner63e3f142007-02-04 07:28:00 +0000605 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000606 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000607 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000608 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
609}
610
611
612/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
613/// were replaced with those specified. If this node is never memoized,
614/// return null, otherwise return a pointer to the slot it would take. If a
615/// node already exists with these operands, the slot will be non-null.
616SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000617 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000618 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000619 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000620 return 0; // Never add these nodes.
621
622 // Check that remaining values produced are not flags.
623 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
624 if (N->getValueType(i) == MVT::Flag)
625 return 0; // Never CSE anything that produces a flag.
626
Jim Laskey583bd472006-10-27 23:46:08 +0000627 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000628 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000629
Evan Cheng9629aba2006-10-11 01:47:58 +0000630 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
631 ID.AddInteger(LD->getAddressingMode());
632 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000633 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000634 ID.AddPointer(LD->getSrcValue());
635 ID.AddInteger(LD->getSrcValueOffset());
636 ID.AddInteger(LD->getAlignment());
637 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000638 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
639 ID.AddInteger(ST->getAddressingMode());
640 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000641 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000642 ID.AddPointer(ST->getSrcValue());
643 ID.AddInteger(ST->getSrcValueOffset());
644 ID.AddInteger(ST->getAlignment());
645 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000646 }
Jim Laskey583bd472006-10-27 23:46:08 +0000647
Chris Lattnera5682852006-08-07 23:03:03 +0000648 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000649}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000650
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000651
Chris Lattner78ec3112003-08-11 14:57:33 +0000652SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000653 while (!AllNodes.empty()) {
654 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000655 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000656 if (N->OperandsNeedDelete)
657 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000658 N->OperandList = 0;
659 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000660 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000661 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000662}
663
Chris Lattner0f2287b2005-04-13 02:38:18 +0000664SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000665 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000666 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000667 return getNode(ISD::AND, Op.getValueType(), Op,
668 getConstant(Imm, Op.getValueType()));
669}
670
Chris Lattner36ce6912005-11-29 06:21:05 +0000671SDOperand SelectionDAG::getString(const std::string &Val) {
672 StringSDNode *&N = StringNodes[Val];
673 if (!N) {
674 N = new StringSDNode(Val);
675 AllNodes.push_back(N);
676 }
677 return SDOperand(N, 0);
678}
679
Chris Lattner61b09412006-08-11 21:01:22 +0000680SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000681 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000682 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000683
Chris Lattner61b09412006-08-11 21:01:22 +0000684 // Mask out any bits that are not valid for this constant.
685 Val &= MVT::getIntVTBitMask(VT);
686
687 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000688 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000689 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000690 ID.AddInteger(Val);
691 void *IP = 0;
692 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
693 return SDOperand(E, 0);
694 SDNode *N = new ConstantSDNode(isT, Val, VT);
695 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000696 AllNodes.push_back(N);
697 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000698}
699
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000700SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000701 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000702 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000703
Dan Gohman7f321562007-06-25 16:23:39 +0000704 MVT::ValueType EltVT =
705 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706
Chris Lattnerd8658612005-02-17 20:17:32 +0000707 // Do the map lookup using the actual bit pattern for the floating point
708 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
709 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000710 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000711 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000712 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000713 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000714 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000715 SDNode *N = NULL;
716 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
717 if (!MVT::isVector(VT))
718 return SDOperand(N, 0);
719 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000720 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000721 CSEMap.InsertNode(N, IP);
722 AllNodes.push_back(N);
723 }
724
Dan Gohman7f321562007-06-25 16:23:39 +0000725 SDOperand Result(N, 0);
726 if (MVT::isVector(VT)) {
727 SmallVector<SDOperand, 8> Ops;
728 Ops.assign(MVT::getVectorNumElements(VT), Result);
729 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
730 }
731 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000732}
733
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000734SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
735 bool isTarget) {
736 MVT::ValueType EltVT =
737 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
738 if (EltVT==MVT::f32)
739 return getConstantFP(APFloat((float)Val), VT, isTarget);
740 else
741 return getConstantFP(APFloat(Val), VT, isTarget);
742}
743
Chris Lattnerc3aae252005-01-07 07:46:32 +0000744SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000745 MVT::ValueType VT, int Offset,
746 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000747 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
748 unsigned Opc;
749 if (GVar && GVar->isThreadLocal())
750 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
751 else
752 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000753 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000754 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000755 ID.AddPointer(GV);
756 ID.AddInteger(Offset);
757 void *IP = 0;
758 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
759 return SDOperand(E, 0);
760 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
761 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000762 AllNodes.push_back(N);
763 return SDOperand(N, 0);
764}
765
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000766SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
767 bool isTarget) {
768 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000769 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000770 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000771 ID.AddInteger(FI);
772 void *IP = 0;
773 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
774 return SDOperand(E, 0);
775 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
776 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000777 AllNodes.push_back(N);
778 return SDOperand(N, 0);
779}
780
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000781SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
782 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000783 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000784 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000785 ID.AddInteger(JTI);
786 void *IP = 0;
787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
788 return SDOperand(E, 0);
789 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
790 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000791 AllNodes.push_back(N);
792 return SDOperand(N, 0);
793}
794
Evan Chengb8973bd2006-01-31 22:23:14 +0000795SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000796 unsigned Alignment, int Offset,
797 bool isTarget) {
798 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000799 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000800 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000801 ID.AddInteger(Alignment);
802 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000803 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000804 void *IP = 0;
805 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
806 return SDOperand(E, 0);
807 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
808 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000809 AllNodes.push_back(N);
810 return SDOperand(N, 0);
811}
812
Chris Lattnerc3aae252005-01-07 07:46:32 +0000813
Evan Chengd6594ae2006-09-12 21:00:35 +0000814SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
815 MVT::ValueType VT,
816 unsigned Alignment, int Offset,
817 bool isTarget) {
818 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000819 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000820 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000821 ID.AddInteger(Alignment);
822 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000823 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000824 void *IP = 0;
825 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
826 return SDOperand(E, 0);
827 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
828 CSEMap.InsertNode(N, IP);
829 AllNodes.push_back(N);
830 return SDOperand(N, 0);
831}
832
833
Chris Lattnerc3aae252005-01-07 07:46:32 +0000834SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000835 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000836 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000837 ID.AddPointer(MBB);
838 void *IP = 0;
839 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
840 return SDOperand(E, 0);
841 SDNode *N = new BasicBlockSDNode(MBB);
842 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000843 AllNodes.push_back(N);
844 return SDOperand(N, 0);
845}
846
Chris Lattner15e4b012005-07-10 00:07:11 +0000847SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
848 if ((unsigned)VT >= ValueTypeNodes.size())
849 ValueTypeNodes.resize(VT+1);
850 if (ValueTypeNodes[VT] == 0) {
851 ValueTypeNodes[VT] = new VTSDNode(VT);
852 AllNodes.push_back(ValueTypeNodes[VT]);
853 }
854
855 return SDOperand(ValueTypeNodes[VT], 0);
856}
857
Chris Lattnerc3aae252005-01-07 07:46:32 +0000858SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
859 SDNode *&N = ExternalSymbols[Sym];
860 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000861 N = new ExternalSymbolSDNode(false, Sym, VT);
862 AllNodes.push_back(N);
863 return SDOperand(N, 0);
864}
865
Chris Lattner809ec112006-01-28 10:09:25 +0000866SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
867 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000868 SDNode *&N = TargetExternalSymbols[Sym];
869 if (N) return SDOperand(N, 0);
870 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000871 AllNodes.push_back(N);
872 return SDOperand(N, 0);
873}
874
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000875SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
876 if ((unsigned)Cond >= CondCodeNodes.size())
877 CondCodeNodes.resize(Cond+1);
878
Chris Lattner079a27a2005-08-09 20:40:02 +0000879 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000880 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000881 AllNodes.push_back(CondCodeNodes[Cond]);
882 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000883 return SDOperand(CondCodeNodes[Cond], 0);
884}
885
Chris Lattner0fdd7682005-08-30 22:38:38 +0000886SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000887 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000888 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000889 ID.AddInteger(RegNo);
890 void *IP = 0;
891 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
892 return SDOperand(E, 0);
893 SDNode *N = new RegisterSDNode(RegNo, VT);
894 CSEMap.InsertNode(N, IP);
895 AllNodes.push_back(N);
896 return SDOperand(N, 0);
897}
898
899SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
900 assert((!V || isa<PointerType>(V->getType())) &&
901 "SrcValue is not a pointer?");
902
Jim Laskey583bd472006-10-27 23:46:08 +0000903 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000904 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000905 ID.AddPointer(V);
906 ID.AddInteger(Offset);
907 void *IP = 0;
908 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
909 return SDOperand(E, 0);
910 SDNode *N = new SrcValueSDNode(V, Offset);
911 CSEMap.InsertNode(N, IP);
912 AllNodes.push_back(N);
913 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000914}
915
Chris Lattner51dabfb2006-10-14 00:41:01 +0000916SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
917 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000918 // These setcc operations always fold.
919 switch (Cond) {
920 default: break;
921 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000922 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000924 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000925
926 case ISD::SETOEQ:
927 case ISD::SETOGT:
928 case ISD::SETOGE:
929 case ISD::SETOLT:
930 case ISD::SETOLE:
931 case ISD::SETONE:
932 case ISD::SETO:
933 case ISD::SETUO:
934 case ISD::SETUEQ:
935 case ISD::SETUNE:
936 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
937 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000938 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000939
Chris Lattner67255a12005-04-07 18:14:58 +0000940 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
941 uint64_t C2 = N2C->getValue();
942 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
943 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000944
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945 // Sign extend the operands if required
946 if (ISD::isSignedIntSetCC(Cond)) {
947 C1 = N1C->getSignExtended();
948 C2 = N2C->getSignExtended();
949 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000950
Chris Lattnerc3aae252005-01-07 07:46:32 +0000951 switch (Cond) {
952 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000953 case ISD::SETEQ: return getConstant(C1 == C2, VT);
954 case ISD::SETNE: return getConstant(C1 != C2, VT);
955 case ISD::SETULT: return getConstant(C1 < C2, VT);
956 case ISD::SETUGT: return getConstant(C1 > C2, VT);
957 case ISD::SETULE: return getConstant(C1 <= C2, VT);
958 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
959 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
960 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
961 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
962 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000963 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000964 }
Chris Lattner67255a12005-04-07 18:14:58 +0000965 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000966 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
967 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +0000968 // No compile time operations on this type yet.
969 if (N1C->getValueType(0) == MVT::ppcf128)
970 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +0000971
972 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +0000973 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000974 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +0000975 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
976 return getNode(ISD::UNDEF, VT);
977 // fall through
978 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
979 case ISD::SETNE: if (R==APFloat::cmpUnordered)
980 return getNode(ISD::UNDEF, VT);
981 // fall through
982 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000983 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +0000984 case ISD::SETLT: if (R==APFloat::cmpUnordered)
985 return getNode(ISD::UNDEF, VT);
986 // fall through
987 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
988 case ISD::SETGT: if (R==APFloat::cmpUnordered)
989 return getNode(ISD::UNDEF, VT);
990 // fall through
991 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
992 case ISD::SETLE: if (R==APFloat::cmpUnordered)
993 return getNode(ISD::UNDEF, VT);
994 // fall through
995 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000996 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +0000997 case ISD::SETGE: if (R==APFloat::cmpUnordered)
998 return getNode(ISD::UNDEF, VT);
999 // fall through
1000 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001001 R==APFloat::cmpEqual, VT);
1002 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1003 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1004 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1005 R==APFloat::cmpEqual, VT);
1006 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1007 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1008 R==APFloat::cmpLessThan, VT);
1009 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1010 R==APFloat::cmpUnordered, VT);
1011 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1012 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 }
1014 } else {
1015 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001016 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001017 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001018
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001019 // Could not fold it.
1020 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001021}
1022
Dan Gohmanea859be2007-06-22 14:59:07 +00001023/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1024/// this predicate to simplify operations downstream. Mask is known to be zero
1025/// for bits that V cannot have.
1026bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1027 unsigned Depth) const {
1028 // The masks are not wide enough to represent this type! Should use APInt.
1029 if (Op.getValueType() == MVT::i128)
1030 return false;
1031
1032 uint64_t KnownZero, KnownOne;
1033 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1034 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1035 return (KnownZero & Mask) == Mask;
1036}
1037
1038/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1039/// known to be either zero or one and return them in the KnownZero/KnownOne
1040/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1041/// processing.
1042void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1043 uint64_t &KnownZero, uint64_t &KnownOne,
1044 unsigned Depth) const {
1045 KnownZero = KnownOne = 0; // Don't know anything.
1046 if (Depth == 6 || Mask == 0)
1047 return; // Limit search depth.
1048
1049 // The masks are not wide enough to represent this type! Should use APInt.
1050 if (Op.getValueType() == MVT::i128)
1051 return;
1052
1053 uint64_t KnownZero2, KnownOne2;
1054
1055 switch (Op.getOpcode()) {
1056 case ISD::Constant:
1057 // We know all of the bits for a constant!
1058 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1059 KnownZero = ~KnownOne & Mask;
1060 return;
1061 case ISD::AND:
1062 // If either the LHS or the RHS are Zero, the result is zero.
1063 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1064 Mask &= ~KnownZero;
1065 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1066 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1067 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1068
1069 // Output known-1 bits are only known if set in both the LHS & RHS.
1070 KnownOne &= KnownOne2;
1071 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1072 KnownZero |= KnownZero2;
1073 return;
1074 case ISD::OR:
1075 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1076 Mask &= ~KnownOne;
1077 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1078 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1079 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1080
1081 // Output known-0 bits are only known if clear in both the LHS & RHS.
1082 KnownZero &= KnownZero2;
1083 // Output known-1 are known to be set if set in either the LHS | RHS.
1084 KnownOne |= KnownOne2;
1085 return;
1086 case ISD::XOR: {
1087 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1088 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1089 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1090 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1091
1092 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1093 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1094 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1095 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1096 KnownZero = KnownZeroOut;
1097 return;
1098 }
1099 case ISD::SELECT:
1100 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1101 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1102 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1103 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1104
1105 // Only known if known in both the LHS and RHS.
1106 KnownOne &= KnownOne2;
1107 KnownZero &= KnownZero2;
1108 return;
1109 case ISD::SELECT_CC:
1110 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1111 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1112 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1113 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1114
1115 // Only known if known in both the LHS and RHS.
1116 KnownOne &= KnownOne2;
1117 KnownZero &= KnownZero2;
1118 return;
1119 case ISD::SETCC:
1120 // If we know the result of a setcc has the top bits zero, use this info.
1121 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1122 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1123 return;
1124 case ISD::SHL:
1125 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1126 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1127 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1128 KnownZero, KnownOne, Depth+1);
1129 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1130 KnownZero <<= SA->getValue();
1131 KnownOne <<= SA->getValue();
1132 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1133 }
1134 return;
1135 case ISD::SRL:
1136 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1137 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1138 MVT::ValueType VT = Op.getValueType();
1139 unsigned ShAmt = SA->getValue();
1140
1141 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1142 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1143 KnownZero, KnownOne, Depth+1);
1144 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1145 KnownZero &= TypeMask;
1146 KnownOne &= TypeMask;
1147 KnownZero >>= ShAmt;
1148 KnownOne >>= ShAmt;
1149
1150 uint64_t HighBits = (1ULL << ShAmt)-1;
1151 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1152 KnownZero |= HighBits; // High bits known zero.
1153 }
1154 return;
1155 case ISD::SRA:
1156 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1157 MVT::ValueType VT = Op.getValueType();
1158 unsigned ShAmt = SA->getValue();
1159
1160 // Compute the new bits that are at the top now.
1161 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1162
1163 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1164 // If any of the demanded bits are produced by the sign extension, we also
1165 // demand the input sign bit.
1166 uint64_t HighBits = (1ULL << ShAmt)-1;
1167 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1168 if (HighBits & Mask)
1169 InDemandedMask |= MVT::getIntVTSignBit(VT);
1170
1171 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1172 Depth+1);
1173 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1174 KnownZero &= TypeMask;
1175 KnownOne &= TypeMask;
1176 KnownZero >>= ShAmt;
1177 KnownOne >>= ShAmt;
1178
1179 // Handle the sign bits.
1180 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1181 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1182
1183 if (KnownZero & SignBit) {
1184 KnownZero |= HighBits; // New bits are known zero.
1185 } else if (KnownOne & SignBit) {
1186 KnownOne |= HighBits; // New bits are known one.
1187 }
1188 }
1189 return;
1190 case ISD::SIGN_EXTEND_INREG: {
1191 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1192
1193 // Sign extension. Compute the demanded bits in the result that are not
1194 // present in the input.
1195 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1196
1197 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1198 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1199
1200 // If the sign extended bits are demanded, we know that the sign
1201 // bit is demanded.
1202 if (NewBits)
1203 InputDemandedBits |= InSignBit;
1204
1205 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1206 KnownZero, KnownOne, Depth+1);
1207 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1208
1209 // If the sign bit of the input is known set or clear, then we know the
1210 // top bits of the result.
1211 if (KnownZero & InSignBit) { // Input sign bit known clear
1212 KnownZero |= NewBits;
1213 KnownOne &= ~NewBits;
1214 } else if (KnownOne & InSignBit) { // Input sign bit known set
1215 KnownOne |= NewBits;
1216 KnownZero &= ~NewBits;
1217 } else { // Input sign bit unknown
1218 KnownZero &= ~NewBits;
1219 KnownOne &= ~NewBits;
1220 }
1221 return;
1222 }
1223 case ISD::CTTZ:
1224 case ISD::CTLZ:
1225 case ISD::CTPOP: {
1226 MVT::ValueType VT = Op.getValueType();
1227 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1228 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1229 KnownOne = 0;
1230 return;
1231 }
1232 case ISD::LOAD: {
1233 if (ISD::isZEXTLoad(Op.Val)) {
1234 LoadSDNode *LD = cast<LoadSDNode>(Op);
1235 MVT::ValueType VT = LD->getLoadedVT();
1236 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1237 }
1238 return;
1239 }
1240 case ISD::ZERO_EXTEND: {
1241 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1242 uint64_t NewBits = (~InMask) & Mask;
1243 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1244 KnownOne, Depth+1);
1245 KnownZero |= NewBits & Mask;
1246 KnownOne &= ~NewBits;
1247 return;
1248 }
1249 case ISD::SIGN_EXTEND: {
1250 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1251 unsigned InBits = MVT::getSizeInBits(InVT);
1252 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1253 uint64_t InSignBit = 1ULL << (InBits-1);
1254 uint64_t NewBits = (~InMask) & Mask;
1255 uint64_t InDemandedBits = Mask & InMask;
1256
1257 // If any of the sign extended bits are demanded, we know that the sign
1258 // bit is demanded.
1259 if (NewBits & Mask)
1260 InDemandedBits |= InSignBit;
1261
1262 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1263 KnownOne, Depth+1);
1264 // If the sign bit is known zero or one, the top bits match.
1265 if (KnownZero & InSignBit) {
1266 KnownZero |= NewBits;
1267 KnownOne &= ~NewBits;
1268 } else if (KnownOne & InSignBit) {
1269 KnownOne |= NewBits;
1270 KnownZero &= ~NewBits;
1271 } else { // Otherwise, top bits aren't known.
1272 KnownOne &= ~NewBits;
1273 KnownZero &= ~NewBits;
1274 }
1275 return;
1276 }
1277 case ISD::ANY_EXTEND: {
1278 MVT::ValueType VT = Op.getOperand(0).getValueType();
1279 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1280 KnownZero, KnownOne, Depth+1);
1281 return;
1282 }
1283 case ISD::TRUNCATE: {
1284 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1285 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1286 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1287 KnownZero &= OutMask;
1288 KnownOne &= OutMask;
1289 break;
1290 }
1291 case ISD::AssertZext: {
1292 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1293 uint64_t InMask = MVT::getIntVTBitMask(VT);
1294 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1295 KnownOne, Depth+1);
1296 KnownZero |= (~InMask) & Mask;
1297 return;
1298 }
1299 case ISD::ADD: {
1300 // If either the LHS or the RHS are Zero, the result is zero.
1301 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1302 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1303 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1304 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1305
1306 // Output known-0 bits are known if clear or set in both the low clear bits
1307 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1308 // low 3 bits clear.
1309 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1310 CountTrailingZeros_64(~KnownZero2));
1311
1312 KnownZero = (1ULL << KnownZeroOut) - 1;
1313 KnownOne = 0;
1314 return;
1315 }
1316 case ISD::SUB: {
1317 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1318 if (!CLHS) return;
1319
1320 // We know that the top bits of C-X are clear if X contains less bits
1321 // than C (i.e. no wrap-around can happen). For example, 20-X is
1322 // positive if we can prove that X is >= 0 and < 16.
1323 MVT::ValueType VT = CLHS->getValueType(0);
1324 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1325 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1326 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1327 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1328 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1329
1330 // If all of the MaskV bits are known to be zero, then we know the output
1331 // top bits are zero, because we now know that the output is from [0-C].
1332 if ((KnownZero & MaskV) == MaskV) {
1333 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1334 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1335 KnownOne = 0; // No one bits known.
1336 } else {
1337 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1338 }
1339 }
1340 return;
1341 }
1342 default:
1343 // Allow the target to implement this method for its nodes.
1344 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1345 case ISD::INTRINSIC_WO_CHAIN:
1346 case ISD::INTRINSIC_W_CHAIN:
1347 case ISD::INTRINSIC_VOID:
1348 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1349 }
1350 return;
1351 }
1352}
1353
1354/// ComputeNumSignBits - Return the number of times the sign bit of the
1355/// register is replicated into the other bits. We know that at least 1 bit
1356/// is always equal to the sign bit (itself), but other cases can give us
1357/// information. For example, immediately after an "SRA X, 2", we know that
1358/// the top 3 bits are all equal to each other, so we return 3.
1359unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1360 MVT::ValueType VT = Op.getValueType();
1361 assert(MVT::isInteger(VT) && "Invalid VT!");
1362 unsigned VTBits = MVT::getSizeInBits(VT);
1363 unsigned Tmp, Tmp2;
1364
1365 if (Depth == 6)
1366 return 1; // Limit search depth.
1367
1368 switch (Op.getOpcode()) {
1369 default: break;
1370 case ISD::AssertSext:
1371 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1372 return VTBits-Tmp+1;
1373 case ISD::AssertZext:
1374 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1375 return VTBits-Tmp;
1376
1377 case ISD::Constant: {
1378 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1379 // If negative, invert the bits, then look at it.
1380 if (Val & MVT::getIntVTSignBit(VT))
1381 Val = ~Val;
1382
1383 // Shift the bits so they are the leading bits in the int64_t.
1384 Val <<= 64-VTBits;
1385
1386 // Return # leading zeros. We use 'min' here in case Val was zero before
1387 // shifting. We don't want to return '64' as for an i32 "0".
1388 return std::min(VTBits, CountLeadingZeros_64(Val));
1389 }
1390
1391 case ISD::SIGN_EXTEND:
1392 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1393 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1394
1395 case ISD::SIGN_EXTEND_INREG:
1396 // Max of the input and what this extends.
1397 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1398 Tmp = VTBits-Tmp+1;
1399
1400 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1401 return std::max(Tmp, Tmp2);
1402
1403 case ISD::SRA:
1404 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1405 // SRA X, C -> adds C sign bits.
1406 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1407 Tmp += C->getValue();
1408 if (Tmp > VTBits) Tmp = VTBits;
1409 }
1410 return Tmp;
1411 case ISD::SHL:
1412 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1413 // shl destroys sign bits.
1414 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1415 if (C->getValue() >= VTBits || // Bad shift.
1416 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1417 return Tmp - C->getValue();
1418 }
1419 break;
1420 case ISD::AND:
1421 case ISD::OR:
1422 case ISD::XOR: // NOT is handled here.
1423 // Logical binary ops preserve the number of sign bits.
1424 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1425 if (Tmp == 1) return 1; // Early out.
1426 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1427 return std::min(Tmp, Tmp2);
1428
1429 case ISD::SELECT:
1430 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1431 if (Tmp == 1) return 1; // Early out.
1432 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1433 return std::min(Tmp, Tmp2);
1434
1435 case ISD::SETCC:
1436 // If setcc returns 0/-1, all bits are sign bits.
1437 if (TLI.getSetCCResultContents() ==
1438 TargetLowering::ZeroOrNegativeOneSetCCResult)
1439 return VTBits;
1440 break;
1441 case ISD::ROTL:
1442 case ISD::ROTR:
1443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1444 unsigned RotAmt = C->getValue() & (VTBits-1);
1445
1446 // Handle rotate right by N like a rotate left by 32-N.
1447 if (Op.getOpcode() == ISD::ROTR)
1448 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1449
1450 // If we aren't rotating out all of the known-in sign bits, return the
1451 // number that are left. This handles rotl(sext(x), 1) for example.
1452 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1453 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1454 }
1455 break;
1456 case ISD::ADD:
1457 // Add can have at most one carry bit. Thus we know that the output
1458 // is, at worst, one more bit than the inputs.
1459 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1460 if (Tmp == 1) return 1; // Early out.
1461
1462 // Special case decrementing a value (ADD X, -1):
1463 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1464 if (CRHS->isAllOnesValue()) {
1465 uint64_t KnownZero, KnownOne;
1466 uint64_t Mask = MVT::getIntVTBitMask(VT);
1467 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1468
1469 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1470 // sign bits set.
1471 if ((KnownZero|1) == Mask)
1472 return VTBits;
1473
1474 // If we are subtracting one from a positive number, there is no carry
1475 // out of the result.
1476 if (KnownZero & MVT::getIntVTSignBit(VT))
1477 return Tmp;
1478 }
1479
1480 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1481 if (Tmp2 == 1) return 1;
1482 return std::min(Tmp, Tmp2)-1;
1483 break;
1484
1485 case ISD::SUB:
1486 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1487 if (Tmp2 == 1) return 1;
1488
1489 // Handle NEG.
1490 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1491 if (CLHS->getValue() == 0) {
1492 uint64_t KnownZero, KnownOne;
1493 uint64_t Mask = MVT::getIntVTBitMask(VT);
1494 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1495 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1496 // sign bits set.
1497 if ((KnownZero|1) == Mask)
1498 return VTBits;
1499
1500 // If the input is known to be positive (the sign bit is known clear),
1501 // the output of the NEG has the same number of sign bits as the input.
1502 if (KnownZero & MVT::getIntVTSignBit(VT))
1503 return Tmp2;
1504
1505 // Otherwise, we treat this like a SUB.
1506 }
1507
1508 // Sub can have at most one carry bit. Thus we know that the output
1509 // is, at worst, one more bit than the inputs.
1510 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1511 if (Tmp == 1) return 1; // Early out.
1512 return std::min(Tmp, Tmp2)-1;
1513 break;
1514 case ISD::TRUNCATE:
1515 // FIXME: it's tricky to do anything useful for this, but it is an important
1516 // case for targets like X86.
1517 break;
1518 }
1519
1520 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1521 if (Op.getOpcode() == ISD::LOAD) {
1522 LoadSDNode *LD = cast<LoadSDNode>(Op);
1523 unsigned ExtType = LD->getExtensionType();
1524 switch (ExtType) {
1525 default: break;
1526 case ISD::SEXTLOAD: // '17' bits known
1527 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1528 return VTBits-Tmp+1;
1529 case ISD::ZEXTLOAD: // '16' bits known
1530 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1531 return VTBits-Tmp;
1532 }
1533 }
1534
1535 // Allow the target to implement this method for its nodes.
1536 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1537 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1538 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1539 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1540 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1541 if (NumBits > 1) return NumBits;
1542 }
1543
1544 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1545 // use this information.
1546 uint64_t KnownZero, KnownOne;
1547 uint64_t Mask = MVT::getIntVTBitMask(VT);
1548 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1549
1550 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1551 if (KnownZero & SignBit) { // SignBit is 0
1552 Mask = KnownZero;
1553 } else if (KnownOne & SignBit) { // SignBit is 1;
1554 Mask = KnownOne;
1555 } else {
1556 // Nothing known.
1557 return 1;
1558 }
1559
1560 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1561 // the number of identical bits in the top of the input value.
1562 Mask ^= ~0ULL;
1563 Mask <<= 64-VTBits;
1564 // Return # leading zeros. We use 'min' here in case Val was zero before
1565 // shifting. We don't want to return '64' as for an i32 "0".
1566 return std::min(VTBits, CountLeadingZeros_64(Mask));
1567}
1568
Chris Lattner51dabfb2006-10-14 00:41:01 +00001569
Chris Lattnerc3aae252005-01-07 07:46:32 +00001570/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001571///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001572SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001573 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001574 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001575 void *IP = 0;
1576 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1577 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001578 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001579 CSEMap.InsertNode(N, IP);
1580
1581 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001582 return SDOperand(N, 0);
1583}
1584
Chris Lattnerc3aae252005-01-07 07:46:32 +00001585SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1586 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001587 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001588 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001589 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1590 uint64_t Val = C->getValue();
1591 switch (Opcode) {
1592 default: break;
1593 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001594 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001595 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1596 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001597 case ISD::UINT_TO_FP:
1598 case ISD::SINT_TO_FP: {
1599 const uint64_t zero[] = {0, 0};
1600 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001601 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001602 MVT::getSizeInBits(Operand.getValueType()),
1603 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001604 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001605 return getConstantFP(apf, VT);
1606 }
Chris Lattner94683772005-12-23 05:30:37 +00001607 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001608 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001609 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001610 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001611 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001612 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001613 case ISD::BSWAP:
1614 switch(VT) {
1615 default: assert(0 && "Invalid bswap!"); break;
1616 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1617 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1618 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1619 }
1620 break;
1621 case ISD::CTPOP:
1622 switch(VT) {
1623 default: assert(0 && "Invalid ctpop!"); break;
1624 case MVT::i1: return getConstant(Val != 0, VT);
1625 case MVT::i8:
1626 Tmp1 = (unsigned)Val & 0xFF;
1627 return getConstant(CountPopulation_32(Tmp1), VT);
1628 case MVT::i16:
1629 Tmp1 = (unsigned)Val & 0xFFFF;
1630 return getConstant(CountPopulation_32(Tmp1), VT);
1631 case MVT::i32:
1632 return getConstant(CountPopulation_32((unsigned)Val), VT);
1633 case MVT::i64:
1634 return getConstant(CountPopulation_64(Val), VT);
1635 }
1636 case ISD::CTLZ:
1637 switch(VT) {
1638 default: assert(0 && "Invalid ctlz!"); break;
1639 case MVT::i1: return getConstant(Val == 0, VT);
1640 case MVT::i8:
1641 Tmp1 = (unsigned)Val & 0xFF;
1642 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1643 case MVT::i16:
1644 Tmp1 = (unsigned)Val & 0xFFFF;
1645 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1646 case MVT::i32:
1647 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1648 case MVT::i64:
1649 return getConstant(CountLeadingZeros_64(Val), VT);
1650 }
1651 case ISD::CTTZ:
1652 switch(VT) {
1653 default: assert(0 && "Invalid cttz!"); break;
1654 case MVT::i1: return getConstant(Val == 0, VT);
1655 case MVT::i8:
1656 Tmp1 = (unsigned)Val | 0x100;
1657 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1658 case MVT::i16:
1659 Tmp1 = (unsigned)Val | 0x10000;
1660 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1661 case MVT::i32:
1662 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1663 case MVT::i64:
1664 return getConstant(CountTrailingZeros_64(Val), VT);
1665 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001666 }
1667 }
1668
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001669 // Constant fold unary operations with a floating point constant operand.
1670 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1671 APFloat V = C->getValueAPF(); // make copy
Chris Lattnerc3aae252005-01-07 07:46:32 +00001672 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001673 case ISD::FNEG:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001674 V.changeSign();
1675 return getConstantFP(V, VT);
Chris Lattner94683772005-12-23 05:30:37 +00001676 case ISD::FABS:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001677 V.clearSign();
1678 return getConstantFP(V, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001679 case ISD::FP_ROUND:
1680 case ISD::FP_EXTEND:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001681 // This can return overflow, underflow, or inexact; we don't care.
1682 // FIXME need to be more flexible about rounding mode.
1683 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
Dale Johannesen73328d12007-09-19 23:55:34 +00001684 VT==MVT::f64 ? APFloat::IEEEdouble :
1685 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1686 VT==MVT::f128 ? APFloat::IEEEquad :
1687 APFloat::Bogus,
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001688 APFloat::rmNearestTiesToEven);
1689 return getConstantFP(V, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001690 case ISD::FP_TO_SINT:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001691 case ISD::FP_TO_UINT: {
1692 integerPart x;
1693 assert(integerPartWidth >= 64);
1694 // FIXME need to be more flexible about rounding mode.
1695 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1696 Opcode==ISD::FP_TO_SINT,
1697 APFloat::rmTowardZero);
1698 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1699 break;
1700 return getConstant(x, VT);
1701 }
Chris Lattner94683772005-12-23 05:30:37 +00001702 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001703 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001704 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001705 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001706 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001707 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001708 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001709 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001710
1711 unsigned OpOpcode = Operand.Val->getOpcode();
1712 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001713 case ISD::TokenFactor:
1714 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001715 case ISD::FP_ROUND:
1716 case ISD::FP_EXTEND:
1717 assert(MVT::isFloatingPoint(VT) &&
1718 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1719 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001720 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001721 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1722 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001723 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001724 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001725 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1726 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1727 break;
1728 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001729 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1730 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001731 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001732 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001733 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001734 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001735 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001736 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001737 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1738 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001739 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001740 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001741 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1742 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1743 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1744 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001745 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001746 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1747 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001748 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001749 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001750 if (OpOpcode == ISD::TRUNCATE)
1751 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001752 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1753 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001754 // If the source is smaller than the dest, we still need an extend.
1755 if (Operand.Val->getOperand(0).getValueType() < VT)
1756 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1757 else if (Operand.Val->getOperand(0).getValueType() > VT)
1758 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1759 else
1760 return Operand.Val->getOperand(0);
1761 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001762 break;
Chris Lattner35481892005-12-23 00:16:34 +00001763 case ISD::BIT_CONVERT:
1764 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001765 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001766 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001767 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001768 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1769 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001770 if (OpOpcode == ISD::UNDEF)
1771 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001772 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001773 case ISD::SCALAR_TO_VECTOR:
1774 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001775 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001776 "Illegal SCALAR_TO_VECTOR node!");
1777 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001778 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001779 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1780 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001781 Operand.Val->getOperand(0));
1782 if (OpOpcode == ISD::FNEG) // --X -> X
1783 return Operand.Val->getOperand(0);
1784 break;
1785 case ISD::FABS:
1786 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1787 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1788 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001789 }
1790
Chris Lattner43247a12005-08-25 19:12:10 +00001791 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001792 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001793 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001794 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001795 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001796 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001797 void *IP = 0;
1798 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1799 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001800 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001801 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001802 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001803 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001804 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001805 AllNodes.push_back(N);
1806 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001807}
1808
Chris Lattner36019aa2005-04-18 03:48:41 +00001809
1810
Chris Lattnerc3aae252005-01-07 07:46:32 +00001811SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1812 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001813#ifndef NDEBUG
1814 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001815 case ISD::TokenFactor:
1816 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1817 N2.getValueType() == MVT::Other && "Invalid token factor!");
1818 break;
Chris Lattner76365122005-01-16 02:23:22 +00001819 case ISD::AND:
1820 case ISD::OR:
1821 case ISD::XOR:
1822 case ISD::UDIV:
1823 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001824 case ISD::MULHU:
1825 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001826 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1827 // fall through
1828 case ISD::ADD:
1829 case ISD::SUB:
1830 case ISD::MUL:
1831 case ISD::SDIV:
1832 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001833 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1834 // fall through.
1835 case ISD::FADD:
1836 case ISD::FSUB:
1837 case ISD::FMUL:
1838 case ISD::FDIV:
1839 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001840 assert(N1.getValueType() == N2.getValueType() &&
1841 N1.getValueType() == VT && "Binary operator types must match!");
1842 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001843 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1844 assert(N1.getValueType() == VT &&
1845 MVT::isFloatingPoint(N1.getValueType()) &&
1846 MVT::isFloatingPoint(N2.getValueType()) &&
1847 "Invalid FCOPYSIGN!");
1848 break;
Chris Lattner76365122005-01-16 02:23:22 +00001849 case ISD::SHL:
1850 case ISD::SRA:
1851 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001852 case ISD::ROTL:
1853 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001854 assert(VT == N1.getValueType() &&
1855 "Shift operators return type must be the same as their first arg");
1856 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001857 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001858 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001859 case ISD::FP_ROUND_INREG: {
1860 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1861 assert(VT == N1.getValueType() && "Not an inreg round!");
1862 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1863 "Cannot FP_ROUND_INREG integer types");
1864 assert(EVT <= VT && "Not rounding down!");
1865 break;
1866 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001867 case ISD::AssertSext:
1868 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001869 case ISD::SIGN_EXTEND_INREG: {
1870 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1871 assert(VT == N1.getValueType() && "Not an inreg extend!");
1872 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1873 "Cannot *_EXTEND_INREG FP types");
1874 assert(EVT <= VT && "Not extending!");
1875 }
1876
Chris Lattner76365122005-01-16 02:23:22 +00001877 default: break;
1878 }
1879#endif
1880
Chris Lattnerc3aae252005-01-07 07:46:32 +00001881 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1882 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1883 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001884 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1885 int64_t Val = N1C->getValue();
1886 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1887 Val <<= 64-FromBits;
1888 Val >>= 64-FromBits;
1889 return getConstant(Val, VT);
1890 }
1891
Chris Lattnerc3aae252005-01-07 07:46:32 +00001892 if (N2C) {
1893 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1894 switch (Opcode) {
1895 case ISD::ADD: return getConstant(C1 + C2, VT);
1896 case ISD::SUB: return getConstant(C1 - C2, VT);
1897 case ISD::MUL: return getConstant(C1 * C2, VT);
1898 case ISD::UDIV:
1899 if (C2) return getConstant(C1 / C2, VT);
1900 break;
1901 case ISD::UREM :
1902 if (C2) return getConstant(C1 % C2, VT);
1903 break;
1904 case ISD::SDIV :
1905 if (C2) return getConstant(N1C->getSignExtended() /
1906 N2C->getSignExtended(), VT);
1907 break;
1908 case ISD::SREM :
1909 if (C2) return getConstant(N1C->getSignExtended() %
1910 N2C->getSignExtended(), VT);
1911 break;
1912 case ISD::AND : return getConstant(C1 & C2, VT);
1913 case ISD::OR : return getConstant(C1 | C2, VT);
1914 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001915 case ISD::SHL : return getConstant(C1 << C2, VT);
1916 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001917 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001918 case ISD::ROTL :
1919 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1920 VT);
1921 case ISD::ROTR :
1922 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1923 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001924 default: break;
1925 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001926 } else { // Cannonicalize constant to RHS if commutative
1927 if (isCommutativeBinOp(Opcode)) {
1928 std::swap(N1C, N2C);
1929 std::swap(N1, N2);
1930 }
1931 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001932 }
1933
1934 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1935 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001936 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001937 if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001938 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1939 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001940 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001941 case ISD::FADD:
1942 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1943 if (s!=APFloat::opInvalidOp)
1944 return getConstantFP(V1, VT);
1945 break;
1946 case ISD::FSUB:
1947 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1948 if (s!=APFloat::opInvalidOp)
1949 return getConstantFP(V1, VT);
1950 break;
1951 case ISD::FMUL:
1952 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1953 if (s!=APFloat::opInvalidOp)
1954 return getConstantFP(V1, VT);
1955 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001956 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001957 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1958 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1959 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001960 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001961 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001962 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1963 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1964 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001965 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001966 case ISD::FCOPYSIGN:
1967 V1.copySign(V2);
1968 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001969 default: break;
1970 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001971 } else { // Cannonicalize constant to RHS if commutative
1972 if (isCommutativeBinOp(Opcode)) {
1973 std::swap(N1CFP, N2CFP);
1974 std::swap(N1, N2);
1975 }
1976 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001977 }
Chris Lattner62b57722006-04-20 05:39:12 +00001978
1979 // Canonicalize an UNDEF to the RHS, even over a constant.
1980 if (N1.getOpcode() == ISD::UNDEF) {
1981 if (isCommutativeBinOp(Opcode)) {
1982 std::swap(N1, N2);
1983 } else {
1984 switch (Opcode) {
1985 case ISD::FP_ROUND_INREG:
1986 case ISD::SIGN_EXTEND_INREG:
1987 case ISD::SUB:
1988 case ISD::FSUB:
1989 case ISD::FDIV:
1990 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001991 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001992 return N1; // fold op(undef, arg2) -> undef
1993 case ISD::UDIV:
1994 case ISD::SDIV:
1995 case ISD::UREM:
1996 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001997 case ISD::SRL:
1998 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00001999 if (!MVT::isVector(VT))
2000 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2001 // For vectors, we can't easily build an all zero vector, just return
2002 // the LHS.
2003 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002004 }
2005 }
2006 }
2007
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002008 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002009 if (N2.getOpcode() == ISD::UNDEF) {
2010 switch (Opcode) {
2011 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002012 case ISD::ADDC:
2013 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002014 case ISD::SUB:
2015 case ISD::FADD:
2016 case ISD::FSUB:
2017 case ISD::FMUL:
2018 case ISD::FDIV:
2019 case ISD::FREM:
2020 case ISD::UDIV:
2021 case ISD::SDIV:
2022 case ISD::UREM:
2023 case ISD::SREM:
2024 case ISD::XOR:
2025 return N2; // fold op(arg1, undef) -> undef
2026 case ISD::MUL:
2027 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002028 case ISD::SRL:
2029 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002030 if (!MVT::isVector(VT))
2031 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2032 // For vectors, we can't easily build an all zero vector, just return
2033 // the LHS.
2034 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002035 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002036 if (!MVT::isVector(VT))
2037 return getConstant(MVT::getIntVTBitMask(VT), VT);
2038 // For vectors, we can't easily build an all one vector, just return
2039 // the LHS.
2040 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002041 case ISD::SRA:
2042 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002043 }
2044 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002045
Chris Lattner51dabfb2006-10-14 00:41:01 +00002046 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002047 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00002048 case ISD::TokenFactor:
2049 // Fold trivial token factors.
2050 if (N1.getOpcode() == ISD::EntryToken) return N2;
2051 if (N2.getOpcode() == ISD::EntryToken) return N1;
2052 break;
2053
Chris Lattner51dabfb2006-10-14 00:41:01 +00002054 case ISD::AND:
2055 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2056 // worth handling here.
2057 if (N2C && N2C->getValue() == 0)
2058 return N2;
2059 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00002060 case ISD::OR:
2061 case ISD::XOR:
2062 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2063 // worth handling here.
2064 if (N2C && N2C->getValue() == 0)
2065 return N1;
2066 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002067 case ISD::FP_ROUND_INREG:
2068 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2069 break;
2070 case ISD::SIGN_EXTEND_INREG: {
2071 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2072 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002073 break;
2074 }
Dan Gohman7f321562007-06-25 16:23:39 +00002075 case ISD::EXTRACT_VECTOR_ELT:
2076 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2077
Dan Gohman743d3a72007-07-10 18:20:44 +00002078 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002079 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002080 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2081 N1.getNumOperands() > 0) {
2082 unsigned Factor =
2083 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2084 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2085 N1.getOperand(N2C->getValue() / Factor),
2086 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002087 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002088
Dan Gohman7f321562007-06-25 16:23:39 +00002089 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2090 // expanding large vector constants.
2091 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2092 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002093
2094 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2095 // operations are lowered to scalars.
2096 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2097 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2098 if (IEC == N2C)
2099 return N1.getOperand(1);
2100 else
2101 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2102 }
Dan Gohman7f321562007-06-25 16:23:39 +00002103 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002104 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002105 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2106
Chris Lattner5c6621c2006-09-19 04:51:23 +00002107 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2108 // 64-bit integers into 32-bit parts. Instead of building the extract of
2109 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002110 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002111 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002112
2113 // EXTRACT_ELEMENT of a constant int is also very common.
2114 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2115 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2116 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002117 }
2118 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002119
Nate Begemaneea805e2005-04-13 21:23:31 +00002120 // FIXME: figure out how to safely handle things like
2121 // int foo(int x) { return 1 << (x & 255); }
2122 // int bar() { return foo(256); }
2123#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002124 case ISD::SHL:
2125 case ISD::SRL:
2126 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002127 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002128 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002129 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002130 else if (N2.getOpcode() == ISD::AND)
2131 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2132 // If the and is only masking out bits that cannot effect the shift,
2133 // eliminate the and.
2134 unsigned NumBits = MVT::getSizeInBits(VT);
2135 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2136 return getNode(Opcode, VT, N1, N2.getOperand(0));
2137 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002138 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002139#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002140 }
2141
Chris Lattner27e9b412005-05-11 18:57:39 +00002142 // Memoize this node if possible.
2143 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002144 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002145 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002146 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002147 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002148 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002149 void *IP = 0;
2150 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2151 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002152 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002153 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002154 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002155 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002156 }
2157
Chris Lattnerc3aae252005-01-07 07:46:32 +00002158 AllNodes.push_back(N);
2159 return SDOperand(N, 0);
2160}
2161
Chris Lattnerc3aae252005-01-07 07:46:32 +00002162SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2163 SDOperand N1, SDOperand N2, SDOperand N3) {
2164 // Perform various simplifications.
2165 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2166 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002167 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002168 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002169 // Use FoldSetCC to simplify SETCC's.
2170 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002171 if (Simp.Val) return Simp;
2172 break;
2173 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002174 case ISD::SELECT:
2175 if (N1C)
2176 if (N1C->getValue())
2177 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002178 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002179 return N3; // select false, X, Y -> Y
2180
2181 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002182 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002183 case ISD::BRCOND:
2184 if (N2C)
2185 if (N2C->getValue()) // Unconditional branch
2186 return getNode(ISD::BR, MVT::Other, N1, N3);
2187 else
2188 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002189 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002190 case ISD::VECTOR_SHUFFLE:
2191 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2192 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2193 N3.getOpcode() == ISD::BUILD_VECTOR &&
2194 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2195 "Illegal VECTOR_SHUFFLE node!");
2196 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002197 case ISD::BIT_CONVERT:
2198 // Fold bit_convert nodes from a type to themselves.
2199 if (N1.getValueType() == VT)
2200 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002201 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002202 }
2203
Chris Lattner43247a12005-08-25 19:12:10 +00002204 // Memoize node if it doesn't produce a flag.
2205 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002206 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002207 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002208 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002209 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002210 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002211 void *IP = 0;
2212 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2213 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002214 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002215 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002216 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002217 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002218 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002219 AllNodes.push_back(N);
2220 return SDOperand(N, 0);
2221}
2222
2223SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002224 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002225 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002226 SDOperand Ops[] = { N1, N2, N3, N4 };
2227 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002228}
2229
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002230SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2231 SDOperand N1, SDOperand N2, SDOperand N3,
2232 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002233 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2234 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002235}
2236
Evan Cheng7038daf2005-12-10 00:37:58 +00002237SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2238 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002239 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002240 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002241 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2242 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002243 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002244 Ty = MVT::getTypeForValueType(VT);
2245 } else if (SV) {
2246 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2247 assert(PT && "Value for load must be a pointer");
2248 Ty = PT->getElementType();
2249 }
2250 assert(Ty && "Could not get type information for load");
2251 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2252 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002253 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002254 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002255 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002256 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002257 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002258 ID.AddInteger(ISD::UNINDEXED);
2259 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002260 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002261 ID.AddPointer(SV);
2262 ID.AddInteger(SVOffset);
2263 ID.AddInteger(Alignment);
2264 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002265 void *IP = 0;
2266 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2267 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002268 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002269 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2270 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002271 CSEMap.InsertNode(N, IP);
2272 AllNodes.push_back(N);
2273 return SDOperand(N, 0);
2274}
2275
2276SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002277 SDOperand Chain, SDOperand Ptr,
2278 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002279 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002280 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002281 // If they are asking for an extending load from/to the same thing, return a
2282 // normal load.
2283 if (VT == EVT)
2284 ExtType = ISD::NON_EXTLOAD;
2285
2286 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002287 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002288 else
2289 assert(EVT < VT && "Should only be an extending load, not truncating!");
2290 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2291 "Cannot sign/zero extend a FP/Vector load!");
2292 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2293 "Cannot convert from FP to Int or Int -> FP!");
2294
Dan Gohman575e2f42007-06-04 15:49:41 +00002295 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2296 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002297 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002298 Ty = MVT::getTypeForValueType(VT);
2299 } else if (SV) {
2300 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2301 assert(PT && "Value for load must be a pointer");
2302 Ty = PT->getElementType();
2303 }
2304 assert(Ty && "Could not get type information for load");
2305 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2306 }
Evan Cheng466685d2006-10-09 20:57:25 +00002307 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002308 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002309 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002310 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002311 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002312 ID.AddInteger(ISD::UNINDEXED);
2313 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002314 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002315 ID.AddPointer(SV);
2316 ID.AddInteger(SVOffset);
2317 ID.AddInteger(Alignment);
2318 ID.AddInteger(isVolatile);
2319 void *IP = 0;
2320 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2321 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002322 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002323 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002324 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002325 AllNodes.push_back(N);
2326 return SDOperand(N, 0);
2327}
2328
Evan Cheng144d8f02006-11-09 17:55:04 +00002329SDOperand
2330SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2331 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002332 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002333 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2334 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002335 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002336 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002337 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002338 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002339 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002340 ID.AddInteger(AM);
2341 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002342 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002343 ID.AddPointer(LD->getSrcValue());
2344 ID.AddInteger(LD->getSrcValueOffset());
2345 ID.AddInteger(LD->getAlignment());
2346 ID.AddInteger(LD->isVolatile());
2347 void *IP = 0;
2348 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2349 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002350 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002351 LD->getExtensionType(), LD->getLoadedVT(),
2352 LD->getSrcValue(), LD->getSrcValueOffset(),
2353 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002354 CSEMap.InsertNode(N, IP);
2355 AllNodes.push_back(N);
2356 return SDOperand(N, 0);
2357}
2358
Jeff Cohend41b30d2006-11-05 19:31:28 +00002359SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002360 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002361 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002362 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002363
Dan Gohman575e2f42007-06-04 15:49:41 +00002364 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2365 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002366 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002367 Ty = MVT::getTypeForValueType(VT);
2368 } else if (SV) {
2369 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2370 assert(PT && "Value for store must be a pointer");
2371 Ty = PT->getElementType();
2372 }
2373 assert(Ty && "Could not get type information for store");
2374 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2375 }
Evan Chengad071e12006-10-05 22:57:11 +00002376 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002377 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002378 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002379 FoldingSetNodeID ID;
2380 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002381 ID.AddInteger(ISD::UNINDEXED);
2382 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002383 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002384 ID.AddPointer(SV);
2385 ID.AddInteger(SVOffset);
2386 ID.AddInteger(Alignment);
2387 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002388 void *IP = 0;
2389 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2390 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002391 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002392 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002393 CSEMap.InsertNode(N, IP);
2394 AllNodes.push_back(N);
2395 return SDOperand(N, 0);
2396}
2397
Jeff Cohend41b30d2006-11-05 19:31:28 +00002398SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002399 SDOperand Ptr, const Value *SV,
2400 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002401 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002402 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002403 bool isTrunc = VT != SVT;
2404
2405 assert(VT > SVT && "Not a truncation?");
2406 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2407 "Can't do FP-INT conversion!");
2408
Dan Gohman575e2f42007-06-04 15:49:41 +00002409 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2410 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002411 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002412 Ty = MVT::getTypeForValueType(VT);
2413 } else if (SV) {
2414 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2415 assert(PT && "Value for store must be a pointer");
2416 Ty = PT->getElementType();
2417 }
2418 assert(Ty && "Could not get type information for store");
2419 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2420 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002421 SDVTList VTs = getVTList(MVT::Other);
2422 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002423 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002424 FoldingSetNodeID ID;
2425 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002426 ID.AddInteger(ISD::UNINDEXED);
2427 ID.AddInteger(isTrunc);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002428 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002429 ID.AddPointer(SV);
2430 ID.AddInteger(SVOffset);
2431 ID.AddInteger(Alignment);
2432 ID.AddInteger(isVolatile);
2433 void *IP = 0;
2434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2435 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002436 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002437 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002438 CSEMap.InsertNode(N, IP);
2439 AllNodes.push_back(N);
2440 return SDOperand(N, 0);
2441}
2442
Evan Cheng144d8f02006-11-09 17:55:04 +00002443SDOperand
2444SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2445 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002446 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2447 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2448 "Store is already a indexed store!");
2449 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2450 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2451 FoldingSetNodeID ID;
2452 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2453 ID.AddInteger(AM);
2454 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002455 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002456 ID.AddPointer(ST->getSrcValue());
2457 ID.AddInteger(ST->getSrcValueOffset());
2458 ID.AddInteger(ST->getAlignment());
2459 ID.AddInteger(ST->isVolatile());
2460 void *IP = 0;
2461 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2462 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002463 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002464 ST->isTruncatingStore(), ST->getStoredVT(),
2465 ST->getSrcValue(), ST->getSrcValueOffset(),
2466 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002467 CSEMap.InsertNode(N, IP);
2468 AllNodes.push_back(N);
2469 return SDOperand(N, 0);
2470}
2471
Nate Begemanacc398c2006-01-25 18:21:52 +00002472SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2473 SDOperand Chain, SDOperand Ptr,
2474 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002475 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002476 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002477}
2478
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002479SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002480 const SDOperand *Ops, unsigned NumOps) {
2481 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002482 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002483 case 1: return getNode(Opcode, VT, Ops[0]);
2484 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2485 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002486 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002487 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002488
Chris Lattneref847df2005-04-09 03:27:28 +00002489 switch (Opcode) {
2490 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002491 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002492 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002493 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2494 "LHS and RHS of condition must have same type!");
2495 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2496 "True and False arms of SelectCC must have same type!");
2497 assert(Ops[2].getValueType() == VT &&
2498 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002499 break;
2500 }
2501 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002502 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002503 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2504 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002505 break;
2506 }
Chris Lattneref847df2005-04-09 03:27:28 +00002507 }
2508
Chris Lattner385328c2005-05-14 07:42:29 +00002509 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002510 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002511 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002512 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002513 FoldingSetNodeID ID;
2514 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002515 void *IP = 0;
2516 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2517 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002518 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002519 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002520 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002521 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002522 }
Chris Lattneref847df2005-04-09 03:27:28 +00002523 AllNodes.push_back(N);
2524 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002525}
2526
Chris Lattner89c34632005-05-14 06:20:26 +00002527SDOperand SelectionDAG::getNode(unsigned Opcode,
2528 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002529 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002530 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2531 Ops, NumOps);
2532}
2533
2534SDOperand SelectionDAG::getNode(unsigned Opcode,
2535 const MVT::ValueType *VTs, unsigned NumVTs,
2536 const SDOperand *Ops, unsigned NumOps) {
2537 if (NumVTs == 1)
2538 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002539 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2540}
2541
2542SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2543 const SDOperand *Ops, unsigned NumOps) {
2544 if (VTList.NumVTs == 1)
2545 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002546
Chris Lattner5f056bf2005-07-10 01:55:33 +00002547 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002548 // FIXME: figure out how to safely handle things like
2549 // int foo(int x) { return 1 << (x & 255); }
2550 // int bar() { return foo(256); }
2551#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002552 case ISD::SRA_PARTS:
2553 case ISD::SRL_PARTS:
2554 case ISD::SHL_PARTS:
2555 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002556 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002557 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2558 else if (N3.getOpcode() == ISD::AND)
2559 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2560 // If the and is only masking out bits that cannot effect the shift,
2561 // eliminate the and.
2562 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2563 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2564 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2565 }
2566 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002567#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002568 }
Chris Lattner89c34632005-05-14 06:20:26 +00002569
Chris Lattner43247a12005-08-25 19:12:10 +00002570 // Memoize the node unless it returns a flag.
2571 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002572 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002573 FoldingSetNodeID ID;
2574 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002575 void *IP = 0;
2576 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2577 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002578 if (NumOps == 1)
2579 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2580 else if (NumOps == 2)
2581 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2582 else if (NumOps == 3)
2583 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2584 else
2585 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002586 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002587 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002588 if (NumOps == 1)
2589 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2590 else if (NumOps == 2)
2591 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2592 else if (NumOps == 3)
2593 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2594 else
2595 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002596 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002597 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002598 return SDOperand(N, 0);
2599}
2600
Dan Gohman08ce9762007-10-08 15:49:58 +00002601SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2602 return getNode(Opcode, VTList, 0, 0);
2603}
2604
2605SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2606 SDOperand N1) {
2607 SDOperand Ops[] = { N1 };
2608 return getNode(Opcode, VTList, Ops, 1);
2609}
2610
2611SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2612 SDOperand N1, SDOperand N2) {
2613 SDOperand Ops[] = { N1, N2 };
2614 return getNode(Opcode, VTList, Ops, 2);
2615}
2616
2617SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2618 SDOperand N1, SDOperand N2, SDOperand N3) {
2619 SDOperand Ops[] = { N1, N2, N3 };
2620 return getNode(Opcode, VTList, Ops, 3);
2621}
2622
2623SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2624 SDOperand N1, SDOperand N2, SDOperand N3,
2625 SDOperand N4) {
2626 SDOperand Ops[] = { N1, N2, N3, N4 };
2627 return getNode(Opcode, VTList, Ops, 4);
2628}
2629
2630SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2631 SDOperand N1, SDOperand N2, SDOperand N3,
2632 SDOperand N4, SDOperand N5) {
2633 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2634 return getNode(Opcode, VTList, Ops, 5);
2635}
2636
Chris Lattner70046e92006-08-15 17:46:01 +00002637SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Dan Gohman6595cb32007-06-27 16:08:04 +00002638 if (!MVT::isExtendedVT(VT))
Dan Gohman7f321562007-06-25 16:23:39 +00002639 return makeVTList(SDNode::getValueTypeList(VT), 1);
2640
2641 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2642 E = VTList.end(); I != E; ++I) {
2643 if (I->size() == 1 && (*I)[0] == VT)
2644 return makeVTList(&(*I)[0], 1);
2645 }
2646 std::vector<MVT::ValueType> V;
2647 V.push_back(VT);
2648 VTList.push_front(V);
2649 return makeVTList(&(*VTList.begin())[0], 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002650}
2651
Chris Lattner70046e92006-08-15 17:46:01 +00002652SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002653 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2654 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002655 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002656 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002657 }
2658 std::vector<MVT::ValueType> V;
2659 V.push_back(VT1);
2660 V.push_back(VT2);
2661 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002662 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002663}
Chris Lattner70046e92006-08-15 17:46:01 +00002664SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2665 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002666 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002667 E = VTList.end(); I != E; ++I) {
2668 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2669 (*I)[2] == VT3)
2670 return makeVTList(&(*I)[0], 3);
2671 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002672 std::vector<MVT::ValueType> V;
2673 V.push_back(VT1);
2674 V.push_back(VT2);
2675 V.push_back(VT3);
2676 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002677 return makeVTList(&(*VTList.begin())[0], 3);
2678}
2679
2680SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2681 switch (NumVTs) {
2682 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002683 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002684 case 2: return getVTList(VTs[0], VTs[1]);
2685 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2686 default: break;
2687 }
2688
2689 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2690 E = VTList.end(); I != E; ++I) {
2691 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2692
2693 bool NoMatch = false;
2694 for (unsigned i = 2; i != NumVTs; ++i)
2695 if (VTs[i] != (*I)[i]) {
2696 NoMatch = true;
2697 break;
2698 }
2699 if (!NoMatch)
2700 return makeVTList(&*I->begin(), NumVTs);
2701 }
2702
2703 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2704 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002705}
2706
2707
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002708/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2709/// specified operands. If the resultant node already exists in the DAG,
2710/// this does not modify the specified node, instead it returns the node that
2711/// already exists. If the resultant node does not exist in the DAG, the
2712/// input node is returned. As a degenerate case, if you specify the same
2713/// input operands as the node already has, the input node is returned.
2714SDOperand SelectionDAG::
2715UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2716 SDNode *N = InN.Val;
2717 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2718
2719 // Check to see if there is no change.
2720 if (Op == N->getOperand(0)) return InN;
2721
2722 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002723 void *InsertPos = 0;
2724 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2725 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002726
2727 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002728 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002729 RemoveNodeFromCSEMaps(N);
2730
2731 // Now we update the operands.
2732 N->OperandList[0].Val->removeUser(N);
2733 Op.Val->addUser(N);
2734 N->OperandList[0] = Op;
2735
2736 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002737 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002738 return InN;
2739}
2740
2741SDOperand SelectionDAG::
2742UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2743 SDNode *N = InN.Val;
2744 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2745
2746 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002747 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2748 return InN; // No operands changed, just return the input node.
2749
2750 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002751 void *InsertPos = 0;
2752 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2753 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002754
2755 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002756 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002757 RemoveNodeFromCSEMaps(N);
2758
2759 // Now we update the operands.
2760 if (N->OperandList[0] != Op1) {
2761 N->OperandList[0].Val->removeUser(N);
2762 Op1.Val->addUser(N);
2763 N->OperandList[0] = Op1;
2764 }
2765 if (N->OperandList[1] != Op2) {
2766 N->OperandList[1].Val->removeUser(N);
2767 Op2.Val->addUser(N);
2768 N->OperandList[1] = Op2;
2769 }
2770
2771 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002772 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002773 return InN;
2774}
2775
2776SDOperand SelectionDAG::
2777UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002778 SDOperand Ops[] = { Op1, Op2, Op3 };
2779 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002780}
2781
2782SDOperand SelectionDAG::
2783UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2784 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002785 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2786 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002787}
2788
2789SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002790UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2791 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002792 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2793 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002794}
2795
2796
2797SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002798UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002799 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002800 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002801 "Update with wrong number of operands");
2802
2803 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002804 bool AnyChange = false;
2805 for (unsigned i = 0; i != NumOps; ++i) {
2806 if (Ops[i] != N->getOperand(i)) {
2807 AnyChange = true;
2808 break;
2809 }
2810 }
2811
2812 // No operands changed, just return the input node.
2813 if (!AnyChange) return InN;
2814
2815 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002816 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002817 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002818 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002819
2820 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002821 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002822 RemoveNodeFromCSEMaps(N);
2823
2824 // Now we update the operands.
2825 for (unsigned i = 0; i != NumOps; ++i) {
2826 if (N->OperandList[i] != Ops[i]) {
2827 N->OperandList[i].Val->removeUser(N);
2828 Ops[i].Val->addUser(N);
2829 N->OperandList[i] = Ops[i];
2830 }
2831 }
2832
2833 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002834 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002835 return InN;
2836}
2837
2838
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002839/// MorphNodeTo - This frees the operands of the current node, resets the
2840/// opcode, types, and operands to the specified value. This should only be
2841/// used by the SelectionDAG class.
2842void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2843 const SDOperand *Ops, unsigned NumOps) {
2844 NodeType = Opc;
2845 ValueList = L.VTs;
2846 NumValues = L.NumVTs;
2847
2848 // Clear the operands list, updating used nodes to remove this from their
2849 // use list.
2850 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2851 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002852
2853 // If NumOps is larger than the # of operands we currently have, reallocate
2854 // the operand list.
2855 if (NumOps > NumOperands) {
2856 if (OperandsNeedDelete)
2857 delete [] OperandList;
2858 OperandList = new SDOperand[NumOps];
2859 OperandsNeedDelete = true;
2860 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002861
2862 // Assign the new operands.
2863 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002864
2865 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2866 OperandList[i] = Ops[i];
2867 SDNode *N = OperandList[i].Val;
2868 N->Uses.push_back(this);
2869 }
2870}
Chris Lattner149c58c2005-08-16 18:17:10 +00002871
2872/// SelectNodeTo - These are used for target selectors to *mutate* the
2873/// specified node to have the specified return type, Target opcode, and
2874/// operands. Note that target opcodes are stored as
2875/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002876///
2877/// Note that SelectNodeTo returns the resultant node. If there is already a
2878/// node of the specified opcode and operands, it returns that node instead of
2879/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002880SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2881 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002882 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002883 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002884 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002885 void *IP = 0;
2886 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002887 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002888
Chris Lattner7651fa42005-08-24 23:00:29 +00002889 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002890
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002891 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002892
Chris Lattner4a283e92006-08-11 18:38:11 +00002893 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002894 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002895}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002896
Evan Cheng95514ba2006-08-26 08:00:10 +00002897SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2898 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002899 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002900 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002901 SDOperand Ops[] = { Op1 };
2902
Jim Laskey583bd472006-10-27 23:46:08 +00002903 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002904 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002905 void *IP = 0;
2906 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002907 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002908
Chris Lattner149c58c2005-08-16 18:17:10 +00002909 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002910 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002911 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002912 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002913}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002914
Evan Cheng95514ba2006-08-26 08:00:10 +00002915SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2916 MVT::ValueType VT, SDOperand Op1,
2917 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002918 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002919 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002920 SDOperand Ops[] = { Op1, Op2 };
2921
Jim Laskey583bd472006-10-27 23:46:08 +00002922 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002923 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002924 void *IP = 0;
2925 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002926 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002927
Chris Lattner149c58c2005-08-16 18:17:10 +00002928 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002929
Chris Lattner63e3f142007-02-04 07:28:00 +00002930 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002931
Chris Lattnera5682852006-08-07 23:03:03 +00002932 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002933 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002934}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002935
Evan Cheng95514ba2006-08-26 08:00:10 +00002936SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2937 MVT::ValueType VT, SDOperand Op1,
2938 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002939 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002940 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002941 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002942 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002943 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002944 void *IP = 0;
2945 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002946 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002947
Chris Lattner149c58c2005-08-16 18:17:10 +00002948 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002949
Chris Lattner63e3f142007-02-04 07:28:00 +00002950 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002951
Chris Lattnera5682852006-08-07 23:03:03 +00002952 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002953 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002954}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002955
Evan Cheng95514ba2006-08-26 08:00:10 +00002956SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002957 MVT::ValueType VT, const SDOperand *Ops,
2958 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002959 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002960 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002961 FoldingSetNodeID ID;
2962 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002963 void *IP = 0;
2964 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002965 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002966
Chris Lattner6b09a292005-08-21 18:49:33 +00002967 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002969
Chris Lattnera5682852006-08-07 23:03:03 +00002970 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002971 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002972}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002973
Evan Cheng95514ba2006-08-26 08:00:10 +00002974SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2975 MVT::ValueType VT1, MVT::ValueType VT2,
2976 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002977 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002978 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002979 SDOperand Ops[] = { Op1, Op2 };
2980 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002981 void *IP = 0;
2982 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002983 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002984
Chris Lattner0fb094f2005-11-19 01:44:53 +00002985 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002986 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002987 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002988 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002989}
2990
Evan Cheng95514ba2006-08-26 08:00:10 +00002991SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2992 MVT::ValueType VT1, MVT::ValueType VT2,
2993 SDOperand Op1, SDOperand Op2,
2994 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002995 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002996 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00002997 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002998 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002999 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003000 void *IP = 0;
3001 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003002 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003003
Chris Lattner0fb094f2005-11-19 01:44:53 +00003004 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003005
Chris Lattner63e3f142007-02-04 07:28:00 +00003006 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003007 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003008 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003009}
3010
Chris Lattner0fb094f2005-11-19 01:44:53 +00003011
Evan Cheng6ae46c42006-02-09 07:15:23 +00003012/// getTargetNode - These are used for target selectors to create a new node
3013/// with specified return type(s), target opcode, and operands.
3014///
3015/// Note that getTargetNode returns the resultant node. If there is already a
3016/// node of the specified opcode and operands, it returns that node instead of
3017/// the current one.
3018SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3019 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3020}
3021SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3022 SDOperand Op1) {
3023 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3024}
3025SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3026 SDOperand Op1, SDOperand Op2) {
3027 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3028}
3029SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003030 SDOperand Op1, SDOperand Op2,
3031 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003032 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3033}
3034SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003035 const SDOperand *Ops, unsigned NumOps) {
3036 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003037}
3038SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003039 MVT::ValueType VT2) {
3040 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3041 SDOperand Op;
3042 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3043}
3044SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003045 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003046 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003047 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003048}
3049SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003050 MVT::ValueType VT2, SDOperand Op1,
3051 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003052 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003053 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003054 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003055}
3056SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003057 MVT::ValueType VT2, SDOperand Op1,
3058 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003059 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003060 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003061 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003062}
Evan Cheng694481e2006-08-27 08:08:54 +00003063SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3064 MVT::ValueType VT2,
3065 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003066 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003067 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003068}
3069SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3070 MVT::ValueType VT2, MVT::ValueType VT3,
3071 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003072 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003073 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003074 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003075}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003076SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3077 MVT::ValueType VT2, MVT::ValueType VT3,
3078 SDOperand Op1, SDOperand Op2,
3079 SDOperand Op3) {
3080 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3081 SDOperand Ops[] = { Op1, Op2, Op3 };
3082 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3083}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003084SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003085 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003086 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003087 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3088 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003089}
Evan Cheng05e69c12007-09-12 23:39:49 +00003090SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3091 MVT::ValueType VT2, MVT::ValueType VT3,
3092 MVT::ValueType VT4,
3093 const SDOperand *Ops, unsigned NumOps) {
3094 std::vector<MVT::ValueType> VTList;
3095 VTList.push_back(VT1);
3096 VTList.push_back(VT2);
3097 VTList.push_back(VT3);
3098 VTList.push_back(VT4);
3099 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3100 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3101}
Evan Cheng39305cf2007-10-05 01:10:49 +00003102SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3103 std::vector<MVT::ValueType> &ResultTys,
3104 const SDOperand *Ops, unsigned NumOps) {
3105 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3106 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3107 Ops, NumOps).Val;
3108}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003109
Evan Cheng99157a02006-08-07 22:13:29 +00003110/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003111/// This can cause recursive merging of nodes in the DAG.
3112///
Chris Lattner8b52f212005-08-26 18:36:28 +00003113/// This version assumes From/To have a single result value.
3114///
Chris Lattner1e111c72005-09-07 05:37:01 +00003115void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3116 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003117 SDNode *From = FromN.Val, *To = ToN.Val;
3118 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3119 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003120 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003121
Chris Lattner8b8749f2005-08-17 19:00:20 +00003122 while (!From->use_empty()) {
3123 // Process users until they are all gone.
3124 SDNode *U = *From->use_begin();
3125
3126 // This node is about to morph, remove its old self from the CSE maps.
3127 RemoveNodeFromCSEMaps(U);
3128
Chris Lattner65113b22005-11-08 22:07:03 +00003129 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3130 I != E; ++I)
3131 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003132 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003133 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003134 To->addUser(U);
3135 }
3136
3137 // Now that we have modified U, add it back to the CSE maps. If it already
3138 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003139 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3140 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003141 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003142 if (Deleted) Deleted->push_back(U);
3143 DeleteNodeNotInCSEMaps(U);
3144 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003145 }
3146}
3147
3148/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3149/// This can cause recursive merging of nodes in the DAG.
3150///
3151/// This version assumes From/To have matching types and numbers of result
3152/// values.
3153///
Chris Lattner1e111c72005-09-07 05:37:01 +00003154void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3155 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003156 assert(From != To && "Cannot replace uses of with self");
3157 assert(From->getNumValues() == To->getNumValues() &&
3158 "Cannot use this version of ReplaceAllUsesWith!");
3159 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003160 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003161 return;
3162 }
3163
3164 while (!From->use_empty()) {
3165 // Process users until they are all gone.
3166 SDNode *U = *From->use_begin();
3167
3168 // This node is about to morph, remove its old self from the CSE maps.
3169 RemoveNodeFromCSEMaps(U);
3170
Chris Lattner65113b22005-11-08 22:07:03 +00003171 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3172 I != E; ++I)
3173 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003174 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003175 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003176 To->addUser(U);
3177 }
3178
3179 // Now that we have modified U, add it back to the CSE maps. If it already
3180 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003181 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3182 ReplaceAllUsesWith(U, Existing, Deleted);
3183 // U is now dead.
3184 if (Deleted) Deleted->push_back(U);
3185 DeleteNodeNotInCSEMaps(U);
3186 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003187 }
3188}
3189
Chris Lattner8b52f212005-08-26 18:36:28 +00003190/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3191/// This can cause recursive merging of nodes in the DAG.
3192///
3193/// This version can replace From with any result values. To must match the
3194/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003195void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003196 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003197 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003198 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003199 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003200 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003201 return;
3202 }
3203
3204 while (!From->use_empty()) {
3205 // Process users until they are all gone.
3206 SDNode *U = *From->use_begin();
3207
3208 // This node is about to morph, remove its old self from the CSE maps.
3209 RemoveNodeFromCSEMaps(U);
3210
Chris Lattner65113b22005-11-08 22:07:03 +00003211 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3212 I != E; ++I)
3213 if (I->Val == From) {
3214 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003215 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003216 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003217 ToOp.Val->addUser(U);
3218 }
3219
3220 // Now that we have modified U, add it back to the CSE maps. If it already
3221 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003222 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3223 ReplaceAllUsesWith(U, Existing, Deleted);
3224 // U is now dead.
3225 if (Deleted) Deleted->push_back(U);
3226 DeleteNodeNotInCSEMaps(U);
3227 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003228 }
3229}
3230
Chris Lattner012f2412006-02-17 21:58:01 +00003231/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3232/// uses of other values produced by From.Val alone. The Deleted vector is
3233/// handled the same was as for ReplaceAllUsesWith.
3234void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3235 std::vector<SDNode*> &Deleted) {
3236 assert(From != To && "Cannot replace a value with itself");
3237 // Handle the simple, trivial, case efficiently.
3238 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3239 ReplaceAllUsesWith(From, To, &Deleted);
3240 return;
3241 }
3242
Chris Lattnercf5640b2007-02-04 00:14:31 +00003243 // Get all of the users of From.Val. We want these in a nice,
3244 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3245 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003246
3247 while (!Users.empty()) {
3248 // We know that this user uses some value of From. If it is the right
3249 // value, update it.
3250 SDNode *User = Users.back();
3251 Users.pop_back();
3252
3253 for (SDOperand *Op = User->OperandList,
3254 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
3255 if (*Op == From) {
3256 // Okay, we know this user needs to be updated. Remove its old self
3257 // from the CSE maps.
3258 RemoveNodeFromCSEMaps(User);
3259
3260 // Update all operands that match "From".
3261 for (; Op != E; ++Op) {
3262 if (*Op == From) {
3263 From.Val->removeUser(User);
3264 *Op = To;
3265 To.Val->addUser(User);
3266 }
3267 }
3268
3269 // Now that we have modified User, add it back to the CSE maps. If it
3270 // already exists there, recursively merge the results together.
3271 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
3272 unsigned NumDeleted = Deleted.size();
3273 ReplaceAllUsesWith(User, Existing, &Deleted);
3274
3275 // User is now dead.
3276 Deleted.push_back(User);
3277 DeleteNodeNotInCSEMaps(User);
3278
3279 // We have to be careful here, because ReplaceAllUsesWith could have
3280 // deleted a user of From, which means there may be dangling pointers
3281 // in the "Users" setvector. Scan over the deleted node pointers and
3282 // remove them from the setvector.
3283 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
3284 Users.remove(Deleted[i]);
3285 }
3286 break; // Exit the operand scanning loop.
3287 }
3288 }
3289 }
3290}
3291
Chris Lattner7b2880c2005-08-24 22:44:39 +00003292
Evan Chenge6f35d82006-08-01 08:20:41 +00003293/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3294/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003295unsigned SelectionDAG::AssignNodeIds() {
3296 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003297 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3298 SDNode *N = I;
3299 N->setNodeId(Id++);
3300 }
3301 return Id;
3302}
3303
Evan Chenge6f35d82006-08-01 08:20:41 +00003304/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003305/// based on their topological order. It returns the maximum id and a vector
3306/// of the SDNodes* in assigned order by reference.
3307unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003308 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003309 std::vector<unsigned> InDegree(DAGSize);
3310 std::vector<SDNode*> Sources;
3311
3312 // Use a two pass approach to avoid using a std::map which is slow.
3313 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003314 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3315 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003316 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003317 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003318 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003319 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003320 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003321 }
3322
Evan Cheng99157a02006-08-07 22:13:29 +00003323 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003324 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003325 SDNode *N = Sources.back();
3326 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003327 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003328 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3329 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003330 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003331 if (Degree == 0)
3332 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003333 }
3334 }
3335
Evan Chengc384d6c2006-08-02 22:00:34 +00003336 // Second pass, assign the actual topological order as node ids.
3337 Id = 0;
3338 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3339 TI != TE; ++TI)
3340 (*TI)->setNodeId(Id++);
3341
3342 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003343}
3344
3345
Evan Cheng091cba12006-07-27 06:39:06 +00003346
Jim Laskey58b968b2005-08-17 20:08:02 +00003347//===----------------------------------------------------------------------===//
3348// SDNode Class
3349//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003350
Chris Lattner917d2c92006-07-19 00:00:37 +00003351// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003352void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003353void UnarySDNode::ANCHOR() {}
3354void BinarySDNode::ANCHOR() {}
3355void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003356void HandleSDNode::ANCHOR() {}
3357void StringSDNode::ANCHOR() {}
3358void ConstantSDNode::ANCHOR() {}
3359void ConstantFPSDNode::ANCHOR() {}
3360void GlobalAddressSDNode::ANCHOR() {}
3361void FrameIndexSDNode::ANCHOR() {}
3362void JumpTableSDNode::ANCHOR() {}
3363void ConstantPoolSDNode::ANCHOR() {}
3364void BasicBlockSDNode::ANCHOR() {}
3365void SrcValueSDNode::ANCHOR() {}
3366void RegisterSDNode::ANCHOR() {}
3367void ExternalSymbolSDNode::ANCHOR() {}
3368void CondCodeSDNode::ANCHOR() {}
3369void VTSDNode::ANCHOR() {}
3370void LoadSDNode::ANCHOR() {}
3371void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003372
Chris Lattner48b85922007-02-04 02:41:42 +00003373HandleSDNode::~HandleSDNode() {
3374 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003375 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003376}
3377
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003378GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3379 MVT::ValueType VT, int o)
3380 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003381 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003382 // Thread Local
3383 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3384 // Non Thread Local
3385 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3386 getSDVTList(VT)), Offset(o) {
3387 TheGlobal = const_cast<GlobalValue*>(GA);
3388}
Chris Lattner48b85922007-02-04 02:41:42 +00003389
Jim Laskey583bd472006-10-27 23:46:08 +00003390/// Profile - Gather unique data for the node.
3391///
3392void SDNode::Profile(FoldingSetNodeID &ID) {
3393 AddNodeIDNode(ID, this);
3394}
3395
Chris Lattnera3255112005-11-08 23:30:28 +00003396/// getValueTypeList - Return a pointer to the specified value type.
3397///
3398MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3399 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3400 VTs[VT] = VT;
3401 return &VTs[VT];
3402}
Chris Lattnera5682852006-08-07 23:03:03 +00003403
Chris Lattner5c884562005-01-12 18:37:47 +00003404/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3405/// indicated value. This method ignores uses of other values defined by this
3406/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003407bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003408 assert(Value < getNumValues() && "Bad value!");
3409
3410 // If there is only one value, this is easy.
3411 if (getNumValues() == 1)
3412 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003413 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003414
Evan Cheng4ee62112006-02-05 06:29:23 +00003415 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003416
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003417 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003418
Chris Lattnerf83482d2006-08-16 20:59:32 +00003419 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003420 SDNode *User = *UI;
3421 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003422 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003423 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3424 if (User->getOperand(i) == TheValue) {
3425 if (NUses == 0)
3426 return false; // too many uses
3427 --NUses;
3428 }
3429 }
3430
3431 // Found exactly the right number of uses?
3432 return NUses == 0;
3433}
3434
3435
Evan Cheng33d55952007-08-02 05:29:38 +00003436/// hasAnyUseOfValue - Return true if there are any use of the indicated
3437/// value. This method ignores uses of other values defined by this operation.
3438bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3439 assert(Value < getNumValues() && "Bad value!");
3440
3441 if (use_size() == 0) return false;
3442
3443 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3444
3445 SmallPtrSet<SDNode*, 32> UsersHandled;
3446
3447 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3448 SDNode *User = *UI;
3449 if (User->getNumOperands() == 1 ||
3450 UsersHandled.insert(User)) // First time we've seen this?
3451 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3452 if (User->getOperand(i) == TheValue) {
3453 return true;
3454 }
3455 }
3456
3457 return false;
3458}
3459
3460
Evan Chenge6e97e62006-11-03 07:31:32 +00003461/// isOnlyUse - Return true if this node is the only use of N.
3462///
Evan Cheng4ee62112006-02-05 06:29:23 +00003463bool SDNode::isOnlyUse(SDNode *N) const {
3464 bool Seen = false;
3465 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3466 SDNode *User = *I;
3467 if (User == this)
3468 Seen = true;
3469 else
3470 return false;
3471 }
3472
3473 return Seen;
3474}
3475
Evan Chenge6e97e62006-11-03 07:31:32 +00003476/// isOperand - Return true if this node is an operand of N.
3477///
Evan Chengbfa284f2006-03-03 06:42:32 +00003478bool SDOperand::isOperand(SDNode *N) const {
3479 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3480 if (*this == N->getOperand(i))
3481 return true;
3482 return false;
3483}
3484
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003485bool SDNode::isOperand(SDNode *N) const {
3486 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3487 if (this == N->OperandList[i].Val)
3488 return true;
3489 return false;
3490}
Evan Cheng4ee62112006-02-05 06:29:23 +00003491
Evan Chengc5fc57d2006-11-03 03:05:24 +00003492static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003493 SmallPtrSet<SDNode *, 32> &Visited) {
3494 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003495 return;
3496
3497 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3498 SDNode *Op = N->getOperand(i).Val;
3499 if (Op == P) {
3500 found = true;
3501 return;
3502 }
3503 findPredecessor(Op, P, found, Visited);
3504 }
3505}
3506
Evan Chenge6e97e62006-11-03 07:31:32 +00003507/// isPredecessor - Return true if this node is a predecessor of N. This node
3508/// is either an operand of N or it can be reached by recursively traversing
3509/// up the operands.
3510/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003511bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003512 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003513 bool found = false;
3514 findPredecessor(N, this, found, Visited);
3515 return found;
3516}
3517
Evan Chengc5484282006-10-04 00:56:09 +00003518uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3519 assert(Num < NumOperands && "Invalid child # of SDNode!");
3520 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3521}
3522
Reid Spencer577cc322007-04-01 07:32:19 +00003523std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003524 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003525 default:
3526 if (getOpcode() < ISD::BUILTIN_OP_END)
3527 return "<<Unknown DAG Node>>";
3528 else {
Evan Cheng72261582005-12-20 06:22:03 +00003529 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003530 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003531 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3532 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003533
Evan Cheng72261582005-12-20 06:22:03 +00003534 TargetLowering &TLI = G->getTargetLoweringInfo();
3535 const char *Name =
3536 TLI.getTargetNodeName(getOpcode());
3537 if (Name) return Name;
3538 }
3539
3540 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003541 }
3542
Andrew Lenharth95762122005-03-31 21:24:06 +00003543 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003544 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003545 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003546 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003547 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003548 case ISD::AssertSext: return "AssertSext";
3549 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003550
3551 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003552 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003553 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003554 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003555
3556 case ISD::Constant: return "Constant";
3557 case ISD::ConstantFP: return "ConstantFP";
3558 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003559 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003560 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003561 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003562 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003563 case ISD::RETURNADDR: return "RETURNADDR";
3564 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003565 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003566 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3567 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003568 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003569 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003570 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003571 case ISD::INTRINSIC_WO_CHAIN: {
3572 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3573 return Intrinsic::getName((Intrinsic::ID)IID);
3574 }
3575 case ISD::INTRINSIC_VOID:
3576 case ISD::INTRINSIC_W_CHAIN: {
3577 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003578 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003579 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003580
Chris Lattnerb2827b02006-03-19 00:52:58 +00003581 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003582 case ISD::TargetConstant: return "TargetConstant";
3583 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003584 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003585 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003586 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003587 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003588 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003589 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003590
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003591 case ISD::CopyToReg: return "CopyToReg";
3592 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003593 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003594 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003595 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003596 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003597 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003598 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003599 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003600
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003601 // Unary operators
3602 case ISD::FABS: return "fabs";
3603 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003604 case ISD::FSQRT: return "fsqrt";
3605 case ISD::FSIN: return "fsin";
3606 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003607 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003608 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003609
3610 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003611 case ISD::ADD: return "add";
3612 case ISD::SUB: return "sub";
3613 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003614 case ISD::MULHU: return "mulhu";
3615 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003616 case ISD::SDIV: return "sdiv";
3617 case ISD::UDIV: return "udiv";
3618 case ISD::SREM: return "srem";
3619 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003620 case ISD::SMUL_LOHI: return "smul_lohi";
3621 case ISD::UMUL_LOHI: return "umul_lohi";
3622 case ISD::SDIVREM: return "sdivrem";
3623 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003624 case ISD::AND: return "and";
3625 case ISD::OR: return "or";
3626 case ISD::XOR: return "xor";
3627 case ISD::SHL: return "shl";
3628 case ISD::SRA: return "sra";
3629 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003630 case ISD::ROTL: return "rotl";
3631 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003632 case ISD::FADD: return "fadd";
3633 case ISD::FSUB: return "fsub";
3634 case ISD::FMUL: return "fmul";
3635 case ISD::FDIV: return "fdiv";
3636 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003637 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003638
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003639 case ISD::SETCC: return "setcc";
3640 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003641 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003642 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003643 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003644 case ISD::CONCAT_VECTORS: return "concat_vectors";
3645 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003646 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003647 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003648 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003649 case ISD::ADDC: return "addc";
3650 case ISD::ADDE: return "adde";
3651 case ISD::SUBC: return "subc";
3652 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003653 case ISD::SHL_PARTS: return "shl_parts";
3654 case ISD::SRA_PARTS: return "sra_parts";
3655 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003656
3657 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3658 case ISD::INSERT_SUBREG: return "insert_subreg";
3659
Chris Lattner7f644642005-04-28 21:44:03 +00003660 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003661 case ISD::SIGN_EXTEND: return "sign_extend";
3662 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003663 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003664 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003665 case ISD::TRUNCATE: return "truncate";
3666 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00003667 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003668 case ISD::FP_EXTEND: return "fp_extend";
3669
3670 case ISD::SINT_TO_FP: return "sint_to_fp";
3671 case ISD::UINT_TO_FP: return "uint_to_fp";
3672 case ISD::FP_TO_SINT: return "fp_to_sint";
3673 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003674 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003675
3676 // Control flow instructions
3677 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003678 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003679 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003680 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003681 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003682 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003683 case ISD::CALLSEQ_START: return "callseq_start";
3684 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003685
3686 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003687 case ISD::LOAD: return "load";
3688 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003689 case ISD::VAARG: return "vaarg";
3690 case ISD::VACOPY: return "vacopy";
3691 case ISD::VAEND: return "vaend";
3692 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003693 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003694 case ISD::EXTRACT_ELEMENT: return "extract_element";
3695 case ISD::BUILD_PAIR: return "build_pair";
3696 case ISD::STACKSAVE: return "stacksave";
3697 case ISD::STACKRESTORE: return "stackrestore";
3698
3699 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003700 case ISD::MEMSET: return "memset";
3701 case ISD::MEMCPY: return "memcpy";
3702 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003703
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003704 // Bit manipulation
3705 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003706 case ISD::CTPOP: return "ctpop";
3707 case ISD::CTTZ: return "cttz";
3708 case ISD::CTLZ: return "ctlz";
3709
Chris Lattner36ce6912005-11-29 06:21:05 +00003710 // Debug info
3711 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003712 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003713
Duncan Sands36397f52007-07-27 12:58:54 +00003714 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003715 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003716
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003717 case ISD::CONDCODE:
3718 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003719 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003720 case ISD::SETOEQ: return "setoeq";
3721 case ISD::SETOGT: return "setogt";
3722 case ISD::SETOGE: return "setoge";
3723 case ISD::SETOLT: return "setolt";
3724 case ISD::SETOLE: return "setole";
3725 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003726
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003727 case ISD::SETO: return "seto";
3728 case ISD::SETUO: return "setuo";
3729 case ISD::SETUEQ: return "setue";
3730 case ISD::SETUGT: return "setugt";
3731 case ISD::SETUGE: return "setuge";
3732 case ISD::SETULT: return "setult";
3733 case ISD::SETULE: return "setule";
3734 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003735
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003736 case ISD::SETEQ: return "seteq";
3737 case ISD::SETGT: return "setgt";
3738 case ISD::SETGE: return "setge";
3739 case ISD::SETLT: return "setlt";
3740 case ISD::SETLE: return "setle";
3741 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003742 }
3743 }
3744}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003745
Evan Cheng144d8f02006-11-09 17:55:04 +00003746const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003747 switch (AM) {
3748 default:
3749 return "";
3750 case ISD::PRE_INC:
3751 return "<pre-inc>";
3752 case ISD::PRE_DEC:
3753 return "<pre-dec>";
3754 case ISD::POST_INC:
3755 return "<post-inc>";
3756 case ISD::POST_DEC:
3757 return "<post-dec>";
3758 }
3759}
3760
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003761void SDNode::dump() const { dump(0); }
3762void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003763 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003764
3765 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003766 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003767 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003768 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003769 else
Bill Wendling832171c2006-12-07 20:04:42 +00003770 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003771 }
Bill Wendling832171c2006-12-07 20:04:42 +00003772 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003773
Bill Wendling832171c2006-12-07 20:04:42 +00003774 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003775 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003776 if (i) cerr << ", ";
3777 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003778 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003779 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003780 }
3781
3782 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003783 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003784 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003785 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3786 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3787 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3788 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3789 else {
3790 cerr << "<APFloat(";
3791 CSDN->getValueAPF().convertToAPInt().dump();
3792 cerr << ")>";
3793 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003794 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003795 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003796 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003797 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003798 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003799 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003800 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003801 else
Bill Wendling832171c2006-12-07 20:04:42 +00003802 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003803 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003804 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003805 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003806 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003807 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003808 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003809 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003810 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003811 else
Bill Wendling832171c2006-12-07 20:04:42 +00003812 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003813 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003814 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003815 else
Bill Wendling832171c2006-12-07 20:04:42 +00003816 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003817 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003818 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003819 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3820 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003821 cerr << LBB->getName() << " ";
3822 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003823 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003824 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003825 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003826 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003827 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003828 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003829 } else if (const ExternalSymbolSDNode *ES =
3830 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003831 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003832 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3833 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003834 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003835 else
Bill Wendling832171c2006-12-07 20:04:42 +00003836 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003837 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003838 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003839 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3840 bool doExt = true;
3841 switch (LD->getExtensionType()) {
3842 default: doExt = false; break;
3843 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003844 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003845 break;
3846 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003847 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003848 break;
3849 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003850 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003851 break;
3852 }
3853 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003854 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003855
Evan Cheng144d8f02006-11-09 17:55:04 +00003856 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003857 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003858 cerr << " " << AM;
Evan Cheng2caccca2006-10-17 21:14:32 +00003859 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3860 if (ST->isTruncatingStore())
Bill Wendling832171c2006-12-07 20:04:42 +00003861 cerr << " <trunc "
3862 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00003863
Evan Cheng144d8f02006-11-09 17:55:04 +00003864 const char *AM = getIndexedModeName(ST->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003865 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003866 cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003867 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003868}
3869
Chris Lattnerde202b32005-11-09 23:47:37 +00003870static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003871 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3872 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003873 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003874 else
Bill Wendling832171c2006-12-07 20:04:42 +00003875 cerr << "\n" << std::string(indent+2, ' ')
3876 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003877
Chris Lattnerea946cd2005-01-09 20:38:33 +00003878
Bill Wendling832171c2006-12-07 20:04:42 +00003879 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003880 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003881}
3882
Chris Lattnerc3aae252005-01-07 07:46:32 +00003883void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003884 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003885 std::vector<const SDNode*> Nodes;
3886 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3887 I != E; ++I)
3888 Nodes.push_back(I);
3889
Chris Lattner49d24712005-01-09 20:26:36 +00003890 std::sort(Nodes.begin(), Nodes.end());
3891
3892 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003893 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003894 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003895 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003896
Jim Laskey26f7fa72006-10-17 19:33:52 +00003897 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003898
Bill Wendling832171c2006-12-07 20:04:42 +00003899 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003900}
3901
Evan Chengd6594ae2006-09-12 21:00:35 +00003902const Type *ConstantPoolSDNode::getType() const {
3903 if (isMachineConstantPoolEntry())
3904 return Val.MachineCPVal->getType();
3905 return Val.ConstVal->getType();
3906}