blob: 05d5ec0ec20417d940ad91ad5f1ad7bb5dd06b51 [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 Johanneseneaf08942007-08-31 04:03:46 +0000112 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->
113 getValueAPF().convertToDouble()) !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000114 (uint64_t)-1)
115 return false;
116 } else {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000117 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->
118 getValueAPF().convertToFloat()) !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000119 (uint32_t)-1)
120 return false;
121 }
Evan Chenga8df1662006-03-27 06:58:47 +0000122 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000123 return false;
124
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
130 return false;
131 return true;
132}
133
134
Evan Cheng4a147842006-03-26 09:50:58 +0000135/// isBuildVectorAllZeros - Return true if the specified node is a
136/// BUILD_VECTOR where all of the elements are 0 or undef.
137bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
140 N = N->getOperand(0).Val;
141
Evan Cheng4a147842006-03-26 09:50:58 +0000142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000143
144 unsigned i = 0, e = N->getNumOperands();
145
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
148 ++i;
149
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
152
153 // Do not accept build_vectors that aren't all constants or which have non-~0
154 // elements.
155 SDOperand Zero = N->getOperand(i);
156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 return false;
159 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000161 return false;
162 } else
163 return false;
164
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 // undefs.
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
170 return false;
171 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000172}
173
Chris Lattnerc3aae252005-01-07 07:46:32 +0000174/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
175/// when given the operation for (X op Y).
176ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
177 // To perform this operation, we just need to swap the L and G bits of the
178 // operation.
179 unsigned OldL = (Operation >> 2) & 1;
180 unsigned OldG = (Operation >> 1) & 1;
181 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
182 (OldL << 1) | // New G bit
183 (OldG << 2)); // New L bit.
184}
185
186/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
187/// 'op' is a valid SetCC operation.
188ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
189 unsigned Operation = Op;
190 if (isInteger)
191 Operation ^= 7; // Flip L, G, E bits, but not U.
192 else
193 Operation ^= 15; // Flip all of the condition bits.
194 if (Operation > ISD::SETTRUE2)
195 Operation &= ~8; // Don't let N and U bits get set.
196 return ISD::CondCode(Operation);
197}
198
199
200/// isSignedOp - For an integer comparison, return 1 if the comparison is a
201/// signed operation and 2 if the result is an unsigned comparison. Return zero
202/// if the operation does not depend on the sign of the input (setne and seteq).
203static int isSignedOp(ISD::CondCode Opcode) {
204 switch (Opcode) {
205 default: assert(0 && "Illegal integer setcc operation!");
206 case ISD::SETEQ:
207 case ISD::SETNE: return 0;
208 case ISD::SETLT:
209 case ISD::SETLE:
210 case ISD::SETGT:
211 case ISD::SETGE: return 1;
212 case ISD::SETULT:
213 case ISD::SETULE:
214 case ISD::SETUGT:
215 case ISD::SETUGE: return 2;
216 }
217}
218
219/// getSetCCOrOperation - Return the result of a logical OR between different
220/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
221/// returns SETCC_INVALID if it is not possible to represent the resultant
222/// comparison.
223ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
224 bool isInteger) {
225 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
226 // Cannot fold a signed integer setcc with an unsigned integer setcc.
227 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000228
Chris Lattnerc3aae252005-01-07 07:46:32 +0000229 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000230
Chris Lattnerc3aae252005-01-07 07:46:32 +0000231 // If the N and U bits get set then the resultant comparison DOES suddenly
232 // care about orderedness, and is true when ordered.
233 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000234 Op &= ~16; // Clear the U bit if the N bit is set.
235
236 // Canonicalize illegal integer setcc's.
237 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
238 Op = ISD::SETNE;
239
Chris Lattnerc3aae252005-01-07 07:46:32 +0000240 return ISD::CondCode(Op);
241}
242
243/// getSetCCAndOperation - Return the result of a logical AND between different
244/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
245/// function returns zero if it is not possible to represent the resultant
246/// comparison.
247ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
248 bool isInteger) {
249 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
250 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000251 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000252
253 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000254 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
255
256 // Canonicalize illegal integer setcc's.
257 if (isInteger) {
258 switch (Result) {
259 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000260 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
261 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
262 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
263 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000264 }
265 }
266
267 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000268}
269
Chris Lattnerb48da392005-01-23 04:39:44 +0000270const TargetMachine &SelectionDAG::getTarget() const {
271 return TLI.getTargetMachine();
272}
273
Jim Laskey58b968b2005-08-17 20:08:02 +0000274//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000275// SDNode Profile Support
276//===----------------------------------------------------------------------===//
277
Jim Laskeydef69b92006-10-27 23:52:51 +0000278/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
279///
Jim Laskey583bd472006-10-27 23:46:08 +0000280static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
281 ID.AddInteger(OpC);
282}
283
284/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
285/// solely with their pointer.
286void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
287 ID.AddPointer(VTList.VTs);
288}
289
Jim Laskeydef69b92006-10-27 23:52:51 +0000290/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
291///
Jim Laskey583bd472006-10-27 23:46:08 +0000292static void AddNodeIDOperands(FoldingSetNodeID &ID,
293 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000294 for (; NumOps; --NumOps, ++Ops) {
295 ID.AddPointer(Ops->Val);
296 ID.AddInteger(Ops->ResNo);
297 }
Jim Laskey583bd472006-10-27 23:46:08 +0000298}
299
Jim Laskey583bd472006-10-27 23:46:08 +0000300static void AddNodeIDNode(FoldingSetNodeID &ID,
301 unsigned short OpC, SDVTList VTList,
302 const SDOperand *OpList, unsigned N) {
303 AddNodeIDOpcode(ID, OpC);
304 AddNodeIDValueTypes(ID, VTList);
305 AddNodeIDOperands(ID, OpList, N);
306}
307
Jim Laskeydef69b92006-10-27 23:52:51 +0000308/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
309/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000310static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
311 AddNodeIDOpcode(ID, N->getOpcode());
312 // Add the return value info.
313 AddNodeIDValueTypes(ID, N->getVTList());
314 // Add the operand info.
315 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
316
317 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000318 switch (N->getOpcode()) {
319 default: break; // Normal nodes don't need extra info.
320 case ISD::TargetConstant:
321 case ISD::Constant:
322 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
323 break;
324 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000325 case ISD::ConstantFP: {
326 APFloat V = cast<ConstantFPSDNode>(N)->getValueAPF();
327 if (&V.getSemantics() == &APFloat::IEEEdouble)
328 ID.AddDouble(V.convertToDouble());
329 else if (&V.getSemantics() == &APFloat::IEEEsingle)
330 ID.AddDouble((double)V.convertToFloat());
331 else
332 assert(0);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000333 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000334 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000335 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000336 case ISD::GlobalAddress:
337 case ISD::TargetGlobalTLSAddress:
338 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000339 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
340 ID.AddPointer(GA->getGlobal());
341 ID.AddInteger(GA->getOffset());
342 break;
343 }
344 case ISD::BasicBlock:
345 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
346 break;
347 case ISD::Register:
348 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
349 break;
350 case ISD::SRCVALUE: {
351 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
352 ID.AddPointer(SV->getValue());
353 ID.AddInteger(SV->getOffset());
354 break;
355 }
356 case ISD::FrameIndex:
357 case ISD::TargetFrameIndex:
358 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
359 break;
360 case ISD::JumpTable:
361 case ISD::TargetJumpTable:
362 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
363 break;
364 case ISD::ConstantPool:
365 case ISD::TargetConstantPool: {
366 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
367 ID.AddInteger(CP->getAlignment());
368 ID.AddInteger(CP->getOffset());
369 if (CP->isMachineConstantPoolEntry())
370 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
371 else
372 ID.AddPointer(CP->getConstVal());
373 break;
374 }
375 case ISD::LOAD: {
376 LoadSDNode *LD = cast<LoadSDNode>(N);
377 ID.AddInteger(LD->getAddressingMode());
378 ID.AddInteger(LD->getExtensionType());
379 ID.AddInteger(LD->getLoadedVT());
380 ID.AddPointer(LD->getSrcValue());
381 ID.AddInteger(LD->getSrcValueOffset());
382 ID.AddInteger(LD->getAlignment());
383 ID.AddInteger(LD->isVolatile());
384 break;
385 }
386 case ISD::STORE: {
387 StoreSDNode *ST = cast<StoreSDNode>(N);
388 ID.AddInteger(ST->getAddressingMode());
389 ID.AddInteger(ST->isTruncatingStore());
390 ID.AddInteger(ST->getStoredVT());
391 ID.AddPointer(ST->getSrcValue());
392 ID.AddInteger(ST->getSrcValueOffset());
393 ID.AddInteger(ST->getAlignment());
394 ID.AddInteger(ST->isVolatile());
395 break;
396 }
Jim Laskey583bd472006-10-27 23:46:08 +0000397 }
398}
399
400//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000401// SelectionDAG Class
402//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000403
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000404/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000405/// SelectionDAG.
406void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000407 // Create a dummy node (which is not added to allnodes), that adds a reference
408 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000409 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000410
Chris Lattner190a4182006-08-04 17:45:20 +0000411 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000412
Chris Lattner190a4182006-08-04 17:45:20 +0000413 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000414 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000415 if (I->use_empty())
416 DeadNodes.push_back(I);
417
418 // Process the worklist, deleting the nodes and adding their uses to the
419 // worklist.
420 while (!DeadNodes.empty()) {
421 SDNode *N = DeadNodes.back();
422 DeadNodes.pop_back();
423
424 // Take the node out of the appropriate CSE map.
425 RemoveNodeFromCSEMaps(N);
426
427 // Next, brutally remove the operand list. This is safe to do, as there are
428 // no cycles in the graph.
429 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
430 SDNode *Operand = I->Val;
431 Operand->removeUser(N);
432
433 // Now that we removed this operand, see if there are no uses of it left.
434 if (Operand->use_empty())
435 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000436 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000437 if (N->OperandsNeedDelete)
438 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000439 N->OperandList = 0;
440 N->NumOperands = 0;
441
442 // Finally, remove N itself.
443 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000444 }
445
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000446 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000447 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000448}
449
Evan Cheng130a6472006-10-12 20:34:05 +0000450void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
451 SmallVector<SDNode*, 16> DeadNodes;
452 DeadNodes.push_back(N);
453
454 // Process the worklist, deleting the nodes and adding their uses to the
455 // worklist.
456 while (!DeadNodes.empty()) {
457 SDNode *N = DeadNodes.back();
458 DeadNodes.pop_back();
459
460 // Take the node out of the appropriate CSE map.
461 RemoveNodeFromCSEMaps(N);
462
463 // Next, brutally remove the operand list. This is safe to do, as there are
464 // no cycles in the graph.
465 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
466 SDNode *Operand = I->Val;
467 Operand->removeUser(N);
468
469 // Now that we removed this operand, see if there are no uses of it left.
470 if (Operand->use_empty())
471 DeadNodes.push_back(Operand);
472 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000473 if (N->OperandsNeedDelete)
474 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000475 N->OperandList = 0;
476 N->NumOperands = 0;
477
478 // Finally, remove N itself.
479 Deleted.push_back(N);
480 AllNodes.erase(N);
481 }
482}
483
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000484void SelectionDAG::DeleteNode(SDNode *N) {
485 assert(N->use_empty() && "Cannot delete a node that is not dead!");
486
487 // First take this out of the appropriate CSE map.
488 RemoveNodeFromCSEMaps(N);
489
Chris Lattner1e111c72005-09-07 05:37:01 +0000490 // Finally, remove uses due to operands of this node, remove from the
491 // AllNodes list, and delete the node.
492 DeleteNodeNotInCSEMaps(N);
493}
494
495void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
496
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000497 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000498 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000499
500 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000501 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
502 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000503 if (N->OperandsNeedDelete)
504 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000505 N->OperandList = 0;
506 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000507
508 delete N;
509}
510
Chris Lattner149c58c2005-08-16 18:17:10 +0000511/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
512/// correspond to it. This is useful when we're about to delete or repurpose
513/// the node. We don't want future request for structurally identical nodes
514/// to return N anymore.
515void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000516 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000517 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000518 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000519 case ISD::STRING:
520 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
521 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000522 case ISD::CONDCODE:
523 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
524 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000525 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000526 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
527 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000528 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000529 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000530 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000531 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000532 Erased =
533 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000534 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000535 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000536 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000537 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
538 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000539 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000540 // Remove it from the CSE Map.
541 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000542 break;
543 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000544#ifndef NDEBUG
545 // Verify that the node was actually in one of the CSE maps, unless it has a
546 // flag result (which cannot be CSE'd) or is one of the special cases that are
547 // not subject to CSE.
548 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000549 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000550 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000551 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000552 assert(0 && "Node is not in map!");
553 }
554#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000555}
556
Chris Lattner8b8749f2005-08-17 19:00:20 +0000557/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
558/// has been taken out and modified in some way. If the specified node already
559/// exists in the CSE maps, do not modify the maps, but return the existing node
560/// instead. If it doesn't exist, add it and return null.
561///
562SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
563 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000564 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000565 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000566
Chris Lattner9f8cc692005-12-19 22:21:21 +0000567 // Check that remaining values produced are not flags.
568 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
569 if (N->getValueType(i) == MVT::Flag)
570 return 0; // Never CSE anything that produces a flag.
571
Chris Lattnera5682852006-08-07 23:03:03 +0000572 SDNode *New = CSEMap.GetOrInsertNode(N);
573 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000574 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000575}
576
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000577/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
578/// were replaced with those specified. If this node is never memoized,
579/// return null, otherwise return a pointer to the slot it would take. If a
580/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000581SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
582 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000583 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000584 return 0; // Never add these nodes.
585
586 // Check that remaining values produced are not flags.
587 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
588 if (N->getValueType(i) == MVT::Flag)
589 return 0; // Never CSE anything that produces a flag.
590
Chris Lattner63e3f142007-02-04 07:28:00 +0000591 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000592 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000593 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000594 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000595}
596
597/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
598/// were replaced with those specified. If this node is never memoized,
599/// return null, otherwise return a pointer to the slot it would take. If a
600/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000601SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
602 SDOperand Op1, SDOperand Op2,
603 void *&InsertPos) {
604 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
605 return 0; // Never add these nodes.
606
607 // Check that remaining values produced are not flags.
608 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
609 if (N->getValueType(i) == MVT::Flag)
610 return 0; // Never CSE anything that produces a flag.
611
Chris Lattner63e3f142007-02-04 07:28:00 +0000612 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000613 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000614 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000615 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
616}
617
618
619/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
620/// were replaced with those specified. If this node is never memoized,
621/// return null, otherwise return a pointer to the slot it would take. If a
622/// node already exists with these operands, the slot will be non-null.
623SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000624 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000625 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000626 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000627 return 0; // Never add these nodes.
628
629 // Check that remaining values produced are not flags.
630 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
631 if (N->getValueType(i) == MVT::Flag)
632 return 0; // Never CSE anything that produces a flag.
633
Jim Laskey583bd472006-10-27 23:46:08 +0000634 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000635 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000636
Evan Cheng9629aba2006-10-11 01:47:58 +0000637 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
638 ID.AddInteger(LD->getAddressingMode());
639 ID.AddInteger(LD->getExtensionType());
Evan Cheng2e49f092006-10-11 07:10:22 +0000640 ID.AddInteger(LD->getLoadedVT());
Evan Cheng9629aba2006-10-11 01:47:58 +0000641 ID.AddPointer(LD->getSrcValue());
642 ID.AddInteger(LD->getSrcValueOffset());
643 ID.AddInteger(LD->getAlignment());
644 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000645 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
646 ID.AddInteger(ST->getAddressingMode());
647 ID.AddInteger(ST->isTruncatingStore());
648 ID.AddInteger(ST->getStoredVT());
649 ID.AddPointer(ST->getSrcValue());
650 ID.AddInteger(ST->getSrcValueOffset());
651 ID.AddInteger(ST->getAlignment());
652 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000653 }
Jim Laskey583bd472006-10-27 23:46:08 +0000654
Chris Lattnera5682852006-08-07 23:03:03 +0000655 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000656}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000657
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000658
Chris Lattner78ec3112003-08-11 14:57:33 +0000659SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000660 while (!AllNodes.empty()) {
661 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000662 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000663 if (N->OperandsNeedDelete)
664 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000665 N->OperandList = 0;
666 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000667 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000668 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000669}
670
Chris Lattner0f2287b2005-04-13 02:38:18 +0000671SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000672 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000673 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000674 return getNode(ISD::AND, Op.getValueType(), Op,
675 getConstant(Imm, Op.getValueType()));
676}
677
Chris Lattner36ce6912005-11-29 06:21:05 +0000678SDOperand SelectionDAG::getString(const std::string &Val) {
679 StringSDNode *&N = StringNodes[Val];
680 if (!N) {
681 N = new StringSDNode(Val);
682 AllNodes.push_back(N);
683 }
684 return SDOperand(N, 0);
685}
686
Chris Lattner61b09412006-08-11 21:01:22 +0000687SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000688 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000689 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000690
Chris Lattner61b09412006-08-11 21:01:22 +0000691 // Mask out any bits that are not valid for this constant.
692 Val &= MVT::getIntVTBitMask(VT);
693
694 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000695 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000696 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000697 ID.AddInteger(Val);
698 void *IP = 0;
699 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
700 return SDOperand(E, 0);
701 SDNode *N = new ConstantSDNode(isT, Val, VT);
702 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000703 AllNodes.push_back(N);
704 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000705}
706
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000707SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000708 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000709 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000710
Dan Gohman7f321562007-06-25 16:23:39 +0000711 MVT::ValueType EltVT =
712 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000713 bool isDouble = (EltVT == MVT::f64);
714 double Val = isDouble ? V.convertToDouble() : (double)V.convertToFloat();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000715
Chris Lattnerd8658612005-02-17 20:17:32 +0000716 // Do the map lookup using the actual bit pattern for the floating point
717 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
718 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000719 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000720 // ?? Should we store float/double/longdouble separately in ID?
Jim Laskey583bd472006-10-27 23:46:08 +0000721 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000722 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Jim Laskey583bd472006-10-27 23:46:08 +0000723 ID.AddDouble(Val);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000724 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000725 SDNode *N = NULL;
726 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
727 if (!MVT::isVector(VT))
728 return SDOperand(N, 0);
729 if (!N) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000730 N = new ConstantFPSDNode(isTarget,
731 isDouble ? APFloat(Val) : APFloat((float)Val), EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000732 CSEMap.InsertNode(N, IP);
733 AllNodes.push_back(N);
734 }
735
Dan Gohman7f321562007-06-25 16:23:39 +0000736 SDOperand Result(N, 0);
737 if (MVT::isVector(VT)) {
738 SmallVector<SDOperand, 8> Ops;
739 Ops.assign(MVT::getVectorNumElements(VT), Result);
740 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
741 }
742 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000743}
744
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000745SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
746 bool isTarget) {
747 MVT::ValueType EltVT =
748 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
749 if (EltVT==MVT::f32)
750 return getConstantFP(APFloat((float)Val), VT, isTarget);
751 else
752 return getConstantFP(APFloat(Val), VT, isTarget);
753}
754
Chris Lattnerc3aae252005-01-07 07:46:32 +0000755SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000756 MVT::ValueType VT, int Offset,
757 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000758 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
759 unsigned Opc;
760 if (GVar && GVar->isThreadLocal())
761 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
762 else
763 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000764 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000765 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000766 ID.AddPointer(GV);
767 ID.AddInteger(Offset);
768 void *IP = 0;
769 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
770 return SDOperand(E, 0);
771 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
772 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000773 AllNodes.push_back(N);
774 return SDOperand(N, 0);
775}
776
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000777SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
778 bool isTarget) {
779 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000780 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000781 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000782 ID.AddInteger(FI);
783 void *IP = 0;
784 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
785 return SDOperand(E, 0);
786 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
787 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000788 AllNodes.push_back(N);
789 return SDOperand(N, 0);
790}
791
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000792SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
793 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000794 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000795 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000796 ID.AddInteger(JTI);
797 void *IP = 0;
798 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
799 return SDOperand(E, 0);
800 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
801 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000802 AllNodes.push_back(N);
803 return SDOperand(N, 0);
804}
805
Evan Chengb8973bd2006-01-31 22:23:14 +0000806SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000807 unsigned Alignment, int Offset,
808 bool isTarget) {
809 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000810 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000811 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000812 ID.AddInteger(Alignment);
813 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000814 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000815 void *IP = 0;
816 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
817 return SDOperand(E, 0);
818 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
819 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000820 AllNodes.push_back(N);
821 return SDOperand(N, 0);
822}
823
Chris Lattnerc3aae252005-01-07 07:46:32 +0000824
Evan Chengd6594ae2006-09-12 21:00:35 +0000825SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
826 MVT::ValueType VT,
827 unsigned Alignment, int Offset,
828 bool isTarget) {
829 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000830 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000831 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000832 ID.AddInteger(Alignment);
833 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000834 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000835 void *IP = 0;
836 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
837 return SDOperand(E, 0);
838 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
839 CSEMap.InsertNode(N, IP);
840 AllNodes.push_back(N);
841 return SDOperand(N, 0);
842}
843
844
Chris Lattnerc3aae252005-01-07 07:46:32 +0000845SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000846 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000847 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000848 ID.AddPointer(MBB);
849 void *IP = 0;
850 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
851 return SDOperand(E, 0);
852 SDNode *N = new BasicBlockSDNode(MBB);
853 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000854 AllNodes.push_back(N);
855 return SDOperand(N, 0);
856}
857
Chris Lattner15e4b012005-07-10 00:07:11 +0000858SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
859 if ((unsigned)VT >= ValueTypeNodes.size())
860 ValueTypeNodes.resize(VT+1);
861 if (ValueTypeNodes[VT] == 0) {
862 ValueTypeNodes[VT] = new VTSDNode(VT);
863 AllNodes.push_back(ValueTypeNodes[VT]);
864 }
865
866 return SDOperand(ValueTypeNodes[VT], 0);
867}
868
Chris Lattnerc3aae252005-01-07 07:46:32 +0000869SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
870 SDNode *&N = ExternalSymbols[Sym];
871 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000872 N = new ExternalSymbolSDNode(false, Sym, VT);
873 AllNodes.push_back(N);
874 return SDOperand(N, 0);
875}
876
Chris Lattner809ec112006-01-28 10:09:25 +0000877SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
878 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000879 SDNode *&N = TargetExternalSymbols[Sym];
880 if (N) return SDOperand(N, 0);
881 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000882 AllNodes.push_back(N);
883 return SDOperand(N, 0);
884}
885
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000886SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
887 if ((unsigned)Cond >= CondCodeNodes.size())
888 CondCodeNodes.resize(Cond+1);
889
Chris Lattner079a27a2005-08-09 20:40:02 +0000890 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000891 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000892 AllNodes.push_back(CondCodeNodes[Cond]);
893 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000894 return SDOperand(CondCodeNodes[Cond], 0);
895}
896
Chris Lattner0fdd7682005-08-30 22:38:38 +0000897SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000898 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000899 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000900 ID.AddInteger(RegNo);
901 void *IP = 0;
902 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
903 return SDOperand(E, 0);
904 SDNode *N = new RegisterSDNode(RegNo, VT);
905 CSEMap.InsertNode(N, IP);
906 AllNodes.push_back(N);
907 return SDOperand(N, 0);
908}
909
910SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
911 assert((!V || isa<PointerType>(V->getType())) &&
912 "SrcValue is not a pointer?");
913
Jim Laskey583bd472006-10-27 23:46:08 +0000914 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000915 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000916 ID.AddPointer(V);
917 ID.AddInteger(Offset);
918 void *IP = 0;
919 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
920 return SDOperand(E, 0);
921 SDNode *N = new SrcValueSDNode(V, Offset);
922 CSEMap.InsertNode(N, IP);
923 AllNodes.push_back(N);
924 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000925}
926
Chris Lattner51dabfb2006-10-14 00:41:01 +0000927SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
928 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929 // These setcc operations always fold.
930 switch (Cond) {
931 default: break;
932 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000933 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000934 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000935 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000936
937 case ISD::SETOEQ:
938 case ISD::SETOGT:
939 case ISD::SETOGE:
940 case ISD::SETOLT:
941 case ISD::SETOLE:
942 case ISD::SETONE:
943 case ISD::SETO:
944 case ISD::SETUO:
945 case ISD::SETUEQ:
946 case ISD::SETUNE:
947 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
948 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000949 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000950
Chris Lattner67255a12005-04-07 18:14:58 +0000951 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
952 uint64_t C2 = N2C->getValue();
953 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
954 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000955
Chris Lattnerc3aae252005-01-07 07:46:32 +0000956 // Sign extend the operands if required
957 if (ISD::isSignedIntSetCC(Cond)) {
958 C1 = N1C->getSignExtended();
959 C2 = N2C->getSignExtended();
960 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000961
Chris Lattnerc3aae252005-01-07 07:46:32 +0000962 switch (Cond) {
963 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000964 case ISD::SETEQ: return getConstant(C1 == C2, VT);
965 case ISD::SETNE: return getConstant(C1 != C2, VT);
966 case ISD::SETULT: return getConstant(C1 < C2, VT);
967 case ISD::SETUGT: return getConstant(C1 > C2, VT);
968 case ISD::SETULE: return getConstant(C1 <= C2, VT);
969 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
970 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
971 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
972 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
973 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000974 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000975 }
Chris Lattner67255a12005-04-07 18:14:58 +0000976 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000977 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
978 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000979
980 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +0000981 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000982 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +0000983 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
984 return getNode(ISD::UNDEF, VT);
985 // fall through
986 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
987 case ISD::SETNE: if (R==APFloat::cmpUnordered)
988 return getNode(ISD::UNDEF, VT);
989 // fall through
990 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000991 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +0000992 case ISD::SETLT: if (R==APFloat::cmpUnordered)
993 return getNode(ISD::UNDEF, VT);
994 // fall through
995 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
996 case ISD::SETGT: if (R==APFloat::cmpUnordered)
997 return getNode(ISD::UNDEF, VT);
998 // fall through
999 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1000 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1001 return getNode(ISD::UNDEF, VT);
1002 // fall through
1003 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001004 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001005 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1006 return getNode(ISD::UNDEF, VT);
1007 // fall through
1008 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001009 R==APFloat::cmpEqual, VT);
1010 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1011 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1012 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1013 R==APFloat::cmpEqual, VT);
1014 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1015 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1016 R==APFloat::cmpLessThan, VT);
1017 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1018 R==APFloat::cmpUnordered, VT);
1019 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1020 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001021 }
1022 } else {
1023 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001024 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001025 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001026
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001027 // Could not fold it.
1028 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001029}
1030
Dan Gohmanea859be2007-06-22 14:59:07 +00001031/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1032/// this predicate to simplify operations downstream. Mask is known to be zero
1033/// for bits that V cannot have.
1034bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1035 unsigned Depth) const {
1036 // The masks are not wide enough to represent this type! Should use APInt.
1037 if (Op.getValueType() == MVT::i128)
1038 return false;
1039
1040 uint64_t KnownZero, KnownOne;
1041 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1042 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1043 return (KnownZero & Mask) == Mask;
1044}
1045
1046/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1047/// known to be either zero or one and return them in the KnownZero/KnownOne
1048/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1049/// processing.
1050void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1051 uint64_t &KnownZero, uint64_t &KnownOne,
1052 unsigned Depth) const {
1053 KnownZero = KnownOne = 0; // Don't know anything.
1054 if (Depth == 6 || Mask == 0)
1055 return; // Limit search depth.
1056
1057 // The masks are not wide enough to represent this type! Should use APInt.
1058 if (Op.getValueType() == MVT::i128)
1059 return;
1060
1061 uint64_t KnownZero2, KnownOne2;
1062
1063 switch (Op.getOpcode()) {
1064 case ISD::Constant:
1065 // We know all of the bits for a constant!
1066 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1067 KnownZero = ~KnownOne & Mask;
1068 return;
1069 case ISD::AND:
1070 // If either the LHS or the RHS are Zero, the result is zero.
1071 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1072 Mask &= ~KnownZero;
1073 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1074 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1075 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1076
1077 // Output known-1 bits are only known if set in both the LHS & RHS.
1078 KnownOne &= KnownOne2;
1079 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1080 KnownZero |= KnownZero2;
1081 return;
1082 case ISD::OR:
1083 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1084 Mask &= ~KnownOne;
1085 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1086 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1087 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1088
1089 // Output known-0 bits are only known if clear in both the LHS & RHS.
1090 KnownZero &= KnownZero2;
1091 // Output known-1 are known to be set if set in either the LHS | RHS.
1092 KnownOne |= KnownOne2;
1093 return;
1094 case ISD::XOR: {
1095 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1096 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1097 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1098 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1099
1100 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1101 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1102 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1103 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1104 KnownZero = KnownZeroOut;
1105 return;
1106 }
1107 case ISD::SELECT:
1108 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1109 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1110 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1111 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1112
1113 // Only known if known in both the LHS and RHS.
1114 KnownOne &= KnownOne2;
1115 KnownZero &= KnownZero2;
1116 return;
1117 case ISD::SELECT_CC:
1118 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1119 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1120 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1121 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1122
1123 // Only known if known in both the LHS and RHS.
1124 KnownOne &= KnownOne2;
1125 KnownZero &= KnownZero2;
1126 return;
1127 case ISD::SETCC:
1128 // If we know the result of a setcc has the top bits zero, use this info.
1129 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1130 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1131 return;
1132 case ISD::SHL:
1133 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1134 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1135 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1136 KnownZero, KnownOne, Depth+1);
1137 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1138 KnownZero <<= SA->getValue();
1139 KnownOne <<= SA->getValue();
1140 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1141 }
1142 return;
1143 case ISD::SRL:
1144 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1145 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1146 MVT::ValueType VT = Op.getValueType();
1147 unsigned ShAmt = SA->getValue();
1148
1149 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1150 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1151 KnownZero, KnownOne, Depth+1);
1152 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1153 KnownZero &= TypeMask;
1154 KnownOne &= TypeMask;
1155 KnownZero >>= ShAmt;
1156 KnownOne >>= ShAmt;
1157
1158 uint64_t HighBits = (1ULL << ShAmt)-1;
1159 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1160 KnownZero |= HighBits; // High bits known zero.
1161 }
1162 return;
1163 case ISD::SRA:
1164 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1165 MVT::ValueType VT = Op.getValueType();
1166 unsigned ShAmt = SA->getValue();
1167
1168 // Compute the new bits that are at the top now.
1169 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1170
1171 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1172 // If any of the demanded bits are produced by the sign extension, we also
1173 // demand the input sign bit.
1174 uint64_t HighBits = (1ULL << ShAmt)-1;
1175 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1176 if (HighBits & Mask)
1177 InDemandedMask |= MVT::getIntVTSignBit(VT);
1178
1179 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1180 Depth+1);
1181 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1182 KnownZero &= TypeMask;
1183 KnownOne &= TypeMask;
1184 KnownZero >>= ShAmt;
1185 KnownOne >>= ShAmt;
1186
1187 // Handle the sign bits.
1188 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1189 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1190
1191 if (KnownZero & SignBit) {
1192 KnownZero |= HighBits; // New bits are known zero.
1193 } else if (KnownOne & SignBit) {
1194 KnownOne |= HighBits; // New bits are known one.
1195 }
1196 }
1197 return;
1198 case ISD::SIGN_EXTEND_INREG: {
1199 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1200
1201 // Sign extension. Compute the demanded bits in the result that are not
1202 // present in the input.
1203 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1204
1205 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1206 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1207
1208 // If the sign extended bits are demanded, we know that the sign
1209 // bit is demanded.
1210 if (NewBits)
1211 InputDemandedBits |= InSignBit;
1212
1213 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1214 KnownZero, KnownOne, Depth+1);
1215 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1216
1217 // If the sign bit of the input is known set or clear, then we know the
1218 // top bits of the result.
1219 if (KnownZero & InSignBit) { // Input sign bit known clear
1220 KnownZero |= NewBits;
1221 KnownOne &= ~NewBits;
1222 } else if (KnownOne & InSignBit) { // Input sign bit known set
1223 KnownOne |= NewBits;
1224 KnownZero &= ~NewBits;
1225 } else { // Input sign bit unknown
1226 KnownZero &= ~NewBits;
1227 KnownOne &= ~NewBits;
1228 }
1229 return;
1230 }
1231 case ISD::CTTZ:
1232 case ISD::CTLZ:
1233 case ISD::CTPOP: {
1234 MVT::ValueType VT = Op.getValueType();
1235 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1236 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1237 KnownOne = 0;
1238 return;
1239 }
1240 case ISD::LOAD: {
1241 if (ISD::isZEXTLoad(Op.Val)) {
1242 LoadSDNode *LD = cast<LoadSDNode>(Op);
1243 MVT::ValueType VT = LD->getLoadedVT();
1244 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1245 }
1246 return;
1247 }
1248 case ISD::ZERO_EXTEND: {
1249 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1250 uint64_t NewBits = (~InMask) & Mask;
1251 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1252 KnownOne, Depth+1);
1253 KnownZero |= NewBits & Mask;
1254 KnownOne &= ~NewBits;
1255 return;
1256 }
1257 case ISD::SIGN_EXTEND: {
1258 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1259 unsigned InBits = MVT::getSizeInBits(InVT);
1260 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1261 uint64_t InSignBit = 1ULL << (InBits-1);
1262 uint64_t NewBits = (~InMask) & Mask;
1263 uint64_t InDemandedBits = Mask & InMask;
1264
1265 // If any of the sign extended bits are demanded, we know that the sign
1266 // bit is demanded.
1267 if (NewBits & Mask)
1268 InDemandedBits |= InSignBit;
1269
1270 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1271 KnownOne, Depth+1);
1272 // If the sign bit is known zero or one, the top bits match.
1273 if (KnownZero & InSignBit) {
1274 KnownZero |= NewBits;
1275 KnownOne &= ~NewBits;
1276 } else if (KnownOne & InSignBit) {
1277 KnownOne |= NewBits;
1278 KnownZero &= ~NewBits;
1279 } else { // Otherwise, top bits aren't known.
1280 KnownOne &= ~NewBits;
1281 KnownZero &= ~NewBits;
1282 }
1283 return;
1284 }
1285 case ISD::ANY_EXTEND: {
1286 MVT::ValueType VT = Op.getOperand(0).getValueType();
1287 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1288 KnownZero, KnownOne, Depth+1);
1289 return;
1290 }
1291 case ISD::TRUNCATE: {
1292 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1293 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1294 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1295 KnownZero &= OutMask;
1296 KnownOne &= OutMask;
1297 break;
1298 }
1299 case ISD::AssertZext: {
1300 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1301 uint64_t InMask = MVT::getIntVTBitMask(VT);
1302 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1303 KnownOne, Depth+1);
1304 KnownZero |= (~InMask) & Mask;
1305 return;
1306 }
1307 case ISD::ADD: {
1308 // If either the LHS or the RHS are Zero, the result is zero.
1309 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1310 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1311 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1312 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1313
1314 // Output known-0 bits are known if clear or set in both the low clear bits
1315 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1316 // low 3 bits clear.
1317 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1318 CountTrailingZeros_64(~KnownZero2));
1319
1320 KnownZero = (1ULL << KnownZeroOut) - 1;
1321 KnownOne = 0;
1322 return;
1323 }
1324 case ISD::SUB: {
1325 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1326 if (!CLHS) return;
1327
1328 // We know that the top bits of C-X are clear if X contains less bits
1329 // than C (i.e. no wrap-around can happen). For example, 20-X is
1330 // positive if we can prove that X is >= 0 and < 16.
1331 MVT::ValueType VT = CLHS->getValueType(0);
1332 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1333 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1334 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1335 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1336 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1337
1338 // If all of the MaskV bits are known to be zero, then we know the output
1339 // top bits are zero, because we now know that the output is from [0-C].
1340 if ((KnownZero & MaskV) == MaskV) {
1341 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1342 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1343 KnownOne = 0; // No one bits known.
1344 } else {
1345 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1346 }
1347 }
1348 return;
1349 }
1350 default:
1351 // Allow the target to implement this method for its nodes.
1352 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1353 case ISD::INTRINSIC_WO_CHAIN:
1354 case ISD::INTRINSIC_W_CHAIN:
1355 case ISD::INTRINSIC_VOID:
1356 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1357 }
1358 return;
1359 }
1360}
1361
1362/// ComputeNumSignBits - Return the number of times the sign bit of the
1363/// register is replicated into the other bits. We know that at least 1 bit
1364/// is always equal to the sign bit (itself), but other cases can give us
1365/// information. For example, immediately after an "SRA X, 2", we know that
1366/// the top 3 bits are all equal to each other, so we return 3.
1367unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1368 MVT::ValueType VT = Op.getValueType();
1369 assert(MVT::isInteger(VT) && "Invalid VT!");
1370 unsigned VTBits = MVT::getSizeInBits(VT);
1371 unsigned Tmp, Tmp2;
1372
1373 if (Depth == 6)
1374 return 1; // Limit search depth.
1375
1376 switch (Op.getOpcode()) {
1377 default: break;
1378 case ISD::AssertSext:
1379 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1380 return VTBits-Tmp+1;
1381 case ISD::AssertZext:
1382 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1383 return VTBits-Tmp;
1384
1385 case ISD::Constant: {
1386 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1387 // If negative, invert the bits, then look at it.
1388 if (Val & MVT::getIntVTSignBit(VT))
1389 Val = ~Val;
1390
1391 // Shift the bits so they are the leading bits in the int64_t.
1392 Val <<= 64-VTBits;
1393
1394 // Return # leading zeros. We use 'min' here in case Val was zero before
1395 // shifting. We don't want to return '64' as for an i32 "0".
1396 return std::min(VTBits, CountLeadingZeros_64(Val));
1397 }
1398
1399 case ISD::SIGN_EXTEND:
1400 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1401 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1402
1403 case ISD::SIGN_EXTEND_INREG:
1404 // Max of the input and what this extends.
1405 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1406 Tmp = VTBits-Tmp+1;
1407
1408 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1409 return std::max(Tmp, Tmp2);
1410
1411 case ISD::SRA:
1412 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1413 // SRA X, C -> adds C sign bits.
1414 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1415 Tmp += C->getValue();
1416 if (Tmp > VTBits) Tmp = VTBits;
1417 }
1418 return Tmp;
1419 case ISD::SHL:
1420 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1421 // shl destroys sign bits.
1422 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1423 if (C->getValue() >= VTBits || // Bad shift.
1424 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1425 return Tmp - C->getValue();
1426 }
1427 break;
1428 case ISD::AND:
1429 case ISD::OR:
1430 case ISD::XOR: // NOT is handled here.
1431 // Logical binary ops preserve the number of sign bits.
1432 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1433 if (Tmp == 1) return 1; // Early out.
1434 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1435 return std::min(Tmp, Tmp2);
1436
1437 case ISD::SELECT:
1438 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1439 if (Tmp == 1) return 1; // Early out.
1440 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1441 return std::min(Tmp, Tmp2);
1442
1443 case ISD::SETCC:
1444 // If setcc returns 0/-1, all bits are sign bits.
1445 if (TLI.getSetCCResultContents() ==
1446 TargetLowering::ZeroOrNegativeOneSetCCResult)
1447 return VTBits;
1448 break;
1449 case ISD::ROTL:
1450 case ISD::ROTR:
1451 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1452 unsigned RotAmt = C->getValue() & (VTBits-1);
1453
1454 // Handle rotate right by N like a rotate left by 32-N.
1455 if (Op.getOpcode() == ISD::ROTR)
1456 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1457
1458 // If we aren't rotating out all of the known-in sign bits, return the
1459 // number that are left. This handles rotl(sext(x), 1) for example.
1460 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1461 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1462 }
1463 break;
1464 case ISD::ADD:
1465 // Add can have at most one carry bit. Thus we know that the output
1466 // is, at worst, one more bit than the inputs.
1467 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1468 if (Tmp == 1) return 1; // Early out.
1469
1470 // Special case decrementing a value (ADD X, -1):
1471 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1472 if (CRHS->isAllOnesValue()) {
1473 uint64_t KnownZero, KnownOne;
1474 uint64_t Mask = MVT::getIntVTBitMask(VT);
1475 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1476
1477 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1478 // sign bits set.
1479 if ((KnownZero|1) == Mask)
1480 return VTBits;
1481
1482 // If we are subtracting one from a positive number, there is no carry
1483 // out of the result.
1484 if (KnownZero & MVT::getIntVTSignBit(VT))
1485 return Tmp;
1486 }
1487
1488 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1489 if (Tmp2 == 1) return 1;
1490 return std::min(Tmp, Tmp2)-1;
1491 break;
1492
1493 case ISD::SUB:
1494 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1495 if (Tmp2 == 1) return 1;
1496
1497 // Handle NEG.
1498 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1499 if (CLHS->getValue() == 0) {
1500 uint64_t KnownZero, KnownOne;
1501 uint64_t Mask = MVT::getIntVTBitMask(VT);
1502 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1503 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1504 // sign bits set.
1505 if ((KnownZero|1) == Mask)
1506 return VTBits;
1507
1508 // If the input is known to be positive (the sign bit is known clear),
1509 // the output of the NEG has the same number of sign bits as the input.
1510 if (KnownZero & MVT::getIntVTSignBit(VT))
1511 return Tmp2;
1512
1513 // Otherwise, we treat this like a SUB.
1514 }
1515
1516 // Sub can have at most one carry bit. Thus we know that the output
1517 // is, at worst, one more bit than the inputs.
1518 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1519 if (Tmp == 1) return 1; // Early out.
1520 return std::min(Tmp, Tmp2)-1;
1521 break;
1522 case ISD::TRUNCATE:
1523 // FIXME: it's tricky to do anything useful for this, but it is an important
1524 // case for targets like X86.
1525 break;
1526 }
1527
1528 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1529 if (Op.getOpcode() == ISD::LOAD) {
1530 LoadSDNode *LD = cast<LoadSDNode>(Op);
1531 unsigned ExtType = LD->getExtensionType();
1532 switch (ExtType) {
1533 default: break;
1534 case ISD::SEXTLOAD: // '17' bits known
1535 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1536 return VTBits-Tmp+1;
1537 case ISD::ZEXTLOAD: // '16' bits known
1538 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1539 return VTBits-Tmp;
1540 }
1541 }
1542
1543 // Allow the target to implement this method for its nodes.
1544 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1545 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1546 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1547 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1548 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1549 if (NumBits > 1) return NumBits;
1550 }
1551
1552 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1553 // use this information.
1554 uint64_t KnownZero, KnownOne;
1555 uint64_t Mask = MVT::getIntVTBitMask(VT);
1556 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1557
1558 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1559 if (KnownZero & SignBit) { // SignBit is 0
1560 Mask = KnownZero;
1561 } else if (KnownOne & SignBit) { // SignBit is 1;
1562 Mask = KnownOne;
1563 } else {
1564 // Nothing known.
1565 return 1;
1566 }
1567
1568 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1569 // the number of identical bits in the top of the input value.
1570 Mask ^= ~0ULL;
1571 Mask <<= 64-VTBits;
1572 // Return # leading zeros. We use 'min' here in case Val was zero before
1573 // shifting. We don't want to return '64' as for an i32 "0".
1574 return std::min(VTBits, CountLeadingZeros_64(Mask));
1575}
1576
Chris Lattner51dabfb2006-10-14 00:41:01 +00001577
Chris Lattnerc3aae252005-01-07 07:46:32 +00001578/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001579///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001580SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001581 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001582 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001583 void *IP = 0;
1584 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1585 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001586 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001587 CSEMap.InsertNode(N, IP);
1588
1589 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001590 return SDOperand(N, 0);
1591}
1592
Chris Lattnerc3aae252005-01-07 07:46:32 +00001593SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1594 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001595 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001596 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001597 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1598 uint64_t Val = C->getValue();
1599 switch (Opcode) {
1600 default: break;
1601 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001602 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001603 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1604 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001605 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1606 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
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 :
1684 APFloat::IEEEdouble,
1685 APFloat::rmNearestTiesToEven);
1686 return getConstantFP(V, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001687 case ISD::FP_TO_SINT:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001688 case ISD::FP_TO_UINT: {
1689 integerPart x;
1690 assert(integerPartWidth >= 64);
1691 // FIXME need to be more flexible about rounding mode.
1692 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1693 Opcode==ISD::FP_TO_SINT,
1694 APFloat::rmTowardZero);
1695 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1696 break;
1697 return getConstant(x, VT);
1698 }
Chris Lattner94683772005-12-23 05:30:37 +00001699 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001700 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001701 return getConstant(FloatToBits(V.convertToFloat()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001702 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001703 return getConstant(DoubleToBits(V.convertToDouble()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001704 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001705 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001706 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001707
1708 unsigned OpOpcode = Operand.Val->getOpcode();
1709 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001710 case ISD::TokenFactor:
1711 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001712 case ISD::FP_ROUND:
1713 case ISD::FP_EXTEND:
1714 assert(MVT::isFloatingPoint(VT) &&
1715 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1716 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001717 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001718 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1719 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001720 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001721 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001722 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1723 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1724 break;
1725 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001726 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1727 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001728 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001729 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001730 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001731 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001732 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001733 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001734 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1735 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001736 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001737 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001738 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1739 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1740 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1741 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001742 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001743 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1744 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001745 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001746 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001747 if (OpOpcode == ISD::TRUNCATE)
1748 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001749 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1750 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001751 // If the source is smaller than the dest, we still need an extend.
1752 if (Operand.Val->getOperand(0).getValueType() < VT)
1753 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1754 else if (Operand.Val->getOperand(0).getValueType() > VT)
1755 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1756 else
1757 return Operand.Val->getOperand(0);
1758 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001759 break;
Chris Lattner35481892005-12-23 00:16:34 +00001760 case ISD::BIT_CONVERT:
1761 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001762 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001763 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001764 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001765 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1766 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001767 if (OpOpcode == ISD::UNDEF)
1768 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001769 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001770 case ISD::SCALAR_TO_VECTOR:
1771 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001772 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001773 "Illegal SCALAR_TO_VECTOR node!");
1774 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001775 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001776 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1777 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001778 Operand.Val->getOperand(0));
1779 if (OpOpcode == ISD::FNEG) // --X -> X
1780 return Operand.Val->getOperand(0);
1781 break;
1782 case ISD::FABS:
1783 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1784 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1785 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001786 }
1787
Chris Lattner43247a12005-08-25 19:12:10 +00001788 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001789 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001790 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001791 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001792 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001793 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001794 void *IP = 0;
1795 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1796 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001797 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001798 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001799 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001800 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001801 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001802 AllNodes.push_back(N);
1803 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001804}
1805
Chris Lattner36019aa2005-04-18 03:48:41 +00001806
1807
Chris Lattnerc3aae252005-01-07 07:46:32 +00001808SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1809 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001810#ifndef NDEBUG
1811 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001812 case ISD::TokenFactor:
1813 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1814 N2.getValueType() == MVT::Other && "Invalid token factor!");
1815 break;
Chris Lattner76365122005-01-16 02:23:22 +00001816 case ISD::AND:
1817 case ISD::OR:
1818 case ISD::XOR:
1819 case ISD::UDIV:
1820 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001821 case ISD::MULHU:
1822 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001823 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1824 // fall through
1825 case ISD::ADD:
1826 case ISD::SUB:
1827 case ISD::MUL:
1828 case ISD::SDIV:
1829 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001830 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1831 // fall through.
1832 case ISD::FADD:
1833 case ISD::FSUB:
1834 case ISD::FMUL:
1835 case ISD::FDIV:
1836 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001837 assert(N1.getValueType() == N2.getValueType() &&
1838 N1.getValueType() == VT && "Binary operator types must match!");
1839 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001840 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1841 assert(N1.getValueType() == VT &&
1842 MVT::isFloatingPoint(N1.getValueType()) &&
1843 MVT::isFloatingPoint(N2.getValueType()) &&
1844 "Invalid FCOPYSIGN!");
1845 break;
Chris Lattner76365122005-01-16 02:23:22 +00001846 case ISD::SHL:
1847 case ISD::SRA:
1848 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001849 case ISD::ROTL:
1850 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001851 assert(VT == N1.getValueType() &&
1852 "Shift operators return type must be the same as their first arg");
1853 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001854 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001855 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001856 case ISD::FP_ROUND_INREG: {
1857 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1858 assert(VT == N1.getValueType() && "Not an inreg round!");
1859 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1860 "Cannot FP_ROUND_INREG integer types");
1861 assert(EVT <= VT && "Not rounding down!");
1862 break;
1863 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001864 case ISD::AssertSext:
1865 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001866 case ISD::SIGN_EXTEND_INREG: {
1867 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1868 assert(VT == N1.getValueType() && "Not an inreg extend!");
1869 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1870 "Cannot *_EXTEND_INREG FP types");
1871 assert(EVT <= VT && "Not extending!");
1872 }
1873
Chris Lattner76365122005-01-16 02:23:22 +00001874 default: break;
1875 }
1876#endif
1877
Chris Lattnerc3aae252005-01-07 07:46:32 +00001878 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1879 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1880 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001881 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1882 int64_t Val = N1C->getValue();
1883 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1884 Val <<= 64-FromBits;
1885 Val >>= 64-FromBits;
1886 return getConstant(Val, VT);
1887 }
1888
Chris Lattnerc3aae252005-01-07 07:46:32 +00001889 if (N2C) {
1890 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1891 switch (Opcode) {
1892 case ISD::ADD: return getConstant(C1 + C2, VT);
1893 case ISD::SUB: return getConstant(C1 - C2, VT);
1894 case ISD::MUL: return getConstant(C1 * C2, VT);
1895 case ISD::UDIV:
1896 if (C2) return getConstant(C1 / C2, VT);
1897 break;
1898 case ISD::UREM :
1899 if (C2) return getConstant(C1 % C2, VT);
1900 break;
1901 case ISD::SDIV :
1902 if (C2) return getConstant(N1C->getSignExtended() /
1903 N2C->getSignExtended(), VT);
1904 break;
1905 case ISD::SREM :
1906 if (C2) return getConstant(N1C->getSignExtended() %
1907 N2C->getSignExtended(), VT);
1908 break;
1909 case ISD::AND : return getConstant(C1 & C2, VT);
1910 case ISD::OR : return getConstant(C1 | C2, VT);
1911 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001912 case ISD::SHL : return getConstant(C1 << C2, VT);
1913 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001914 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001915 case ISD::ROTL :
1916 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1917 VT);
1918 case ISD::ROTR :
1919 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1920 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001921 default: break;
1922 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001923 } else { // Cannonicalize constant to RHS if commutative
1924 if (isCommutativeBinOp(Opcode)) {
1925 std::swap(N1C, N2C);
1926 std::swap(N1, N2);
1927 }
1928 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001929 }
1930
1931 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1932 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001933 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001934 if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001935 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1936 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001937 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001938 case ISD::FADD:
1939 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1940 if (s!=APFloat::opInvalidOp)
1941 return getConstantFP(V1, VT);
1942 break;
1943 case ISD::FSUB:
1944 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1945 if (s!=APFloat::opInvalidOp)
1946 return getConstantFP(V1, VT);
1947 break;
1948 case ISD::FMUL:
1949 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1950 if (s!=APFloat::opInvalidOp)
1951 return getConstantFP(V1, VT);
1952 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001953 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001954 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1955 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1956 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001957 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001958 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001959 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1960 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1961 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001962 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001963 case ISD::FCOPYSIGN:
1964 V1.copySign(V2);
1965 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001966 default: break;
1967 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001968 } else { // Cannonicalize constant to RHS if commutative
1969 if (isCommutativeBinOp(Opcode)) {
1970 std::swap(N1CFP, N2CFP);
1971 std::swap(N1, N2);
1972 }
1973 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001974 }
Chris Lattner62b57722006-04-20 05:39:12 +00001975
1976 // Canonicalize an UNDEF to the RHS, even over a constant.
1977 if (N1.getOpcode() == ISD::UNDEF) {
1978 if (isCommutativeBinOp(Opcode)) {
1979 std::swap(N1, N2);
1980 } else {
1981 switch (Opcode) {
1982 case ISD::FP_ROUND_INREG:
1983 case ISD::SIGN_EXTEND_INREG:
1984 case ISD::SUB:
1985 case ISD::FSUB:
1986 case ISD::FDIV:
1987 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001988 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001989 return N1; // fold op(undef, arg2) -> undef
1990 case ISD::UDIV:
1991 case ISD::SDIV:
1992 case ISD::UREM:
1993 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001994 case ISD::SRL:
1995 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00001996 if (!MVT::isVector(VT))
1997 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1998 // For vectors, we can't easily build an all zero vector, just return
1999 // the LHS.
2000 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002001 }
2002 }
2003 }
2004
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002005 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002006 if (N2.getOpcode() == ISD::UNDEF) {
2007 switch (Opcode) {
2008 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002009 case ISD::ADDC:
2010 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002011 case ISD::SUB:
2012 case ISD::FADD:
2013 case ISD::FSUB:
2014 case ISD::FMUL:
2015 case ISD::FDIV:
2016 case ISD::FREM:
2017 case ISD::UDIV:
2018 case ISD::SDIV:
2019 case ISD::UREM:
2020 case ISD::SREM:
2021 case ISD::XOR:
2022 return N2; // fold op(arg1, undef) -> undef
2023 case ISD::MUL:
2024 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002025 case ISD::SRL:
2026 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002027 if (!MVT::isVector(VT))
2028 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2029 // For vectors, we can't easily build an all zero vector, just return
2030 // the LHS.
2031 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002032 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002033 if (!MVT::isVector(VT))
2034 return getConstant(MVT::getIntVTBitMask(VT), VT);
2035 // For vectors, we can't easily build an all one vector, just return
2036 // the LHS.
2037 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002038 case ISD::SRA:
2039 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002040 }
2041 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002042
Chris Lattner51dabfb2006-10-14 00:41:01 +00002043 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002044 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00002045 case ISD::TokenFactor:
2046 // Fold trivial token factors.
2047 if (N1.getOpcode() == ISD::EntryToken) return N2;
2048 if (N2.getOpcode() == ISD::EntryToken) return N1;
2049 break;
2050
Chris Lattner51dabfb2006-10-14 00:41:01 +00002051 case ISD::AND:
2052 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2053 // worth handling here.
2054 if (N2C && N2C->getValue() == 0)
2055 return N2;
2056 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00002057 case ISD::OR:
2058 case ISD::XOR:
2059 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2060 // worth handling here.
2061 if (N2C && N2C->getValue() == 0)
2062 return N1;
2063 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002064 case ISD::FP_ROUND_INREG:
2065 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2066 break;
2067 case ISD::SIGN_EXTEND_INREG: {
2068 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2069 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002070 break;
2071 }
Dan Gohman7f321562007-06-25 16:23:39 +00002072 case ISD::EXTRACT_VECTOR_ELT:
2073 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2074
Dan Gohman743d3a72007-07-10 18:20:44 +00002075 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002076 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002077 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2078 N1.getNumOperands() > 0) {
2079 unsigned Factor =
2080 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2081 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2082 N1.getOperand(N2C->getValue() / Factor),
2083 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002084 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002085
Dan Gohman7f321562007-06-25 16:23:39 +00002086 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2087 // expanding large vector constants.
2088 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2089 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002090
2091 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2092 // operations are lowered to scalars.
2093 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2094 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2095 if (IEC == N2C)
2096 return N1.getOperand(1);
2097 else
2098 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2099 }
Dan Gohman7f321562007-06-25 16:23:39 +00002100 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002101 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002102 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2103
Chris Lattner5c6621c2006-09-19 04:51:23 +00002104 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2105 // 64-bit integers into 32-bit parts. Instead of building the extract of
2106 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002107 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002108 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002109
2110 // EXTRACT_ELEMENT of a constant int is also very common.
2111 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2112 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2113 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002114 }
2115 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002116
Nate Begemaneea805e2005-04-13 21:23:31 +00002117 // FIXME: figure out how to safely handle things like
2118 // int foo(int x) { return 1 << (x & 255); }
2119 // int bar() { return foo(256); }
2120#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002121 case ISD::SHL:
2122 case ISD::SRL:
2123 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002124 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002125 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002126 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002127 else if (N2.getOpcode() == ISD::AND)
2128 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2129 // If the and is only masking out bits that cannot effect the shift,
2130 // eliminate the and.
2131 unsigned NumBits = MVT::getSizeInBits(VT);
2132 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2133 return getNode(Opcode, VT, N1, N2.getOperand(0));
2134 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002135 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002136#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002137 }
2138
Chris Lattner27e9b412005-05-11 18:57:39 +00002139 // Memoize this node if possible.
2140 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002141 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002142 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002143 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002144 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002145 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002146 void *IP = 0;
2147 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2148 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002149 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002150 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002151 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002152 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002153 }
2154
Chris Lattnerc3aae252005-01-07 07:46:32 +00002155 AllNodes.push_back(N);
2156 return SDOperand(N, 0);
2157}
2158
Chris Lattnerc3aae252005-01-07 07:46:32 +00002159SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2160 SDOperand N1, SDOperand N2, SDOperand N3) {
2161 // Perform various simplifications.
2162 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2163 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002164 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002165 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002166 // Use FoldSetCC to simplify SETCC's.
2167 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002168 if (Simp.Val) return Simp;
2169 break;
2170 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002171 case ISD::SELECT:
2172 if (N1C)
2173 if (N1C->getValue())
2174 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002175 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002176 return N3; // select false, X, Y -> Y
2177
2178 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002179 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002180 case ISD::BRCOND:
2181 if (N2C)
2182 if (N2C->getValue()) // Unconditional branch
2183 return getNode(ISD::BR, MVT::Other, N1, N3);
2184 else
2185 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002186 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002187 case ISD::VECTOR_SHUFFLE:
2188 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2189 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2190 N3.getOpcode() == ISD::BUILD_VECTOR &&
2191 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2192 "Illegal VECTOR_SHUFFLE node!");
2193 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002194 case ISD::BIT_CONVERT:
2195 // Fold bit_convert nodes from a type to themselves.
2196 if (N1.getValueType() == VT)
2197 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002198 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002199 }
2200
Chris Lattner43247a12005-08-25 19:12:10 +00002201 // Memoize node if it doesn't produce a flag.
2202 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002203 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002204 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002205 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002206 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002207 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002208 void *IP = 0;
2209 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2210 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002211 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002212 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002213 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002214 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002215 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002216 AllNodes.push_back(N);
2217 return SDOperand(N, 0);
2218}
2219
2220SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002221 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002222 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002223 SDOperand Ops[] = { N1, N2, N3, N4 };
2224 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002225}
2226
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002227SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2228 SDOperand N1, SDOperand N2, SDOperand N3,
2229 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002230 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2231 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002232}
2233
Evan Cheng7038daf2005-12-10 00:37:58 +00002234SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2235 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002236 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002237 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002238 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2239 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002240 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002241 Ty = MVT::getTypeForValueType(VT);
2242 } else if (SV) {
2243 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2244 assert(PT && "Value for load must be a pointer");
2245 Ty = PT->getElementType();
2246 }
2247 assert(Ty && "Could not get type information for load");
2248 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2249 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002250 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002251 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002252 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002253 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002254 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002255 ID.AddInteger(ISD::UNINDEXED);
2256 ID.AddInteger(ISD::NON_EXTLOAD);
2257 ID.AddInteger(VT);
2258 ID.AddPointer(SV);
2259 ID.AddInteger(SVOffset);
2260 ID.AddInteger(Alignment);
2261 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002262 void *IP = 0;
2263 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2264 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002265 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002266 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2267 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002268 CSEMap.InsertNode(N, IP);
2269 AllNodes.push_back(N);
2270 return SDOperand(N, 0);
2271}
2272
2273SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002274 SDOperand Chain, SDOperand Ptr,
2275 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002276 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002277 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002278 // If they are asking for an extending load from/to the same thing, return a
2279 // normal load.
2280 if (VT == EVT)
2281 ExtType = ISD::NON_EXTLOAD;
2282
2283 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002284 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002285 else
2286 assert(EVT < VT && "Should only be an extending load, not truncating!");
2287 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2288 "Cannot sign/zero extend a FP/Vector load!");
2289 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2290 "Cannot convert from FP to Int or Int -> FP!");
2291
Dan Gohman575e2f42007-06-04 15:49:41 +00002292 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2293 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002294 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002295 Ty = MVT::getTypeForValueType(VT);
2296 } else if (SV) {
2297 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2298 assert(PT && "Value for load must be a pointer");
2299 Ty = PT->getElementType();
2300 }
2301 assert(Ty && "Could not get type information for load");
2302 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2303 }
Evan Cheng466685d2006-10-09 20:57:25 +00002304 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002305 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002306 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002307 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002308 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002309 ID.AddInteger(ISD::UNINDEXED);
2310 ID.AddInteger(ExtType);
2311 ID.AddInteger(EVT);
2312 ID.AddPointer(SV);
2313 ID.AddInteger(SVOffset);
2314 ID.AddInteger(Alignment);
2315 ID.AddInteger(isVolatile);
2316 void *IP = 0;
2317 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2318 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002319 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002320 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002321 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002322 AllNodes.push_back(N);
2323 return SDOperand(N, 0);
2324}
2325
Evan Cheng144d8f02006-11-09 17:55:04 +00002326SDOperand
2327SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2328 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002329 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002330 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2331 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002332 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002333 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002334 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002335 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002336 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002337 ID.AddInteger(AM);
2338 ID.AddInteger(LD->getExtensionType());
2339 ID.AddInteger(LD->getLoadedVT());
2340 ID.AddPointer(LD->getSrcValue());
2341 ID.AddInteger(LD->getSrcValueOffset());
2342 ID.AddInteger(LD->getAlignment());
2343 ID.AddInteger(LD->isVolatile());
2344 void *IP = 0;
2345 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2346 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002347 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002348 LD->getExtensionType(), LD->getLoadedVT(),
2349 LD->getSrcValue(), LD->getSrcValueOffset(),
2350 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002351 CSEMap.InsertNode(N, IP);
2352 AllNodes.push_back(N);
2353 return SDOperand(N, 0);
2354}
2355
Jeff Cohend41b30d2006-11-05 19:31:28 +00002356SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002357 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002358 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002359 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002360
Dan Gohman575e2f42007-06-04 15:49:41 +00002361 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2362 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002363 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002364 Ty = MVT::getTypeForValueType(VT);
2365 } else if (SV) {
2366 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2367 assert(PT && "Value for store must be a pointer");
2368 Ty = PT->getElementType();
2369 }
2370 assert(Ty && "Could not get type information for store");
2371 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2372 }
Evan Chengad071e12006-10-05 22:57:11 +00002373 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002374 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002375 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002376 FoldingSetNodeID ID;
2377 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002378 ID.AddInteger(ISD::UNINDEXED);
2379 ID.AddInteger(false);
2380 ID.AddInteger(VT);
2381 ID.AddPointer(SV);
2382 ID.AddInteger(SVOffset);
2383 ID.AddInteger(Alignment);
2384 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002385 void *IP = 0;
2386 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2387 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002388 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002389 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002390 CSEMap.InsertNode(N, IP);
2391 AllNodes.push_back(N);
2392 return SDOperand(N, 0);
2393}
2394
Jeff Cohend41b30d2006-11-05 19:31:28 +00002395SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002396 SDOperand Ptr, const Value *SV,
2397 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002398 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002399 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002400 bool isTrunc = VT != SVT;
2401
2402 assert(VT > SVT && "Not a truncation?");
2403 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2404 "Can't do FP-INT conversion!");
2405
Dan Gohman575e2f42007-06-04 15:49:41 +00002406 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2407 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002408 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002409 Ty = MVT::getTypeForValueType(VT);
2410 } else if (SV) {
2411 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2412 assert(PT && "Value for store must be a pointer");
2413 Ty = PT->getElementType();
2414 }
2415 assert(Ty && "Could not get type information for store");
2416 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2417 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002418 SDVTList VTs = getVTList(MVT::Other);
2419 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002420 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002421 FoldingSetNodeID ID;
2422 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002423 ID.AddInteger(ISD::UNINDEXED);
2424 ID.AddInteger(isTrunc);
2425 ID.AddInteger(SVT);
2426 ID.AddPointer(SV);
2427 ID.AddInteger(SVOffset);
2428 ID.AddInteger(Alignment);
2429 ID.AddInteger(isVolatile);
2430 void *IP = 0;
2431 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2432 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002433 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002434 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002435 CSEMap.InsertNode(N, IP);
2436 AllNodes.push_back(N);
2437 return SDOperand(N, 0);
2438}
2439
Evan Cheng144d8f02006-11-09 17:55:04 +00002440SDOperand
2441SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2442 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002443 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2444 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2445 "Store is already a indexed store!");
2446 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2447 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2448 FoldingSetNodeID ID;
2449 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2450 ID.AddInteger(AM);
2451 ID.AddInteger(ST->isTruncatingStore());
2452 ID.AddInteger(ST->getStoredVT());
2453 ID.AddPointer(ST->getSrcValue());
2454 ID.AddInteger(ST->getSrcValueOffset());
2455 ID.AddInteger(ST->getAlignment());
2456 ID.AddInteger(ST->isVolatile());
2457 void *IP = 0;
2458 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2459 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002460 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002461 ST->isTruncatingStore(), ST->getStoredVT(),
2462 ST->getSrcValue(), ST->getSrcValueOffset(),
2463 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002464 CSEMap.InsertNode(N, IP);
2465 AllNodes.push_back(N);
2466 return SDOperand(N, 0);
2467}
2468
Nate Begemanacc398c2006-01-25 18:21:52 +00002469SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2470 SDOperand Chain, SDOperand Ptr,
2471 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002472 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002473 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002474}
2475
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002476SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002477 const SDOperand *Ops, unsigned NumOps) {
2478 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002479 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002480 case 1: return getNode(Opcode, VT, Ops[0]);
2481 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2482 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002483 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002484 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002485
Chris Lattneref847df2005-04-09 03:27:28 +00002486 switch (Opcode) {
2487 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002488 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002489 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002490 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2491 "LHS and RHS of condition must have same type!");
2492 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2493 "True and False arms of SelectCC must have same type!");
2494 assert(Ops[2].getValueType() == VT &&
2495 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002496 break;
2497 }
2498 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002499 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002500 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2501 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002502 break;
2503 }
Chris Lattneref847df2005-04-09 03:27:28 +00002504 }
2505
Chris Lattner385328c2005-05-14 07:42:29 +00002506 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002507 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002508 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002509 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002510 FoldingSetNodeID ID;
2511 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002512 void *IP = 0;
2513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2514 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002515 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002516 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002517 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002518 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002519 }
Chris Lattneref847df2005-04-09 03:27:28 +00002520 AllNodes.push_back(N);
2521 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002522}
2523
Chris Lattner89c34632005-05-14 06:20:26 +00002524SDOperand SelectionDAG::getNode(unsigned Opcode,
2525 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002526 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002527 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2528 Ops, NumOps);
2529}
2530
2531SDOperand SelectionDAG::getNode(unsigned Opcode,
2532 const MVT::ValueType *VTs, unsigned NumVTs,
2533 const SDOperand *Ops, unsigned NumOps) {
2534 if (NumVTs == 1)
2535 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002536 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2537}
2538
2539SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2540 const SDOperand *Ops, unsigned NumOps) {
2541 if (VTList.NumVTs == 1)
2542 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002543
Chris Lattner5f056bf2005-07-10 01:55:33 +00002544 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002545 // FIXME: figure out how to safely handle things like
2546 // int foo(int x) { return 1 << (x & 255); }
2547 // int bar() { return foo(256); }
2548#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002549 case ISD::SRA_PARTS:
2550 case ISD::SRL_PARTS:
2551 case ISD::SHL_PARTS:
2552 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002553 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002554 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2555 else if (N3.getOpcode() == ISD::AND)
2556 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2557 // If the and is only masking out bits that cannot effect the shift,
2558 // eliminate the and.
2559 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2560 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2561 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2562 }
2563 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002564#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002565 }
Chris Lattner89c34632005-05-14 06:20:26 +00002566
Chris Lattner43247a12005-08-25 19:12:10 +00002567 // Memoize the node unless it returns a flag.
2568 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002569 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002570 FoldingSetNodeID ID;
2571 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002572 void *IP = 0;
2573 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2574 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002575 if (NumOps == 1)
2576 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2577 else if (NumOps == 2)
2578 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2579 else if (NumOps == 3)
2580 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2581 else
2582 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002583 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002584 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002585 if (NumOps == 1)
2586 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2587 else if (NumOps == 2)
2588 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2589 else if (NumOps == 3)
2590 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2591 else
2592 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002593 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002594 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002595 return SDOperand(N, 0);
2596}
2597
Chris Lattner70046e92006-08-15 17:46:01 +00002598SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Dan Gohman6595cb32007-06-27 16:08:04 +00002599 if (!MVT::isExtendedVT(VT))
Dan Gohman7f321562007-06-25 16:23:39 +00002600 return makeVTList(SDNode::getValueTypeList(VT), 1);
2601
2602 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2603 E = VTList.end(); I != E; ++I) {
2604 if (I->size() == 1 && (*I)[0] == VT)
2605 return makeVTList(&(*I)[0], 1);
2606 }
2607 std::vector<MVT::ValueType> V;
2608 V.push_back(VT);
2609 VTList.push_front(V);
2610 return makeVTList(&(*VTList.begin())[0], 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002611}
2612
Chris Lattner70046e92006-08-15 17:46:01 +00002613SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002614 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2615 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002616 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002617 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002618 }
2619 std::vector<MVT::ValueType> V;
2620 V.push_back(VT1);
2621 V.push_back(VT2);
2622 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002623 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002624}
Chris Lattner70046e92006-08-15 17:46:01 +00002625SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2626 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002627 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002628 E = VTList.end(); I != E; ++I) {
2629 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2630 (*I)[2] == VT3)
2631 return makeVTList(&(*I)[0], 3);
2632 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002633 std::vector<MVT::ValueType> V;
2634 V.push_back(VT1);
2635 V.push_back(VT2);
2636 V.push_back(VT3);
2637 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002638 return makeVTList(&(*VTList.begin())[0], 3);
2639}
2640
2641SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2642 switch (NumVTs) {
2643 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002644 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002645 case 2: return getVTList(VTs[0], VTs[1]);
2646 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2647 default: break;
2648 }
2649
2650 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2651 E = VTList.end(); I != E; ++I) {
2652 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2653
2654 bool NoMatch = false;
2655 for (unsigned i = 2; i != NumVTs; ++i)
2656 if (VTs[i] != (*I)[i]) {
2657 NoMatch = true;
2658 break;
2659 }
2660 if (!NoMatch)
2661 return makeVTList(&*I->begin(), NumVTs);
2662 }
2663
2664 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2665 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002666}
2667
2668
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002669/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2670/// specified operands. If the resultant node already exists in the DAG,
2671/// this does not modify the specified node, instead it returns the node that
2672/// already exists. If the resultant node does not exist in the DAG, the
2673/// input node is returned. As a degenerate case, if you specify the same
2674/// input operands as the node already has, the input node is returned.
2675SDOperand SelectionDAG::
2676UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2677 SDNode *N = InN.Val;
2678 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2679
2680 // Check to see if there is no change.
2681 if (Op == N->getOperand(0)) return InN;
2682
2683 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002684 void *InsertPos = 0;
2685 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2686 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002687
2688 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002689 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002690 RemoveNodeFromCSEMaps(N);
2691
2692 // Now we update the operands.
2693 N->OperandList[0].Val->removeUser(N);
2694 Op.Val->addUser(N);
2695 N->OperandList[0] = Op;
2696
2697 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002698 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002699 return InN;
2700}
2701
2702SDOperand SelectionDAG::
2703UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2704 SDNode *N = InN.Val;
2705 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2706
2707 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002708 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2709 return InN; // No operands changed, just return the input node.
2710
2711 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002712 void *InsertPos = 0;
2713 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2714 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002715
2716 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002717 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002718 RemoveNodeFromCSEMaps(N);
2719
2720 // Now we update the operands.
2721 if (N->OperandList[0] != Op1) {
2722 N->OperandList[0].Val->removeUser(N);
2723 Op1.Val->addUser(N);
2724 N->OperandList[0] = Op1;
2725 }
2726 if (N->OperandList[1] != Op2) {
2727 N->OperandList[1].Val->removeUser(N);
2728 Op2.Val->addUser(N);
2729 N->OperandList[1] = Op2;
2730 }
2731
2732 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002733 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002734 return InN;
2735}
2736
2737SDOperand SelectionDAG::
2738UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002739 SDOperand Ops[] = { Op1, Op2, Op3 };
2740 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002741}
2742
2743SDOperand SelectionDAG::
2744UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2745 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002746 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2747 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002748}
2749
2750SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002751UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2752 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002753 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2754 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002755}
2756
2757
2758SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002759UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002760 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002761 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002762 "Update with wrong number of operands");
2763
2764 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002765 bool AnyChange = false;
2766 for (unsigned i = 0; i != NumOps; ++i) {
2767 if (Ops[i] != N->getOperand(i)) {
2768 AnyChange = true;
2769 break;
2770 }
2771 }
2772
2773 // No operands changed, just return the input node.
2774 if (!AnyChange) return InN;
2775
2776 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002777 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002778 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002779 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002780
2781 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002782 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002783 RemoveNodeFromCSEMaps(N);
2784
2785 // Now we update the operands.
2786 for (unsigned i = 0; i != NumOps; ++i) {
2787 if (N->OperandList[i] != Ops[i]) {
2788 N->OperandList[i].Val->removeUser(N);
2789 Ops[i].Val->addUser(N);
2790 N->OperandList[i] = Ops[i];
2791 }
2792 }
2793
2794 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002795 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002796 return InN;
2797}
2798
2799
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002800/// MorphNodeTo - This frees the operands of the current node, resets the
2801/// opcode, types, and operands to the specified value. This should only be
2802/// used by the SelectionDAG class.
2803void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2804 const SDOperand *Ops, unsigned NumOps) {
2805 NodeType = Opc;
2806 ValueList = L.VTs;
2807 NumValues = L.NumVTs;
2808
2809 // Clear the operands list, updating used nodes to remove this from their
2810 // use list.
2811 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2812 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002813
2814 // If NumOps is larger than the # of operands we currently have, reallocate
2815 // the operand list.
2816 if (NumOps > NumOperands) {
2817 if (OperandsNeedDelete)
2818 delete [] OperandList;
2819 OperandList = new SDOperand[NumOps];
2820 OperandsNeedDelete = true;
2821 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002822
2823 // Assign the new operands.
2824 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002825
2826 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2827 OperandList[i] = Ops[i];
2828 SDNode *N = OperandList[i].Val;
2829 N->Uses.push_back(this);
2830 }
2831}
Chris Lattner149c58c2005-08-16 18:17:10 +00002832
2833/// SelectNodeTo - These are used for target selectors to *mutate* the
2834/// specified node to have the specified return type, Target opcode, and
2835/// operands. Note that target opcodes are stored as
2836/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002837///
2838/// Note that SelectNodeTo returns the resultant node. If there is already a
2839/// node of the specified opcode and operands, it returns that node instead of
2840/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002841SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2842 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002843 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002844 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002845 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002846 void *IP = 0;
2847 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002848 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002849
Chris Lattner7651fa42005-08-24 23:00:29 +00002850 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002851
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002852 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002853
Chris Lattner4a283e92006-08-11 18:38:11 +00002854 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002855 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002856}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002857
Evan Cheng95514ba2006-08-26 08:00:10 +00002858SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2859 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002860 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002861 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002862 SDOperand Ops[] = { Op1 };
2863
Jim Laskey583bd472006-10-27 23:46:08 +00002864 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002865 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002866 void *IP = 0;
2867 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002868 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002869
Chris Lattner149c58c2005-08-16 18:17:10 +00002870 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002871 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002872 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002873 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002874}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002875
Evan Cheng95514ba2006-08-26 08:00:10 +00002876SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2877 MVT::ValueType VT, SDOperand Op1,
2878 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002879 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002880 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002881 SDOperand Ops[] = { Op1, Op2 };
2882
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, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002885 void *IP = 0;
2886 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002887 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002888
Chris Lattner149c58c2005-08-16 18:17:10 +00002889 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002890
Chris Lattner63e3f142007-02-04 07:28:00 +00002891 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002892
Chris Lattnera5682852006-08-07 23:03:03 +00002893 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002894 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +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,
2899 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002900 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002901 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002902 SDOperand Ops[] = { Op1, Op2, Op3 };
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, 3);
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 Lattner67612a12007-02-04 02:32:44 +00002910
Chris Lattner63e3f142007-02-04 07:28:00 +00002911 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002912
Chris Lattnera5682852006-08-07 23:03:03 +00002913 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002914 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002915}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002916
Evan Cheng95514ba2006-08-26 08:00:10 +00002917SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002918 MVT::ValueType VT, const SDOperand *Ops,
2919 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002920 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002921 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002922 FoldingSetNodeID ID;
2923 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
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 Lattner6b09a292005-08-21 18:49:33 +00002928 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002929 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002930
Chris Lattnera5682852006-08-07 23:03:03 +00002931 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002932 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002933}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002934
Evan Cheng95514ba2006-08-26 08:00:10 +00002935SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2936 MVT::ValueType VT1, MVT::ValueType VT2,
2937 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002938 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002939 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002940 SDOperand Ops[] = { Op1, Op2 };
2941 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002942 void *IP = 0;
2943 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002944 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002945
Chris Lattner0fb094f2005-11-19 01:44:53 +00002946 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002947 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002948 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002949 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002950}
2951
Evan Cheng95514ba2006-08-26 08:00:10 +00002952SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2953 MVT::ValueType VT1, MVT::ValueType VT2,
2954 SDOperand Op1, SDOperand Op2,
2955 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002956 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002957 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00002958 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002959 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002960 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002961 void *IP = 0;
2962 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002963 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002964
Chris Lattner0fb094f2005-11-19 01:44:53 +00002965 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002966
Chris Lattner63e3f142007-02-04 07:28:00 +00002967 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002968 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002969 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002970}
2971
Chris Lattner0fb094f2005-11-19 01:44:53 +00002972
Evan Cheng6ae46c42006-02-09 07:15:23 +00002973/// getTargetNode - These are used for target selectors to create a new node
2974/// with specified return type(s), target opcode, and operands.
2975///
2976/// Note that getTargetNode returns the resultant node. If there is already a
2977/// node of the specified opcode and operands, it returns that node instead of
2978/// the current one.
2979SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2980 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2981}
2982SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2983 SDOperand Op1) {
2984 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2985}
2986SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2987 SDOperand Op1, SDOperand Op2) {
2988 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2989}
2990SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002991 SDOperand Op1, SDOperand Op2,
2992 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002993 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2994}
2995SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002996 const SDOperand *Ops, unsigned NumOps) {
2997 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002998}
2999SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3000 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003001 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003002 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003003}
3004SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003005 MVT::ValueType VT2, SDOperand Op1,
3006 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003007 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003008 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003009 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003010}
3011SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003012 MVT::ValueType VT2, SDOperand Op1,
3013 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003014 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003015 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003016 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003017}
Evan Cheng694481e2006-08-27 08:08:54 +00003018SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3019 MVT::ValueType VT2,
3020 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003021 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003022 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003023}
3024SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3025 MVT::ValueType VT2, MVT::ValueType VT3,
3026 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003027 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003028 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003029 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003030}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003031SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3032 MVT::ValueType VT2, MVT::ValueType VT3,
3033 SDOperand Op1, SDOperand Op2,
3034 SDOperand Op3) {
3035 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3036 SDOperand Ops[] = { Op1, Op2, Op3 };
3037 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3038}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003039SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003040 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003041 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003042 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3043 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003044}
3045
Evan Cheng99157a02006-08-07 22:13:29 +00003046/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003047/// This can cause recursive merging of nodes in the DAG.
3048///
Chris Lattner8b52f212005-08-26 18:36:28 +00003049/// This version assumes From/To have a single result value.
3050///
Chris Lattner1e111c72005-09-07 05:37:01 +00003051void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3052 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003053 SDNode *From = FromN.Val, *To = ToN.Val;
3054 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3055 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003056 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003057
Chris Lattner8b8749f2005-08-17 19:00:20 +00003058 while (!From->use_empty()) {
3059 // Process users until they are all gone.
3060 SDNode *U = *From->use_begin();
3061
3062 // This node is about to morph, remove its old self from the CSE maps.
3063 RemoveNodeFromCSEMaps(U);
3064
Chris Lattner65113b22005-11-08 22:07:03 +00003065 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3066 I != E; ++I)
3067 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003068 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003069 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003070 To->addUser(U);
3071 }
3072
3073 // Now that we have modified U, add it back to the CSE maps. If it already
3074 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003075 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3076 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003077 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003078 if (Deleted) Deleted->push_back(U);
3079 DeleteNodeNotInCSEMaps(U);
3080 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003081 }
3082}
3083
3084/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3085/// This can cause recursive merging of nodes in the DAG.
3086///
3087/// This version assumes From/To have matching types and numbers of result
3088/// values.
3089///
Chris Lattner1e111c72005-09-07 05:37:01 +00003090void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3091 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003092 assert(From != To && "Cannot replace uses of with self");
3093 assert(From->getNumValues() == To->getNumValues() &&
3094 "Cannot use this version of ReplaceAllUsesWith!");
3095 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003096 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003097 return;
3098 }
3099
3100 while (!From->use_empty()) {
3101 // Process users until they are all gone.
3102 SDNode *U = *From->use_begin();
3103
3104 // This node is about to morph, remove its old self from the CSE maps.
3105 RemoveNodeFromCSEMaps(U);
3106
Chris Lattner65113b22005-11-08 22:07:03 +00003107 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3108 I != E; ++I)
3109 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003110 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003111 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003112 To->addUser(U);
3113 }
3114
3115 // Now that we have modified U, add it back to the CSE maps. If it already
3116 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003117 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3118 ReplaceAllUsesWith(U, Existing, Deleted);
3119 // U is now dead.
3120 if (Deleted) Deleted->push_back(U);
3121 DeleteNodeNotInCSEMaps(U);
3122 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003123 }
3124}
3125
Chris Lattner8b52f212005-08-26 18:36:28 +00003126/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3127/// This can cause recursive merging of nodes in the DAG.
3128///
3129/// This version can replace From with any result values. To must match the
3130/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003131void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003132 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003133 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003134 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003135 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003136 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003137 return;
3138 }
3139
3140 while (!From->use_empty()) {
3141 // Process users until they are all gone.
3142 SDNode *U = *From->use_begin();
3143
3144 // This node is about to morph, remove its old self from the CSE maps.
3145 RemoveNodeFromCSEMaps(U);
3146
Chris Lattner65113b22005-11-08 22:07:03 +00003147 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3148 I != E; ++I)
3149 if (I->Val == From) {
3150 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003151 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003152 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003153 ToOp.Val->addUser(U);
3154 }
3155
3156 // Now that we have modified U, add it back to the CSE maps. If it already
3157 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003158 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3159 ReplaceAllUsesWith(U, Existing, Deleted);
3160 // U is now dead.
3161 if (Deleted) Deleted->push_back(U);
3162 DeleteNodeNotInCSEMaps(U);
3163 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003164 }
3165}
3166
Chris Lattner012f2412006-02-17 21:58:01 +00003167/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3168/// uses of other values produced by From.Val alone. The Deleted vector is
3169/// handled the same was as for ReplaceAllUsesWith.
3170void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
3171 std::vector<SDNode*> &Deleted) {
3172 assert(From != To && "Cannot replace a value with itself");
3173 // Handle the simple, trivial, case efficiently.
3174 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
3175 ReplaceAllUsesWith(From, To, &Deleted);
3176 return;
3177 }
3178
Chris Lattnercf5640b2007-02-04 00:14:31 +00003179 // Get all of the users of From.Val. We want these in a nice,
3180 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3181 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003182
3183 while (!Users.empty()) {
3184 // We know that this user uses some value of From. If it is the right
3185 // value, update it.
3186 SDNode *User = Users.back();
3187 Users.pop_back();
3188
3189 for (SDOperand *Op = User->OperandList,
3190 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
3191 if (*Op == From) {
3192 // Okay, we know this user needs to be updated. Remove its old self
3193 // from the CSE maps.
3194 RemoveNodeFromCSEMaps(User);
3195
3196 // Update all operands that match "From".
3197 for (; Op != E; ++Op) {
3198 if (*Op == From) {
3199 From.Val->removeUser(User);
3200 *Op = To;
3201 To.Val->addUser(User);
3202 }
3203 }
3204
3205 // Now that we have modified User, add it back to the CSE maps. If it
3206 // already exists there, recursively merge the results together.
3207 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
3208 unsigned NumDeleted = Deleted.size();
3209 ReplaceAllUsesWith(User, Existing, &Deleted);
3210
3211 // User is now dead.
3212 Deleted.push_back(User);
3213 DeleteNodeNotInCSEMaps(User);
3214
3215 // We have to be careful here, because ReplaceAllUsesWith could have
3216 // deleted a user of From, which means there may be dangling pointers
3217 // in the "Users" setvector. Scan over the deleted node pointers and
3218 // remove them from the setvector.
3219 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
3220 Users.remove(Deleted[i]);
3221 }
3222 break; // Exit the operand scanning loop.
3223 }
3224 }
3225 }
3226}
3227
Chris Lattner7b2880c2005-08-24 22:44:39 +00003228
Evan Chenge6f35d82006-08-01 08:20:41 +00003229/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3230/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003231unsigned SelectionDAG::AssignNodeIds() {
3232 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003233 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3234 SDNode *N = I;
3235 N->setNodeId(Id++);
3236 }
3237 return Id;
3238}
3239
Evan Chenge6f35d82006-08-01 08:20:41 +00003240/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003241/// based on their topological order. It returns the maximum id and a vector
3242/// of the SDNodes* in assigned order by reference.
3243unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003244 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003245 std::vector<unsigned> InDegree(DAGSize);
3246 std::vector<SDNode*> Sources;
3247
3248 // Use a two pass approach to avoid using a std::map which is slow.
3249 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003250 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3251 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003252 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003253 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003254 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003255 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003256 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003257 }
3258
Evan Cheng99157a02006-08-07 22:13:29 +00003259 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003260 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003261 SDNode *N = Sources.back();
3262 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003263 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003264 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3265 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003266 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003267 if (Degree == 0)
3268 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003269 }
3270 }
3271
Evan Chengc384d6c2006-08-02 22:00:34 +00003272 // Second pass, assign the actual topological order as node ids.
3273 Id = 0;
3274 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3275 TI != TE; ++TI)
3276 (*TI)->setNodeId(Id++);
3277
3278 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003279}
3280
3281
Evan Cheng091cba12006-07-27 06:39:06 +00003282
Jim Laskey58b968b2005-08-17 20:08:02 +00003283//===----------------------------------------------------------------------===//
3284// SDNode Class
3285//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003286
Chris Lattner917d2c92006-07-19 00:00:37 +00003287// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003288void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003289void UnarySDNode::ANCHOR() {}
3290void BinarySDNode::ANCHOR() {}
3291void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003292void HandleSDNode::ANCHOR() {}
3293void StringSDNode::ANCHOR() {}
3294void ConstantSDNode::ANCHOR() {}
3295void ConstantFPSDNode::ANCHOR() {}
3296void GlobalAddressSDNode::ANCHOR() {}
3297void FrameIndexSDNode::ANCHOR() {}
3298void JumpTableSDNode::ANCHOR() {}
3299void ConstantPoolSDNode::ANCHOR() {}
3300void BasicBlockSDNode::ANCHOR() {}
3301void SrcValueSDNode::ANCHOR() {}
3302void RegisterSDNode::ANCHOR() {}
3303void ExternalSymbolSDNode::ANCHOR() {}
3304void CondCodeSDNode::ANCHOR() {}
3305void VTSDNode::ANCHOR() {}
3306void LoadSDNode::ANCHOR() {}
3307void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003308
Chris Lattner48b85922007-02-04 02:41:42 +00003309HandleSDNode::~HandleSDNode() {
3310 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003311 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003312}
3313
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003314GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3315 MVT::ValueType VT, int o)
3316 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003317 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003318 // Thread Local
3319 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3320 // Non Thread Local
3321 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3322 getSDVTList(VT)), Offset(o) {
3323 TheGlobal = const_cast<GlobalValue*>(GA);
3324}
Chris Lattner48b85922007-02-04 02:41:42 +00003325
Jim Laskey583bd472006-10-27 23:46:08 +00003326/// Profile - Gather unique data for the node.
3327///
3328void SDNode::Profile(FoldingSetNodeID &ID) {
3329 AddNodeIDNode(ID, this);
3330}
3331
Chris Lattnera3255112005-11-08 23:30:28 +00003332/// getValueTypeList - Return a pointer to the specified value type.
3333///
3334MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3335 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3336 VTs[VT] = VT;
3337 return &VTs[VT];
3338}
Chris Lattnera5682852006-08-07 23:03:03 +00003339
Chris Lattner5c884562005-01-12 18:37:47 +00003340/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3341/// indicated value. This method ignores uses of other values defined by this
3342/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003343bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003344 assert(Value < getNumValues() && "Bad value!");
3345
3346 // If there is only one value, this is easy.
3347 if (getNumValues() == 1)
3348 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003349 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003350
Evan Cheng4ee62112006-02-05 06:29:23 +00003351 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003352
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003353 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003354
Chris Lattnerf83482d2006-08-16 20:59:32 +00003355 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003356 SDNode *User = *UI;
3357 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003358 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003359 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3360 if (User->getOperand(i) == TheValue) {
3361 if (NUses == 0)
3362 return false; // too many uses
3363 --NUses;
3364 }
3365 }
3366
3367 // Found exactly the right number of uses?
3368 return NUses == 0;
3369}
3370
3371
Evan Cheng33d55952007-08-02 05:29:38 +00003372/// hasAnyUseOfValue - Return true if there are any use of the indicated
3373/// value. This method ignores uses of other values defined by this operation.
3374bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3375 assert(Value < getNumValues() && "Bad value!");
3376
3377 if (use_size() == 0) return false;
3378
3379 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3380
3381 SmallPtrSet<SDNode*, 32> UsersHandled;
3382
3383 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3384 SDNode *User = *UI;
3385 if (User->getNumOperands() == 1 ||
3386 UsersHandled.insert(User)) // First time we've seen this?
3387 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3388 if (User->getOperand(i) == TheValue) {
3389 return true;
3390 }
3391 }
3392
3393 return false;
3394}
3395
3396
Evan Chenge6e97e62006-11-03 07:31:32 +00003397/// isOnlyUse - Return true if this node is the only use of N.
3398///
Evan Cheng4ee62112006-02-05 06:29:23 +00003399bool SDNode::isOnlyUse(SDNode *N) const {
3400 bool Seen = false;
3401 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3402 SDNode *User = *I;
3403 if (User == this)
3404 Seen = true;
3405 else
3406 return false;
3407 }
3408
3409 return Seen;
3410}
3411
Evan Chenge6e97e62006-11-03 07:31:32 +00003412/// isOperand - Return true if this node is an operand of N.
3413///
Evan Chengbfa284f2006-03-03 06:42:32 +00003414bool SDOperand::isOperand(SDNode *N) const {
3415 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3416 if (*this == N->getOperand(i))
3417 return true;
3418 return false;
3419}
3420
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003421bool SDNode::isOperand(SDNode *N) const {
3422 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3423 if (this == N->OperandList[i].Val)
3424 return true;
3425 return false;
3426}
Evan Cheng4ee62112006-02-05 06:29:23 +00003427
Evan Chengc5fc57d2006-11-03 03:05:24 +00003428static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003429 SmallPtrSet<SDNode *, 32> &Visited) {
3430 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003431 return;
3432
3433 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3434 SDNode *Op = N->getOperand(i).Val;
3435 if (Op == P) {
3436 found = true;
3437 return;
3438 }
3439 findPredecessor(Op, P, found, Visited);
3440 }
3441}
3442
Evan Chenge6e97e62006-11-03 07:31:32 +00003443/// isPredecessor - Return true if this node is a predecessor of N. This node
3444/// is either an operand of N or it can be reached by recursively traversing
3445/// up the operands.
3446/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003447bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003448 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003449 bool found = false;
3450 findPredecessor(N, this, found, Visited);
3451 return found;
3452}
3453
Evan Chengc5484282006-10-04 00:56:09 +00003454uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3455 assert(Num < NumOperands && "Invalid child # of SDNode!");
3456 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3457}
3458
Reid Spencer577cc322007-04-01 07:32:19 +00003459std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003460 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003461 default:
3462 if (getOpcode() < ISD::BUILTIN_OP_END)
3463 return "<<Unknown DAG Node>>";
3464 else {
Evan Cheng72261582005-12-20 06:22:03 +00003465 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003466 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003467 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3468 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003469
Evan Cheng72261582005-12-20 06:22:03 +00003470 TargetLowering &TLI = G->getTargetLoweringInfo();
3471 const char *Name =
3472 TLI.getTargetNodeName(getOpcode());
3473 if (Name) return Name;
3474 }
3475
3476 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003477 }
3478
Andrew Lenharth95762122005-03-31 21:24:06 +00003479 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003480 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003481 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003482 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003483 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003484 case ISD::AssertSext: return "AssertSext";
3485 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003486
3487 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003488 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003489 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003490 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003491
3492 case ISD::Constant: return "Constant";
3493 case ISD::ConstantFP: return "ConstantFP";
3494 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003495 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003496 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003497 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003498 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003499 case ISD::RETURNADDR: return "RETURNADDR";
3500 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003501 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003502 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3503 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003504 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003505 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003506 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003507 case ISD::INTRINSIC_WO_CHAIN: {
3508 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3509 return Intrinsic::getName((Intrinsic::ID)IID);
3510 }
3511 case ISD::INTRINSIC_VOID:
3512 case ISD::INTRINSIC_W_CHAIN: {
3513 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003514 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003515 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003516
Chris Lattnerb2827b02006-03-19 00:52:58 +00003517 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003518 case ISD::TargetConstant: return "TargetConstant";
3519 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003520 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003521 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003522 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003523 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003524 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003525 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003526
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003527 case ISD::CopyToReg: return "CopyToReg";
3528 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003529 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003530 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003531 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003532 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003533 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003534 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003535 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003536
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003537 // Unary operators
3538 case ISD::FABS: return "fabs";
3539 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003540 case ISD::FSQRT: return "fsqrt";
3541 case ISD::FSIN: return "fsin";
3542 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003543 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003544
3545 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003546 case ISD::ADD: return "add";
3547 case ISD::SUB: return "sub";
3548 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003549 case ISD::MULHU: return "mulhu";
3550 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003551 case ISD::SDIV: return "sdiv";
3552 case ISD::UDIV: return "udiv";
3553 case ISD::SREM: return "srem";
3554 case ISD::UREM: return "urem";
3555 case ISD::AND: return "and";
3556 case ISD::OR: return "or";
3557 case ISD::XOR: return "xor";
3558 case ISD::SHL: return "shl";
3559 case ISD::SRA: return "sra";
3560 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003561 case ISD::ROTL: return "rotl";
3562 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003563 case ISD::FADD: return "fadd";
3564 case ISD::FSUB: return "fsub";
3565 case ISD::FMUL: return "fmul";
3566 case ISD::FDIV: return "fdiv";
3567 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003568 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003569
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003570 case ISD::SETCC: return "setcc";
3571 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003572 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003573 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003574 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003575 case ISD::CONCAT_VECTORS: return "concat_vectors";
3576 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003577 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003578 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003579 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003580 case ISD::ADDC: return "addc";
3581 case ISD::ADDE: return "adde";
3582 case ISD::SUBC: return "subc";
3583 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003584 case ISD::SHL_PARTS: return "shl_parts";
3585 case ISD::SRA_PARTS: return "sra_parts";
3586 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003587
3588 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3589 case ISD::INSERT_SUBREG: return "insert_subreg";
3590
Chris Lattner7f644642005-04-28 21:44:03 +00003591 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003592 case ISD::SIGN_EXTEND: return "sign_extend";
3593 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003594 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003595 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003596 case ISD::TRUNCATE: return "truncate";
3597 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00003598 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003599 case ISD::FP_EXTEND: return "fp_extend";
3600
3601 case ISD::SINT_TO_FP: return "sint_to_fp";
3602 case ISD::UINT_TO_FP: return "uint_to_fp";
3603 case ISD::FP_TO_SINT: return "fp_to_sint";
3604 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003605 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003606
3607 // Control flow instructions
3608 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003609 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003610 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003611 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003612 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003613 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003614 case ISD::CALLSEQ_START: return "callseq_start";
3615 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003616
3617 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003618 case ISD::LOAD: return "load";
3619 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003620 case ISD::VAARG: return "vaarg";
3621 case ISD::VACOPY: return "vacopy";
3622 case ISD::VAEND: return "vaend";
3623 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003624 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003625 case ISD::EXTRACT_ELEMENT: return "extract_element";
3626 case ISD::BUILD_PAIR: return "build_pair";
3627 case ISD::STACKSAVE: return "stacksave";
3628 case ISD::STACKRESTORE: return "stackrestore";
3629
3630 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003631 case ISD::MEMSET: return "memset";
3632 case ISD::MEMCPY: return "memcpy";
3633 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003634
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003635 // Bit manipulation
3636 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003637 case ISD::CTPOP: return "ctpop";
3638 case ISD::CTTZ: return "cttz";
3639 case ISD::CTLZ: return "ctlz";
3640
Chris Lattner36ce6912005-11-29 06:21:05 +00003641 // Debug info
3642 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003643 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003644
Duncan Sands36397f52007-07-27 12:58:54 +00003645 // Trampolines
3646 case ISD::ADJUST_TRAMP: return "adjust_tramp";
3647 case ISD::TRAMPOLINE: return "trampoline";
3648
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003649 case ISD::CONDCODE:
3650 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003651 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003652 case ISD::SETOEQ: return "setoeq";
3653 case ISD::SETOGT: return "setogt";
3654 case ISD::SETOGE: return "setoge";
3655 case ISD::SETOLT: return "setolt";
3656 case ISD::SETOLE: return "setole";
3657 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003658
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003659 case ISD::SETO: return "seto";
3660 case ISD::SETUO: return "setuo";
3661 case ISD::SETUEQ: return "setue";
3662 case ISD::SETUGT: return "setugt";
3663 case ISD::SETUGE: return "setuge";
3664 case ISD::SETULT: return "setult";
3665 case ISD::SETULE: return "setule";
3666 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003667
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003668 case ISD::SETEQ: return "seteq";
3669 case ISD::SETGT: return "setgt";
3670 case ISD::SETGE: return "setge";
3671 case ISD::SETLT: return "setlt";
3672 case ISD::SETLE: return "setle";
3673 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003674 }
3675 }
3676}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003677
Evan Cheng144d8f02006-11-09 17:55:04 +00003678const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003679 switch (AM) {
3680 default:
3681 return "";
3682 case ISD::PRE_INC:
3683 return "<pre-inc>";
3684 case ISD::PRE_DEC:
3685 return "<pre-dec>";
3686 case ISD::POST_INC:
3687 return "<post-inc>";
3688 case ISD::POST_DEC:
3689 return "<post-dec>";
3690 }
3691}
3692
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003693void SDNode::dump() const { dump(0); }
3694void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003695 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003696
3697 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003698 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003699 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003700 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003701 else
Bill Wendling832171c2006-12-07 20:04:42 +00003702 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003703 }
Bill Wendling832171c2006-12-07 20:04:42 +00003704 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003705
Bill Wendling832171c2006-12-07 20:04:42 +00003706 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003707 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003708 if (i) cerr << ", ";
3709 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003710 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003711 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003712 }
3713
3714 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003715 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003716 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003717 cerr << "<" << (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle ?
3718 CSDN->getValueAPF().convertToFloat() :
3719 CSDN->getValueAPF().convertToDouble()) << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003720 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003721 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003722 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003723 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003724 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003725 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003726 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003727 else
Bill Wendling832171c2006-12-07 20:04:42 +00003728 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003729 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003730 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003731 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003732 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003733 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003734 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003735 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003736 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003737 else
Bill Wendling832171c2006-12-07 20:04:42 +00003738 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003739 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003740 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003741 else
Bill Wendling832171c2006-12-07 20:04:42 +00003742 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003743 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003744 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003745 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3746 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003747 cerr << LBB->getName() << " ";
3748 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003749 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003750 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003751 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003752 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003753 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003754 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003755 } else if (const ExternalSymbolSDNode *ES =
3756 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003757 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003758 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3759 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003760 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003761 else
Bill Wendling832171c2006-12-07 20:04:42 +00003762 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003763 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003764 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003765 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3766 bool doExt = true;
3767 switch (LD->getExtensionType()) {
3768 default: doExt = false; break;
3769 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003770 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003771 break;
3772 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003773 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003774 break;
3775 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003776 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003777 break;
3778 }
3779 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003780 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003781
Evan Cheng144d8f02006-11-09 17:55:04 +00003782 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003783 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003784 cerr << " " << AM;
Evan Cheng2caccca2006-10-17 21:14:32 +00003785 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3786 if (ST->isTruncatingStore())
Bill Wendling832171c2006-12-07 20:04:42 +00003787 cerr << " <trunc "
3788 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00003789
Evan Cheng144d8f02006-11-09 17:55:04 +00003790 const char *AM = getIndexedModeName(ST->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003791 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003792 cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003793 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003794}
3795
Chris Lattnerde202b32005-11-09 23:47:37 +00003796static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003797 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3798 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003799 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003800 else
Bill Wendling832171c2006-12-07 20:04:42 +00003801 cerr << "\n" << std::string(indent+2, ' ')
3802 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003803
Chris Lattnerea946cd2005-01-09 20:38:33 +00003804
Bill Wendling832171c2006-12-07 20:04:42 +00003805 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003806 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003807}
3808
Chris Lattnerc3aae252005-01-07 07:46:32 +00003809void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003810 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003811 std::vector<const SDNode*> Nodes;
3812 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3813 I != E; ++I)
3814 Nodes.push_back(I);
3815
Chris Lattner49d24712005-01-09 20:26:36 +00003816 std::sort(Nodes.begin(), Nodes.end());
3817
3818 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003819 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003820 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003821 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003822
Jim Laskey26f7fa72006-10-17 19:33:52 +00003823 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003824
Bill Wendling832171c2006-12-07 20:04:42 +00003825 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003826}
3827
Evan Chengd6594ae2006-09-12 21:00:35 +00003828const Type *ConstantPoolSDNode::getType() const {
3829 if (isMachineConstantPoolEntry())
3830 return Val.MachineCPVal->getType();
3831 return Val.ConstVal->getType();
3832}