blob: eeec961ceded2efb7609a173c29885dfc34e896d [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"
Evan Chenga844bde2008-02-02 04:07:54 +000023#include "llvm/CodeGen/MachineModuleInfo.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
Chris Lattnerf8dc0612008-02-03 06:49:24 +000046SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
47
Jim Laskey58b968b2005-08-17 20:08:02 +000048//===----------------------------------------------------------------------===//
49// ConstantFPSDNode Class
50//===----------------------------------------------------------------------===//
51
52/// isExactlyValue - We don't rely on operator== working on double values, as
53/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
54/// As such, this method can be used to do an exact bit-for-bit comparison of
55/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000056bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000057 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000058}
59
Dale Johannesenf04afdb2007-08-30 00:23:21 +000060bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
61 const APFloat& Val) {
62 // convert modifies in place, so make a copy.
63 APFloat Val2 = APFloat(Val);
64 switch (VT) {
65 default:
66 return false; // These can't be represented as floating point!
67
68 // FIXME rounding mode needs to be more flexible
69 case MVT::f32:
70 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
71 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
72 APFloat::opOK;
73 case MVT::f64:
74 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
75 &Val2.getSemantics() == &APFloat::IEEEdouble ||
76 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
77 APFloat::opOK;
78 // TODO: Figure out how to test if we can use a shorter type instead!
79 case MVT::f80:
80 case MVT::f128:
81 case MVT::ppcf128:
82 return true;
83 }
84}
85
Jim Laskey58b968b2005-08-17 20:08:02 +000086//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000087// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000088//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000089
Evan Chenga8df1662006-03-27 06:58:47 +000090/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000091/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000092bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000093 // Look through a bit convert.
94 if (N->getOpcode() == ISD::BIT_CONVERT)
95 N = N->getOperand(0).Val;
96
Evan Chenga8df1662006-03-27 06:58:47 +000097 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000098
99 unsigned i = 0, e = N->getNumOperands();
100
101 // Skip over all of the undef values.
102 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
103 ++i;
104
105 // Do not accept an all-undef vector.
106 if (i == e) return false;
107
108 // Do not accept build_vectors that aren't all constants or which have non-~0
109 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000110 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000111 if (isa<ConstantSDNode>(NotZero)) {
112 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
113 return false;
114 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000115 MVT::ValueType VT = NotZero.getValueType();
116 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000117 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
118 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000119 return false;
120 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000121 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
122 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000123 (uint32_t)-1)
124 return false;
125 }
Evan Chenga8df1662006-03-27 06:58:47 +0000126 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000127 return false;
128
129 // Okay, we have at least one ~0 value, check to see if the rest match or are
130 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000131 for (++i; i != e; ++i)
132 if (N->getOperand(i) != NotZero &&
133 N->getOperand(i).getOpcode() != ISD::UNDEF)
134 return false;
135 return true;
136}
137
138
Evan Cheng4a147842006-03-26 09:50:58 +0000139/// isBuildVectorAllZeros - Return true if the specified node is a
140/// BUILD_VECTOR where all of the elements are 0 or undef.
141bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000142 // Look through a bit convert.
143 if (N->getOpcode() == ISD::BIT_CONVERT)
144 N = N->getOperand(0).Val;
145
Evan Cheng4a147842006-03-26 09:50:58 +0000146 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000147
148 unsigned i = 0, e = N->getNumOperands();
149
150 // Skip over all of the undef values.
151 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
152 ++i;
153
154 // Do not accept an all-undef vector.
155 if (i == e) return false;
156
157 // Do not accept build_vectors that aren't all constants or which have non-~0
158 // elements.
159 SDOperand Zero = N->getOperand(i);
160 if (isa<ConstantSDNode>(Zero)) {
161 if (!cast<ConstantSDNode>(Zero)->isNullValue())
162 return false;
163 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000164 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000165 return false;
166 } else
167 return false;
168
169 // Okay, we have at least one ~0 value, check to see if the rest match or are
170 // undefs.
171 for (++i; i != e; ++i)
172 if (N->getOperand(i) != Zero &&
173 N->getOperand(i).getOpcode() != ISD::UNDEF)
174 return false;
175 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000176}
177
Evan Chengbb81d972008-01-31 09:59:15 +0000178/// isDebugLabel - Return true if the specified node represents a debug
179/// label (i.e. ISD::LABEL or TargetInstrInfo::LANEL node and third operand
180/// is 0).
181bool ISD::isDebugLabel(const SDNode *N) {
182 SDOperand Zero;
183 if (N->getOpcode() == ISD::LABEL)
184 Zero = N->getOperand(2);
185 else if (N->isTargetOpcode() &&
186 N->getTargetOpcode() == TargetInstrInfo::LABEL)
187 // Chain moved to last operand.
188 Zero = N->getOperand(1);
189 else
190 return false;
191 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
192}
193
Chris Lattnerc3aae252005-01-07 07:46:32 +0000194/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
195/// when given the operation for (X op Y).
196ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
197 // To perform this operation, we just need to swap the L and G bits of the
198 // operation.
199 unsigned OldL = (Operation >> 2) & 1;
200 unsigned OldG = (Operation >> 1) & 1;
201 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
202 (OldL << 1) | // New G bit
203 (OldG << 2)); // New L bit.
204}
205
206/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
207/// 'op' is a valid SetCC operation.
208ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
209 unsigned Operation = Op;
210 if (isInteger)
211 Operation ^= 7; // Flip L, G, E bits, but not U.
212 else
213 Operation ^= 15; // Flip all of the condition bits.
214 if (Operation > ISD::SETTRUE2)
215 Operation &= ~8; // Don't let N and U bits get set.
216 return ISD::CondCode(Operation);
217}
218
219
220/// isSignedOp - For an integer comparison, return 1 if the comparison is a
221/// signed operation and 2 if the result is an unsigned comparison. Return zero
222/// if the operation does not depend on the sign of the input (setne and seteq).
223static int isSignedOp(ISD::CondCode Opcode) {
224 switch (Opcode) {
225 default: assert(0 && "Illegal integer setcc operation!");
226 case ISD::SETEQ:
227 case ISD::SETNE: return 0;
228 case ISD::SETLT:
229 case ISD::SETLE:
230 case ISD::SETGT:
231 case ISD::SETGE: return 1;
232 case ISD::SETULT:
233 case ISD::SETULE:
234 case ISD::SETUGT:
235 case ISD::SETUGE: return 2;
236 }
237}
238
239/// getSetCCOrOperation - Return the result of a logical OR between different
240/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
241/// returns SETCC_INVALID if it is not possible to represent the resultant
242/// comparison.
243ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
244 bool isInteger) {
245 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
246 // Cannot fold a signed integer setcc with an unsigned integer setcc.
247 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000248
Chris Lattnerc3aae252005-01-07 07:46:32 +0000249 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000250
Chris Lattnerc3aae252005-01-07 07:46:32 +0000251 // If the N and U bits get set then the resultant comparison DOES suddenly
252 // care about orderedness, and is true when ordered.
253 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000254 Op &= ~16; // Clear the U bit if the N bit is set.
255
256 // Canonicalize illegal integer setcc's.
257 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
258 Op = ISD::SETNE;
259
Chris Lattnerc3aae252005-01-07 07:46:32 +0000260 return ISD::CondCode(Op);
261}
262
263/// getSetCCAndOperation - Return the result of a logical AND between different
264/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
265/// function returns zero if it is not possible to represent the resultant
266/// comparison.
267ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
268 bool isInteger) {
269 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
270 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000271 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272
273 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000274 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
275
276 // Canonicalize illegal integer setcc's.
277 if (isInteger) {
278 switch (Result) {
279 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000280 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
281 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
282 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
283 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000284 }
285 }
286
287 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000288}
289
Chris Lattnerb48da392005-01-23 04:39:44 +0000290const TargetMachine &SelectionDAG::getTarget() const {
291 return TLI.getTargetMachine();
292}
293
Jim Laskey58b968b2005-08-17 20:08:02 +0000294//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000295// SDNode Profile Support
296//===----------------------------------------------------------------------===//
297
Jim Laskeydef69b92006-10-27 23:52:51 +0000298/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
299///
Jim Laskey583bd472006-10-27 23:46:08 +0000300static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
301 ID.AddInteger(OpC);
302}
303
304/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
305/// solely with their pointer.
306void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
307 ID.AddPointer(VTList.VTs);
308}
309
Jim Laskeydef69b92006-10-27 23:52:51 +0000310/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
311///
Jim Laskey583bd472006-10-27 23:46:08 +0000312static void AddNodeIDOperands(FoldingSetNodeID &ID,
313 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000314 for (; NumOps; --NumOps, ++Ops) {
315 ID.AddPointer(Ops->Val);
316 ID.AddInteger(Ops->ResNo);
317 }
Jim Laskey583bd472006-10-27 23:46:08 +0000318}
319
Jim Laskey583bd472006-10-27 23:46:08 +0000320static void AddNodeIDNode(FoldingSetNodeID &ID,
321 unsigned short OpC, SDVTList VTList,
322 const SDOperand *OpList, unsigned N) {
323 AddNodeIDOpcode(ID, OpC);
324 AddNodeIDValueTypes(ID, VTList);
325 AddNodeIDOperands(ID, OpList, N);
326}
327
Jim Laskeydef69b92006-10-27 23:52:51 +0000328/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
329/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000330static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
331 AddNodeIDOpcode(ID, N->getOpcode());
332 // Add the return value info.
333 AddNodeIDValueTypes(ID, N->getVTList());
334 // Add the operand info.
335 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
336
337 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000338 switch (N->getOpcode()) {
339 default: break; // Normal nodes don't need extra info.
340 case ISD::TargetConstant:
341 case ISD::Constant:
342 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
343 break;
344 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000345 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000346 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000347 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000348 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000349 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000350 case ISD::GlobalAddress:
351 case ISD::TargetGlobalTLSAddress:
352 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000353 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
354 ID.AddPointer(GA->getGlobal());
355 ID.AddInteger(GA->getOffset());
356 break;
357 }
358 case ISD::BasicBlock:
359 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
360 break;
361 case ISD::Register:
362 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
363 break;
Evan Cheng334dc1f2008-01-31 21:00:00 +0000364 case ISD::SRCVALUE: {
365 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
366 ID.AddPointer(SV->getValue());
367 ID.AddInteger(SV->getOffset());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 break;
369 }
370 case ISD::FrameIndex:
371 case ISD::TargetFrameIndex:
372 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
373 break;
374 case ISD::JumpTable:
375 case ISD::TargetJumpTable:
376 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
377 break;
378 case ISD::ConstantPool:
379 case ISD::TargetConstantPool: {
380 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
381 ID.AddInteger(CP->getAlignment());
382 ID.AddInteger(CP->getOffset());
383 if (CP->isMachineConstantPoolEntry())
384 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
385 else
386 ID.AddPointer(CP->getConstVal());
387 break;
388 }
389 case ISD::LOAD: {
390 LoadSDNode *LD = cast<LoadSDNode>(N);
391 ID.AddInteger(LD->getAddressingMode());
392 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000393 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000394 ID.AddInteger(LD->getAlignment());
395 ID.AddInteger(LD->isVolatile());
396 break;
397 }
398 case ISD::STORE: {
399 StoreSDNode *ST = cast<StoreSDNode>(N);
400 ID.AddInteger(ST->getAddressingMode());
401 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000402 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000403 ID.AddInteger(ST->getAlignment());
404 ID.AddInteger(ST->isVolatile());
405 break;
406 }
Jim Laskey583bd472006-10-27 23:46:08 +0000407 }
408}
409
410//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000411// SelectionDAG Class
412//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000413
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000414/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000415/// SelectionDAG.
416void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000417 // Create a dummy node (which is not added to allnodes), that adds a reference
418 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000419 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000420
Chris Lattner190a4182006-08-04 17:45:20 +0000421 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000422
Chris Lattner190a4182006-08-04 17:45:20 +0000423 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000424 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000425 if (I->use_empty())
426 DeadNodes.push_back(I);
427
428 // Process the worklist, deleting the nodes and adding their uses to the
429 // worklist.
430 while (!DeadNodes.empty()) {
431 SDNode *N = DeadNodes.back();
432 DeadNodes.pop_back();
433
434 // Take the node out of the appropriate CSE map.
435 RemoveNodeFromCSEMaps(N);
436
437 // Next, brutally remove the operand list. This is safe to do, as there are
438 // no cycles in the graph.
439 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
440 SDNode *Operand = I->Val;
441 Operand->removeUser(N);
442
443 // Now that we removed this operand, see if there are no uses of it left.
444 if (Operand->use_empty())
445 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000446 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000447 if (N->OperandsNeedDelete)
448 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000449 N->OperandList = 0;
450 N->NumOperands = 0;
451
452 // Finally, remove N itself.
453 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000454 }
455
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000456 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000457 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000458}
459
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000460void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000461 SmallVector<SDNode*, 16> DeadNodes;
462 DeadNodes.push_back(N);
463
464 // Process the worklist, deleting the nodes and adding their uses to the
465 // worklist.
466 while (!DeadNodes.empty()) {
467 SDNode *N = DeadNodes.back();
468 DeadNodes.pop_back();
469
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000470 if (UpdateListener)
471 UpdateListener->NodeDeleted(N);
472
Evan Cheng130a6472006-10-12 20:34:05 +0000473 // Take the node out of the appropriate CSE map.
474 RemoveNodeFromCSEMaps(N);
475
476 // Next, brutally remove the operand list. This is safe to do, as there are
477 // no cycles in the graph.
478 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
479 SDNode *Operand = I->Val;
480 Operand->removeUser(N);
481
482 // Now that we removed this operand, see if there are no uses of it left.
483 if (Operand->use_empty())
484 DeadNodes.push_back(Operand);
485 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000486 if (N->OperandsNeedDelete)
487 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000488 N->OperandList = 0;
489 N->NumOperands = 0;
490
491 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000492 AllNodes.erase(N);
493 }
494}
495
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000496void SelectionDAG::DeleteNode(SDNode *N) {
497 assert(N->use_empty() && "Cannot delete a node that is not dead!");
498
499 // First take this out of the appropriate CSE map.
500 RemoveNodeFromCSEMaps(N);
501
Chris Lattner1e111c72005-09-07 05:37:01 +0000502 // Finally, remove uses due to operands of this node, remove from the
503 // AllNodes list, and delete the node.
504 DeleteNodeNotInCSEMaps(N);
505}
506
507void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
508
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000509 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000510 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000511
512 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000513 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
514 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000515 if (N->OperandsNeedDelete)
516 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000517 N->OperandList = 0;
518 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000519
520 delete N;
521}
522
Chris Lattner149c58c2005-08-16 18:17:10 +0000523/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
524/// correspond to it. This is useful when we're about to delete or repurpose
525/// the node. We don't want future request for structurally identical nodes
526/// to return N anymore.
527void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000528 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000529 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000530 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000531 case ISD::STRING:
532 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
533 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000534 case ISD::CONDCODE:
535 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
536 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000537 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000538 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
539 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000540 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000541 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000542 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000543 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000544 Erased =
545 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000546 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000547 case ISD::VALUETYPE: {
548 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
549 if (MVT::isExtendedVT(VT)) {
550 Erased = ExtendedValueTypeNodes.erase(VT);
551 } else {
552 Erased = ValueTypeNodes[VT] != 0;
553 ValueTypeNodes[VT] = 0;
554 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000555 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000556 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000557 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000558 // Remove it from the CSE Map.
559 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000560 break;
561 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000562#ifndef NDEBUG
563 // Verify that the node was actually in one of the CSE maps, unless it has a
564 // flag result (which cannot be CSE'd) or is one of the special cases that are
565 // not subject to CSE.
566 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000567 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000568 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000569 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000570 assert(0 && "Node is not in map!");
571 }
572#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000573}
574
Chris Lattner8b8749f2005-08-17 19:00:20 +0000575/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
576/// has been taken out and modified in some way. If the specified node already
577/// exists in the CSE maps, do not modify the maps, but return the existing node
578/// instead. If it doesn't exist, add it and return null.
579///
580SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
581 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000582 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000583 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000584
Chris Lattner9f8cc692005-12-19 22:21:21 +0000585 // Check that remaining values produced are not flags.
586 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
587 if (N->getValueType(i) == MVT::Flag)
588 return 0; // Never CSE anything that produces a flag.
589
Chris Lattnera5682852006-08-07 23:03:03 +0000590 SDNode *New = CSEMap.GetOrInsertNode(N);
591 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000592 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000593}
594
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000595/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
596/// were replaced with those specified. If this node is never memoized,
597/// return null, otherwise return a pointer to the slot it would take. If a
598/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000599SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
600 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000601 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000602 return 0; // Never add these nodes.
603
604 // Check that remaining values produced are not flags.
605 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
606 if (N->getValueType(i) == MVT::Flag)
607 return 0; // Never CSE anything that produces a flag.
608
Chris Lattner63e3f142007-02-04 07:28:00 +0000609 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000610 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000611 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000612 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000613}
614
615/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
616/// were replaced with those specified. If this node is never memoized,
617/// return null, otherwise return a pointer to the slot it would take. If a
618/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000619SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
620 SDOperand Op1, SDOperand Op2,
621 void *&InsertPos) {
622 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
623 return 0; // Never add these nodes.
624
625 // Check that remaining values produced are not flags.
626 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
627 if (N->getValueType(i) == MVT::Flag)
628 return 0; // Never CSE anything that produces a flag.
629
Chris Lattner63e3f142007-02-04 07:28:00 +0000630 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000631 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000632 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000633 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
634}
635
636
637/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
638/// were replaced with those specified. If this node is never memoized,
639/// return null, otherwise return a pointer to the slot it would take. If a
640/// node already exists with these operands, the slot will be non-null.
641SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000642 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000643 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000644 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000645 return 0; // Never add these nodes.
646
647 // Check that remaining values produced are not flags.
648 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
649 if (N->getValueType(i) == MVT::Flag)
650 return 0; // Never CSE anything that produces a flag.
651
Jim Laskey583bd472006-10-27 23:46:08 +0000652 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000653 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000654
Evan Cheng9629aba2006-10-11 01:47:58 +0000655 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
656 ID.AddInteger(LD->getAddressingMode());
657 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000658 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000659 ID.AddInteger(LD->getAlignment());
660 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000661 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
662 ID.AddInteger(ST->getAddressingMode());
663 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000664 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000665 ID.AddInteger(ST->getAlignment());
666 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000667 }
Jim Laskey583bd472006-10-27 23:46:08 +0000668
Chris Lattnera5682852006-08-07 23:03:03 +0000669 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000670}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000671
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000672
Chris Lattner78ec3112003-08-11 14:57:33 +0000673SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000674 while (!AllNodes.empty()) {
675 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000676 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000677 if (N->OperandsNeedDelete)
678 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000679 N->OperandList = 0;
680 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000681 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000682 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000683}
684
Chris Lattner0f2287b2005-04-13 02:38:18 +0000685SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000686 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000687 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000688 return getNode(ISD::AND, Op.getValueType(), Op,
689 getConstant(Imm, Op.getValueType()));
690}
691
Chris Lattner36ce6912005-11-29 06:21:05 +0000692SDOperand SelectionDAG::getString(const std::string &Val) {
693 StringSDNode *&N = StringNodes[Val];
694 if (!N) {
695 N = new StringSDNode(Val);
696 AllNodes.push_back(N);
697 }
698 return SDOperand(N, 0);
699}
700
Chris Lattner61b09412006-08-11 21:01:22 +0000701SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000702 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000703
704 MVT::ValueType EltVT =
705 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000706
Chris Lattner61b09412006-08-11 21:01:22 +0000707 // Mask out any bits that are not valid for this constant.
Dan Gohman89081322007-12-12 22:21:26 +0000708 Val &= MVT::getIntVTBitMask(EltVT);
Chris Lattner61b09412006-08-11 21:01:22 +0000709
710 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000711 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000712 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000713 ID.AddInteger(Val);
714 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000715 SDNode *N = NULL;
716 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
717 if (!MVT::isVector(VT))
718 return SDOperand(N, 0);
719 if (!N) {
720 N = new ConstantSDNode(isT, Val, EltVT);
721 CSEMap.InsertNode(N, IP);
722 AllNodes.push_back(N);
723 }
724
725 SDOperand Result(N, 0);
726 if (MVT::isVector(VT)) {
727 SmallVector<SDOperand, 8> Ops;
728 Ops.assign(MVT::getVectorNumElements(VT), Result);
729 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
730 }
731 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000732}
733
Chris Lattner0bd48932008-01-17 07:00:52 +0000734SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
735 return getConstant(Val, TLI.getPointerTy(), isTarget);
736}
737
738
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000739SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000740 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000741 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000742
Dan Gohman7f321562007-06-25 16:23:39 +0000743 MVT::ValueType EltVT =
744 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000745
Chris Lattnerd8658612005-02-17 20:17:32 +0000746 // Do the map lookup using the actual bit pattern for the floating point
747 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
748 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000749 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000750 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000751 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000752 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000753 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000754 SDNode *N = NULL;
755 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
756 if (!MVT::isVector(VT))
757 return SDOperand(N, 0);
758 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000759 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000760 CSEMap.InsertNode(N, IP);
761 AllNodes.push_back(N);
762 }
763
Dan Gohman7f321562007-06-25 16:23:39 +0000764 SDOperand Result(N, 0);
765 if (MVT::isVector(VT)) {
766 SmallVector<SDOperand, 8> Ops;
767 Ops.assign(MVT::getVectorNumElements(VT), Result);
768 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
769 }
770 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000771}
772
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000773SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
774 bool isTarget) {
775 MVT::ValueType EltVT =
776 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
777 if (EltVT==MVT::f32)
778 return getConstantFP(APFloat((float)Val), VT, isTarget);
779 else
780 return getConstantFP(APFloat(Val), VT, isTarget);
781}
782
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000784 MVT::ValueType VT, int Offset,
785 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000786 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
787 unsigned Opc;
788 if (GVar && GVar->isThreadLocal())
789 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
790 else
791 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000792 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000793 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000794 ID.AddPointer(GV);
795 ID.AddInteger(Offset);
796 void *IP = 0;
797 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
798 return SDOperand(E, 0);
799 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
800 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000801 AllNodes.push_back(N);
802 return SDOperand(N, 0);
803}
804
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000805SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
806 bool isTarget) {
807 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
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(FI);
811 void *IP = 0;
812 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
813 return SDOperand(E, 0);
814 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
815 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000816 AllNodes.push_back(N);
817 return SDOperand(N, 0);
818}
819
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000820SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
821 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000822 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000823 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000824 ID.AddInteger(JTI);
825 void *IP = 0;
826 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
827 return SDOperand(E, 0);
828 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
829 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000830 AllNodes.push_back(N);
831 return SDOperand(N, 0);
832}
833
Evan Chengb8973bd2006-01-31 22:23:14 +0000834SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000835 unsigned Alignment, int Offset,
836 bool isTarget) {
837 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000838 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000839 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000840 ID.AddInteger(Alignment);
841 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000842 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000843 void *IP = 0;
844 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
845 return SDOperand(E, 0);
846 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
847 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000848 AllNodes.push_back(N);
849 return SDOperand(N, 0);
850}
851
Chris Lattnerc3aae252005-01-07 07:46:32 +0000852
Evan Chengd6594ae2006-09-12 21:00:35 +0000853SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
854 MVT::ValueType VT,
855 unsigned Alignment, int Offset,
856 bool isTarget) {
857 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000858 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000859 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000860 ID.AddInteger(Alignment);
861 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000862 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000863 void *IP = 0;
864 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
865 return SDOperand(E, 0);
866 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
867 CSEMap.InsertNode(N, IP);
868 AllNodes.push_back(N);
869 return SDOperand(N, 0);
870}
871
872
Chris Lattnerc3aae252005-01-07 07:46:32 +0000873SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000874 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000875 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000876 ID.AddPointer(MBB);
877 void *IP = 0;
878 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
879 return SDOperand(E, 0);
880 SDNode *N = new BasicBlockSDNode(MBB);
881 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000882 AllNodes.push_back(N);
883 return SDOperand(N, 0);
884}
885
Chris Lattner15e4b012005-07-10 00:07:11 +0000886SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000887 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000888 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000889
Duncan Sandsf411b832007-10-17 13:49:58 +0000890 SDNode *&N = MVT::isExtendedVT(VT) ?
891 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
892
893 if (N) return SDOperand(N, 0);
894 N = new VTSDNode(VT);
895 AllNodes.push_back(N);
896 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000897}
898
Chris Lattnerc3aae252005-01-07 07:46:32 +0000899SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
900 SDNode *&N = ExternalSymbols[Sym];
901 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000902 N = new ExternalSymbolSDNode(false, Sym, VT);
903 AllNodes.push_back(N);
904 return SDOperand(N, 0);
905}
906
Chris Lattner809ec112006-01-28 10:09:25 +0000907SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
908 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000909 SDNode *&N = TargetExternalSymbols[Sym];
910 if (N) return SDOperand(N, 0);
911 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000912 AllNodes.push_back(N);
913 return SDOperand(N, 0);
914}
915
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000916SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
917 if ((unsigned)Cond >= CondCodeNodes.size())
918 CondCodeNodes.resize(Cond+1);
919
Chris Lattner079a27a2005-08-09 20:40:02 +0000920 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000921 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000922 AllNodes.push_back(CondCodeNodes[Cond]);
923 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000924 return SDOperand(CondCodeNodes[Cond], 0);
925}
926
Chris Lattner0fdd7682005-08-30 22:38:38 +0000927SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000928 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000929 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000930 ID.AddInteger(RegNo);
931 void *IP = 0;
932 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
933 return SDOperand(E, 0);
934 SDNode *N = new RegisterSDNode(RegNo, VT);
935 CSEMap.InsertNode(N, IP);
936 AllNodes.push_back(N);
937 return SDOperand(N, 0);
938}
939
Evan Cheng334dc1f2008-01-31 21:00:00 +0000940SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Chris Lattner61b09412006-08-11 21:01:22 +0000941 assert((!V || isa<PointerType>(V->getType())) &&
942 "SrcValue is not a pointer?");
943
Jim Laskey583bd472006-10-27 23:46:08 +0000944 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000945 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000946 ID.AddPointer(V);
Evan Cheng334dc1f2008-01-31 21:00:00 +0000947 ID.AddInteger(Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000948 void *IP = 0;
949 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
950 return SDOperand(E, 0);
Evan Cheng334dc1f2008-01-31 21:00:00 +0000951 SDNode *N = new SrcValueSDNode(V, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000952 CSEMap.InsertNode(N, IP);
953 AllNodes.push_back(N);
954 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000955}
956
Chris Lattner37ce9df2007-10-15 17:47:20 +0000957/// CreateStackTemporary - Create a stack temporary, suitable for holding the
958/// specified value type.
959SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
960 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
961 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
962 const Type *Ty = MVT::getTypeForValueType(VT);
963 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
964 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
965 return getFrameIndex(FrameIdx, TLI.getPointerTy());
966}
967
968
Chris Lattner51dabfb2006-10-14 00:41:01 +0000969SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
970 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 // These setcc operations always fold.
972 switch (Cond) {
973 default: break;
974 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000975 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000976 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000977 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000978
979 case ISD::SETOEQ:
980 case ISD::SETOGT:
981 case ISD::SETOGE:
982 case ISD::SETOLT:
983 case ISD::SETOLE:
984 case ISD::SETONE:
985 case ISD::SETO:
986 case ISD::SETUO:
987 case ISD::SETUEQ:
988 case ISD::SETUNE:
989 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
990 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000991 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000992
Chris Lattner67255a12005-04-07 18:14:58 +0000993 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
994 uint64_t C2 = N2C->getValue();
995 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
996 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000997
Chris Lattnerc3aae252005-01-07 07:46:32 +0000998 // Sign extend the operands if required
999 if (ISD::isSignedIntSetCC(Cond)) {
1000 C1 = N1C->getSignExtended();
1001 C2 = N2C->getSignExtended();
1002 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001003
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004 switch (Cond) {
1005 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001006 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1007 case ISD::SETNE: return getConstant(C1 != C2, VT);
1008 case ISD::SETULT: return getConstant(C1 < C2, VT);
1009 case ISD::SETUGT: return getConstant(C1 > C2, VT);
1010 case ISD::SETULE: return getConstant(C1 <= C2, VT);
1011 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
1012 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
1013 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
1014 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
1015 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001016 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001017 }
Chris Lattner67255a12005-04-07 18:14:58 +00001018 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001019 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1020 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001021 // No compile time operations on this type yet.
1022 if (N1C->getValueType(0) == MVT::ppcf128)
1023 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001024
1025 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001026 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001027 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001028 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1029 return getNode(ISD::UNDEF, VT);
1030 // fall through
1031 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1032 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1033 return getNode(ISD::UNDEF, VT);
1034 // fall through
1035 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001036 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001037 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1038 return getNode(ISD::UNDEF, VT);
1039 // fall through
1040 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1041 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1042 return getNode(ISD::UNDEF, VT);
1043 // fall through
1044 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1045 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1046 return getNode(ISD::UNDEF, VT);
1047 // fall through
1048 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001049 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001050 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1051 return getNode(ISD::UNDEF, VT);
1052 // fall through
1053 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001054 R==APFloat::cmpEqual, VT);
1055 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1056 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1057 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1058 R==APFloat::cmpEqual, VT);
1059 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1060 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1061 R==APFloat::cmpLessThan, VT);
1062 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1063 R==APFloat::cmpUnordered, VT);
1064 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1065 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066 }
1067 } else {
1068 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001069 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001071
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001072 // Could not fold it.
1073 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074}
1075
Dan Gohmanea859be2007-06-22 14:59:07 +00001076/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1077/// this predicate to simplify operations downstream. Mask is known to be zero
1078/// for bits that V cannot have.
1079bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1080 unsigned Depth) const {
1081 // The masks are not wide enough to represent this type! Should use APInt.
1082 if (Op.getValueType() == MVT::i128)
1083 return false;
1084
1085 uint64_t KnownZero, KnownOne;
1086 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1087 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1088 return (KnownZero & Mask) == Mask;
1089}
1090
1091/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1092/// known to be either zero or one and return them in the KnownZero/KnownOne
1093/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1094/// processing.
1095void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1096 uint64_t &KnownZero, uint64_t &KnownOne,
1097 unsigned Depth) const {
1098 KnownZero = KnownOne = 0; // Don't know anything.
1099 if (Depth == 6 || Mask == 0)
1100 return; // Limit search depth.
1101
1102 // The masks are not wide enough to represent this type! Should use APInt.
1103 if (Op.getValueType() == MVT::i128)
1104 return;
1105
1106 uint64_t KnownZero2, KnownOne2;
1107
1108 switch (Op.getOpcode()) {
1109 case ISD::Constant:
1110 // We know all of the bits for a constant!
1111 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1112 KnownZero = ~KnownOne & Mask;
1113 return;
1114 case ISD::AND:
1115 // If either the LHS or the RHS are Zero, the result is zero.
1116 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1117 Mask &= ~KnownZero;
1118 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1119 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1120 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1121
1122 // Output known-1 bits are only known if set in both the LHS & RHS.
1123 KnownOne &= KnownOne2;
1124 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1125 KnownZero |= KnownZero2;
1126 return;
1127 case ISD::OR:
1128 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1129 Mask &= ~KnownOne;
1130 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1131 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1132 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1133
1134 // Output known-0 bits are only known if clear in both the LHS & RHS.
1135 KnownZero &= KnownZero2;
1136 // Output known-1 are known to be set if set in either the LHS | RHS.
1137 KnownOne |= KnownOne2;
1138 return;
1139 case ISD::XOR: {
1140 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1141 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1142 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1143 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1144
1145 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1146 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1147 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1148 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1149 KnownZero = KnownZeroOut;
1150 return;
1151 }
1152 case ISD::SELECT:
1153 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1154 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1155 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1156 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1157
1158 // Only known if known in both the LHS and RHS.
1159 KnownOne &= KnownOne2;
1160 KnownZero &= KnownZero2;
1161 return;
1162 case ISD::SELECT_CC:
1163 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1164 ComputeMaskedBits(Op.getOperand(2), 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::SETCC:
1173 // If we know the result of a setcc has the top bits zero, use this info.
1174 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1175 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1176 return;
1177 case ISD::SHL:
1178 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1179 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1180 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1181 KnownZero, KnownOne, Depth+1);
1182 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1183 KnownZero <<= SA->getValue();
1184 KnownOne <<= SA->getValue();
1185 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1186 }
1187 return;
1188 case ISD::SRL:
1189 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1190 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1191 MVT::ValueType VT = Op.getValueType();
1192 unsigned ShAmt = SA->getValue();
1193
1194 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1195 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1196 KnownZero, KnownOne, Depth+1);
1197 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1198 KnownZero &= TypeMask;
1199 KnownOne &= TypeMask;
1200 KnownZero >>= ShAmt;
1201 KnownOne >>= ShAmt;
1202
1203 uint64_t HighBits = (1ULL << ShAmt)-1;
1204 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1205 KnownZero |= HighBits; // High bits known zero.
1206 }
1207 return;
1208 case ISD::SRA:
1209 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1210 MVT::ValueType VT = Op.getValueType();
1211 unsigned ShAmt = SA->getValue();
1212
1213 // Compute the new bits that are at the top now.
1214 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1215
1216 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1217 // If any of the demanded bits are produced by the sign extension, we also
1218 // demand the input sign bit.
1219 uint64_t HighBits = (1ULL << ShAmt)-1;
1220 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1221 if (HighBits & Mask)
1222 InDemandedMask |= MVT::getIntVTSignBit(VT);
1223
1224 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1225 Depth+1);
1226 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1227 KnownZero &= TypeMask;
1228 KnownOne &= TypeMask;
1229 KnownZero >>= ShAmt;
1230 KnownOne >>= ShAmt;
1231
1232 // Handle the sign bits.
1233 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1234 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1235
1236 if (KnownZero & SignBit) {
1237 KnownZero |= HighBits; // New bits are known zero.
1238 } else if (KnownOne & SignBit) {
1239 KnownOne |= HighBits; // New bits are known one.
1240 }
1241 }
1242 return;
1243 case ISD::SIGN_EXTEND_INREG: {
1244 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1245
1246 // Sign extension. Compute the demanded bits in the result that are not
1247 // present in the input.
1248 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1249
1250 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1251 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1252
1253 // If the sign extended bits are demanded, we know that the sign
1254 // bit is demanded.
1255 if (NewBits)
1256 InputDemandedBits |= InSignBit;
1257
1258 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1259 KnownZero, KnownOne, Depth+1);
1260 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1261
1262 // If the sign bit of the input is known set or clear, then we know the
1263 // top bits of the result.
1264 if (KnownZero & InSignBit) { // Input sign bit known clear
1265 KnownZero |= NewBits;
1266 KnownOne &= ~NewBits;
1267 } else if (KnownOne & InSignBit) { // Input sign bit known set
1268 KnownOne |= NewBits;
1269 KnownZero &= ~NewBits;
1270 } else { // Input sign bit unknown
1271 KnownZero &= ~NewBits;
1272 KnownOne &= ~NewBits;
1273 }
1274 return;
1275 }
1276 case ISD::CTTZ:
1277 case ISD::CTLZ:
1278 case ISD::CTPOP: {
1279 MVT::ValueType VT = Op.getValueType();
1280 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1281 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1282 KnownOne = 0;
1283 return;
1284 }
1285 case ISD::LOAD: {
1286 if (ISD::isZEXTLoad(Op.Val)) {
1287 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001288 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmanea859be2007-06-22 14:59:07 +00001289 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1290 }
1291 return;
1292 }
1293 case ISD::ZERO_EXTEND: {
1294 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1295 uint64_t NewBits = (~InMask) & Mask;
1296 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1297 KnownOne, Depth+1);
1298 KnownZero |= NewBits & Mask;
1299 KnownOne &= ~NewBits;
1300 return;
1301 }
1302 case ISD::SIGN_EXTEND: {
1303 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1304 unsigned InBits = MVT::getSizeInBits(InVT);
1305 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1306 uint64_t InSignBit = 1ULL << (InBits-1);
1307 uint64_t NewBits = (~InMask) & Mask;
1308 uint64_t InDemandedBits = Mask & InMask;
1309
1310 // If any of the sign extended bits are demanded, we know that the sign
1311 // bit is demanded.
1312 if (NewBits & Mask)
1313 InDemandedBits |= InSignBit;
1314
1315 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1316 KnownOne, Depth+1);
1317 // If the sign bit is known zero or one, the top bits match.
1318 if (KnownZero & InSignBit) {
1319 KnownZero |= NewBits;
1320 KnownOne &= ~NewBits;
1321 } else if (KnownOne & InSignBit) {
1322 KnownOne |= NewBits;
1323 KnownZero &= ~NewBits;
1324 } else { // Otherwise, top bits aren't known.
1325 KnownOne &= ~NewBits;
1326 KnownZero &= ~NewBits;
1327 }
1328 return;
1329 }
1330 case ISD::ANY_EXTEND: {
1331 MVT::ValueType VT = Op.getOperand(0).getValueType();
1332 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1333 KnownZero, KnownOne, Depth+1);
1334 return;
1335 }
1336 case ISD::TRUNCATE: {
1337 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1338 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1339 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1340 KnownZero &= OutMask;
1341 KnownOne &= OutMask;
1342 break;
1343 }
1344 case ISD::AssertZext: {
1345 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1346 uint64_t InMask = MVT::getIntVTBitMask(VT);
1347 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1348 KnownOne, Depth+1);
1349 KnownZero |= (~InMask) & Mask;
1350 return;
1351 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001352 case ISD::FGETSIGN:
1353 // All bits are zero except the low bit.
1354 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1355 return;
1356
Dan Gohmanea859be2007-06-22 14:59:07 +00001357 case ISD::ADD: {
1358 // If either the LHS or the RHS are Zero, the result is zero.
1359 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1360 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1361 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1362 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1363
1364 // Output known-0 bits are known if clear or set in both the low clear bits
1365 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1366 // low 3 bits clear.
1367 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1368 CountTrailingZeros_64(~KnownZero2));
1369
1370 KnownZero = (1ULL << KnownZeroOut) - 1;
1371 KnownOne = 0;
1372 return;
1373 }
1374 case ISD::SUB: {
1375 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1376 if (!CLHS) return;
1377
1378 // We know that the top bits of C-X are clear if X contains less bits
1379 // than C (i.e. no wrap-around can happen). For example, 20-X is
1380 // positive if we can prove that X is >= 0 and < 16.
1381 MVT::ValueType VT = CLHS->getValueType(0);
1382 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1383 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1384 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1385 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1386 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1387
1388 // If all of the MaskV bits are known to be zero, then we know the output
1389 // top bits are zero, because we now know that the output is from [0-C].
1390 if ((KnownZero & MaskV) == MaskV) {
1391 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1392 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1393 KnownOne = 0; // No one bits known.
1394 } else {
1395 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1396 }
1397 }
1398 return;
1399 }
1400 default:
1401 // Allow the target to implement this method for its nodes.
1402 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1403 case ISD::INTRINSIC_WO_CHAIN:
1404 case ISD::INTRINSIC_W_CHAIN:
1405 case ISD::INTRINSIC_VOID:
1406 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1407 }
1408 return;
1409 }
1410}
1411
1412/// ComputeNumSignBits - Return the number of times the sign bit of the
1413/// register is replicated into the other bits. We know that at least 1 bit
1414/// is always equal to the sign bit (itself), but other cases can give us
1415/// information. For example, immediately after an "SRA X, 2", we know that
1416/// the top 3 bits are all equal to each other, so we return 3.
1417unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1418 MVT::ValueType VT = Op.getValueType();
1419 assert(MVT::isInteger(VT) && "Invalid VT!");
1420 unsigned VTBits = MVT::getSizeInBits(VT);
1421 unsigned Tmp, Tmp2;
1422
1423 if (Depth == 6)
1424 return 1; // Limit search depth.
1425
1426 switch (Op.getOpcode()) {
1427 default: break;
1428 case ISD::AssertSext:
1429 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1430 return VTBits-Tmp+1;
1431 case ISD::AssertZext:
1432 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1433 return VTBits-Tmp;
1434
1435 case ISD::Constant: {
1436 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1437 // If negative, invert the bits, then look at it.
1438 if (Val & MVT::getIntVTSignBit(VT))
1439 Val = ~Val;
1440
1441 // Shift the bits so they are the leading bits in the int64_t.
1442 Val <<= 64-VTBits;
1443
1444 // Return # leading zeros. We use 'min' here in case Val was zero before
1445 // shifting. We don't want to return '64' as for an i32 "0".
1446 return std::min(VTBits, CountLeadingZeros_64(Val));
1447 }
1448
1449 case ISD::SIGN_EXTEND:
1450 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1451 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1452
1453 case ISD::SIGN_EXTEND_INREG:
1454 // Max of the input and what this extends.
1455 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1456 Tmp = VTBits-Tmp+1;
1457
1458 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1459 return std::max(Tmp, Tmp2);
1460
1461 case ISD::SRA:
1462 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1463 // SRA X, C -> adds C sign bits.
1464 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1465 Tmp += C->getValue();
1466 if (Tmp > VTBits) Tmp = VTBits;
1467 }
1468 return Tmp;
1469 case ISD::SHL:
1470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1471 // shl destroys sign bits.
1472 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1473 if (C->getValue() >= VTBits || // Bad shift.
1474 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1475 return Tmp - C->getValue();
1476 }
1477 break;
1478 case ISD::AND:
1479 case ISD::OR:
1480 case ISD::XOR: // NOT is handled here.
1481 // Logical binary ops preserve the number of sign bits.
1482 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1483 if (Tmp == 1) return 1; // Early out.
1484 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1485 return std::min(Tmp, Tmp2);
1486
1487 case ISD::SELECT:
1488 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1489 if (Tmp == 1) return 1; // Early out.
1490 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1491 return std::min(Tmp, Tmp2);
1492
1493 case ISD::SETCC:
1494 // If setcc returns 0/-1, all bits are sign bits.
1495 if (TLI.getSetCCResultContents() ==
1496 TargetLowering::ZeroOrNegativeOneSetCCResult)
1497 return VTBits;
1498 break;
1499 case ISD::ROTL:
1500 case ISD::ROTR:
1501 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1502 unsigned RotAmt = C->getValue() & (VTBits-1);
1503
1504 // Handle rotate right by N like a rotate left by 32-N.
1505 if (Op.getOpcode() == ISD::ROTR)
1506 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1507
1508 // If we aren't rotating out all of the known-in sign bits, return the
1509 // number that are left. This handles rotl(sext(x), 1) for example.
1510 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1511 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1512 }
1513 break;
1514 case ISD::ADD:
1515 // Add can have at most one carry bit. Thus we know that the output
1516 // is, at worst, one more bit than the inputs.
1517 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1518 if (Tmp == 1) return 1; // Early out.
1519
1520 // Special case decrementing a value (ADD X, -1):
1521 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1522 if (CRHS->isAllOnesValue()) {
1523 uint64_t KnownZero, KnownOne;
1524 uint64_t Mask = MVT::getIntVTBitMask(VT);
1525 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1526
1527 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1528 // sign bits set.
1529 if ((KnownZero|1) == Mask)
1530 return VTBits;
1531
1532 // If we are subtracting one from a positive number, there is no carry
1533 // out of the result.
1534 if (KnownZero & MVT::getIntVTSignBit(VT))
1535 return Tmp;
1536 }
1537
1538 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1539 if (Tmp2 == 1) return 1;
1540 return std::min(Tmp, Tmp2)-1;
1541 break;
1542
1543 case ISD::SUB:
1544 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1545 if (Tmp2 == 1) return 1;
1546
1547 // Handle NEG.
1548 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1549 if (CLHS->getValue() == 0) {
1550 uint64_t KnownZero, KnownOne;
1551 uint64_t Mask = MVT::getIntVTBitMask(VT);
1552 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1553 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1554 // sign bits set.
1555 if ((KnownZero|1) == Mask)
1556 return VTBits;
1557
1558 // If the input is known to be positive (the sign bit is known clear),
1559 // the output of the NEG has the same number of sign bits as the input.
1560 if (KnownZero & MVT::getIntVTSignBit(VT))
1561 return Tmp2;
1562
1563 // Otherwise, we treat this like a SUB.
1564 }
1565
1566 // Sub can have at most one carry bit. Thus we know that the output
1567 // is, at worst, one more bit than the inputs.
1568 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1569 if (Tmp == 1) return 1; // Early out.
1570 return std::min(Tmp, Tmp2)-1;
1571 break;
1572 case ISD::TRUNCATE:
1573 // FIXME: it's tricky to do anything useful for this, but it is an important
1574 // case for targets like X86.
1575 break;
1576 }
1577
1578 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1579 if (Op.getOpcode() == ISD::LOAD) {
1580 LoadSDNode *LD = cast<LoadSDNode>(Op);
1581 unsigned ExtType = LD->getExtensionType();
1582 switch (ExtType) {
1583 default: break;
1584 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001585 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001586 return VTBits-Tmp+1;
1587 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001588 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001589 return VTBits-Tmp;
1590 }
1591 }
1592
1593 // Allow the target to implement this method for its nodes.
1594 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1595 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1596 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1597 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1598 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1599 if (NumBits > 1) return NumBits;
1600 }
1601
1602 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1603 // use this information.
1604 uint64_t KnownZero, KnownOne;
1605 uint64_t Mask = MVT::getIntVTBitMask(VT);
1606 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1607
1608 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1609 if (KnownZero & SignBit) { // SignBit is 0
1610 Mask = KnownZero;
1611 } else if (KnownOne & SignBit) { // SignBit is 1;
1612 Mask = KnownOne;
1613 } else {
1614 // Nothing known.
1615 return 1;
1616 }
1617
1618 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1619 // the number of identical bits in the top of the input value.
1620 Mask ^= ~0ULL;
1621 Mask <<= 64-VTBits;
1622 // Return # leading zeros. We use 'min' here in case Val was zero before
1623 // shifting. We don't want to return '64' as for an i32 "0".
1624 return std::min(VTBits, CountLeadingZeros_64(Mask));
1625}
1626
Chris Lattner51dabfb2006-10-14 00:41:01 +00001627
Evan Chenga844bde2008-02-02 04:07:54 +00001628bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1629 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1630 if (!GA) return false;
1631 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1632 if (!GV) return false;
1633 MachineModuleInfo *MMI = getMachineModuleInfo();
1634 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1635}
1636
1637
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
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003177
Evan Cheng99157a02006-08-07 22:13:29 +00003178/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003179/// This can cause recursive merging of nodes in the DAG.
3180///
Chris Lattner11d049c2008-02-03 03:35:22 +00003181/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003182///
Chris Lattner11d049c2008-02-03 03:35:22 +00003183void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003184 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003185 SDNode *From = FromN.Val;
3186 // FIXME: This works around a dag isel emitter bug.
3187 if (From->getNumValues() == 1 && FromN.ResNo != 0)
3188 return; // FIXME: THIS IS BOGUS
3189
3190 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003191 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003192 assert(From != To.Val && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003193
Chris Lattner8b8749f2005-08-17 19:00:20 +00003194 while (!From->use_empty()) {
3195 // Process users until they are all gone.
3196 SDNode *U = *From->use_begin();
3197
3198 // This node is about to morph, remove its old self from the CSE maps.
3199 RemoveNodeFromCSEMaps(U);
3200
Chris Lattner65113b22005-11-08 22:07:03 +00003201 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3202 I != E; ++I)
3203 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003204 From->removeUser(U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003205 *I = To;
3206 To.Val->addUser(U);
Chris Lattner8b52f212005-08-26 18:36:28 +00003207 }
3208
3209 // Now that we have modified U, add it back to the CSE maps. If it already
3210 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003211 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003212 ReplaceAllUsesWith(U, Existing, UpdateListener);
3213 // U is now dead. Inform the listener if it exists and delete it.
3214 if (UpdateListener)
3215 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003216 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003217 } else {
3218 // If the node doesn't already exist, we updated it. Inform a listener if
3219 // it exists.
3220 if (UpdateListener)
3221 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003222 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003223 }
3224}
3225
3226/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3227/// This can cause recursive merging of nodes in the DAG.
3228///
3229/// This version assumes From/To have matching types and numbers of result
3230/// values.
3231///
Chris Lattner1e111c72005-09-07 05:37:01 +00003232void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003233 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003234 assert(From != To && "Cannot replace uses of with self");
3235 assert(From->getNumValues() == To->getNumValues() &&
3236 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003237 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003238 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3239 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003240
3241 while (!From->use_empty()) {
3242 // Process users until they are all gone.
3243 SDNode *U = *From->use_begin();
3244
3245 // This node is about to morph, remove its old self from the CSE maps.
3246 RemoveNodeFromCSEMaps(U);
3247
Chris Lattner65113b22005-11-08 22:07:03 +00003248 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3249 I != E; ++I)
3250 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003251 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003252 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003253 To->addUser(U);
3254 }
3255
3256 // Now that we have modified U, add it back to the CSE maps. If it already
3257 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003258 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003259 ReplaceAllUsesWith(U, Existing, UpdateListener);
3260 // U is now dead. Inform the listener if it exists and delete it.
3261 if (UpdateListener)
3262 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003263 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003264 } else {
3265 // If the node doesn't already exist, we updated it. Inform a listener if
3266 // it exists.
3267 if (UpdateListener)
3268 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003269 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003270 }
3271}
3272
Chris Lattner8b52f212005-08-26 18:36:28 +00003273/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3274/// This can cause recursive merging of nodes in the DAG.
3275///
3276/// This version can replace From with any result values. To must match the
3277/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003278void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003279 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003280 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003281 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003282 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003283
3284 while (!From->use_empty()) {
3285 // Process users until they are all gone.
3286 SDNode *U = *From->use_begin();
3287
3288 // This node is about to morph, remove its old self from the CSE maps.
3289 RemoveNodeFromCSEMaps(U);
3290
Chris Lattner65113b22005-11-08 22:07:03 +00003291 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3292 I != E; ++I)
3293 if (I->Val == From) {
3294 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003295 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003296 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003297 ToOp.Val->addUser(U);
3298 }
3299
3300 // Now that we have modified U, add it back to the CSE maps. If it already
3301 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003302 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003303 ReplaceAllUsesWith(U, Existing, UpdateListener);
3304 // U is now dead. Inform the listener if it exists and delete it.
3305 if (UpdateListener)
3306 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003307 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003308 } else {
3309 // If the node doesn't already exist, we updated it. Inform a listener if
3310 // it exists.
3311 if (UpdateListener)
3312 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003313 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003314 }
3315}
3316
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003317namespace {
3318 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3319 /// any deleted nodes from the set passed into its constructor and recursively
3320 /// notifies another update listener if specified.
3321 class ChainedSetUpdaterListener :
3322 public SelectionDAG::DAGUpdateListener {
3323 SmallSetVector<SDNode*, 16> &Set;
3324 SelectionDAG::DAGUpdateListener *Chain;
3325 public:
3326 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3327 SelectionDAG::DAGUpdateListener *chain)
3328 : Set(set), Chain(chain) {}
3329
3330 virtual void NodeDeleted(SDNode *N) {
3331 Set.remove(N);
3332 if (Chain) Chain->NodeDeleted(N);
3333 }
3334 virtual void NodeUpdated(SDNode *N) {
3335 if (Chain) Chain->NodeUpdated(N);
3336 }
3337 };
3338}
3339
Chris Lattner012f2412006-02-17 21:58:01 +00003340/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3341/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003342/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00003343void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003344 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00003345 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003346
Chris Lattner012f2412006-02-17 21:58:01 +00003347 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003348 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003349 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00003350 return;
3351 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003352
3353 if (From.use_empty()) return;
3354
Chris Lattnercf5640b2007-02-04 00:14:31 +00003355 // Get all of the users of From.Val. We want these in a nice,
3356 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3357 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003358
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003359 // When one of the recursive merges deletes nodes from the graph, we need to
3360 // make sure that UpdateListener is notified *and* that the node is removed
3361 // from Users if present. CSUL does this.
3362 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00003363
Chris Lattner012f2412006-02-17 21:58:01 +00003364 while (!Users.empty()) {
3365 // We know that this user uses some value of From. If it is the right
3366 // value, update it.
3367 SDNode *User = Users.back();
3368 Users.pop_back();
3369
Chris Lattner01d029b2007-10-15 06:10:22 +00003370 // Scan for an operand that matches From.
3371 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3372 for (; Op != E; ++Op)
3373 if (*Op == From) break;
3374
3375 // If there are no matches, the user must use some other result of From.
3376 if (Op == E) continue;
3377
3378 // Okay, we know this user needs to be updated. Remove its old self
3379 // from the CSE maps.
3380 RemoveNodeFromCSEMaps(User);
3381
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003382 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00003383 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003384 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003385 From.Val->removeUser(User);
3386 *Op = To;
3387 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003388 }
3389 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003390
3391 // Now that we have modified User, add it back to the CSE maps. If it
3392 // already exists there, recursively merge the results together.
3393 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003394 if (!Existing) {
3395 if (UpdateListener) UpdateListener->NodeUpdated(User);
3396 continue; // Continue on to next user.
3397 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003398
3399 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003400 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00003401 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003402 // can cause deletion of nodes that used the old value. To handle this, we
3403 // use CSUL to remove them from the Users set.
3404 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00003405
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003406 // User is now dead. Notify a listener if present.
3407 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00003408 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003409 }
3410}
3411
Chris Lattner7b2880c2005-08-24 22:44:39 +00003412
Evan Chenge6f35d82006-08-01 08:20:41 +00003413/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3414/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003415unsigned SelectionDAG::AssignNodeIds() {
3416 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003417 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3418 SDNode *N = I;
3419 N->setNodeId(Id++);
3420 }
3421 return Id;
3422}
3423
Evan Chenge6f35d82006-08-01 08:20:41 +00003424/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003425/// based on their topological order. It returns the maximum id and a vector
3426/// of the SDNodes* in assigned order by reference.
3427unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003428 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003429 std::vector<unsigned> InDegree(DAGSize);
3430 std::vector<SDNode*> Sources;
3431
3432 // Use a two pass approach to avoid using a std::map which is slow.
3433 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003434 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3435 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003436 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003437 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003438 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003439 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003440 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003441 }
3442
Evan Cheng99157a02006-08-07 22:13:29 +00003443 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003444 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003445 SDNode *N = Sources.back();
3446 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003447 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003448 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3449 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003450 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003451 if (Degree == 0)
3452 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003453 }
3454 }
3455
Evan Chengc384d6c2006-08-02 22:00:34 +00003456 // Second pass, assign the actual topological order as node ids.
3457 Id = 0;
3458 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3459 TI != TE; ++TI)
3460 (*TI)->setNodeId(Id++);
3461
3462 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003463}
3464
3465
Evan Cheng091cba12006-07-27 06:39:06 +00003466
Jim Laskey58b968b2005-08-17 20:08:02 +00003467//===----------------------------------------------------------------------===//
3468// SDNode Class
3469//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003470
Chris Lattner917d2c92006-07-19 00:00:37 +00003471// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003472void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003473void UnarySDNode::ANCHOR() {}
3474void BinarySDNode::ANCHOR() {}
3475void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003476void HandleSDNode::ANCHOR() {}
3477void StringSDNode::ANCHOR() {}
3478void ConstantSDNode::ANCHOR() {}
3479void ConstantFPSDNode::ANCHOR() {}
3480void GlobalAddressSDNode::ANCHOR() {}
3481void FrameIndexSDNode::ANCHOR() {}
3482void JumpTableSDNode::ANCHOR() {}
3483void ConstantPoolSDNode::ANCHOR() {}
3484void BasicBlockSDNode::ANCHOR() {}
3485void SrcValueSDNode::ANCHOR() {}
3486void RegisterSDNode::ANCHOR() {}
3487void ExternalSymbolSDNode::ANCHOR() {}
3488void CondCodeSDNode::ANCHOR() {}
3489void VTSDNode::ANCHOR() {}
3490void LoadSDNode::ANCHOR() {}
3491void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003492
Chris Lattner48b85922007-02-04 02:41:42 +00003493HandleSDNode::~HandleSDNode() {
3494 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003495 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003496}
3497
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003498GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3499 MVT::ValueType VT, int o)
3500 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003501 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003502 // Thread Local
3503 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3504 // Non Thread Local
3505 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3506 getSDVTList(VT)), Offset(o) {
3507 TheGlobal = const_cast<GlobalValue*>(GA);
3508}
Chris Lattner48b85922007-02-04 02:41:42 +00003509
Jim Laskey583bd472006-10-27 23:46:08 +00003510/// Profile - Gather unique data for the node.
3511///
3512void SDNode::Profile(FoldingSetNodeID &ID) {
3513 AddNodeIDNode(ID, this);
3514}
3515
Chris Lattnera3255112005-11-08 23:30:28 +00003516/// getValueTypeList - Return a pointer to the specified value type.
3517///
3518MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003519 if (MVT::isExtendedVT(VT)) {
3520 static std::set<MVT::ValueType> EVTs;
3521 return (MVT::ValueType *)&(*EVTs.insert(VT).first);
3522 } else {
3523 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3524 VTs[VT] = VT;
3525 return &VTs[VT];
3526 }
Chris Lattnera3255112005-11-08 23:30:28 +00003527}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003528
Chris Lattner5c884562005-01-12 18:37:47 +00003529/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3530/// indicated value. This method ignores uses of other values defined by this
3531/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003532bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003533 assert(Value < getNumValues() && "Bad value!");
3534
3535 // If there is only one value, this is easy.
3536 if (getNumValues() == 1)
3537 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003538 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003539
Evan Cheng4ee62112006-02-05 06:29:23 +00003540 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003541
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003542 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003543
Chris Lattnerf83482d2006-08-16 20:59:32 +00003544 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003545 SDNode *User = *UI;
3546 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003547 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003548 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3549 if (User->getOperand(i) == TheValue) {
3550 if (NUses == 0)
3551 return false; // too many uses
3552 --NUses;
3553 }
3554 }
3555
3556 // Found exactly the right number of uses?
3557 return NUses == 0;
3558}
3559
3560
Evan Cheng33d55952007-08-02 05:29:38 +00003561/// hasAnyUseOfValue - Return true if there are any use of the indicated
3562/// value. This method ignores uses of other values defined by this operation.
3563bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3564 assert(Value < getNumValues() && "Bad value!");
3565
Dan Gohman30359592008-01-29 13:02:09 +00003566 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003567
3568 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3569
3570 SmallPtrSet<SDNode*, 32> UsersHandled;
3571
3572 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3573 SDNode *User = *UI;
3574 if (User->getNumOperands() == 1 ||
3575 UsersHandled.insert(User)) // First time we've seen this?
3576 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3577 if (User->getOperand(i) == TheValue) {
3578 return true;
3579 }
3580 }
3581
3582 return false;
3583}
3584
3585
Evan Chenge6e97e62006-11-03 07:31:32 +00003586/// isOnlyUse - Return true if this node is the only use of N.
3587///
Evan Cheng4ee62112006-02-05 06:29:23 +00003588bool SDNode::isOnlyUse(SDNode *N) const {
3589 bool Seen = false;
3590 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3591 SDNode *User = *I;
3592 if (User == this)
3593 Seen = true;
3594 else
3595 return false;
3596 }
3597
3598 return Seen;
3599}
3600
Evan Chenge6e97e62006-11-03 07:31:32 +00003601/// isOperand - Return true if this node is an operand of N.
3602///
Evan Chengbfa284f2006-03-03 06:42:32 +00003603bool SDOperand::isOperand(SDNode *N) const {
3604 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3605 if (*this == N->getOperand(i))
3606 return true;
3607 return false;
3608}
3609
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003610bool SDNode::isOperand(SDNode *N) const {
3611 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3612 if (this == N->OperandList[i].Val)
3613 return true;
3614 return false;
3615}
Evan Cheng4ee62112006-02-05 06:29:23 +00003616
Chris Lattner572dee72008-01-16 05:49:24 +00003617/// reachesChainWithoutSideEffects - Return true if this operand (which must
3618/// be a chain) reaches the specified operand without crossing any
3619/// side-effecting instructions. In practice, this looks through token
3620/// factors and non-volatile loads. In order to remain efficient, this only
3621/// looks a couple of nodes in, it does not do an exhaustive search.
3622bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3623 unsigned Depth) const {
3624 if (*this == Dest) return true;
3625
3626 // Don't search too deeply, we just want to be able to see through
3627 // TokenFactor's etc.
3628 if (Depth == 0) return false;
3629
3630 // If this is a token factor, all inputs to the TF happen in parallel. If any
3631 // of the operands of the TF reach dest, then we can do the xform.
3632 if (getOpcode() == ISD::TokenFactor) {
3633 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3634 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3635 return true;
3636 return false;
3637 }
3638
3639 // Loads don't have side effects, look through them.
3640 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3641 if (!Ld->isVolatile())
3642 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3643 }
3644 return false;
3645}
3646
3647
Evan Chengc5fc57d2006-11-03 03:05:24 +00003648static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003649 SmallPtrSet<SDNode *, 32> &Visited) {
3650 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003651 return;
3652
3653 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3654 SDNode *Op = N->getOperand(i).Val;
3655 if (Op == P) {
3656 found = true;
3657 return;
3658 }
3659 findPredecessor(Op, P, found, Visited);
3660 }
3661}
3662
Evan Chenge6e97e62006-11-03 07:31:32 +00003663/// isPredecessor - Return true if this node is a predecessor of N. This node
3664/// is either an operand of N or it can be reached by recursively traversing
3665/// up the operands.
3666/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003667bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003668 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003669 bool found = false;
3670 findPredecessor(N, this, found, Visited);
3671 return found;
3672}
3673
Evan Chengc5484282006-10-04 00:56:09 +00003674uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3675 assert(Num < NumOperands && "Invalid child # of SDNode!");
3676 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3677}
3678
Reid Spencer577cc322007-04-01 07:32:19 +00003679std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003680 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003681 default:
3682 if (getOpcode() < ISD::BUILTIN_OP_END)
3683 return "<<Unknown DAG Node>>";
3684 else {
Evan Cheng72261582005-12-20 06:22:03 +00003685 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003686 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003687 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003688 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003689
Evan Cheng72261582005-12-20 06:22:03 +00003690 TargetLowering &TLI = G->getTargetLoweringInfo();
3691 const char *Name =
3692 TLI.getTargetNodeName(getOpcode());
3693 if (Name) return Name;
3694 }
3695
3696 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003697 }
3698
Andrew Lenharth95762122005-03-31 21:24:06 +00003699 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003700 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003701 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003702 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003703 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003704 case ISD::AssertSext: return "AssertSext";
3705 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003706
3707 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003708 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003709 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003710 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003711
3712 case ISD::Constant: return "Constant";
3713 case ISD::ConstantFP: return "ConstantFP";
3714 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003715 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003716 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003717 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003718 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003719 case ISD::RETURNADDR: return "RETURNADDR";
3720 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003721 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003722 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3723 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003724 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003725 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003726 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003727 case ISD::INTRINSIC_WO_CHAIN: {
3728 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3729 return Intrinsic::getName((Intrinsic::ID)IID);
3730 }
3731 case ISD::INTRINSIC_VOID:
3732 case ISD::INTRINSIC_W_CHAIN: {
3733 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003734 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003735 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003736
Chris Lattnerb2827b02006-03-19 00:52:58 +00003737 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003738 case ISD::TargetConstant: return "TargetConstant";
3739 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003740 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003741 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003742 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003743 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003744 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003745 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003746
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003747 case ISD::CopyToReg: return "CopyToReg";
3748 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003749 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003750 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003751 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003752 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00003753 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003754 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003755 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003756 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003757
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003758 // Unary operators
3759 case ISD::FABS: return "fabs";
3760 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003761 case ISD::FSQRT: return "fsqrt";
3762 case ISD::FSIN: return "fsin";
3763 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003764 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003765 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003766
3767 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003768 case ISD::ADD: return "add";
3769 case ISD::SUB: return "sub";
3770 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003771 case ISD::MULHU: return "mulhu";
3772 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003773 case ISD::SDIV: return "sdiv";
3774 case ISD::UDIV: return "udiv";
3775 case ISD::SREM: return "srem";
3776 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003777 case ISD::SMUL_LOHI: return "smul_lohi";
3778 case ISD::UMUL_LOHI: return "umul_lohi";
3779 case ISD::SDIVREM: return "sdivrem";
3780 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003781 case ISD::AND: return "and";
3782 case ISD::OR: return "or";
3783 case ISD::XOR: return "xor";
3784 case ISD::SHL: return "shl";
3785 case ISD::SRA: return "sra";
3786 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003787 case ISD::ROTL: return "rotl";
3788 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003789 case ISD::FADD: return "fadd";
3790 case ISD::FSUB: return "fsub";
3791 case ISD::FMUL: return "fmul";
3792 case ISD::FDIV: return "fdiv";
3793 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003794 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003795 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003796
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003797 case ISD::SETCC: return "setcc";
3798 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003799 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003800 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003801 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003802 case ISD::CONCAT_VECTORS: return "concat_vectors";
3803 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003804 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003805 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003806 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003807 case ISD::ADDC: return "addc";
3808 case ISD::ADDE: return "adde";
3809 case ISD::SUBC: return "subc";
3810 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003811 case ISD::SHL_PARTS: return "shl_parts";
3812 case ISD::SRA_PARTS: return "sra_parts";
3813 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003814
3815 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3816 case ISD::INSERT_SUBREG: return "insert_subreg";
3817
Chris Lattner7f644642005-04-28 21:44:03 +00003818 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003819 case ISD::SIGN_EXTEND: return "sign_extend";
3820 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003821 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003822 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003823 case ISD::TRUNCATE: return "truncate";
3824 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003825 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003826 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003827 case ISD::FP_EXTEND: return "fp_extend";
3828
3829 case ISD::SINT_TO_FP: return "sint_to_fp";
3830 case ISD::UINT_TO_FP: return "uint_to_fp";
3831 case ISD::FP_TO_SINT: return "fp_to_sint";
3832 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003833 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003834
3835 // Control flow instructions
3836 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003837 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003838 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003839 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003840 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003841 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003842 case ISD::CALLSEQ_START: return "callseq_start";
3843 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003844
3845 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003846 case ISD::LOAD: return "load";
3847 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003848 case ISD::VAARG: return "vaarg";
3849 case ISD::VACOPY: return "vacopy";
3850 case ISD::VAEND: return "vaend";
3851 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003852 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003853 case ISD::EXTRACT_ELEMENT: return "extract_element";
3854 case ISD::BUILD_PAIR: return "build_pair";
3855 case ISD::STACKSAVE: return "stacksave";
3856 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003857 case ISD::TRAP: return "trap";
3858
Chris Lattner5a67afc2006-01-13 02:39:42 +00003859 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003860 case ISD::MEMSET: return "memset";
3861 case ISD::MEMCPY: return "memcpy";
3862 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003863
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003864 // Bit manipulation
3865 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003866 case ISD::CTPOP: return "ctpop";
3867 case ISD::CTTZ: return "cttz";
3868 case ISD::CTLZ: return "ctlz";
3869
Chris Lattner36ce6912005-11-29 06:21:05 +00003870 // Debug info
3871 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003872 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003873
Duncan Sands36397f52007-07-27 12:58:54 +00003874 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003875 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003876
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003877 case ISD::CONDCODE:
3878 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003879 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003880 case ISD::SETOEQ: return "setoeq";
3881 case ISD::SETOGT: return "setogt";
3882 case ISD::SETOGE: return "setoge";
3883 case ISD::SETOLT: return "setolt";
3884 case ISD::SETOLE: return "setole";
3885 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003886
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003887 case ISD::SETO: return "seto";
3888 case ISD::SETUO: return "setuo";
3889 case ISD::SETUEQ: return "setue";
3890 case ISD::SETUGT: return "setugt";
3891 case ISD::SETUGE: return "setuge";
3892 case ISD::SETULT: return "setult";
3893 case ISD::SETULE: return "setule";
3894 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003895
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003896 case ISD::SETEQ: return "seteq";
3897 case ISD::SETGT: return "setgt";
3898 case ISD::SETGE: return "setge";
3899 case ISD::SETLT: return "setlt";
3900 case ISD::SETLE: return "setle";
3901 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003902 }
3903 }
3904}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003905
Evan Cheng144d8f02006-11-09 17:55:04 +00003906const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003907 switch (AM) {
3908 default:
3909 return "";
3910 case ISD::PRE_INC:
3911 return "<pre-inc>";
3912 case ISD::PRE_DEC:
3913 return "<pre-dec>";
3914 case ISD::POST_INC:
3915 return "<post-inc>";
3916 case ISD::POST_DEC:
3917 return "<post-dec>";
3918 }
3919}
3920
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003921void SDNode::dump() const { dump(0); }
3922void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003923 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003924
3925 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003926 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003927 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003928 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003929 else
Bill Wendling832171c2006-12-07 20:04:42 +00003930 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003931 }
Bill Wendling832171c2006-12-07 20:04:42 +00003932 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003933
Bill Wendling832171c2006-12-07 20:04:42 +00003934 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003935 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003936 if (i) cerr << ", ";
3937 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003938 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003939 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003940 }
3941
Evan Chengce254432007-12-11 02:08:35 +00003942 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3943 SDNode *Mask = getOperand(2).Val;
3944 cerr << "<";
3945 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3946 if (i) cerr << ",";
3947 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3948 cerr << "u";
3949 else
3950 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
3951 }
3952 cerr << ">";
3953 }
3954
Chris Lattnerc3aae252005-01-07 07:46:32 +00003955 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003956 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003957 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003958 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3959 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3960 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3961 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3962 else {
3963 cerr << "<APFloat(";
3964 CSDN->getValueAPF().convertToAPInt().dump();
3965 cerr << ")>";
3966 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003967 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003968 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003969 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003970 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003971 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003972 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003973 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003974 else
Bill Wendling832171c2006-12-07 20:04:42 +00003975 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003976 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003977 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003978 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003979 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003980 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003981 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003982 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003983 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003984 else
Bill Wendling832171c2006-12-07 20:04:42 +00003985 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003986 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003987 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003988 else
Bill Wendling832171c2006-12-07 20:04:42 +00003989 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003990 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003991 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003992 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3993 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003994 cerr << LBB->getName() << " ";
3995 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003996 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003997 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003998 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003999 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004000 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004001 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004002 } else if (const ExternalSymbolSDNode *ES =
4003 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004004 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004005 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4006 if (M->getValue())
Evan Cheng334dc1f2008-01-31 21:00:00 +00004007 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004008 else
Evan Cheng334dc1f2008-01-31 21:00:00 +00004009 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00004010 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004011 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004012 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004013 const Value *SrcValue = LD->getSrcValue();
4014 int SrcOffset = LD->getSrcValueOffset();
4015 cerr << " <";
4016 if (SrcValue)
4017 cerr << SrcValue;
4018 else
4019 cerr << "null";
4020 cerr << ":" << SrcOffset << ">";
4021
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004022 bool doExt = true;
4023 switch (LD->getExtensionType()) {
4024 default: doExt = false; break;
4025 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004026 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004027 break;
4028 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004029 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004030 break;
4031 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004032 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004033 break;
4034 }
4035 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004036 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004037
Evan Cheng144d8f02006-11-09 17:55:04 +00004038 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004039 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004040 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004041 if (LD->isVolatile())
4042 cerr << " <volatile>";
4043 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004044 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004045 const Value *SrcValue = ST->getSrcValue();
4046 int SrcOffset = ST->getSrcValueOffset();
4047 cerr << " <";
4048 if (SrcValue)
4049 cerr << SrcValue;
4050 else
4051 cerr << "null";
4052 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004053
4054 if (ST->isTruncatingStore())
4055 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004056 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004057
4058 const char *AM = getIndexedModeName(ST->getAddressingMode());
4059 if (*AM)
4060 cerr << " " << AM;
4061 if (ST->isVolatile())
4062 cerr << " <volatile>";
4063 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004064 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004065}
4066
Chris Lattnerde202b32005-11-09 23:47:37 +00004067static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004068 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4069 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004070 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004071 else
Bill Wendling832171c2006-12-07 20:04:42 +00004072 cerr << "\n" << std::string(indent+2, ' ')
4073 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004074
Chris Lattnerea946cd2005-01-09 20:38:33 +00004075
Bill Wendling832171c2006-12-07 20:04:42 +00004076 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004077 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004078}
4079
Chris Lattnerc3aae252005-01-07 07:46:32 +00004080void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004081 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004082 std::vector<const SDNode*> Nodes;
4083 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4084 I != E; ++I)
4085 Nodes.push_back(I);
4086
Chris Lattner49d24712005-01-09 20:26:36 +00004087 std::sort(Nodes.begin(), Nodes.end());
4088
4089 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004090 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004091 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004092 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004093
Jim Laskey26f7fa72006-10-17 19:33:52 +00004094 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004095
Bill Wendling832171c2006-12-07 20:04:42 +00004096 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004097}
4098
Evan Chengd6594ae2006-09-12 21:00:35 +00004099const Type *ConstantPoolSDNode::getType() const {
4100 if (isMachineConstantPoolEntry())
4101 return Val.MachineCPVal->getType();
4102 return Val.ConstVal->getType();
4103}