blob: 118f980c10f7b7261018a1d09bbd216b2bc36bc6 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohmanc6c391d2008-01-31 00:25:39 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000025#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000026#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000027#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000030#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000031#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000032#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000033#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000034#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattner0b3e5252006-08-15 19:11:05 +000039/// makeVTList - Return an instance of the SDVTList struct initialized with the
40/// specified members.
41static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
42 SDVTList Res = {VTs, NumVTs};
43 return Res;
44}
45
Jim Laskey58b968b2005-08-17 20:08:02 +000046//===----------------------------------------------------------------------===//
47// ConstantFPSDNode Class
48//===----------------------------------------------------------------------===//
49
50/// isExactlyValue - We don't rely on operator== working on double values, as
51/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
52/// As such, this method can be used to do an exact bit-for-bit comparison of
53/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000054bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000055 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000056}
57
Dale Johannesenf04afdb2007-08-30 00:23:21 +000058bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
59 const APFloat& Val) {
60 // convert modifies in place, so make a copy.
61 APFloat Val2 = APFloat(Val);
62 switch (VT) {
63 default:
64 return false; // These can't be represented as floating point!
65
66 // FIXME rounding mode needs to be more flexible
67 case MVT::f32:
68 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
69 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
70 APFloat::opOK;
71 case MVT::f64:
72 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
73 &Val2.getSemantics() == &APFloat::IEEEdouble ||
74 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
75 APFloat::opOK;
76 // TODO: Figure out how to test if we can use a shorter type instead!
77 case MVT::f80:
78 case MVT::f128:
79 case MVT::ppcf128:
80 return true;
81 }
82}
83
Jim Laskey58b968b2005-08-17 20:08:02 +000084//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000085// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000086//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000087
Evan Chenga8df1662006-03-27 06:58:47 +000088/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000089/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000090bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000091 // Look through a bit convert.
92 if (N->getOpcode() == ISD::BIT_CONVERT)
93 N = N->getOperand(0).Val;
94
Evan Chenga8df1662006-03-27 06:58:47 +000095 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000096
97 unsigned i = 0, e = N->getNumOperands();
98
99 // Skip over all of the undef values.
100 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
101 ++i;
102
103 // Do not accept an all-undef vector.
104 if (i == e) return false;
105
106 // Do not accept build_vectors that aren't all constants or which have non-~0
107 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000108 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000109 if (isa<ConstantSDNode>(NotZero)) {
110 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
111 return false;
112 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000113 MVT::ValueType VT = NotZero.getValueType();
114 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000115 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
116 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000117 return false;
118 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000119 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
120 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000121 (uint32_t)-1)
122 return false;
123 }
Evan Chenga8df1662006-03-27 06:58:47 +0000124 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000125 return false;
126
127 // Okay, we have at least one ~0 value, check to see if the rest match or are
128 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000129 for (++i; i != e; ++i)
130 if (N->getOperand(i) != NotZero &&
131 N->getOperand(i).getOpcode() != ISD::UNDEF)
132 return false;
133 return true;
134}
135
136
Evan Cheng4a147842006-03-26 09:50:58 +0000137/// isBuildVectorAllZeros - Return true if the specified node is a
138/// BUILD_VECTOR where all of the elements are 0 or undef.
139bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000140 // Look through a bit convert.
141 if (N->getOpcode() == ISD::BIT_CONVERT)
142 N = N->getOperand(0).Val;
143
Evan Cheng4a147842006-03-26 09:50:58 +0000144 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000145
146 unsigned i = 0, e = N->getNumOperands();
147
148 // Skip over all of the undef values.
149 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
150 ++i;
151
152 // Do not accept an all-undef vector.
153 if (i == e) return false;
154
155 // Do not accept build_vectors that aren't all constants or which have non-~0
156 // elements.
157 SDOperand Zero = N->getOperand(i);
158 if (isa<ConstantSDNode>(Zero)) {
159 if (!cast<ConstantSDNode>(Zero)->isNullValue())
160 return false;
161 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000162 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000163 return false;
164 } else
165 return false;
166
167 // Okay, we have at least one ~0 value, check to see if the rest match or are
168 // undefs.
169 for (++i; i != e; ++i)
170 if (N->getOperand(i) != Zero &&
171 N->getOperand(i).getOpcode() != ISD::UNDEF)
172 return false;
173 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000174}
175
Chris Lattnerc3aae252005-01-07 07:46:32 +0000176/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
177/// when given the operation for (X op Y).
178ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
179 // To perform this operation, we just need to swap the L and G bits of the
180 // operation.
181 unsigned OldL = (Operation >> 2) & 1;
182 unsigned OldG = (Operation >> 1) & 1;
183 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
184 (OldL << 1) | // New G bit
185 (OldG << 2)); // New L bit.
186}
187
188/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
189/// 'op' is a valid SetCC operation.
190ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
191 unsigned Operation = Op;
192 if (isInteger)
193 Operation ^= 7; // Flip L, G, E bits, but not U.
194 else
195 Operation ^= 15; // Flip all of the condition bits.
196 if (Operation > ISD::SETTRUE2)
197 Operation &= ~8; // Don't let N and U bits get set.
198 return ISD::CondCode(Operation);
199}
200
201
202/// isSignedOp - For an integer comparison, return 1 if the comparison is a
203/// signed operation and 2 if the result is an unsigned comparison. Return zero
204/// if the operation does not depend on the sign of the input (setne and seteq).
205static int isSignedOp(ISD::CondCode Opcode) {
206 switch (Opcode) {
207 default: assert(0 && "Illegal integer setcc operation!");
208 case ISD::SETEQ:
209 case ISD::SETNE: return 0;
210 case ISD::SETLT:
211 case ISD::SETLE:
212 case ISD::SETGT:
213 case ISD::SETGE: return 1;
214 case ISD::SETULT:
215 case ISD::SETULE:
216 case ISD::SETUGT:
217 case ISD::SETUGE: return 2;
218 }
219}
220
221/// getSetCCOrOperation - Return the result of a logical OR between different
222/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
223/// returns SETCC_INVALID if it is not possible to represent the resultant
224/// comparison.
225ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
226 bool isInteger) {
227 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
228 // Cannot fold a signed integer setcc with an unsigned integer setcc.
229 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000230
Chris Lattnerc3aae252005-01-07 07:46:32 +0000231 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000232
Chris Lattnerc3aae252005-01-07 07:46:32 +0000233 // If the N and U bits get set then the resultant comparison DOES suddenly
234 // care about orderedness, and is true when ordered.
235 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000236 Op &= ~16; // Clear the U bit if the N bit is set.
237
238 // Canonicalize illegal integer setcc's.
239 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
240 Op = ISD::SETNE;
241
Chris Lattnerc3aae252005-01-07 07:46:32 +0000242 return ISD::CondCode(Op);
243}
244
245/// getSetCCAndOperation - Return the result of a logical AND between different
246/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
247/// function returns zero if it is not possible to represent the resultant
248/// comparison.
249ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
250 bool isInteger) {
251 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
252 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000253 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000254
255 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000256 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
257
258 // Canonicalize illegal integer setcc's.
259 if (isInteger) {
260 switch (Result) {
261 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000262 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
263 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
264 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
265 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000266 }
267 }
268
269 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000270}
271
Chris Lattnerb48da392005-01-23 04:39:44 +0000272const TargetMachine &SelectionDAG::getTarget() const {
273 return TLI.getTargetMachine();
274}
275
Jim Laskey58b968b2005-08-17 20:08:02 +0000276//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000277// SDNode Profile Support
278//===----------------------------------------------------------------------===//
279
Jim Laskeydef69b92006-10-27 23:52:51 +0000280/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
281///
Jim Laskey583bd472006-10-27 23:46:08 +0000282static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
283 ID.AddInteger(OpC);
284}
285
286/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
287/// solely with their pointer.
288void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
289 ID.AddPointer(VTList.VTs);
290}
291
Jim Laskeydef69b92006-10-27 23:52:51 +0000292/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
293///
Jim Laskey583bd472006-10-27 23:46:08 +0000294static void AddNodeIDOperands(FoldingSetNodeID &ID,
295 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000296 for (; NumOps; --NumOps, ++Ops) {
297 ID.AddPointer(Ops->Val);
298 ID.AddInteger(Ops->ResNo);
299 }
Jim Laskey583bd472006-10-27 23:46:08 +0000300}
301
Jim Laskey583bd472006-10-27 23:46:08 +0000302static void AddNodeIDNode(FoldingSetNodeID &ID,
303 unsigned short OpC, SDVTList VTList,
304 const SDOperand *OpList, unsigned N) {
305 AddNodeIDOpcode(ID, OpC);
306 AddNodeIDValueTypes(ID, VTList);
307 AddNodeIDOperands(ID, OpList, N);
308}
309
Jim Laskeydef69b92006-10-27 23:52:51 +0000310/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
311/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000312static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
313 AddNodeIDOpcode(ID, N->getOpcode());
314 // Add the return value info.
315 AddNodeIDValueTypes(ID, N->getVTList());
316 // Add the operand info.
317 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
318
319 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000320 switch (N->getOpcode()) {
321 default: break; // Normal nodes don't need extra info.
322 case ISD::TargetConstant:
323 case ISD::Constant:
324 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
325 break;
326 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000327 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000328 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000329 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000330 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000331 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000332 case ISD::GlobalAddress:
333 case ISD::TargetGlobalTLSAddress:
334 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000335 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
336 ID.AddPointer(GA->getGlobal());
337 ID.AddInteger(GA->getOffset());
338 break;
339 }
340 case ISD::BasicBlock:
341 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
342 break;
343 case ISD::Register:
344 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
345 break;
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000346 case ISD::SRCVALUE:
347 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
348 break;
349 case ISD::MEMOPERAND: {
350 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
351 ID.AddPointer(MO.getValue());
352 ID.AddInteger(MO.getFlags());
353 ID.AddInteger(MO.getOffset());
354 ID.AddInteger(MO.getSize());
355 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000356 break;
357 }
358 case ISD::FrameIndex:
359 case ISD::TargetFrameIndex:
360 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
361 break;
362 case ISD::JumpTable:
363 case ISD::TargetJumpTable:
364 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
365 break;
366 case ISD::ConstantPool:
367 case ISD::TargetConstantPool: {
368 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
369 ID.AddInteger(CP->getAlignment());
370 ID.AddInteger(CP->getOffset());
371 if (CP->isMachineConstantPoolEntry())
372 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
373 else
374 ID.AddPointer(CP->getConstVal());
375 break;
376 }
377 case ISD::LOAD: {
378 LoadSDNode *LD = cast<LoadSDNode>(N);
379 ID.AddInteger(LD->getAddressingMode());
380 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000381 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000382 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());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000390 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000391 ID.AddInteger(ST->getAlignment());
392 ID.AddInteger(ST->isVolatile());
393 break;
394 }
Jim Laskey583bd472006-10-27 23:46:08 +0000395 }
396}
397
398//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000399// SelectionDAG Class
400//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000401
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000402/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000403/// SelectionDAG.
404void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000405 // Create a dummy node (which is not added to allnodes), that adds a reference
406 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000407 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000408
Chris Lattner190a4182006-08-04 17:45:20 +0000409 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000410
Chris Lattner190a4182006-08-04 17:45:20 +0000411 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000412 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000413 if (I->use_empty())
414 DeadNodes.push_back(I);
415
416 // Process the worklist, deleting the nodes and adding their uses to the
417 // worklist.
418 while (!DeadNodes.empty()) {
419 SDNode *N = DeadNodes.back();
420 DeadNodes.pop_back();
421
422 // Take the node out of the appropriate CSE map.
423 RemoveNodeFromCSEMaps(N);
424
425 // Next, brutally remove the operand list. This is safe to do, as there are
426 // no cycles in the graph.
427 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
428 SDNode *Operand = I->Val;
429 Operand->removeUser(N);
430
431 // Now that we removed this operand, see if there are no uses of it left.
432 if (Operand->use_empty())
433 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000434 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000435 if (N->OperandsNeedDelete)
436 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000437 N->OperandList = 0;
438 N->NumOperands = 0;
439
440 // Finally, remove N itself.
441 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000442 }
443
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000444 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000445 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000446}
447
Evan Cheng130a6472006-10-12 20:34:05 +0000448void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
449 SmallVector<SDNode*, 16> DeadNodes;
450 DeadNodes.push_back(N);
451
452 // Process the worklist, deleting the nodes and adding their uses to the
453 // worklist.
454 while (!DeadNodes.empty()) {
455 SDNode *N = DeadNodes.back();
456 DeadNodes.pop_back();
457
458 // Take the node out of the appropriate CSE map.
459 RemoveNodeFromCSEMaps(N);
460
461 // Next, brutally remove the operand list. This is safe to do, as there are
462 // no cycles in the graph.
463 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
464 SDNode *Operand = I->Val;
465 Operand->removeUser(N);
466
467 // Now that we removed this operand, see if there are no uses of it left.
468 if (Operand->use_empty())
469 DeadNodes.push_back(Operand);
470 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000471 if (N->OperandsNeedDelete)
472 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000473 N->OperandList = 0;
474 N->NumOperands = 0;
475
476 // Finally, remove N itself.
477 Deleted.push_back(N);
478 AllNodes.erase(N);
479 }
480}
481
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000482void SelectionDAG::DeleteNode(SDNode *N) {
483 assert(N->use_empty() && "Cannot delete a node that is not dead!");
484
485 // First take this out of the appropriate CSE map.
486 RemoveNodeFromCSEMaps(N);
487
Chris Lattner1e111c72005-09-07 05:37:01 +0000488 // Finally, remove uses due to operands of this node, remove from the
489 // AllNodes list, and delete the node.
490 DeleteNodeNotInCSEMaps(N);
491}
492
493void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
494
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000495 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000496 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000497
498 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000499 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
500 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000501 if (N->OperandsNeedDelete)
502 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000503 N->OperandList = 0;
504 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000505
506 delete N;
507}
508
Chris Lattner149c58c2005-08-16 18:17:10 +0000509/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
510/// correspond to it. This is useful when we're about to delete or repurpose
511/// the node. We don't want future request for structurally identical nodes
512/// to return N anymore.
513void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000514 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000515 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000516 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000517 case ISD::STRING:
518 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
519 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000520 case ISD::CONDCODE:
521 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
522 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000523 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000524 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
525 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000526 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000527 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000528 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000529 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000530 Erased =
531 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000532 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000533 case ISD::VALUETYPE: {
534 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
535 if (MVT::isExtendedVT(VT)) {
536 Erased = ExtendedValueTypeNodes.erase(VT);
537 } else {
538 Erased = ValueTypeNodes[VT] != 0;
539 ValueTypeNodes[VT] = 0;
540 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000541 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000542 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000543 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000544 // Remove it from the CSE Map.
545 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000546 break;
547 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000548#ifndef NDEBUG
549 // Verify that the node was actually in one of the CSE maps, unless it has a
550 // flag result (which cannot be CSE'd) or is one of the special cases that are
551 // not subject to CSE.
552 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000553 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000554 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000555 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000556 assert(0 && "Node is not in map!");
557 }
558#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000559}
560
Chris Lattner8b8749f2005-08-17 19:00:20 +0000561/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
562/// has been taken out and modified in some way. If the specified node already
563/// exists in the CSE maps, do not modify the maps, but return the existing node
564/// instead. If it doesn't exist, add it and return null.
565///
566SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
567 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000568 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000569 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000570
Chris Lattner9f8cc692005-12-19 22:21:21 +0000571 // Check that remaining values produced are not flags.
572 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
573 if (N->getValueType(i) == MVT::Flag)
574 return 0; // Never CSE anything that produces a flag.
575
Chris Lattnera5682852006-08-07 23:03:03 +0000576 SDNode *New = CSEMap.GetOrInsertNode(N);
577 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000578 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000579}
580
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000581/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
582/// were replaced with those specified. If this node is never memoized,
583/// return null, otherwise return a pointer to the slot it would take. If a
584/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000585SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
586 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000587 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000588 return 0; // Never add these nodes.
589
590 // Check that remaining values produced are not flags.
591 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
592 if (N->getValueType(i) == MVT::Flag)
593 return 0; // Never CSE anything that produces a flag.
594
Chris Lattner63e3f142007-02-04 07:28:00 +0000595 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000596 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000597 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000598 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000599}
600
601/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
602/// were replaced with those specified. If this node is never memoized,
603/// return null, otherwise return a pointer to the slot it would take. If a
604/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000605SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
606 SDOperand Op1, SDOperand Op2,
607 void *&InsertPos) {
608 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
609 return 0; // Never add these nodes.
610
611 // Check that remaining values produced are not flags.
612 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
613 if (N->getValueType(i) == MVT::Flag)
614 return 0; // Never CSE anything that produces a flag.
615
Chris Lattner63e3f142007-02-04 07:28:00 +0000616 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000617 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000618 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000619 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
620}
621
622
623/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
624/// were replaced with those specified. If this node is never memoized,
625/// return null, otherwise return a pointer to the slot it would take. If a
626/// node already exists with these operands, the slot will be non-null.
627SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000628 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000629 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000630 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000631 return 0; // Never add these nodes.
632
633 // Check that remaining values produced are not flags.
634 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
635 if (N->getValueType(i) == MVT::Flag)
636 return 0; // Never CSE anything that produces a flag.
637
Jim Laskey583bd472006-10-27 23:46:08 +0000638 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000639 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000640
Evan Cheng9629aba2006-10-11 01:47:58 +0000641 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
642 ID.AddInteger(LD->getAddressingMode());
643 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000644 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000645 ID.AddInteger(LD->getAlignment());
646 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000647 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
648 ID.AddInteger(ST->getAddressingMode());
649 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000650 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000651 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!");
Dan Gohman89081322007-12-12 22:21:26 +0000689
690 MVT::ValueType EltVT =
691 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000692
Chris Lattner61b09412006-08-11 21:01:22 +0000693 // Mask out any bits that are not valid for this constant.
Dan Gohman89081322007-12-12 22:21:26 +0000694 Val &= MVT::getIntVTBitMask(EltVT);
Chris Lattner61b09412006-08-11 21:01:22 +0000695
696 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000697 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000698 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000699 ID.AddInteger(Val);
700 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000701 SDNode *N = NULL;
702 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
703 if (!MVT::isVector(VT))
704 return SDOperand(N, 0);
705 if (!N) {
706 N = new ConstantSDNode(isT, Val, EltVT);
707 CSEMap.InsertNode(N, IP);
708 AllNodes.push_back(N);
709 }
710
711 SDOperand Result(N, 0);
712 if (MVT::isVector(VT)) {
713 SmallVector<SDOperand, 8> Ops;
714 Ops.assign(MVT::getVectorNumElements(VT), Result);
715 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
716 }
717 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000718}
719
Chris Lattner0bd48932008-01-17 07:00:52 +0000720SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
721 return getConstant(Val, TLI.getPointerTy(), isTarget);
722}
723
724
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000725SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000726 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000728
Dan Gohman7f321562007-06-25 16:23:39 +0000729 MVT::ValueType EltVT =
730 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000731
Chris Lattnerd8658612005-02-17 20:17:32 +0000732 // Do the map lookup using the actual bit pattern for the floating point
733 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
734 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000735 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000736 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000737 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000738 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000739 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000740 SDNode *N = NULL;
741 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
742 if (!MVT::isVector(VT))
743 return SDOperand(N, 0);
744 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000745 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000746 CSEMap.InsertNode(N, IP);
747 AllNodes.push_back(N);
748 }
749
Dan Gohman7f321562007-06-25 16:23:39 +0000750 SDOperand Result(N, 0);
751 if (MVT::isVector(VT)) {
752 SmallVector<SDOperand, 8> Ops;
753 Ops.assign(MVT::getVectorNumElements(VT), Result);
754 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
755 }
756 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000757}
758
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000759SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
760 bool isTarget) {
761 MVT::ValueType EltVT =
762 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
763 if (EltVT==MVT::f32)
764 return getConstantFP(APFloat((float)Val), VT, isTarget);
765 else
766 return getConstantFP(APFloat(Val), VT, isTarget);
767}
768
Chris Lattnerc3aae252005-01-07 07:46:32 +0000769SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000770 MVT::ValueType VT, int Offset,
771 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000772 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
773 unsigned Opc;
774 if (GVar && GVar->isThreadLocal())
775 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
776 else
777 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000778 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000779 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000780 ID.AddPointer(GV);
781 ID.AddInteger(Offset);
782 void *IP = 0;
783 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
784 return SDOperand(E, 0);
785 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
786 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000787 AllNodes.push_back(N);
788 return SDOperand(N, 0);
789}
790
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000791SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
792 bool isTarget) {
793 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
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(FI);
797 void *IP = 0;
798 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
799 return SDOperand(E, 0);
800 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
801 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000802 AllNodes.push_back(N);
803 return SDOperand(N, 0);
804}
805
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000806SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
807 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000808 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000809 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000810 ID.AddInteger(JTI);
811 void *IP = 0;
812 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
813 return SDOperand(E, 0);
814 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
815 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000816 AllNodes.push_back(N);
817 return SDOperand(N, 0);
818}
819
Evan Chengb8973bd2006-01-31 22:23:14 +0000820SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000821 unsigned Alignment, int Offset,
822 bool isTarget) {
823 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000824 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000825 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000826 ID.AddInteger(Alignment);
827 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000828 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000829 void *IP = 0;
830 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
831 return SDOperand(E, 0);
832 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
833 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000834 AllNodes.push_back(N);
835 return SDOperand(N, 0);
836}
837
Chris Lattnerc3aae252005-01-07 07:46:32 +0000838
Evan Chengd6594ae2006-09-12 21:00:35 +0000839SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
840 MVT::ValueType VT,
841 unsigned Alignment, int Offset,
842 bool isTarget) {
843 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000844 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000845 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000846 ID.AddInteger(Alignment);
847 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000848 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000849 void *IP = 0;
850 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
851 return SDOperand(E, 0);
852 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
853 CSEMap.InsertNode(N, IP);
854 AllNodes.push_back(N);
855 return SDOperand(N, 0);
856}
857
858
Chris Lattnerc3aae252005-01-07 07:46:32 +0000859SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000860 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000861 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000862 ID.AddPointer(MBB);
863 void *IP = 0;
864 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
865 return SDOperand(E, 0);
866 SDNode *N = new BasicBlockSDNode(MBB);
867 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000868 AllNodes.push_back(N);
869 return SDOperand(N, 0);
870}
871
Chris Lattner15e4b012005-07-10 00:07:11 +0000872SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000873 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000874 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000875
Duncan Sandsf411b832007-10-17 13:49:58 +0000876 SDNode *&N = MVT::isExtendedVT(VT) ?
877 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
878
879 if (N) return SDOperand(N, 0);
880 N = new VTSDNode(VT);
881 AllNodes.push_back(N);
882 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000883}
884
Chris Lattnerc3aae252005-01-07 07:46:32 +0000885SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
886 SDNode *&N = ExternalSymbols[Sym];
887 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000888 N = new ExternalSymbolSDNode(false, Sym, VT);
889 AllNodes.push_back(N);
890 return SDOperand(N, 0);
891}
892
Chris Lattner809ec112006-01-28 10:09:25 +0000893SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
894 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000895 SDNode *&N = TargetExternalSymbols[Sym];
896 if (N) return SDOperand(N, 0);
897 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000898 AllNodes.push_back(N);
899 return SDOperand(N, 0);
900}
901
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000902SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
903 if ((unsigned)Cond >= CondCodeNodes.size())
904 CondCodeNodes.resize(Cond+1);
905
Chris Lattner079a27a2005-08-09 20:40:02 +0000906 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000907 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000908 AllNodes.push_back(CondCodeNodes[Cond]);
909 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000910 return SDOperand(CondCodeNodes[Cond], 0);
911}
912
Chris Lattner0fdd7682005-08-30 22:38:38 +0000913SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000914 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000915 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000916 ID.AddInteger(RegNo);
917 void *IP = 0;
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new RegisterSDNode(RegNo, VT);
921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
924}
925
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000926SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +0000927 assert((!V || isa<PointerType>(V->getType())) &&
928 "SrcValue is not a pointer?");
929
Jim Laskey583bd472006-10-27 23:46:08 +0000930 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000931 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000932 ID.AddPointer(V);
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000933
Chris Lattner61b09412006-08-11 21:01:22 +0000934 void *IP = 0;
935 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
936 return SDOperand(E, 0);
Dan Gohmanc6c391d2008-01-31 00:25:39 +0000937
938 SDNode *N = new SrcValueSDNode(V);
939 CSEMap.InsertNode(N, IP);
940 AllNodes.push_back(N);
941 return SDOperand(N, 0);
942}
943
944SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
945 const Value *v = MO.getValue();
946 assert((!v || isa<PointerType>(v->getType())) &&
947 "SrcValue is not a pointer?");
948
949 FoldingSetNodeID ID;
950 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
951 ID.AddPointer(v);
952 ID.AddInteger(MO.getFlags());
953 ID.AddInteger(MO.getOffset());
954 ID.AddInteger(MO.getSize());
955 ID.AddInteger(MO.getAlignment());
956
957 void *IP = 0;
958 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
959 return SDOperand(E, 0);
960
961 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +0000962 CSEMap.InsertNode(N, IP);
963 AllNodes.push_back(N);
964 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000965}
966
Chris Lattner37ce9df2007-10-15 17:47:20 +0000967/// CreateStackTemporary - Create a stack temporary, suitable for holding the
968/// specified value type.
969SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
970 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
971 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
972 const Type *Ty = MVT::getTypeForValueType(VT);
973 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
974 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
975 return getFrameIndex(FrameIdx, TLI.getPointerTy());
976}
977
978
Chris Lattner51dabfb2006-10-14 00:41:01 +0000979SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
980 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000981 // These setcc operations always fold.
982 switch (Cond) {
983 default: break;
984 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000985 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000987 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000988
989 case ISD::SETOEQ:
990 case ISD::SETOGT:
991 case ISD::SETOGE:
992 case ISD::SETOLT:
993 case ISD::SETOLE:
994 case ISD::SETONE:
995 case ISD::SETO:
996 case ISD::SETUO:
997 case ISD::SETUEQ:
998 case ISD::SETUNE:
999 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1000 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001001 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001002
Chris Lattner67255a12005-04-07 18:14:58 +00001003 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
1004 uint64_t C2 = N2C->getValue();
1005 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1006 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001007
Chris Lattnerc3aae252005-01-07 07:46:32 +00001008 // Sign extend the operands if required
1009 if (ISD::isSignedIntSetCC(Cond)) {
1010 C1 = N1C->getSignExtended();
1011 C2 = N2C->getSignExtended();
1012 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001013
Chris Lattnerc3aae252005-01-07 07:46:32 +00001014 switch (Cond) {
1015 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001016 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1017 case ISD::SETNE: return getConstant(C1 != C2, VT);
1018 case ISD::SETULT: return getConstant(C1 < C2, VT);
1019 case ISD::SETUGT: return getConstant(C1 > C2, VT);
1020 case ISD::SETULE: return getConstant(C1 <= C2, VT);
1021 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
1022 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
1023 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
1024 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
1025 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001026 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001027 }
Chris Lattner67255a12005-04-07 18:14:58 +00001028 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001029 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1030 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001031 // No compile time operations on this type yet.
1032 if (N1C->getValueType(0) == MVT::ppcf128)
1033 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001034
1035 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001036 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001037 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001038 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1039 return getNode(ISD::UNDEF, VT);
1040 // fall through
1041 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1042 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1043 return getNode(ISD::UNDEF, VT);
1044 // fall through
1045 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001046 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001047 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1048 return getNode(ISD::UNDEF, VT);
1049 // fall through
1050 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1051 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1052 return getNode(ISD::UNDEF, VT);
1053 // fall through
1054 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1055 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1056 return getNode(ISD::UNDEF, VT);
1057 // fall through
1058 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001059 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001060 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1061 return getNode(ISD::UNDEF, VT);
1062 // fall through
1063 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001064 R==APFloat::cmpEqual, VT);
1065 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1066 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1067 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1068 R==APFloat::cmpEqual, VT);
1069 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1070 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1071 R==APFloat::cmpLessThan, VT);
1072 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1073 R==APFloat::cmpUnordered, VT);
1074 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1075 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076 }
1077 } else {
1078 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001079 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001080 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001081
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001082 // Could not fold it.
1083 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001084}
1085
Dan Gohmanea859be2007-06-22 14:59:07 +00001086/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1087/// this predicate to simplify operations downstream. Mask is known to be zero
1088/// for bits that V cannot have.
1089bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1090 unsigned Depth) const {
1091 // The masks are not wide enough to represent this type! Should use APInt.
1092 if (Op.getValueType() == MVT::i128)
1093 return false;
1094
1095 uint64_t KnownZero, KnownOne;
1096 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1097 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1098 return (KnownZero & Mask) == Mask;
1099}
1100
1101/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1102/// known to be either zero or one and return them in the KnownZero/KnownOne
1103/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1104/// processing.
1105void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1106 uint64_t &KnownZero, uint64_t &KnownOne,
1107 unsigned Depth) const {
1108 KnownZero = KnownOne = 0; // Don't know anything.
1109 if (Depth == 6 || Mask == 0)
1110 return; // Limit search depth.
1111
1112 // The masks are not wide enough to represent this type! Should use APInt.
1113 if (Op.getValueType() == MVT::i128)
1114 return;
1115
1116 uint64_t KnownZero2, KnownOne2;
1117
1118 switch (Op.getOpcode()) {
1119 case ISD::Constant:
1120 // We know all of the bits for a constant!
1121 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1122 KnownZero = ~KnownOne & Mask;
1123 return;
1124 case ISD::AND:
1125 // If either the LHS or the RHS are Zero, the result is zero.
1126 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1127 Mask &= ~KnownZero;
1128 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1129 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1130 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1131
1132 // Output known-1 bits are only known if set in both the LHS & RHS.
1133 KnownOne &= KnownOne2;
1134 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1135 KnownZero |= KnownZero2;
1136 return;
1137 case ISD::OR:
1138 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1139 Mask &= ~KnownOne;
1140 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1141 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1142 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1143
1144 // Output known-0 bits are only known if clear in both the LHS & RHS.
1145 KnownZero &= KnownZero2;
1146 // Output known-1 are known to be set if set in either the LHS | RHS.
1147 KnownOne |= KnownOne2;
1148 return;
1149 case ISD::XOR: {
1150 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1151 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1152 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1153 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1154
1155 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1156 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1157 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1158 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1159 KnownZero = KnownZeroOut;
1160 return;
1161 }
1162 case ISD::SELECT:
1163 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1164 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1165 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1166 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1167
1168 // Only known if known in both the LHS and RHS.
1169 KnownOne &= KnownOne2;
1170 KnownZero &= KnownZero2;
1171 return;
1172 case ISD::SELECT_CC:
1173 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1174 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1175 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1176 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1177
1178 // Only known if known in both the LHS and RHS.
1179 KnownOne &= KnownOne2;
1180 KnownZero &= KnownZero2;
1181 return;
1182 case ISD::SETCC:
1183 // If we know the result of a setcc has the top bits zero, use this info.
1184 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1185 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1186 return;
1187 case ISD::SHL:
1188 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1189 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1190 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1191 KnownZero, KnownOne, Depth+1);
1192 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1193 KnownZero <<= SA->getValue();
1194 KnownOne <<= SA->getValue();
1195 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1196 }
1197 return;
1198 case ISD::SRL:
1199 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1200 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1201 MVT::ValueType VT = Op.getValueType();
1202 unsigned ShAmt = SA->getValue();
1203
1204 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1205 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1206 KnownZero, KnownOne, Depth+1);
1207 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1208 KnownZero &= TypeMask;
1209 KnownOne &= TypeMask;
1210 KnownZero >>= ShAmt;
1211 KnownOne >>= ShAmt;
1212
1213 uint64_t HighBits = (1ULL << ShAmt)-1;
1214 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1215 KnownZero |= HighBits; // High bits known zero.
1216 }
1217 return;
1218 case ISD::SRA:
1219 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1220 MVT::ValueType VT = Op.getValueType();
1221 unsigned ShAmt = SA->getValue();
1222
1223 // Compute the new bits that are at the top now.
1224 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1225
1226 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1227 // If any of the demanded bits are produced by the sign extension, we also
1228 // demand the input sign bit.
1229 uint64_t HighBits = (1ULL << ShAmt)-1;
1230 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1231 if (HighBits & Mask)
1232 InDemandedMask |= MVT::getIntVTSignBit(VT);
1233
1234 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1235 Depth+1);
1236 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1237 KnownZero &= TypeMask;
1238 KnownOne &= TypeMask;
1239 KnownZero >>= ShAmt;
1240 KnownOne >>= ShAmt;
1241
1242 // Handle the sign bits.
1243 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1244 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1245
1246 if (KnownZero & SignBit) {
1247 KnownZero |= HighBits; // New bits are known zero.
1248 } else if (KnownOne & SignBit) {
1249 KnownOne |= HighBits; // New bits are known one.
1250 }
1251 }
1252 return;
1253 case ISD::SIGN_EXTEND_INREG: {
1254 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1255
1256 // Sign extension. Compute the demanded bits in the result that are not
1257 // present in the input.
1258 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1259
1260 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1261 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1262
1263 // If the sign extended bits are demanded, we know that the sign
1264 // bit is demanded.
1265 if (NewBits)
1266 InputDemandedBits |= InSignBit;
1267
1268 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1269 KnownZero, KnownOne, Depth+1);
1270 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1271
1272 // If the sign bit of the input is known set or clear, then we know the
1273 // top bits of the result.
1274 if (KnownZero & InSignBit) { // Input sign bit known clear
1275 KnownZero |= NewBits;
1276 KnownOne &= ~NewBits;
1277 } else if (KnownOne & InSignBit) { // Input sign bit known set
1278 KnownOne |= NewBits;
1279 KnownZero &= ~NewBits;
1280 } else { // Input sign bit unknown
1281 KnownZero &= ~NewBits;
1282 KnownOne &= ~NewBits;
1283 }
1284 return;
1285 }
1286 case ISD::CTTZ:
1287 case ISD::CTLZ:
1288 case ISD::CTPOP: {
1289 MVT::ValueType VT = Op.getValueType();
1290 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1291 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1292 KnownOne = 0;
1293 return;
1294 }
1295 case ISD::LOAD: {
1296 if (ISD::isZEXTLoad(Op.Val)) {
1297 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001298 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmanea859be2007-06-22 14:59:07 +00001299 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1300 }
1301 return;
1302 }
1303 case ISD::ZERO_EXTEND: {
1304 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1305 uint64_t NewBits = (~InMask) & Mask;
1306 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1307 KnownOne, Depth+1);
1308 KnownZero |= NewBits & Mask;
1309 KnownOne &= ~NewBits;
1310 return;
1311 }
1312 case ISD::SIGN_EXTEND: {
1313 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1314 unsigned InBits = MVT::getSizeInBits(InVT);
1315 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1316 uint64_t InSignBit = 1ULL << (InBits-1);
1317 uint64_t NewBits = (~InMask) & Mask;
1318 uint64_t InDemandedBits = Mask & InMask;
1319
1320 // If any of the sign extended bits are demanded, we know that the sign
1321 // bit is demanded.
1322 if (NewBits & Mask)
1323 InDemandedBits |= InSignBit;
1324
1325 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1326 KnownOne, Depth+1);
1327 // If the sign bit is known zero or one, the top bits match.
1328 if (KnownZero & InSignBit) {
1329 KnownZero |= NewBits;
1330 KnownOne &= ~NewBits;
1331 } else if (KnownOne & InSignBit) {
1332 KnownOne |= NewBits;
1333 KnownZero &= ~NewBits;
1334 } else { // Otherwise, top bits aren't known.
1335 KnownOne &= ~NewBits;
1336 KnownZero &= ~NewBits;
1337 }
1338 return;
1339 }
1340 case ISD::ANY_EXTEND: {
1341 MVT::ValueType VT = Op.getOperand(0).getValueType();
1342 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1343 KnownZero, KnownOne, Depth+1);
1344 return;
1345 }
1346 case ISD::TRUNCATE: {
1347 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1348 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1349 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1350 KnownZero &= OutMask;
1351 KnownOne &= OutMask;
1352 break;
1353 }
1354 case ISD::AssertZext: {
1355 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1356 uint64_t InMask = MVT::getIntVTBitMask(VT);
1357 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1358 KnownOne, Depth+1);
1359 KnownZero |= (~InMask) & Mask;
1360 return;
1361 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001362 case ISD::FGETSIGN:
1363 // All bits are zero except the low bit.
1364 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1365 return;
1366
Dan Gohmanea859be2007-06-22 14:59:07 +00001367 case ISD::ADD: {
1368 // If either the LHS or the RHS are Zero, the result is zero.
1369 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1370 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1371 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1372 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1373
1374 // Output known-0 bits are known if clear or set in both the low clear bits
1375 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1376 // low 3 bits clear.
1377 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1378 CountTrailingZeros_64(~KnownZero2));
1379
1380 KnownZero = (1ULL << KnownZeroOut) - 1;
1381 KnownOne = 0;
1382 return;
1383 }
1384 case ISD::SUB: {
1385 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1386 if (!CLHS) return;
1387
1388 // We know that the top bits of C-X are clear if X contains less bits
1389 // than C (i.e. no wrap-around can happen). For example, 20-X is
1390 // positive if we can prove that X is >= 0 and < 16.
1391 MVT::ValueType VT = CLHS->getValueType(0);
1392 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1393 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1394 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1395 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1396 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1397
1398 // If all of the MaskV bits are known to be zero, then we know the output
1399 // top bits are zero, because we now know that the output is from [0-C].
1400 if ((KnownZero & MaskV) == MaskV) {
1401 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1402 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1403 KnownOne = 0; // No one bits known.
1404 } else {
1405 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1406 }
1407 }
1408 return;
1409 }
1410 default:
1411 // Allow the target to implement this method for its nodes.
1412 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1413 case ISD::INTRINSIC_WO_CHAIN:
1414 case ISD::INTRINSIC_W_CHAIN:
1415 case ISD::INTRINSIC_VOID:
1416 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1417 }
1418 return;
1419 }
1420}
1421
1422/// ComputeNumSignBits - Return the number of times the sign bit of the
1423/// register is replicated into the other bits. We know that at least 1 bit
1424/// is always equal to the sign bit (itself), but other cases can give us
1425/// information. For example, immediately after an "SRA X, 2", we know that
1426/// the top 3 bits are all equal to each other, so we return 3.
1427unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1428 MVT::ValueType VT = Op.getValueType();
1429 assert(MVT::isInteger(VT) && "Invalid VT!");
1430 unsigned VTBits = MVT::getSizeInBits(VT);
1431 unsigned Tmp, Tmp2;
1432
1433 if (Depth == 6)
1434 return 1; // Limit search depth.
1435
1436 switch (Op.getOpcode()) {
1437 default: break;
1438 case ISD::AssertSext:
1439 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1440 return VTBits-Tmp+1;
1441 case ISD::AssertZext:
1442 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1443 return VTBits-Tmp;
1444
1445 case ISD::Constant: {
1446 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1447 // If negative, invert the bits, then look at it.
1448 if (Val & MVT::getIntVTSignBit(VT))
1449 Val = ~Val;
1450
1451 // Shift the bits so they are the leading bits in the int64_t.
1452 Val <<= 64-VTBits;
1453
1454 // Return # leading zeros. We use 'min' here in case Val was zero before
1455 // shifting. We don't want to return '64' as for an i32 "0".
1456 return std::min(VTBits, CountLeadingZeros_64(Val));
1457 }
1458
1459 case ISD::SIGN_EXTEND:
1460 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1461 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1462
1463 case ISD::SIGN_EXTEND_INREG:
1464 // Max of the input and what this extends.
1465 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1466 Tmp = VTBits-Tmp+1;
1467
1468 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1469 return std::max(Tmp, Tmp2);
1470
1471 case ISD::SRA:
1472 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1473 // SRA X, C -> adds C sign bits.
1474 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1475 Tmp += C->getValue();
1476 if (Tmp > VTBits) Tmp = VTBits;
1477 }
1478 return Tmp;
1479 case ISD::SHL:
1480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1481 // shl destroys sign bits.
1482 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1483 if (C->getValue() >= VTBits || // Bad shift.
1484 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1485 return Tmp - C->getValue();
1486 }
1487 break;
1488 case ISD::AND:
1489 case ISD::OR:
1490 case ISD::XOR: // NOT is handled here.
1491 // Logical binary ops preserve the number of sign bits.
1492 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1493 if (Tmp == 1) return 1; // Early out.
1494 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1495 return std::min(Tmp, Tmp2);
1496
1497 case ISD::SELECT:
1498 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1499 if (Tmp == 1) return 1; // Early out.
1500 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1501 return std::min(Tmp, Tmp2);
1502
1503 case ISD::SETCC:
1504 // If setcc returns 0/-1, all bits are sign bits.
1505 if (TLI.getSetCCResultContents() ==
1506 TargetLowering::ZeroOrNegativeOneSetCCResult)
1507 return VTBits;
1508 break;
1509 case ISD::ROTL:
1510 case ISD::ROTR:
1511 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1512 unsigned RotAmt = C->getValue() & (VTBits-1);
1513
1514 // Handle rotate right by N like a rotate left by 32-N.
1515 if (Op.getOpcode() == ISD::ROTR)
1516 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1517
1518 // If we aren't rotating out all of the known-in sign bits, return the
1519 // number that are left. This handles rotl(sext(x), 1) for example.
1520 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1521 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1522 }
1523 break;
1524 case ISD::ADD:
1525 // Add can have at most one carry bit. Thus we know that the output
1526 // is, at worst, one more bit than the inputs.
1527 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1528 if (Tmp == 1) return 1; // Early out.
1529
1530 // Special case decrementing a value (ADD X, -1):
1531 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1532 if (CRHS->isAllOnesValue()) {
1533 uint64_t KnownZero, KnownOne;
1534 uint64_t Mask = MVT::getIntVTBitMask(VT);
1535 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1536
1537 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1538 // sign bits set.
1539 if ((KnownZero|1) == Mask)
1540 return VTBits;
1541
1542 // If we are subtracting one from a positive number, there is no carry
1543 // out of the result.
1544 if (KnownZero & MVT::getIntVTSignBit(VT))
1545 return Tmp;
1546 }
1547
1548 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1549 if (Tmp2 == 1) return 1;
1550 return std::min(Tmp, Tmp2)-1;
1551 break;
1552
1553 case ISD::SUB:
1554 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1555 if (Tmp2 == 1) return 1;
1556
1557 // Handle NEG.
1558 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1559 if (CLHS->getValue() == 0) {
1560 uint64_t KnownZero, KnownOne;
1561 uint64_t Mask = MVT::getIntVTBitMask(VT);
1562 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1563 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1564 // sign bits set.
1565 if ((KnownZero|1) == Mask)
1566 return VTBits;
1567
1568 // If the input is known to be positive (the sign bit is known clear),
1569 // the output of the NEG has the same number of sign bits as the input.
1570 if (KnownZero & MVT::getIntVTSignBit(VT))
1571 return Tmp2;
1572
1573 // Otherwise, we treat this like a SUB.
1574 }
1575
1576 // Sub can have at most one carry bit. Thus we know that the output
1577 // is, at worst, one more bit than the inputs.
1578 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1579 if (Tmp == 1) return 1; // Early out.
1580 return std::min(Tmp, Tmp2)-1;
1581 break;
1582 case ISD::TRUNCATE:
1583 // FIXME: it's tricky to do anything useful for this, but it is an important
1584 // case for targets like X86.
1585 break;
1586 }
1587
1588 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1589 if (Op.getOpcode() == ISD::LOAD) {
1590 LoadSDNode *LD = cast<LoadSDNode>(Op);
1591 unsigned ExtType = LD->getExtensionType();
1592 switch (ExtType) {
1593 default: break;
1594 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001595 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001596 return VTBits-Tmp+1;
1597 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001598 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001599 return VTBits-Tmp;
1600 }
1601 }
1602
1603 // Allow the target to implement this method for its nodes.
1604 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1605 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1606 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1607 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1608 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1609 if (NumBits > 1) return NumBits;
1610 }
1611
1612 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1613 // use this information.
1614 uint64_t KnownZero, KnownOne;
1615 uint64_t Mask = MVT::getIntVTBitMask(VT);
1616 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1617
1618 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1619 if (KnownZero & SignBit) { // SignBit is 0
1620 Mask = KnownZero;
1621 } else if (KnownOne & SignBit) { // SignBit is 1;
1622 Mask = KnownOne;
1623 } else {
1624 // Nothing known.
1625 return 1;
1626 }
1627
1628 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1629 // the number of identical bits in the top of the input value.
1630 Mask ^= ~0ULL;
1631 Mask <<= 64-VTBits;
1632 // Return # leading zeros. We use 'min' here in case Val was zero before
1633 // shifting. We don't want to return '64' as for an i32 "0".
1634 return std::min(VTBits, CountLeadingZeros_64(Mask));
1635}
1636
Chris Lattner51dabfb2006-10-14 00:41:01 +00001637
Chris Lattnerc3aae252005-01-07 07:46:32 +00001638/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001639///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001640SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001641 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001642 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001643 void *IP = 0;
1644 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1645 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001646 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001647 CSEMap.InsertNode(N, IP);
1648
1649 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001650 return SDOperand(N, 0);
1651}
1652
Chris Lattnerc3aae252005-01-07 07:46:32 +00001653SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1654 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001655 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001656 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001657 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1658 uint64_t Val = C->getValue();
1659 switch (Opcode) {
1660 default: break;
1661 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001662 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001663 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1664 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001665 case ISD::UINT_TO_FP:
1666 case ISD::SINT_TO_FP: {
1667 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001668 // No compile time operations on this type.
1669 if (VT==MVT::ppcf128)
1670 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00001671 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001672 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001673 MVT::getSizeInBits(Operand.getValueType()),
1674 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001675 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001676 return getConstantFP(apf, VT);
1677 }
Chris Lattner94683772005-12-23 05:30:37 +00001678 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001679 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001680 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001681 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001682 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001683 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001684 case ISD::BSWAP:
1685 switch(VT) {
1686 default: assert(0 && "Invalid bswap!"); break;
1687 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1688 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1689 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1690 }
1691 break;
1692 case ISD::CTPOP:
1693 switch(VT) {
1694 default: assert(0 && "Invalid ctpop!"); break;
1695 case MVT::i1: return getConstant(Val != 0, VT);
1696 case MVT::i8:
1697 Tmp1 = (unsigned)Val & 0xFF;
1698 return getConstant(CountPopulation_32(Tmp1), VT);
1699 case MVT::i16:
1700 Tmp1 = (unsigned)Val & 0xFFFF;
1701 return getConstant(CountPopulation_32(Tmp1), VT);
1702 case MVT::i32:
1703 return getConstant(CountPopulation_32((unsigned)Val), VT);
1704 case MVT::i64:
1705 return getConstant(CountPopulation_64(Val), VT);
1706 }
1707 case ISD::CTLZ:
1708 switch(VT) {
1709 default: assert(0 && "Invalid ctlz!"); break;
1710 case MVT::i1: return getConstant(Val == 0, VT);
1711 case MVT::i8:
1712 Tmp1 = (unsigned)Val & 0xFF;
1713 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1714 case MVT::i16:
1715 Tmp1 = (unsigned)Val & 0xFFFF;
1716 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1717 case MVT::i32:
1718 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1719 case MVT::i64:
1720 return getConstant(CountLeadingZeros_64(Val), VT);
1721 }
1722 case ISD::CTTZ:
1723 switch(VT) {
1724 default: assert(0 && "Invalid cttz!"); break;
1725 case MVT::i1: return getConstant(Val == 0, VT);
1726 case MVT::i8:
1727 Tmp1 = (unsigned)Val | 0x100;
1728 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1729 case MVT::i16:
1730 Tmp1 = (unsigned)Val | 0x10000;
1731 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1732 case MVT::i32:
1733 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1734 case MVT::i64:
1735 return getConstant(CountTrailingZeros_64(Val), VT);
1736 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001737 }
1738 }
1739
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001740 // Constant fold unary operations with a floating point constant operand.
1741 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1742 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001743 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001744 switch (Opcode) {
1745 case ISD::FNEG:
1746 V.changeSign();
1747 return getConstantFP(V, VT);
1748 case ISD::FABS:
1749 V.clearSign();
1750 return getConstantFP(V, VT);
1751 case ISD::FP_ROUND:
1752 case ISD::FP_EXTEND:
1753 // This can return overflow, underflow, or inexact; we don't care.
1754 // FIXME need to be more flexible about rounding mode.
1755 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1756 VT==MVT::f64 ? APFloat::IEEEdouble :
1757 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1758 VT==MVT::f128 ? APFloat::IEEEquad :
1759 APFloat::Bogus,
1760 APFloat::rmNearestTiesToEven);
1761 return getConstantFP(V, VT);
1762 case ISD::FP_TO_SINT:
1763 case ISD::FP_TO_UINT: {
1764 integerPart x;
1765 assert(integerPartWidth >= 64);
1766 // FIXME need to be more flexible about rounding mode.
1767 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1768 Opcode==ISD::FP_TO_SINT,
1769 APFloat::rmTowardZero);
1770 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1771 break;
1772 return getConstant(x, VT);
1773 }
1774 case ISD::BIT_CONVERT:
1775 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1776 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1777 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1778 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001779 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001780 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001781 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001782 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001783
1784 unsigned OpOpcode = Operand.Val->getOpcode();
1785 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001786 case ISD::TokenFactor:
1787 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001788 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001789 case ISD::FP_EXTEND:
1790 assert(MVT::isFloatingPoint(VT) &&
1791 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001792 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001793 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00001794 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001795 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1796 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001797 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001798 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1799 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001800 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1801 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1802 break;
1803 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001804 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1805 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001806 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001807 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1808 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001809 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001810 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001811 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001812 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001813 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1814 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001815 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001816 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1817 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001818 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1819 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1820 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1821 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001822 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001823 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1824 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001825 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001826 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1827 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001828 if (OpOpcode == ISD::TRUNCATE)
1829 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001830 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1831 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001832 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001833 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1834 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001835 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001836 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1837 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001838 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1839 else
1840 return Operand.Val->getOperand(0);
1841 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001842 break;
Chris Lattner35481892005-12-23 00:16:34 +00001843 case ISD::BIT_CONVERT:
1844 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001845 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001846 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001847 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001848 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1849 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001850 if (OpOpcode == ISD::UNDEF)
1851 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001852 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001853 case ISD::SCALAR_TO_VECTOR:
1854 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001855 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001856 "Illegal SCALAR_TO_VECTOR node!");
1857 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001858 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001859 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1860 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001861 Operand.Val->getOperand(0));
1862 if (OpOpcode == ISD::FNEG) // --X -> X
1863 return Operand.Val->getOperand(0);
1864 break;
1865 case ISD::FABS:
1866 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1867 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1868 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001869 }
1870
Chris Lattner43247a12005-08-25 19:12:10 +00001871 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001872 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001873 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001874 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001875 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001876 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001877 void *IP = 0;
1878 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1879 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001880 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001881 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001882 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001883 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001884 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001885 AllNodes.push_back(N);
1886 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001887}
1888
Chris Lattner36019aa2005-04-18 03:48:41 +00001889
1890
Chris Lattnerc3aae252005-01-07 07:46:32 +00001891SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1892 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001893 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1894 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001895 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001896 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001897 case ISD::TokenFactor:
1898 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1899 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001900 // Fold trivial token factors.
1901 if (N1.getOpcode() == ISD::EntryToken) return N2;
1902 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001903 break;
Chris Lattner76365122005-01-16 02:23:22 +00001904 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001905 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1906 N1.getValueType() == VT && "Binary operator types must match!");
1907 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1908 // worth handling here.
1909 if (N2C && N2C->getValue() == 0)
1910 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001911 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1912 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001913 break;
Chris Lattner76365122005-01-16 02:23:22 +00001914 case ISD::OR:
1915 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001916 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1917 N1.getValueType() == VT && "Binary operator types must match!");
1918 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1919 // worth handling here.
1920 if (N2C && N2C->getValue() == 0)
1921 return N1;
1922 break;
Chris Lattner76365122005-01-16 02:23:22 +00001923 case ISD::UDIV:
1924 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001925 case ISD::MULHU:
1926 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001927 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1928 // fall through
1929 case ISD::ADD:
1930 case ISD::SUB:
1931 case ISD::MUL:
1932 case ISD::SDIV:
1933 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001934 case ISD::FADD:
1935 case ISD::FSUB:
1936 case ISD::FMUL:
1937 case ISD::FDIV:
1938 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001939 assert(N1.getValueType() == N2.getValueType() &&
1940 N1.getValueType() == VT && "Binary operator types must match!");
1941 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001942 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1943 assert(N1.getValueType() == VT &&
1944 MVT::isFloatingPoint(N1.getValueType()) &&
1945 MVT::isFloatingPoint(N2.getValueType()) &&
1946 "Invalid FCOPYSIGN!");
1947 break;
Chris Lattner76365122005-01-16 02:23:22 +00001948 case ISD::SHL:
1949 case ISD::SRA:
1950 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001951 case ISD::ROTL:
1952 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001953 assert(VT == N1.getValueType() &&
1954 "Shift operators return type must be the same as their first arg");
1955 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001956 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001957 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001958 case ISD::FP_ROUND_INREG: {
1959 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1960 assert(VT == N1.getValueType() && "Not an inreg round!");
1961 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1962 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001963 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1964 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001965 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00001966 break;
1967 }
Chris Lattner0bd48932008-01-17 07:00:52 +00001968 case ISD::FP_ROUND:
1969 assert(MVT::isFloatingPoint(VT) &&
1970 MVT::isFloatingPoint(N1.getValueType()) &&
1971 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
1972 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001973 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00001974 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00001975 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001976 case ISD::AssertZext: {
1977 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1978 assert(VT == N1.getValueType() && "Not an inreg extend!");
1979 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1980 "Cannot *_EXTEND_INREG FP types");
1981 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1982 "Not extending!");
1983 break;
1984 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001985 case ISD::SIGN_EXTEND_INREG: {
1986 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1987 assert(VT == N1.getValueType() && "Not an inreg extend!");
1988 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1989 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001990 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1991 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001992 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001993
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001994 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001995 int64_t Val = N1C->getValue();
1996 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1997 Val <<= 64-FromBits;
1998 Val >>= 64-FromBits;
1999 return getConstant(Val, VT);
2000 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002001 break;
2002 }
2003 case ISD::EXTRACT_VECTOR_ELT:
2004 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2005
2006 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2007 // expanding copies of large vectors from registers.
2008 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2009 N1.getNumOperands() > 0) {
2010 unsigned Factor =
2011 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2012 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2013 N1.getOperand(N2C->getValue() / Factor),
2014 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2015 }
2016
2017 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2018 // expanding large vector constants.
2019 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2020 return N1.getOperand(N2C->getValue());
2021
2022 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2023 // operations are lowered to scalars.
2024 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2025 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2026 if (IEC == N2C)
2027 return N1.getOperand(1);
2028 else
2029 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2030 }
2031 break;
2032 case ISD::EXTRACT_ELEMENT:
2033 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002034
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002035 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2036 // 64-bit integers into 32-bit parts. Instead of building the extract of
2037 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2038 if (N1.getOpcode() == ISD::BUILD_PAIR)
2039 return N1.getOperand(N2C->getValue());
2040
2041 // EXTRACT_ELEMENT of a constant int is also very common.
2042 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2043 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2044 return getConstant(C->getValue() >> Shift, VT);
2045 }
2046 break;
2047 }
2048
2049 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002050 if (N2C) {
2051 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
2052 switch (Opcode) {
2053 case ISD::ADD: return getConstant(C1 + C2, VT);
2054 case ISD::SUB: return getConstant(C1 - C2, VT);
2055 case ISD::MUL: return getConstant(C1 * C2, VT);
2056 case ISD::UDIV:
2057 if (C2) return getConstant(C1 / C2, VT);
2058 break;
2059 case ISD::UREM :
2060 if (C2) return getConstant(C1 % C2, VT);
2061 break;
2062 case ISD::SDIV :
2063 if (C2) return getConstant(N1C->getSignExtended() /
2064 N2C->getSignExtended(), VT);
2065 break;
2066 case ISD::SREM :
2067 if (C2) return getConstant(N1C->getSignExtended() %
2068 N2C->getSignExtended(), VT);
2069 break;
2070 case ISD::AND : return getConstant(C1 & C2, VT);
2071 case ISD::OR : return getConstant(C1 | C2, VT);
2072 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002073 case ISD::SHL : return getConstant(C1 << C2, VT);
2074 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00002075 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00002076 case ISD::ROTL :
2077 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
2078 VT);
2079 case ISD::ROTR :
2080 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
2081 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002082 default: break;
2083 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002084 } else { // Cannonicalize constant to RHS if commutative
2085 if (isCommutativeBinOp(Opcode)) {
2086 std::swap(N1C, N2C);
2087 std::swap(N1, N2);
2088 }
2089 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002090 }
2091
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002092 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002093 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2094 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002095 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002096 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2097 // Cannonicalize constant to RHS if commutative
2098 std::swap(N1CFP, N2CFP);
2099 std::swap(N1, N2);
2100 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002101 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2102 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002103 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002104 case ISD::FADD:
2105 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002106 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002107 return getConstantFP(V1, VT);
2108 break;
2109 case ISD::FSUB:
2110 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2111 if (s!=APFloat::opInvalidOp)
2112 return getConstantFP(V1, VT);
2113 break;
2114 case ISD::FMUL:
2115 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2116 if (s!=APFloat::opInvalidOp)
2117 return getConstantFP(V1, VT);
2118 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002119 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002120 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2121 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2122 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002123 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002124 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002125 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2126 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2127 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002128 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002129 case ISD::FCOPYSIGN:
2130 V1.copySign(V2);
2131 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002132 default: break;
2133 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002134 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002135 }
Chris Lattner62b57722006-04-20 05:39:12 +00002136
2137 // Canonicalize an UNDEF to the RHS, even over a constant.
2138 if (N1.getOpcode() == ISD::UNDEF) {
2139 if (isCommutativeBinOp(Opcode)) {
2140 std::swap(N1, N2);
2141 } else {
2142 switch (Opcode) {
2143 case ISD::FP_ROUND_INREG:
2144 case ISD::SIGN_EXTEND_INREG:
2145 case ISD::SUB:
2146 case ISD::FSUB:
2147 case ISD::FDIV:
2148 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002149 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002150 return N1; // fold op(undef, arg2) -> undef
2151 case ISD::UDIV:
2152 case ISD::SDIV:
2153 case ISD::UREM:
2154 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002155 case ISD::SRL:
2156 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002157 if (!MVT::isVector(VT))
2158 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2159 // For vectors, we can't easily build an all zero vector, just return
2160 // the LHS.
2161 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002162 }
2163 }
2164 }
2165
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002166 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002167 if (N2.getOpcode() == ISD::UNDEF) {
2168 switch (Opcode) {
2169 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002170 case ISD::ADDC:
2171 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002172 case ISD::SUB:
2173 case ISD::FADD:
2174 case ISD::FSUB:
2175 case ISD::FMUL:
2176 case ISD::FDIV:
2177 case ISD::FREM:
2178 case ISD::UDIV:
2179 case ISD::SDIV:
2180 case ISD::UREM:
2181 case ISD::SREM:
2182 case ISD::XOR:
2183 return N2; // fold op(arg1, undef) -> undef
2184 case ISD::MUL:
2185 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002186 case ISD::SRL:
2187 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002188 if (!MVT::isVector(VT))
2189 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2190 // For vectors, we can't easily build an all zero vector, just return
2191 // the LHS.
2192 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002193 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002194 if (!MVT::isVector(VT))
2195 return getConstant(MVT::getIntVTBitMask(VT), VT);
2196 // For vectors, we can't easily build an all one vector, just return
2197 // the LHS.
2198 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002199 case ISD::SRA:
2200 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002201 }
2202 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002203
Chris Lattner27e9b412005-05-11 18:57:39 +00002204 // Memoize this node if possible.
2205 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002206 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002207 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002208 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002209 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002210 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002211 void *IP = 0;
2212 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2213 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002214 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002215 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002216 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002217 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002218 }
2219
Chris Lattnerc3aae252005-01-07 07:46:32 +00002220 AllNodes.push_back(N);
2221 return SDOperand(N, 0);
2222}
2223
Chris Lattnerc3aae252005-01-07 07:46:32 +00002224SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2225 SDOperand N1, SDOperand N2, SDOperand N3) {
2226 // Perform various simplifications.
2227 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2228 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002229 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002230 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002231 // Use FoldSetCC to simplify SETCC's.
2232 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002233 if (Simp.Val) return Simp;
2234 break;
2235 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002236 case ISD::SELECT:
2237 if (N1C)
2238 if (N1C->getValue())
2239 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002240 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002241 return N3; // select false, X, Y -> Y
2242
2243 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002244 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002245 case ISD::BRCOND:
2246 if (N2C)
2247 if (N2C->getValue()) // Unconditional branch
2248 return getNode(ISD::BR, MVT::Other, N1, N3);
2249 else
2250 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002251 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002252 case ISD::VECTOR_SHUFFLE:
2253 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2254 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2255 N3.getOpcode() == ISD::BUILD_VECTOR &&
2256 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2257 "Illegal VECTOR_SHUFFLE node!");
2258 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002259 case ISD::BIT_CONVERT:
2260 // Fold bit_convert nodes from a type to themselves.
2261 if (N1.getValueType() == VT)
2262 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002263 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002264 }
2265
Chris Lattner43247a12005-08-25 19:12:10 +00002266 // Memoize node if it doesn't produce a flag.
2267 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002268 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002269 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002270 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002271 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002272 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002273 void *IP = 0;
2274 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2275 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002276 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002277 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002278 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002279 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002280 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002281 AllNodes.push_back(N);
2282 return SDOperand(N, 0);
2283}
2284
2285SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002286 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002287 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002288 SDOperand Ops[] = { N1, N2, N3, N4 };
2289 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002290}
2291
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002292SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2293 SDOperand N1, SDOperand N2, SDOperand N3,
2294 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002295 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2296 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002297}
2298
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002299SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2300 SDOperand Src, SDOperand Size,
2301 SDOperand Align,
2302 SDOperand AlwaysInline) {
2303 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2304 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2305}
2306
2307SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2308 SDOperand Src, SDOperand Size,
2309 SDOperand Align,
2310 SDOperand AlwaysInline) {
2311 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2312 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2313}
2314
2315SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2316 SDOperand Src, SDOperand Size,
2317 SDOperand Align,
2318 SDOperand AlwaysInline) {
2319 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2320 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2321}
2322
Evan Cheng7038daf2005-12-10 00:37:58 +00002323SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2324 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002325 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002326 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002327 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2328 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002329 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002330 Ty = MVT::getTypeForValueType(VT);
2331 } else if (SV) {
2332 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2333 assert(PT && "Value for load must be a pointer");
2334 Ty = PT->getElementType();
2335 }
2336 assert(Ty && "Could not get type information for load");
2337 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2338 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002339 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002340 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002341 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002342 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002343 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002344 ID.AddInteger(ISD::UNINDEXED);
2345 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002346 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002347 ID.AddInteger(Alignment);
2348 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002349 void *IP = 0;
2350 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2351 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002352 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002353 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2354 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002355 CSEMap.InsertNode(N, IP);
2356 AllNodes.push_back(N);
2357 return SDOperand(N, 0);
2358}
2359
2360SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002361 SDOperand Chain, SDOperand Ptr,
2362 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002363 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002364 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002365 // If they are asking for an extending load from/to the same thing, return a
2366 // normal load.
2367 if (VT == EVT)
Duncan Sands5d868b12007-10-19 13:05:40 +00002368 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00002369
2370 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002371 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002372 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002373 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2374 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002375 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2376 "Cannot sign/zero extend a FP/Vector load!");
2377 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2378 "Cannot convert from FP to Int or Int -> FP!");
2379
Dan Gohman575e2f42007-06-04 15:49:41 +00002380 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2381 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002382 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002383 Ty = MVT::getTypeForValueType(VT);
2384 } else if (SV) {
2385 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2386 assert(PT && "Value for load must be a pointer");
2387 Ty = PT->getElementType();
2388 }
2389 assert(Ty && "Could not get type information for load");
2390 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2391 }
Evan Cheng466685d2006-10-09 20:57:25 +00002392 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002393 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002394 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002395 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002396 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002397 ID.AddInteger(ISD::UNINDEXED);
2398 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002399 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002400 ID.AddInteger(Alignment);
2401 ID.AddInteger(isVolatile);
2402 void *IP = 0;
2403 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2404 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002405 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002406 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002407 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002408 AllNodes.push_back(N);
2409 return SDOperand(N, 0);
2410}
2411
Evan Cheng144d8f02006-11-09 17:55:04 +00002412SDOperand
2413SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2414 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002415 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002416 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2417 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002418 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002419 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002420 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002421 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002422 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002423 ID.AddInteger(AM);
2424 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002425 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002426 ID.AddInteger(LD->getAlignment());
2427 ID.AddInteger(LD->isVolatile());
2428 void *IP = 0;
2429 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2430 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002431 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002432 LD->getExtensionType(), LD->getMemoryVT(),
Evan Cheng2caccca2006-10-17 21:14:32 +00002433 LD->getSrcValue(), LD->getSrcValueOffset(),
2434 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002435 CSEMap.InsertNode(N, IP);
2436 AllNodes.push_back(N);
2437 return SDOperand(N, 0);
2438}
2439
Jeff Cohend41b30d2006-11-05 19:31:28 +00002440SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002441 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002442 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002443 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002444
Dan Gohman575e2f42007-06-04 15:49:41 +00002445 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2446 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002447 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002448 Ty = MVT::getTypeForValueType(VT);
2449 } else if (SV) {
2450 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2451 assert(PT && "Value for store must be a pointer");
2452 Ty = PT->getElementType();
2453 }
2454 assert(Ty && "Could not get type information for store");
2455 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2456 }
Evan Chengad071e12006-10-05 22:57:11 +00002457 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002458 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002459 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002460 FoldingSetNodeID ID;
2461 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002462 ID.AddInteger(ISD::UNINDEXED);
2463 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002464 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002465 ID.AddInteger(Alignment);
2466 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002467 void *IP = 0;
2468 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2469 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002470 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002471 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002472 CSEMap.InsertNode(N, IP);
2473 AllNodes.push_back(N);
2474 return SDOperand(N, 0);
2475}
2476
Jeff Cohend41b30d2006-11-05 19:31:28 +00002477SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002478 SDOperand Ptr, const Value *SV,
2479 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002480 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002481 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002482
2483 if (VT == SVT)
2484 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002485
Duncan Sandsaf47b112007-10-16 09:56:48 +00002486 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2487 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002488 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2489 "Can't do FP-INT conversion!");
2490
Dan Gohman575e2f42007-06-04 15:49:41 +00002491 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2492 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002493 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002494 Ty = MVT::getTypeForValueType(VT);
2495 } else if (SV) {
2496 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2497 assert(PT && "Value for store must be a pointer");
2498 Ty = PT->getElementType();
2499 }
2500 assert(Ty && "Could not get type information for store");
2501 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2502 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002503 SDVTList VTs = getVTList(MVT::Other);
2504 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002505 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002506 FoldingSetNodeID ID;
2507 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002508 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002509 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002510 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002511 ID.AddInteger(Alignment);
2512 ID.AddInteger(isVolatile);
2513 void *IP = 0;
2514 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2515 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002516 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002517 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002518 CSEMap.InsertNode(N, IP);
2519 AllNodes.push_back(N);
2520 return SDOperand(N, 0);
2521}
2522
Evan Cheng144d8f02006-11-09 17:55:04 +00002523SDOperand
2524SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2525 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002526 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2527 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2528 "Store is already a indexed store!");
2529 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2530 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2531 FoldingSetNodeID ID;
2532 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2533 ID.AddInteger(AM);
2534 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002535 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002536 ID.AddInteger(ST->getAlignment());
2537 ID.AddInteger(ST->isVolatile());
2538 void *IP = 0;
2539 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2540 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002541 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002542 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002543 ST->getSrcValue(), ST->getSrcValueOffset(),
2544 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002545 CSEMap.InsertNode(N, IP);
2546 AllNodes.push_back(N);
2547 return SDOperand(N, 0);
2548}
2549
Nate Begemanacc398c2006-01-25 18:21:52 +00002550SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2551 SDOperand Chain, SDOperand Ptr,
2552 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002553 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002554 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002555}
2556
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002557SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002558 const SDOperand *Ops, unsigned NumOps) {
2559 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002560 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002561 case 1: return getNode(Opcode, VT, Ops[0]);
2562 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2563 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002564 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002565 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002566
Chris Lattneref847df2005-04-09 03:27:28 +00002567 switch (Opcode) {
2568 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002569 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002570 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002571 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2572 "LHS and RHS of condition must have same type!");
2573 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2574 "True and False arms of SelectCC must have same type!");
2575 assert(Ops[2].getValueType() == VT &&
2576 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002577 break;
2578 }
2579 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002580 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002581 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2582 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002583 break;
2584 }
Chris Lattneref847df2005-04-09 03:27:28 +00002585 }
2586
Chris Lattner385328c2005-05-14 07:42:29 +00002587 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002588 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002589 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002590 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002591 FoldingSetNodeID ID;
2592 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002593 void *IP = 0;
2594 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2595 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002596 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002597 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002598 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002599 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002600 }
Chris Lattneref847df2005-04-09 03:27:28 +00002601 AllNodes.push_back(N);
2602 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002603}
2604
Chris Lattner89c34632005-05-14 06:20:26 +00002605SDOperand SelectionDAG::getNode(unsigned Opcode,
2606 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002607 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002608 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2609 Ops, NumOps);
2610}
2611
2612SDOperand SelectionDAG::getNode(unsigned Opcode,
2613 const MVT::ValueType *VTs, unsigned NumVTs,
2614 const SDOperand *Ops, unsigned NumOps) {
2615 if (NumVTs == 1)
2616 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002617 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2618}
2619
2620SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2621 const SDOperand *Ops, unsigned NumOps) {
2622 if (VTList.NumVTs == 1)
2623 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002624
Chris Lattner5f056bf2005-07-10 01:55:33 +00002625 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002626 // FIXME: figure out how to safely handle things like
2627 // int foo(int x) { return 1 << (x & 255); }
2628 // int bar() { return foo(256); }
2629#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002630 case ISD::SRA_PARTS:
2631 case ISD::SRL_PARTS:
2632 case ISD::SHL_PARTS:
2633 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002634 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002635 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2636 else if (N3.getOpcode() == ISD::AND)
2637 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2638 // If the and is only masking out bits that cannot effect the shift,
2639 // eliminate the and.
2640 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2641 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2642 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2643 }
2644 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002645#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002646 }
Chris Lattner89c34632005-05-14 06:20:26 +00002647
Chris Lattner43247a12005-08-25 19:12:10 +00002648 // Memoize the node unless it returns a flag.
2649 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002650 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002651 FoldingSetNodeID ID;
2652 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002653 void *IP = 0;
2654 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2655 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002656 if (NumOps == 1)
2657 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2658 else if (NumOps == 2)
2659 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2660 else if (NumOps == 3)
2661 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2662 else
2663 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002664 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002665 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002666 if (NumOps == 1)
2667 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2668 else if (NumOps == 2)
2669 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2670 else if (NumOps == 3)
2671 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2672 else
2673 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002674 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002675 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002676 return SDOperand(N, 0);
2677}
2678
Dan Gohman08ce9762007-10-08 15:49:58 +00002679SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2680 return getNode(Opcode, VTList, 0, 0);
2681}
2682
2683SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2684 SDOperand N1) {
2685 SDOperand Ops[] = { N1 };
2686 return getNode(Opcode, VTList, Ops, 1);
2687}
2688
2689SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2690 SDOperand N1, SDOperand N2) {
2691 SDOperand Ops[] = { N1, N2 };
2692 return getNode(Opcode, VTList, Ops, 2);
2693}
2694
2695SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2696 SDOperand N1, SDOperand N2, SDOperand N3) {
2697 SDOperand Ops[] = { N1, N2, N3 };
2698 return getNode(Opcode, VTList, Ops, 3);
2699}
2700
2701SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2702 SDOperand N1, SDOperand N2, SDOperand N3,
2703 SDOperand N4) {
2704 SDOperand Ops[] = { N1, N2, N3, N4 };
2705 return getNode(Opcode, VTList, Ops, 4);
2706}
2707
2708SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2709 SDOperand N1, SDOperand N2, SDOperand N3,
2710 SDOperand N4, SDOperand N5) {
2711 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2712 return getNode(Opcode, VTList, Ops, 5);
2713}
2714
Chris Lattner70046e92006-08-15 17:46:01 +00002715SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002716 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002717}
2718
Chris Lattner70046e92006-08-15 17:46:01 +00002719SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002720 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2721 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002722 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002723 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002724 }
2725 std::vector<MVT::ValueType> V;
2726 V.push_back(VT1);
2727 V.push_back(VT2);
2728 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002729 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002730}
Chris Lattner70046e92006-08-15 17:46:01 +00002731SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2732 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002733 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002734 E = VTList.end(); I != E; ++I) {
2735 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2736 (*I)[2] == VT3)
2737 return makeVTList(&(*I)[0], 3);
2738 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002739 std::vector<MVT::ValueType> V;
2740 V.push_back(VT1);
2741 V.push_back(VT2);
2742 V.push_back(VT3);
2743 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002744 return makeVTList(&(*VTList.begin())[0], 3);
2745}
2746
2747SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2748 switch (NumVTs) {
2749 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002750 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002751 case 2: return getVTList(VTs[0], VTs[1]);
2752 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2753 default: break;
2754 }
2755
2756 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2757 E = VTList.end(); I != E; ++I) {
2758 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2759
2760 bool NoMatch = false;
2761 for (unsigned i = 2; i != NumVTs; ++i)
2762 if (VTs[i] != (*I)[i]) {
2763 NoMatch = true;
2764 break;
2765 }
2766 if (!NoMatch)
2767 return makeVTList(&*I->begin(), NumVTs);
2768 }
2769
2770 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2771 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002772}
2773
2774
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002775/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2776/// specified operands. If the resultant node already exists in the DAG,
2777/// this does not modify the specified node, instead it returns the node that
2778/// already exists. If the resultant node does not exist in the DAG, the
2779/// input node is returned. As a degenerate case, if you specify the same
2780/// input operands as the node already has, the input node is returned.
2781SDOperand SelectionDAG::
2782UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2783 SDNode *N = InN.Val;
2784 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2785
2786 // Check to see if there is no change.
2787 if (Op == N->getOperand(0)) return InN;
2788
2789 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002790 void *InsertPos = 0;
2791 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2792 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002793
2794 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002795 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002796 RemoveNodeFromCSEMaps(N);
2797
2798 // Now we update the operands.
2799 N->OperandList[0].Val->removeUser(N);
2800 Op.Val->addUser(N);
2801 N->OperandList[0] = Op;
2802
2803 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002804 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002805 return InN;
2806}
2807
2808SDOperand SelectionDAG::
2809UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2810 SDNode *N = InN.Val;
2811 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2812
2813 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002814 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2815 return InN; // No operands changed, just return the input node.
2816
2817 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002818 void *InsertPos = 0;
2819 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2820 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002821
2822 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002823 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002824 RemoveNodeFromCSEMaps(N);
2825
2826 // Now we update the operands.
2827 if (N->OperandList[0] != Op1) {
2828 N->OperandList[0].Val->removeUser(N);
2829 Op1.Val->addUser(N);
2830 N->OperandList[0] = Op1;
2831 }
2832 if (N->OperandList[1] != Op2) {
2833 N->OperandList[1].Val->removeUser(N);
2834 Op2.Val->addUser(N);
2835 N->OperandList[1] = Op2;
2836 }
2837
2838 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002839 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002840 return InN;
2841}
2842
2843SDOperand SelectionDAG::
2844UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002845 SDOperand Ops[] = { Op1, Op2, Op3 };
2846 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002847}
2848
2849SDOperand SelectionDAG::
2850UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2851 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002852 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2853 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002854}
2855
2856SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002857UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2858 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002859 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2860 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002861}
2862
2863
2864SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002865UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002866 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002867 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002868 "Update with wrong number of operands");
2869
2870 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002871 bool AnyChange = false;
2872 for (unsigned i = 0; i != NumOps; ++i) {
2873 if (Ops[i] != N->getOperand(i)) {
2874 AnyChange = true;
2875 break;
2876 }
2877 }
2878
2879 // No operands changed, just return the input node.
2880 if (!AnyChange) return InN;
2881
2882 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002883 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002884 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002885 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002886
2887 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002888 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002889 RemoveNodeFromCSEMaps(N);
2890
2891 // Now we update the operands.
2892 for (unsigned i = 0; i != NumOps; ++i) {
2893 if (N->OperandList[i] != Ops[i]) {
2894 N->OperandList[i].Val->removeUser(N);
2895 Ops[i].Val->addUser(N);
2896 N->OperandList[i] = Ops[i];
2897 }
2898 }
2899
2900 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002901 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002902 return InN;
2903}
2904
2905
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002906/// MorphNodeTo - This frees the operands of the current node, resets the
2907/// opcode, types, and operands to the specified value. This should only be
2908/// used by the SelectionDAG class.
2909void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2910 const SDOperand *Ops, unsigned NumOps) {
2911 NodeType = Opc;
2912 ValueList = L.VTs;
2913 NumValues = L.NumVTs;
2914
2915 // Clear the operands list, updating used nodes to remove this from their
2916 // use list.
2917 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2918 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002919
2920 // If NumOps is larger than the # of operands we currently have, reallocate
2921 // the operand list.
2922 if (NumOps > NumOperands) {
2923 if (OperandsNeedDelete)
2924 delete [] OperandList;
2925 OperandList = new SDOperand[NumOps];
2926 OperandsNeedDelete = true;
2927 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002928
2929 // Assign the new operands.
2930 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002931
2932 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2933 OperandList[i] = Ops[i];
2934 SDNode *N = OperandList[i].Val;
2935 N->Uses.push_back(this);
2936 }
2937}
Chris Lattner149c58c2005-08-16 18:17:10 +00002938
2939/// SelectNodeTo - These are used for target selectors to *mutate* the
2940/// specified node to have the specified return type, Target opcode, and
2941/// operands. Note that target opcodes are stored as
2942/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002943///
2944/// Note that SelectNodeTo returns the resultant node. If there is already a
2945/// node of the specified opcode and operands, it returns that node instead of
2946/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002947SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2948 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002949 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002950 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002951 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002952 void *IP = 0;
2953 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002954 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002955
Chris Lattner7651fa42005-08-24 23:00:29 +00002956 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002957
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002958 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002959
Chris Lattner4a283e92006-08-11 18:38:11 +00002960 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002961 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002962}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002963
Evan Cheng95514ba2006-08-26 08:00:10 +00002964SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2965 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002966 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002967 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002968 SDOperand Ops[] = { Op1 };
2969
Jim Laskey583bd472006-10-27 23:46:08 +00002970 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002971 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002972 void *IP = 0;
2973 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002974 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002975
Chris Lattner149c58c2005-08-16 18:17:10 +00002976 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002977 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002978 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002979 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002980}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002981
Evan Cheng95514ba2006-08-26 08:00:10 +00002982SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2983 MVT::ValueType VT, SDOperand Op1,
2984 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002985 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002986 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002987 SDOperand Ops[] = { Op1, Op2 };
2988
Jim Laskey583bd472006-10-27 23:46:08 +00002989 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002990 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002991 void *IP = 0;
2992 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002993 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002994
Chris Lattner149c58c2005-08-16 18:17:10 +00002995 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002996
Chris Lattner63e3f142007-02-04 07:28:00 +00002997 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002998
Chris Lattnera5682852006-08-07 23:03:03 +00002999 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003000 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003001}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003002
Evan Cheng95514ba2006-08-26 08:00:10 +00003003SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3004 MVT::ValueType VT, SDOperand Op1,
3005 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003006 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003007 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003008 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003009 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003010 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003011 void *IP = 0;
3012 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003013 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003014
Chris Lattner149c58c2005-08-16 18:17:10 +00003015 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003016
Chris Lattner63e3f142007-02-04 07:28:00 +00003017 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003018
Chris Lattnera5682852006-08-07 23:03:03 +00003019 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003020 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003021}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003022
Evan Cheng95514ba2006-08-26 08:00:10 +00003023SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003024 MVT::ValueType VT, const SDOperand *Ops,
3025 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003026 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003027 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003028 FoldingSetNodeID ID;
3029 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003030 void *IP = 0;
3031 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003032 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003033
Chris Lattner6b09a292005-08-21 18:49:33 +00003034 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003035 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003036
Chris Lattnera5682852006-08-07 23:03:03 +00003037 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003038 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003039}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003040
Evan Cheng95514ba2006-08-26 08:00:10 +00003041SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3042 MVT::ValueType VT1, MVT::ValueType VT2,
3043 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003044 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003045 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003046 SDOperand Ops[] = { Op1, Op2 };
3047 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003048 void *IP = 0;
3049 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003050 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003051
Chris Lattner0fb094f2005-11-19 01:44:53 +00003052 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003053 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003054 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003055 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003056}
3057
Evan Cheng95514ba2006-08-26 08:00:10 +00003058SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3059 MVT::ValueType VT1, MVT::ValueType VT2,
3060 SDOperand Op1, SDOperand Op2,
3061 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003062 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003063 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003064 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003065 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003066 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003067 void *IP = 0;
3068 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003069 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003070
Chris Lattner0fb094f2005-11-19 01:44:53 +00003071 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003072
Chris Lattner63e3f142007-02-04 07:28:00 +00003073 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003074 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003075 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003076}
3077
Chris Lattner0fb094f2005-11-19 01:44:53 +00003078
Evan Cheng6ae46c42006-02-09 07:15:23 +00003079/// getTargetNode - These are used for target selectors to create a new node
3080/// with specified return type(s), target opcode, and operands.
3081///
3082/// Note that getTargetNode returns the resultant node. If there is already a
3083/// node of the specified opcode and operands, it returns that node instead of
3084/// the current one.
3085SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3086 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3087}
3088SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3089 SDOperand Op1) {
3090 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3091}
3092SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3093 SDOperand Op1, SDOperand Op2) {
3094 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3095}
3096SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003097 SDOperand Op1, SDOperand Op2,
3098 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003099 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3100}
3101SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003102 const SDOperand *Ops, unsigned NumOps) {
3103 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003104}
3105SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003106 MVT::ValueType VT2) {
3107 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3108 SDOperand Op;
3109 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3110}
3111SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003112 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003113 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003114 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003115}
3116SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003117 MVT::ValueType VT2, SDOperand Op1,
3118 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003119 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003120 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003121 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003122}
3123SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003124 MVT::ValueType VT2, SDOperand Op1,
3125 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003126 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003127 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003128 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003129}
Evan Cheng694481e2006-08-27 08:08:54 +00003130SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3131 MVT::ValueType VT2,
3132 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003133 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003134 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003135}
3136SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3137 MVT::ValueType VT2, MVT::ValueType VT3,
3138 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003139 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003140 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003141 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003142}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003143SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3144 MVT::ValueType VT2, MVT::ValueType VT3,
3145 SDOperand Op1, SDOperand Op2,
3146 SDOperand Op3) {
3147 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3148 SDOperand Ops[] = { Op1, Op2, Op3 };
3149 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3150}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003151SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003152 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003153 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003154 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3155 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003156}
Evan Cheng05e69c12007-09-12 23:39:49 +00003157SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3158 MVT::ValueType VT2, MVT::ValueType VT3,
3159 MVT::ValueType VT4,
3160 const SDOperand *Ops, unsigned NumOps) {
3161 std::vector<MVT::ValueType> VTList;
3162 VTList.push_back(VT1);
3163 VTList.push_back(VT2);
3164 VTList.push_back(VT3);
3165 VTList.push_back(VT4);
3166 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3167 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3168}
Evan Cheng39305cf2007-10-05 01:10:49 +00003169SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3170 std::vector<MVT::ValueType> &ResultTys,
3171 const SDOperand *Ops, unsigned NumOps) {
3172 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3173 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3174 Ops, NumOps).Val;
3175}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003176
Evan Cheng99157a02006-08-07 22:13:29 +00003177/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003178/// This can cause recursive merging of nodes in the DAG.
3179///
Chris Lattner8b52f212005-08-26 18:36:28 +00003180/// This version assumes From/To have a single result value.
3181///
Chris Lattner1e111c72005-09-07 05:37:01 +00003182void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3183 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003184 SDNode *From = FromN.Val, *To = ToN.Val;
3185 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3186 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003187 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003188
Chris Lattner8b8749f2005-08-17 19:00:20 +00003189 while (!From->use_empty()) {
3190 // Process users until they are all gone.
3191 SDNode *U = *From->use_begin();
3192
3193 // This node is about to morph, remove its old self from the CSE maps.
3194 RemoveNodeFromCSEMaps(U);
3195
Chris Lattner65113b22005-11-08 22:07:03 +00003196 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3197 I != E; ++I)
3198 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003199 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003200 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003201 To->addUser(U);
3202 }
3203
3204 // Now that we have modified U, add it back to the CSE maps. If it already
3205 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003206 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3207 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003208 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003209 if (Deleted) Deleted->push_back(U);
3210 DeleteNodeNotInCSEMaps(U);
3211 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003212 }
3213}
3214
3215/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3216/// This can cause recursive merging of nodes in the DAG.
3217///
3218/// This version assumes From/To have matching types and numbers of result
3219/// values.
3220///
Chris Lattner1e111c72005-09-07 05:37:01 +00003221void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3222 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003223 assert(From != To && "Cannot replace uses of with self");
3224 assert(From->getNumValues() == To->getNumValues() &&
3225 "Cannot use this version of ReplaceAllUsesWith!");
3226 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003227 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003228 return;
3229 }
3230
3231 while (!From->use_empty()) {
3232 // Process users until they are all gone.
3233 SDNode *U = *From->use_begin();
3234
3235 // This node is about to morph, remove its old self from the CSE maps.
3236 RemoveNodeFromCSEMaps(U);
3237
Chris Lattner65113b22005-11-08 22:07:03 +00003238 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3239 I != E; ++I)
3240 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003241 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003242 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003243 To->addUser(U);
3244 }
3245
3246 // Now that we have modified U, add it back to the CSE maps. If it already
3247 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003248 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3249 ReplaceAllUsesWith(U, Existing, Deleted);
3250 // U is now dead.
3251 if (Deleted) Deleted->push_back(U);
3252 DeleteNodeNotInCSEMaps(U);
3253 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003254 }
3255}
3256
Chris Lattner8b52f212005-08-26 18:36:28 +00003257/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3258/// This can cause recursive merging of nodes in the DAG.
3259///
3260/// This version can replace From with any result values. To must match the
3261/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003262void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003263 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003264 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003265 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003266 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003267 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003268 return;
3269 }
3270
3271 while (!From->use_empty()) {
3272 // Process users until they are all gone.
3273 SDNode *U = *From->use_begin();
3274
3275 // This node is about to morph, remove its old self from the CSE maps.
3276 RemoveNodeFromCSEMaps(U);
3277
Chris Lattner65113b22005-11-08 22:07:03 +00003278 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3279 I != E; ++I)
3280 if (I->Val == From) {
3281 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003282 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003283 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003284 ToOp.Val->addUser(U);
3285 }
3286
3287 // Now that we have modified U, add it back to the CSE maps. If it already
3288 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003289 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3290 ReplaceAllUsesWith(U, Existing, Deleted);
3291 // U is now dead.
3292 if (Deleted) Deleted->push_back(U);
3293 DeleteNodeNotInCSEMaps(U);
3294 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003295 }
3296}
3297
Chris Lattner012f2412006-02-17 21:58:01 +00003298/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3299/// uses of other values produced by From.Val alone. The Deleted vector is
3300/// handled the same was as for ReplaceAllUsesWith.
3301void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003302 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003303 assert(From != To && "Cannot replace a value with itself");
3304 // Handle the simple, trivial, case efficiently.
3305 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003306 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003307 return;
3308 }
3309
Chris Lattnercf5640b2007-02-04 00:14:31 +00003310 // Get all of the users of From.Val. We want these in a nice,
3311 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3312 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003313
Chris Lattner01d029b2007-10-15 06:10:22 +00003314 std::vector<SDNode*> LocalDeletionVector;
3315
3316 // Pick a deletion vector to use. If the user specified one, use theirs,
3317 // otherwise use a local one.
3318 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003319 while (!Users.empty()) {
3320 // We know that this user uses some value of From. If it is the right
3321 // value, update it.
3322 SDNode *User = Users.back();
3323 Users.pop_back();
3324
Chris Lattner01d029b2007-10-15 06:10:22 +00003325 // Scan for an operand that matches From.
3326 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3327 for (; Op != E; ++Op)
3328 if (*Op == From) break;
3329
3330 // If there are no matches, the user must use some other result of From.
3331 if (Op == E) continue;
3332
3333 // Okay, we know this user needs to be updated. Remove its old self
3334 // from the CSE maps.
3335 RemoveNodeFromCSEMaps(User);
3336
3337 // Update all operands that match "From".
3338 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003339 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003340 From.Val->removeUser(User);
3341 *Op = To;
3342 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003343 }
3344 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003345
3346 // Now that we have modified User, add it back to the CSE maps. If it
3347 // already exists there, recursively merge the results together.
3348 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3349 if (!Existing) continue; // Continue on to next user.
3350
3351 // If there was already an existing matching node, use ReplaceAllUsesWith
3352 // to replace the dead one with the existing one. However, this can cause
3353 // recursive merging of other unrelated nodes down the line. The merging
3354 // can cause deletion of nodes that used the old value. In this case,
3355 // we have to be certain to remove them from the Users set.
3356 unsigned NumDeleted = DeleteVector->size();
3357 ReplaceAllUsesWith(User, Existing, DeleteVector);
3358
3359 // User is now dead.
3360 DeleteVector->push_back(User);
3361 DeleteNodeNotInCSEMaps(User);
3362
3363 // We have to be careful here, because ReplaceAllUsesWith could have
3364 // deleted a user of From, which means there may be dangling pointers
3365 // in the "Users" setvector. Scan over the deleted node pointers and
3366 // remove them from the setvector.
3367 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3368 Users.remove((*DeleteVector)[i]);
3369
3370 // If the user doesn't need the set of deleted elements, don't retain them
3371 // to the next loop iteration.
3372 if (Deleted == 0)
3373 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003374 }
3375}
3376
Chris Lattner7b2880c2005-08-24 22:44:39 +00003377
Evan Chenge6f35d82006-08-01 08:20:41 +00003378/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3379/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003380unsigned SelectionDAG::AssignNodeIds() {
3381 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003382 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3383 SDNode *N = I;
3384 N->setNodeId(Id++);
3385 }
3386 return Id;
3387}
3388
Evan Chenge6f35d82006-08-01 08:20:41 +00003389/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003390/// based on their topological order. It returns the maximum id and a vector
3391/// of the SDNodes* in assigned order by reference.
3392unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003393 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003394 std::vector<unsigned> InDegree(DAGSize);
3395 std::vector<SDNode*> Sources;
3396
3397 // Use a two pass approach to avoid using a std::map which is slow.
3398 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003399 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3400 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003401 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003402 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003403 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003404 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003405 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003406 }
3407
Evan Cheng99157a02006-08-07 22:13:29 +00003408 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003409 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003410 SDNode *N = Sources.back();
3411 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003412 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003413 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3414 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003415 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003416 if (Degree == 0)
3417 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003418 }
3419 }
3420
Evan Chengc384d6c2006-08-02 22:00:34 +00003421 // Second pass, assign the actual topological order as node ids.
3422 Id = 0;
3423 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3424 TI != TE; ++TI)
3425 (*TI)->setNodeId(Id++);
3426
3427 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003428}
3429
3430
Evan Cheng091cba12006-07-27 06:39:06 +00003431
Jim Laskey58b968b2005-08-17 20:08:02 +00003432//===----------------------------------------------------------------------===//
3433// SDNode Class
3434//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003435
Chris Lattner917d2c92006-07-19 00:00:37 +00003436// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003437void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003438void UnarySDNode::ANCHOR() {}
3439void BinarySDNode::ANCHOR() {}
3440void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003441void HandleSDNode::ANCHOR() {}
3442void StringSDNode::ANCHOR() {}
3443void ConstantSDNode::ANCHOR() {}
3444void ConstantFPSDNode::ANCHOR() {}
3445void GlobalAddressSDNode::ANCHOR() {}
3446void FrameIndexSDNode::ANCHOR() {}
3447void JumpTableSDNode::ANCHOR() {}
3448void ConstantPoolSDNode::ANCHOR() {}
3449void BasicBlockSDNode::ANCHOR() {}
3450void SrcValueSDNode::ANCHOR() {}
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003451void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003452void RegisterSDNode::ANCHOR() {}
3453void ExternalSymbolSDNode::ANCHOR() {}
3454void CondCodeSDNode::ANCHOR() {}
3455void VTSDNode::ANCHOR() {}
3456void LoadSDNode::ANCHOR() {}
3457void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003458
Chris Lattner48b85922007-02-04 02:41:42 +00003459HandleSDNode::~HandleSDNode() {
3460 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003461 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003462}
3463
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003464GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3465 MVT::ValueType VT, int o)
3466 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003467 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003468 // Thread Local
3469 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3470 // Non Thread Local
3471 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3472 getSDVTList(VT)), Offset(o) {
3473 TheGlobal = const_cast<GlobalValue*>(GA);
3474}
Chris Lattner48b85922007-02-04 02:41:42 +00003475
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003476/// getMemOperand - Return a MemOperand object describing the memory
3477/// reference performed by this load or store.
3478MemOperand LSBaseSDNode::getMemOperand() const {
3479 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3480 int Flags =
3481 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3482 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3483
3484 // Check if the load references a frame index, and does not have
3485 // an SV attached.
3486 const FrameIndexSDNode *FI =
3487 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3488 if (!getSrcValue() && FI)
3489 return MemOperand(&PseudoSourceValue::FPRel, Flags,
3490 FI->getIndex(), Size, Alignment);
3491 else
3492 return MemOperand(getSrcValue(), Flags,
3493 getSrcValueOffset(), Size, Alignment);
3494}
3495
Jim Laskey583bd472006-10-27 23:46:08 +00003496/// Profile - Gather unique data for the node.
3497///
3498void SDNode::Profile(FoldingSetNodeID &ID) {
3499 AddNodeIDNode(ID, this);
3500}
3501
Chris Lattnera3255112005-11-08 23:30:28 +00003502/// getValueTypeList - Return a pointer to the specified value type.
3503///
3504MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003505 if (MVT::isExtendedVT(VT)) {
3506 static std::set<MVT::ValueType> EVTs;
3507 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3508 } else {
3509 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3510 VTs[VT] = VT;
3511 return &VTs[VT];
3512 }
Chris Lattnera3255112005-11-08 23:30:28 +00003513}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003514
Chris Lattner5c884562005-01-12 18:37:47 +00003515/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3516/// indicated value. This method ignores uses of other values defined by this
3517/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003518bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003519 assert(Value < getNumValues() && "Bad value!");
3520
3521 // If there is only one value, this is easy.
3522 if (getNumValues() == 1)
3523 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003524 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003525
Evan Cheng4ee62112006-02-05 06:29:23 +00003526 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003527
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003528 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003529
Chris Lattnerf83482d2006-08-16 20:59:32 +00003530 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003531 SDNode *User = *UI;
3532 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003533 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003534 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3535 if (User->getOperand(i) == TheValue) {
3536 if (NUses == 0)
3537 return false; // too many uses
3538 --NUses;
3539 }
3540 }
3541
3542 // Found exactly the right number of uses?
3543 return NUses == 0;
3544}
3545
3546
Evan Cheng33d55952007-08-02 05:29:38 +00003547/// hasAnyUseOfValue - Return true if there are any use of the indicated
3548/// value. This method ignores uses of other values defined by this operation.
3549bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3550 assert(Value < getNumValues() && "Bad value!");
3551
Dan Gohman30359592008-01-29 13:02:09 +00003552 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003553
3554 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3555
3556 SmallPtrSet<SDNode*, 32> UsersHandled;
3557
3558 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3559 SDNode *User = *UI;
3560 if (User->getNumOperands() == 1 ||
3561 UsersHandled.insert(User)) // First time we've seen this?
3562 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3563 if (User->getOperand(i) == TheValue) {
3564 return true;
3565 }
3566 }
3567
3568 return false;
3569}
3570
3571
Evan Chenge6e97e62006-11-03 07:31:32 +00003572/// isOnlyUse - Return true if this node is the only use of N.
3573///
Evan Cheng4ee62112006-02-05 06:29:23 +00003574bool SDNode::isOnlyUse(SDNode *N) const {
3575 bool Seen = false;
3576 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3577 SDNode *User = *I;
3578 if (User == this)
3579 Seen = true;
3580 else
3581 return false;
3582 }
3583
3584 return Seen;
3585}
3586
Evan Chenge6e97e62006-11-03 07:31:32 +00003587/// isOperand - Return true if this node is an operand of N.
3588///
Evan Chengbfa284f2006-03-03 06:42:32 +00003589bool SDOperand::isOperand(SDNode *N) const {
3590 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3591 if (*this == N->getOperand(i))
3592 return true;
3593 return false;
3594}
3595
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003596bool SDNode::isOperand(SDNode *N) const {
3597 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3598 if (this == N->OperandList[i].Val)
3599 return true;
3600 return false;
3601}
Evan Cheng4ee62112006-02-05 06:29:23 +00003602
Chris Lattner572dee72008-01-16 05:49:24 +00003603/// reachesChainWithoutSideEffects - Return true if this operand (which must
3604/// be a chain) reaches the specified operand without crossing any
3605/// side-effecting instructions. In practice, this looks through token
3606/// factors and non-volatile loads. In order to remain efficient, this only
3607/// looks a couple of nodes in, it does not do an exhaustive search.
3608bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3609 unsigned Depth) const {
3610 if (*this == Dest) return true;
3611
3612 // Don't search too deeply, we just want to be able to see through
3613 // TokenFactor's etc.
3614 if (Depth == 0) return false;
3615
3616 // If this is a token factor, all inputs to the TF happen in parallel. If any
3617 // of the operands of the TF reach dest, then we can do the xform.
3618 if (getOpcode() == ISD::TokenFactor) {
3619 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3620 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3621 return true;
3622 return false;
3623 }
3624
3625 // Loads don't have side effects, look through them.
3626 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3627 if (!Ld->isVolatile())
3628 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3629 }
3630 return false;
3631}
3632
3633
Evan Chengc5fc57d2006-11-03 03:05:24 +00003634static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003635 SmallPtrSet<SDNode *, 32> &Visited) {
3636 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003637 return;
3638
3639 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3640 SDNode *Op = N->getOperand(i).Val;
3641 if (Op == P) {
3642 found = true;
3643 return;
3644 }
3645 findPredecessor(Op, P, found, Visited);
3646 }
3647}
3648
Evan Chenge6e97e62006-11-03 07:31:32 +00003649/// isPredecessor - Return true if this node is a predecessor of N. This node
3650/// is either an operand of N or it can be reached by recursively traversing
3651/// up the operands.
3652/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003653bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003654 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003655 bool found = false;
3656 findPredecessor(N, this, found, Visited);
3657 return found;
3658}
3659
Evan Chengc5484282006-10-04 00:56:09 +00003660uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3661 assert(Num < NumOperands && "Invalid child # of SDNode!");
3662 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3663}
3664
Reid Spencer577cc322007-04-01 07:32:19 +00003665std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003666 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003667 default:
3668 if (getOpcode() < ISD::BUILTIN_OP_END)
3669 return "<<Unknown DAG Node>>";
3670 else {
Evan Cheng72261582005-12-20 06:22:03 +00003671 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003672 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003673 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003674 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003675
Evan Cheng72261582005-12-20 06:22:03 +00003676 TargetLowering &TLI = G->getTargetLoweringInfo();
3677 const char *Name =
3678 TLI.getTargetNodeName(getOpcode());
3679 if (Name) return Name;
3680 }
3681
3682 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003683 }
3684
Andrew Lenharth95762122005-03-31 21:24:06 +00003685 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003686 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003687 case ISD::SRCVALUE: return "SrcValue";
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003688 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003689 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003690 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003691 case ISD::AssertSext: return "AssertSext";
3692 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003693
3694 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003695 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003696 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003697 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003698
3699 case ISD::Constant: return "Constant";
3700 case ISD::ConstantFP: return "ConstantFP";
3701 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003702 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003703 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003704 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003705 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003706 case ISD::RETURNADDR: return "RETURNADDR";
3707 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003708 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003709 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3710 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003711 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003712 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003713 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003714 case ISD::INTRINSIC_WO_CHAIN: {
3715 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3716 return Intrinsic::getName((Intrinsic::ID)IID);
3717 }
3718 case ISD::INTRINSIC_VOID:
3719 case ISD::INTRINSIC_W_CHAIN: {
3720 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003721 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003722 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003723
Chris Lattnerb2827b02006-03-19 00:52:58 +00003724 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003725 case ISD::TargetConstant: return "TargetConstant";
3726 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003727 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003728 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003729 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003730 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003731 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003732 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003733
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003734 case ISD::CopyToReg: return "CopyToReg";
3735 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003736 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003737 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003738 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003739 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003740 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003741 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003742 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003743
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003744 // Unary operators
3745 case ISD::FABS: return "fabs";
3746 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003747 case ISD::FSQRT: return "fsqrt";
3748 case ISD::FSIN: return "fsin";
3749 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003750 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003751 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003752
3753 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003754 case ISD::ADD: return "add";
3755 case ISD::SUB: return "sub";
3756 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003757 case ISD::MULHU: return "mulhu";
3758 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003759 case ISD::SDIV: return "sdiv";
3760 case ISD::UDIV: return "udiv";
3761 case ISD::SREM: return "srem";
3762 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003763 case ISD::SMUL_LOHI: return "smul_lohi";
3764 case ISD::UMUL_LOHI: return "umul_lohi";
3765 case ISD::SDIVREM: return "sdivrem";
3766 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003767 case ISD::AND: return "and";
3768 case ISD::OR: return "or";
3769 case ISD::XOR: return "xor";
3770 case ISD::SHL: return "shl";
3771 case ISD::SRA: return "sra";
3772 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003773 case ISD::ROTL: return "rotl";
3774 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003775 case ISD::FADD: return "fadd";
3776 case ISD::FSUB: return "fsub";
3777 case ISD::FMUL: return "fmul";
3778 case ISD::FDIV: return "fdiv";
3779 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003780 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003781 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003782
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003783 case ISD::SETCC: return "setcc";
3784 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003785 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003786 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003787 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003788 case ISD::CONCAT_VECTORS: return "concat_vectors";
3789 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003790 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003791 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003792 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003793 case ISD::ADDC: return "addc";
3794 case ISD::ADDE: return "adde";
3795 case ISD::SUBC: return "subc";
3796 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003797 case ISD::SHL_PARTS: return "shl_parts";
3798 case ISD::SRA_PARTS: return "sra_parts";
3799 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003800
3801 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3802 case ISD::INSERT_SUBREG: return "insert_subreg";
3803
Chris Lattner7f644642005-04-28 21:44:03 +00003804 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003805 case ISD::SIGN_EXTEND: return "sign_extend";
3806 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003807 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003808 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003809 case ISD::TRUNCATE: return "truncate";
3810 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003811 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003812 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003813 case ISD::FP_EXTEND: return "fp_extend";
3814
3815 case ISD::SINT_TO_FP: return "sint_to_fp";
3816 case ISD::UINT_TO_FP: return "uint_to_fp";
3817 case ISD::FP_TO_SINT: return "fp_to_sint";
3818 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003819 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003820
3821 // Control flow instructions
3822 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003823 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003824 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003825 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003826 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003827 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003828 case ISD::CALLSEQ_START: return "callseq_start";
3829 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003830
3831 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003832 case ISD::LOAD: return "load";
3833 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003834 case ISD::VAARG: return "vaarg";
3835 case ISD::VACOPY: return "vacopy";
3836 case ISD::VAEND: return "vaend";
3837 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003838 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003839 case ISD::EXTRACT_ELEMENT: return "extract_element";
3840 case ISD::BUILD_PAIR: return "build_pair";
3841 case ISD::STACKSAVE: return "stacksave";
3842 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003843 case ISD::TRAP: return "trap";
3844
Chris Lattner5a67afc2006-01-13 02:39:42 +00003845 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003846 case ISD::MEMSET: return "memset";
3847 case ISD::MEMCPY: return "memcpy";
3848 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003849
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003850 // Bit manipulation
3851 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003852 case ISD::CTPOP: return "ctpop";
3853 case ISD::CTTZ: return "cttz";
3854 case ISD::CTLZ: return "ctlz";
3855
Chris Lattner36ce6912005-11-29 06:21:05 +00003856 // Debug info
3857 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003858 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003859
Duncan Sands36397f52007-07-27 12:58:54 +00003860 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003861 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003862
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003863 case ISD::CONDCODE:
3864 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003865 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003866 case ISD::SETOEQ: return "setoeq";
3867 case ISD::SETOGT: return "setogt";
3868 case ISD::SETOGE: return "setoge";
3869 case ISD::SETOLT: return "setolt";
3870 case ISD::SETOLE: return "setole";
3871 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003872
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003873 case ISD::SETO: return "seto";
3874 case ISD::SETUO: return "setuo";
3875 case ISD::SETUEQ: return "setue";
3876 case ISD::SETUGT: return "setugt";
3877 case ISD::SETUGE: return "setuge";
3878 case ISD::SETULT: return "setult";
3879 case ISD::SETULE: return "setule";
3880 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003881
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003882 case ISD::SETEQ: return "seteq";
3883 case ISD::SETGT: return "setgt";
3884 case ISD::SETGE: return "setge";
3885 case ISD::SETLT: return "setlt";
3886 case ISD::SETLE: return "setle";
3887 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003888 }
3889 }
3890}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003891
Evan Cheng144d8f02006-11-09 17:55:04 +00003892const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003893 switch (AM) {
3894 default:
3895 return "";
3896 case ISD::PRE_INC:
3897 return "<pre-inc>";
3898 case ISD::PRE_DEC:
3899 return "<pre-dec>";
3900 case ISD::POST_INC:
3901 return "<post-inc>";
3902 case ISD::POST_DEC:
3903 return "<post-dec>";
3904 }
3905}
3906
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003907void SDNode::dump() const { dump(0); }
3908void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003909 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003910
3911 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003912 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003913 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003914 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003915 else
Bill Wendling832171c2006-12-07 20:04:42 +00003916 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003917 }
Bill Wendling832171c2006-12-07 20:04:42 +00003918 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003919
Bill Wendling832171c2006-12-07 20:04:42 +00003920 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003921 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003922 if (i) cerr << ", ";
3923 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003924 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003925 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003926 }
3927
Evan Chengce254432007-12-11 02:08:35 +00003928 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3929 SDNode *Mask = getOperand(2).Val;
3930 cerr << "<";
3931 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3932 if (i) cerr << ",";
3933 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3934 cerr << "u";
3935 else
3936 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3937 }
3938 cerr << ">";
3939 }
3940
Chris Lattnerc3aae252005-01-07 07:46:32 +00003941 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003942 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003943 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003944 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3945 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3946 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3947 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3948 else {
3949 cerr << "<APFloat(";
3950 CSDN->getValueAPF().convertToAPInt().dump();
3951 cerr << ")>";
3952 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003953 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003954 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003955 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003956 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003957 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003958 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003959 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003960 else
Bill Wendling832171c2006-12-07 20:04:42 +00003961 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003962 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003963 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003964 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003965 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003966 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003967 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003968 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003969 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003970 else
Bill Wendling832171c2006-12-07 20:04:42 +00003971 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003972 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003973 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003974 else
Bill Wendling832171c2006-12-07 20:04:42 +00003975 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003976 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003977 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003978 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3979 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003980 cerr << LBB->getName() << " ";
3981 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003982 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003983 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003984 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003985 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003986 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003987 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003988 } else if (const ExternalSymbolSDNode *ES =
3989 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003990 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003991 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3992 if (M->getValue())
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003993 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003994 else
Dan Gohmanc6c391d2008-01-31 00:25:39 +00003995 cerr << "<null>";
3996 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
3997 if (M->MO.getValue())
3998 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
3999 else
4000 cerr << "<null:" << M->MO.getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00004001 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004002 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004003 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004004 const Value *SrcValue = LD->getSrcValue();
4005 int SrcOffset = LD->getSrcValueOffset();
4006 cerr << " <";
4007 if (SrcValue)
4008 cerr << SrcValue;
4009 else
4010 cerr << "null";
4011 cerr << ":" << SrcOffset << ">";
4012
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004013 bool doExt = true;
4014 switch (LD->getExtensionType()) {
4015 default: doExt = false; break;
4016 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004017 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004018 break;
4019 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004020 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004021 break;
4022 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004023 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004024 break;
4025 }
4026 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004027 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004028
Evan Cheng144d8f02006-11-09 17:55:04 +00004029 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004030 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004031 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004032 if (LD->isVolatile())
4033 cerr << " <volatile>";
4034 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004035 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004036 const Value *SrcValue = ST->getSrcValue();
4037 int SrcOffset = ST->getSrcValueOffset();
4038 cerr << " <";
4039 if (SrcValue)
4040 cerr << SrcValue;
4041 else
4042 cerr << "null";
4043 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004044
4045 if (ST->isTruncatingStore())
4046 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004047 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004048
4049 const char *AM = getIndexedModeName(ST->getAddressingMode());
4050 if (*AM)
4051 cerr << " " << AM;
4052 if (ST->isVolatile())
4053 cerr << " <volatile>";
4054 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004055 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004056}
4057
Chris Lattnerde202b32005-11-09 23:47:37 +00004058static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004059 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4060 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004061 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004062 else
Bill Wendling832171c2006-12-07 20:04:42 +00004063 cerr << "\n" << std::string(indent+2, ' ')
4064 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004065
Chris Lattnerea946cd2005-01-09 20:38:33 +00004066
Bill Wendling832171c2006-12-07 20:04:42 +00004067 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004068 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004069}
4070
Chris Lattnerc3aae252005-01-07 07:46:32 +00004071void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004072 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004073 std::vector<const SDNode*> Nodes;
4074 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4075 I != E; ++I)
4076 Nodes.push_back(I);
4077
Chris Lattner49d24712005-01-09 20:26:36 +00004078 std::sort(Nodes.begin(), Nodes.end());
4079
4080 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004081 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004082 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004083 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004084
Jim Laskey26f7fa72006-10-17 19:33:52 +00004085 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004086
Bill Wendling832171c2006-12-07 20:04:42 +00004087 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004088}
4089
Evan Chengd6594ae2006-09-12 21:00:35 +00004090const Type *ConstantPoolSDNode::getType() const {
4091 if (isMachineConstantPoolEntry())
4092 return Val.MachineCPVal->getType();
4093 return Val.ConstVal->getType();
4094}