blob: a62ea53142061acb272b7c3291eaa90426c9ead5 [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"
Dan Gohman69de1932008-02-06 22:27:42 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000026#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000027#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000028#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000031#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000032#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000033#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000034#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000035#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000036#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000037#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000038using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000039
Chris Lattner0b3e5252006-08-15 19:11:05 +000040/// makeVTList - Return an instance of the SDVTList struct initialized with the
41/// specified members.
42static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
43 SDVTList Res = {VTs, NumVTs};
44 return Res;
45}
46
Chris Lattnerf8dc0612008-02-03 06:49:24 +000047SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
48
Jim Laskey58b968b2005-08-17 20:08:02 +000049//===----------------------------------------------------------------------===//
50// ConstantFPSDNode Class
51//===----------------------------------------------------------------------===//
52
53/// isExactlyValue - We don't rely on operator== working on double values, as
54/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
55/// As such, this method can be used to do an exact bit-for-bit comparison of
56/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000057bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000058 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000059}
60
Dale Johannesenf04afdb2007-08-30 00:23:21 +000061bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
62 const APFloat& Val) {
63 // convert modifies in place, so make a copy.
64 APFloat Val2 = APFloat(Val);
65 switch (VT) {
66 default:
67 return false; // These can't be represented as floating point!
68
69 // FIXME rounding mode needs to be more flexible
70 case MVT::f32:
71 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
72 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
73 APFloat::opOK;
74 case MVT::f64:
75 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
76 &Val2.getSemantics() == &APFloat::IEEEdouble ||
77 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
78 APFloat::opOK;
79 // TODO: Figure out how to test if we can use a shorter type instead!
80 case MVT::f80:
81 case MVT::f128:
82 case MVT::ppcf128:
83 return true;
84 }
85}
86
Jim Laskey58b968b2005-08-17 20:08:02 +000087//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000088// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000089//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000090
Evan Chenga8df1662006-03-27 06:58:47 +000091/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000092/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000093bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000094 // Look through a bit convert.
95 if (N->getOpcode() == ISD::BIT_CONVERT)
96 N = N->getOperand(0).Val;
97
Evan Chenga8df1662006-03-27 06:58:47 +000098 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000099
100 unsigned i = 0, e = N->getNumOperands();
101
102 // Skip over all of the undef values.
103 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
104 ++i;
105
106 // Do not accept an all-undef vector.
107 if (i == e) return false;
108
109 // Do not accept build_vectors that aren't all constants or which have non-~0
110 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000111 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000112 if (isa<ConstantSDNode>(NotZero)) {
113 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
114 return false;
115 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000116 MVT::ValueType VT = NotZero.getValueType();
117 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000118 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
119 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000120 return false;
121 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000122 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
123 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000124 (uint32_t)-1)
125 return false;
126 }
Evan Chenga8df1662006-03-27 06:58:47 +0000127 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000128 return false;
129
130 // Okay, we have at least one ~0 value, check to see if the rest match or are
131 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000132 for (++i; i != e; ++i)
133 if (N->getOperand(i) != NotZero &&
134 N->getOperand(i).getOpcode() != ISD::UNDEF)
135 return false;
136 return true;
137}
138
139
Evan Cheng4a147842006-03-26 09:50:58 +0000140/// isBuildVectorAllZeros - Return true if the specified node is a
141/// BUILD_VECTOR where all of the elements are 0 or undef.
142bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000143 // Look through a bit convert.
144 if (N->getOpcode() == ISD::BIT_CONVERT)
145 N = N->getOperand(0).Val;
146
Evan Cheng4a147842006-03-26 09:50:58 +0000147 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000148
149 unsigned i = 0, e = N->getNumOperands();
150
151 // Skip over all of the undef values.
152 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
153 ++i;
154
155 // Do not accept an all-undef vector.
156 if (i == e) return false;
157
158 // Do not accept build_vectors that aren't all constants or which have non-~0
159 // elements.
160 SDOperand Zero = N->getOperand(i);
161 if (isa<ConstantSDNode>(Zero)) {
162 if (!cast<ConstantSDNode>(Zero)->isNullValue())
163 return false;
164 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000165 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000166 return false;
167 } else
168 return false;
169
170 // Okay, we have at least one ~0 value, check to see if the rest match or are
171 // undefs.
172 for (++i; i != e; ++i)
173 if (N->getOperand(i) != Zero &&
174 N->getOperand(i).getOpcode() != ISD::UNDEF)
175 return false;
176 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000177}
178
Evan Chengbb81d972008-01-31 09:59:15 +0000179/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengfc718542008-02-04 23:10:38 +0000180/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Chengbb81d972008-01-31 09:59:15 +0000181/// is 0).
182bool ISD::isDebugLabel(const SDNode *N) {
183 SDOperand Zero;
184 if (N->getOpcode() == ISD::LABEL)
185 Zero = N->getOperand(2);
186 else if (N->isTargetOpcode() &&
187 N->getTargetOpcode() == TargetInstrInfo::LABEL)
188 // Chain moved to last operand.
189 Zero = N->getOperand(1);
190 else
191 return false;
192 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
193}
194
Chris Lattnerc3aae252005-01-07 07:46:32 +0000195/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
196/// when given the operation for (X op Y).
197ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
198 // To perform this operation, we just need to swap the L and G bits of the
199 // operation.
200 unsigned OldL = (Operation >> 2) & 1;
201 unsigned OldG = (Operation >> 1) & 1;
202 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
203 (OldL << 1) | // New G bit
204 (OldG << 2)); // New L bit.
205}
206
207/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
208/// 'op' is a valid SetCC operation.
209ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
210 unsigned Operation = Op;
211 if (isInteger)
212 Operation ^= 7; // Flip L, G, E bits, but not U.
213 else
214 Operation ^= 15; // Flip all of the condition bits.
215 if (Operation > ISD::SETTRUE2)
216 Operation &= ~8; // Don't let N and U bits get set.
217 return ISD::CondCode(Operation);
218}
219
220
221/// isSignedOp - For an integer comparison, return 1 if the comparison is a
222/// signed operation and 2 if the result is an unsigned comparison. Return zero
223/// if the operation does not depend on the sign of the input (setne and seteq).
224static int isSignedOp(ISD::CondCode Opcode) {
225 switch (Opcode) {
226 default: assert(0 && "Illegal integer setcc operation!");
227 case ISD::SETEQ:
228 case ISD::SETNE: return 0;
229 case ISD::SETLT:
230 case ISD::SETLE:
231 case ISD::SETGT:
232 case ISD::SETGE: return 1;
233 case ISD::SETULT:
234 case ISD::SETULE:
235 case ISD::SETUGT:
236 case ISD::SETUGE: return 2;
237 }
238}
239
240/// getSetCCOrOperation - Return the result of a logical OR between different
241/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
242/// returns SETCC_INVALID if it is not possible to represent the resultant
243/// comparison.
244ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
245 bool isInteger) {
246 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
247 // Cannot fold a signed integer setcc with an unsigned integer setcc.
248 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000249
Chris Lattnerc3aae252005-01-07 07:46:32 +0000250 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000251
Chris Lattnerc3aae252005-01-07 07:46:32 +0000252 // If the N and U bits get set then the resultant comparison DOES suddenly
253 // care about orderedness, and is true when ordered.
254 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000255 Op &= ~16; // Clear the U bit if the N bit is set.
256
257 // Canonicalize illegal integer setcc's.
258 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
259 Op = ISD::SETNE;
260
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261 return ISD::CondCode(Op);
262}
263
264/// getSetCCAndOperation - Return the result of a logical AND between different
265/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
266/// function returns zero if it is not possible to represent the resultant
267/// comparison.
268ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
269 bool isInteger) {
270 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
271 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000272 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273
274 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000275 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
276
277 // Canonicalize illegal integer setcc's.
278 if (isInteger) {
279 switch (Result) {
280 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000281 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
282 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
283 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
284 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000285 }
286 }
287
288 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000289}
290
Chris Lattnerb48da392005-01-23 04:39:44 +0000291const TargetMachine &SelectionDAG::getTarget() const {
292 return TLI.getTargetMachine();
293}
294
Jim Laskey58b968b2005-08-17 20:08:02 +0000295//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000296// SDNode Profile Support
297//===----------------------------------------------------------------------===//
298
Jim Laskeydef69b92006-10-27 23:52:51 +0000299/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
300///
Jim Laskey583bd472006-10-27 23:46:08 +0000301static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
302 ID.AddInteger(OpC);
303}
304
305/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
306/// solely with their pointer.
307void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
308 ID.AddPointer(VTList.VTs);
309}
310
Jim Laskeydef69b92006-10-27 23:52:51 +0000311/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
312///
Jim Laskey583bd472006-10-27 23:46:08 +0000313static void AddNodeIDOperands(FoldingSetNodeID &ID,
314 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000315 for (; NumOps; --NumOps, ++Ops) {
316 ID.AddPointer(Ops->Val);
317 ID.AddInteger(Ops->ResNo);
318 }
Jim Laskey583bd472006-10-27 23:46:08 +0000319}
320
Jim Laskey583bd472006-10-27 23:46:08 +0000321static void AddNodeIDNode(FoldingSetNodeID &ID,
322 unsigned short OpC, SDVTList VTList,
323 const SDOperand *OpList, unsigned N) {
324 AddNodeIDOpcode(ID, OpC);
325 AddNodeIDValueTypes(ID, VTList);
326 AddNodeIDOperands(ID, OpList, N);
327}
328
Jim Laskeydef69b92006-10-27 23:52:51 +0000329/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
330/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000331static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
332 AddNodeIDOpcode(ID, N->getOpcode());
333 // Add the return value info.
334 AddNodeIDValueTypes(ID, N->getVTList());
335 // Add the operand info.
336 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
337
338 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000339 switch (N->getOpcode()) {
340 default: break; // Normal nodes don't need extra info.
341 case ISD::TargetConstant:
342 case ISD::Constant:
343 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
344 break;
345 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000346 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000347 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000348 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000349 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000350 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000351 case ISD::GlobalAddress:
352 case ISD::TargetGlobalTLSAddress:
353 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000354 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
355 ID.AddPointer(GA->getGlobal());
356 ID.AddInteger(GA->getOffset());
357 break;
358 }
359 case ISD::BasicBlock:
360 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
361 break;
362 case ISD::Register:
363 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
364 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000365 case ISD::SRCVALUE:
366 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
367 break;
368 case ISD::MEMOPERAND: {
369 const MemOperand &MO = cast<MemOperandSDNode>(N)->MO;
370 ID.AddPointer(MO.getValue());
371 ID.AddInteger(MO.getFlags());
372 ID.AddInteger(MO.getOffset());
373 ID.AddInteger(MO.getSize());
374 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 break;
376 }
377 case ISD::FrameIndex:
378 case ISD::TargetFrameIndex:
379 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
380 break;
381 case ISD::JumpTable:
382 case ISD::TargetJumpTable:
383 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
384 break;
385 case ISD::ConstantPool:
386 case ISD::TargetConstantPool: {
387 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
388 ID.AddInteger(CP->getAlignment());
389 ID.AddInteger(CP->getOffset());
390 if (CP->isMachineConstantPoolEntry())
391 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
392 else
393 ID.AddPointer(CP->getConstVal());
394 break;
395 }
396 case ISD::LOAD: {
397 LoadSDNode *LD = cast<LoadSDNode>(N);
398 ID.AddInteger(LD->getAddressingMode());
399 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000400 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000401 ID.AddInteger(LD->getAlignment());
402 ID.AddInteger(LD->isVolatile());
403 break;
404 }
405 case ISD::STORE: {
406 StoreSDNode *ST = cast<StoreSDNode>(N);
407 ID.AddInteger(ST->getAddressingMode());
408 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000409 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000410 ID.AddInteger(ST->getAlignment());
411 ID.AddInteger(ST->isVolatile());
412 break;
413 }
Jim Laskey583bd472006-10-27 23:46:08 +0000414 }
415}
416
417//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000418// SelectionDAG Class
419//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000420
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000421/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000422/// SelectionDAG.
423void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000424 // Create a dummy node (which is not added to allnodes), that adds a reference
425 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000426 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000427
Chris Lattner190a4182006-08-04 17:45:20 +0000428 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000429
Chris Lattner190a4182006-08-04 17:45:20 +0000430 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000431 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000432 if (I->use_empty())
433 DeadNodes.push_back(I);
434
435 // Process the worklist, deleting the nodes and adding their uses to the
436 // worklist.
437 while (!DeadNodes.empty()) {
438 SDNode *N = DeadNodes.back();
439 DeadNodes.pop_back();
440
441 // Take the node out of the appropriate CSE map.
442 RemoveNodeFromCSEMaps(N);
443
444 // Next, brutally remove the operand list. This is safe to do, as there are
445 // no cycles in the graph.
446 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
447 SDNode *Operand = I->Val;
448 Operand->removeUser(N);
449
450 // Now that we removed this operand, see if there are no uses of it left.
451 if (Operand->use_empty())
452 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000453 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000454 if (N->OperandsNeedDelete)
455 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000456 N->OperandList = 0;
457 N->NumOperands = 0;
458
459 // Finally, remove N itself.
460 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000461 }
462
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000463 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000464 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000465}
466
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000467void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000468 SmallVector<SDNode*, 16> DeadNodes;
469 DeadNodes.push_back(N);
470
471 // Process the worklist, deleting the nodes and adding their uses to the
472 // worklist.
473 while (!DeadNodes.empty()) {
474 SDNode *N = DeadNodes.back();
475 DeadNodes.pop_back();
476
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000477 if (UpdateListener)
478 UpdateListener->NodeDeleted(N);
479
Evan Cheng130a6472006-10-12 20:34:05 +0000480 // Take the node out of the appropriate CSE map.
481 RemoveNodeFromCSEMaps(N);
482
483 // Next, brutally remove the operand list. This is safe to do, as there are
484 // no cycles in the graph.
485 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
486 SDNode *Operand = I->Val;
487 Operand->removeUser(N);
488
489 // Now that we removed this operand, see if there are no uses of it left.
490 if (Operand->use_empty())
491 DeadNodes.push_back(Operand);
492 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000493 if (N->OperandsNeedDelete)
494 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000495 N->OperandList = 0;
496 N->NumOperands = 0;
497
498 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000499 AllNodes.erase(N);
500 }
501}
502
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000503void SelectionDAG::DeleteNode(SDNode *N) {
504 assert(N->use_empty() && "Cannot delete a node that is not dead!");
505
506 // First take this out of the appropriate CSE map.
507 RemoveNodeFromCSEMaps(N);
508
Chris Lattner1e111c72005-09-07 05:37:01 +0000509 // Finally, remove uses due to operands of this node, remove from the
510 // AllNodes list, and delete the node.
511 DeleteNodeNotInCSEMaps(N);
512}
513
514void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
515
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000516 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000517 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000518
519 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000520 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
521 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000522 if (N->OperandsNeedDelete)
523 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000524 N->OperandList = 0;
525 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000526
527 delete N;
528}
529
Chris Lattner149c58c2005-08-16 18:17:10 +0000530/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
531/// correspond to it. This is useful when we're about to delete or repurpose
532/// the node. We don't want future request for structurally identical nodes
533/// to return N anymore.
534void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000535 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000536 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000537 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000538 case ISD::STRING:
539 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
540 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000541 case ISD::CONDCODE:
542 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
543 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000544 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000545 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
546 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000547 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000548 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000549 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000550 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000551 Erased =
552 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000553 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000554 case ISD::VALUETYPE: {
555 MVT::ValueType VT = cast<VTSDNode>(N)->getVT();
556 if (MVT::isExtendedVT(VT)) {
557 Erased = ExtendedValueTypeNodes.erase(VT);
558 } else {
559 Erased = ValueTypeNodes[VT] != 0;
560 ValueTypeNodes[VT] = 0;
561 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000562 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000563 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000564 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000565 // Remove it from the CSE Map.
566 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000567 break;
568 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000569#ifndef NDEBUG
570 // Verify that the node was actually in one of the CSE maps, unless it has a
571 // flag result (which cannot be CSE'd) or is one of the special cases that are
572 // not subject to CSE.
573 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000574 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000575 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000576 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000577 assert(0 && "Node is not in map!");
578 }
579#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000580}
581
Chris Lattner8b8749f2005-08-17 19:00:20 +0000582/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
583/// has been taken out and modified in some way. If the specified node already
584/// exists in the CSE maps, do not modify the maps, but return the existing node
585/// instead. If it doesn't exist, add it and return null.
586///
587SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
588 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000589 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000590 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000591
Chris Lattner9f8cc692005-12-19 22:21:21 +0000592 // Check that remaining values produced are not flags.
593 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
594 if (N->getValueType(i) == MVT::Flag)
595 return 0; // Never CSE anything that produces a flag.
596
Chris Lattnera5682852006-08-07 23:03:03 +0000597 SDNode *New = CSEMap.GetOrInsertNode(N);
598 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000599 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000600}
601
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000602/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
603/// were replaced with those specified. If this node is never memoized,
604/// return null, otherwise return a pointer to the slot it would take. If a
605/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000606SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
607 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000608 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000609 return 0; // Never add these nodes.
610
611 // Check that remaining values produced are not flags.
612 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
613 if (N->getValueType(i) == MVT::Flag)
614 return 0; // Never CSE anything that produces a flag.
615
Chris Lattner63e3f142007-02-04 07:28:00 +0000616 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000617 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000618 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000619 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000620}
621
622/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
623/// were replaced with those specified. If this node is never memoized,
624/// return null, otherwise return a pointer to the slot it would take. If a
625/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000626SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
627 SDOperand Op1, SDOperand Op2,
628 void *&InsertPos) {
629 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
630 return 0; // Never add these nodes.
631
632 // Check that remaining values produced are not flags.
633 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
634 if (N->getValueType(i) == MVT::Flag)
635 return 0; // Never CSE anything that produces a flag.
636
Chris Lattner63e3f142007-02-04 07:28:00 +0000637 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000638 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000639 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000640 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
641}
642
643
644/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
645/// were replaced with those specified. If this node is never memoized,
646/// return null, otherwise return a pointer to the slot it would take. If a
647/// node already exists with these operands, the slot will be non-null.
648SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000649 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000650 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000651 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000652 return 0; // Never add these nodes.
653
654 // Check that remaining values produced are not flags.
655 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
656 if (N->getValueType(i) == MVT::Flag)
657 return 0; // Never CSE anything that produces a flag.
658
Jim Laskey583bd472006-10-27 23:46:08 +0000659 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000660 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000661
Evan Cheng9629aba2006-10-11 01:47:58 +0000662 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
663 ID.AddInteger(LD->getAddressingMode());
664 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000665 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000666 ID.AddInteger(LD->getAlignment());
667 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000668 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
669 ID.AddInteger(ST->getAddressingMode());
670 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000671 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000672 ID.AddInteger(ST->getAlignment());
673 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000674 }
Jim Laskey583bd472006-10-27 23:46:08 +0000675
Chris Lattnera5682852006-08-07 23:03:03 +0000676 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000677}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000678
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000679
Chris Lattner78ec3112003-08-11 14:57:33 +0000680SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000681 while (!AllNodes.empty()) {
682 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000683 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000684 if (N->OperandsNeedDelete)
685 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000686 N->OperandList = 0;
687 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000688 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000689 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000690}
691
Chris Lattner0f2287b2005-04-13 02:38:18 +0000692SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000693 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000694 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000695 return getNode(ISD::AND, Op.getValueType(), Op,
696 getConstant(Imm, Op.getValueType()));
697}
698
Chris Lattner36ce6912005-11-29 06:21:05 +0000699SDOperand SelectionDAG::getString(const std::string &Val) {
700 StringSDNode *&N = StringNodes[Val];
701 if (!N) {
702 N = new StringSDNode(Val);
703 AllNodes.push_back(N);
704 }
705 return SDOperand(N, 0);
706}
707
Chris Lattner61b09412006-08-11 21:01:22 +0000708SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000709 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000710
711 MVT::ValueType EltVT =
712 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000713
Chris Lattner61b09412006-08-11 21:01:22 +0000714 // Mask out any bits that are not valid for this constant.
Dan Gohman89081322007-12-12 22:21:26 +0000715 Val &= MVT::getIntVTBitMask(EltVT);
Chris Lattner61b09412006-08-11 21:01:22 +0000716
717 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000718 FoldingSetNodeID ID;
Dan Gohman89081322007-12-12 22:21:26 +0000719 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000720 ID.AddInteger(Val);
721 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000722 SDNode *N = NULL;
723 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
724 if (!MVT::isVector(VT))
725 return SDOperand(N, 0);
726 if (!N) {
727 N = new ConstantSDNode(isT, Val, EltVT);
728 CSEMap.InsertNode(N, IP);
729 AllNodes.push_back(N);
730 }
731
732 SDOperand Result(N, 0);
733 if (MVT::isVector(VT)) {
734 SmallVector<SDOperand, 8> Ops;
735 Ops.assign(MVT::getVectorNumElements(VT), Result);
736 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
737 }
738 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000739}
740
Chris Lattner0bd48932008-01-17 07:00:52 +0000741SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
742 return getConstant(Val, TLI.getPointerTy(), isTarget);
743}
744
745
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000746SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000747 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000748 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000749
Dan Gohman7f321562007-06-25 16:23:39 +0000750 MVT::ValueType EltVT =
751 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000752
Chris Lattnerd8658612005-02-17 20:17:32 +0000753 // Do the map lookup using the actual bit pattern for the floating point
754 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
755 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000756 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000757 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000758 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000759 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000760 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000761 SDNode *N = NULL;
762 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
763 if (!MVT::isVector(VT))
764 return SDOperand(N, 0);
765 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000766 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000767 CSEMap.InsertNode(N, IP);
768 AllNodes.push_back(N);
769 }
770
Dan Gohman7f321562007-06-25 16:23:39 +0000771 SDOperand Result(N, 0);
772 if (MVT::isVector(VT)) {
773 SmallVector<SDOperand, 8> Ops;
774 Ops.assign(MVT::getVectorNumElements(VT), Result);
775 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
776 }
777 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000778}
779
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000780SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
781 bool isTarget) {
782 MVT::ValueType EltVT =
783 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
784 if (EltVT==MVT::f32)
785 return getConstantFP(APFloat((float)Val), VT, isTarget);
786 else
787 return getConstantFP(APFloat(Val), VT, isTarget);
788}
789
Chris Lattnerc3aae252005-01-07 07:46:32 +0000790SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000791 MVT::ValueType VT, int Offset,
792 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000793 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
794 unsigned Opc;
795 if (GVar && GVar->isThreadLocal())
796 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
797 else
798 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000799 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000800 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000801 ID.AddPointer(GV);
802 ID.AddInteger(Offset);
803 void *IP = 0;
804 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
805 return SDOperand(E, 0);
806 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
807 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000808 AllNodes.push_back(N);
809 return SDOperand(N, 0);
810}
811
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000812SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
813 bool isTarget) {
814 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000815 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000816 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000817 ID.AddInteger(FI);
818 void *IP = 0;
819 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
820 return SDOperand(E, 0);
821 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
822 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000823 AllNodes.push_back(N);
824 return SDOperand(N, 0);
825}
826
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000827SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
828 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000829 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000830 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000831 ID.AddInteger(JTI);
832 void *IP = 0;
833 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
834 return SDOperand(E, 0);
835 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
836 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000837 AllNodes.push_back(N);
838 return SDOperand(N, 0);
839}
840
Evan Chengb8973bd2006-01-31 22:23:14 +0000841SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000842 unsigned Alignment, int Offset,
843 bool isTarget) {
844 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000845 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000846 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000847 ID.AddInteger(Alignment);
848 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000849 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000850 void *IP = 0;
851 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
852 return SDOperand(E, 0);
853 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
854 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000855 AllNodes.push_back(N);
856 return SDOperand(N, 0);
857}
858
Chris Lattnerc3aae252005-01-07 07:46:32 +0000859
Evan Chengd6594ae2006-09-12 21:00:35 +0000860SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
861 MVT::ValueType VT,
862 unsigned Alignment, int Offset,
863 bool isTarget) {
864 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000865 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000866 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000867 ID.AddInteger(Alignment);
868 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000869 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000870 void *IP = 0;
871 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
872 return SDOperand(E, 0);
873 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
874 CSEMap.InsertNode(N, IP);
875 AllNodes.push_back(N);
876 return SDOperand(N, 0);
877}
878
879
Chris Lattnerc3aae252005-01-07 07:46:32 +0000880SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000881 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000882 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000883 ID.AddPointer(MBB);
884 void *IP = 0;
885 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
886 return SDOperand(E, 0);
887 SDNode *N = new BasicBlockSDNode(MBB);
888 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000889 AllNodes.push_back(N);
890 return SDOperand(N, 0);
891}
892
Chris Lattner15e4b012005-07-10 00:07:11 +0000893SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000894 if (!MVT::isExtendedVT(VT) && (unsigned)VT >= ValueTypeNodes.size())
Chris Lattner15e4b012005-07-10 00:07:11 +0000895 ValueTypeNodes.resize(VT+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000896
Duncan Sandsf411b832007-10-17 13:49:58 +0000897 SDNode *&N = MVT::isExtendedVT(VT) ?
898 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT];
899
900 if (N) return SDOperand(N, 0);
901 N = new VTSDNode(VT);
902 AllNodes.push_back(N);
903 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000904}
905
Chris Lattnerc3aae252005-01-07 07:46:32 +0000906SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
907 SDNode *&N = ExternalSymbols[Sym];
908 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000909 N = new ExternalSymbolSDNode(false, Sym, VT);
910 AllNodes.push_back(N);
911 return SDOperand(N, 0);
912}
913
Chris Lattner809ec112006-01-28 10:09:25 +0000914SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
915 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000916 SDNode *&N = TargetExternalSymbols[Sym];
917 if (N) return SDOperand(N, 0);
918 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000919 AllNodes.push_back(N);
920 return SDOperand(N, 0);
921}
922
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000923SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
924 if ((unsigned)Cond >= CondCodeNodes.size())
925 CondCodeNodes.resize(Cond+1);
926
Chris Lattner079a27a2005-08-09 20:40:02 +0000927 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000928 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000929 AllNodes.push_back(CondCodeNodes[Cond]);
930 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000931 return SDOperand(CondCodeNodes[Cond], 0);
932}
933
Chris Lattner0fdd7682005-08-30 22:38:38 +0000934SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000935 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000936 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000937 ID.AddInteger(RegNo);
938 void *IP = 0;
939 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
940 return SDOperand(E, 0);
941 SDNode *N = new RegisterSDNode(RegNo, VT);
942 CSEMap.InsertNode(N, IP);
943 AllNodes.push_back(N);
944 return SDOperand(N, 0);
945}
946
Dan Gohman69de1932008-02-06 22:27:42 +0000947SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +0000948 assert((!V || isa<PointerType>(V->getType())) &&
949 "SrcValue is not a pointer?");
950
Jim Laskey583bd472006-10-27 23:46:08 +0000951 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000952 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000953 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +0000954
Chris Lattner61b09412006-08-11 21:01:22 +0000955 void *IP = 0;
956 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
957 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +0000958
959 SDNode *N = new SrcValueSDNode(V);
960 CSEMap.InsertNode(N, IP);
961 AllNodes.push_back(N);
962 return SDOperand(N, 0);
963}
964
965SDOperand SelectionDAG::getMemOperand(const MemOperand &MO) {
966 const Value *v = MO.getValue();
967 assert((!v || isa<PointerType>(v->getType())) &&
968 "SrcValue is not a pointer?");
969
970 FoldingSetNodeID ID;
971 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
972 ID.AddPointer(v);
973 ID.AddInteger(MO.getFlags());
974 ID.AddInteger(MO.getOffset());
975 ID.AddInteger(MO.getSize());
976 ID.AddInteger(MO.getAlignment());
977
978 void *IP = 0;
979 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
980 return SDOperand(E, 0);
981
982 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +0000983 CSEMap.InsertNode(N, IP);
984 AllNodes.push_back(N);
985 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000986}
987
Chris Lattner37ce9df2007-10-15 17:47:20 +0000988/// CreateStackTemporary - Create a stack temporary, suitable for holding the
989/// specified value type.
990SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
991 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
992 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
993 const Type *Ty = MVT::getTypeForValueType(VT);
994 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
995 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
996 return getFrameIndex(FrameIdx, TLI.getPointerTy());
997}
998
999
Chris Lattner51dabfb2006-10-14 00:41:01 +00001000SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
1001 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001002 // These setcc operations always fold.
1003 switch (Cond) {
1004 default: break;
1005 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001006 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001007 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001008 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001009
1010 case ISD::SETOEQ:
1011 case ISD::SETOGT:
1012 case ISD::SETOGE:
1013 case ISD::SETOLT:
1014 case ISD::SETOLE:
1015 case ISD::SETONE:
1016 case ISD::SETO:
1017 case ISD::SETUO:
1018 case ISD::SETUEQ:
1019 case ISD::SETUNE:
1020 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
1021 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001022 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001023
Chris Lattner67255a12005-04-07 18:14:58 +00001024 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
1025 uint64_t C2 = N2C->getValue();
1026 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1027 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001028
Chris Lattnerc3aae252005-01-07 07:46:32 +00001029 // Sign extend the operands if required
1030 if (ISD::isSignedIntSetCC(Cond)) {
1031 C1 = N1C->getSignExtended();
1032 C2 = N2C->getSignExtended();
1033 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001034
Chris Lattnerc3aae252005-01-07 07:46:32 +00001035 switch (Cond) {
1036 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001037 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1038 case ISD::SETNE: return getConstant(C1 != C2, VT);
1039 case ISD::SETULT: return getConstant(C1 < C2, VT);
1040 case ISD::SETUGT: return getConstant(C1 > C2, VT);
1041 case ISD::SETULE: return getConstant(C1 <= C2, VT);
1042 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
1043 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
1044 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
1045 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
1046 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001047 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001048 }
Chris Lattner67255a12005-04-07 18:14:58 +00001049 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001050 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1051 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001052 // No compile time operations on this type yet.
1053 if (N1C->getValueType(0) == MVT::ppcf128)
1054 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001055
1056 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001057 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001058 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001059 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1060 return getNode(ISD::UNDEF, VT);
1061 // fall through
1062 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1063 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1064 return getNode(ISD::UNDEF, VT);
1065 // fall through
1066 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001067 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001068 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1069 return getNode(ISD::UNDEF, VT);
1070 // fall through
1071 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1072 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1073 return getNode(ISD::UNDEF, VT);
1074 // fall through
1075 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1076 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1077 return getNode(ISD::UNDEF, VT);
1078 // fall through
1079 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001080 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001081 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1082 return getNode(ISD::UNDEF, VT);
1083 // fall through
1084 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001085 R==APFloat::cmpEqual, VT);
1086 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1087 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1088 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1089 R==APFloat::cmpEqual, VT);
1090 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1091 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1092 R==APFloat::cmpLessThan, VT);
1093 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1094 R==APFloat::cmpUnordered, VT);
1095 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1096 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001097 }
1098 } else {
1099 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001100 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001101 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001102
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001103 // Could not fold it.
1104 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105}
1106
Dan Gohmanea859be2007-06-22 14:59:07 +00001107/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1108/// this predicate to simplify operations downstream. Mask is known to be zero
1109/// for bits that V cannot have.
1110bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1111 unsigned Depth) const {
1112 // The masks are not wide enough to represent this type! Should use APInt.
1113 if (Op.getValueType() == MVT::i128)
1114 return false;
1115
1116 uint64_t KnownZero, KnownOne;
1117 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1118 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1119 return (KnownZero & Mask) == Mask;
1120}
1121
1122/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1123/// known to be either zero or one and return them in the KnownZero/KnownOne
1124/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1125/// processing.
1126void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1127 uint64_t &KnownZero, uint64_t &KnownOne,
1128 unsigned Depth) const {
1129 KnownZero = KnownOne = 0; // Don't know anything.
1130 if (Depth == 6 || Mask == 0)
1131 return; // Limit search depth.
1132
1133 // The masks are not wide enough to represent this type! Should use APInt.
1134 if (Op.getValueType() == MVT::i128)
1135 return;
1136
1137 uint64_t KnownZero2, KnownOne2;
1138
1139 switch (Op.getOpcode()) {
1140 case ISD::Constant:
1141 // We know all of the bits for a constant!
1142 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1143 KnownZero = ~KnownOne & Mask;
1144 return;
1145 case ISD::AND:
1146 // If either the LHS or the RHS are Zero, the result is zero.
1147 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1148 Mask &= ~KnownZero;
1149 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1150 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1151 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1152
1153 // Output known-1 bits are only known if set in both the LHS & RHS.
1154 KnownOne &= KnownOne2;
1155 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1156 KnownZero |= KnownZero2;
1157 return;
1158 case ISD::OR:
1159 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1160 Mask &= ~KnownOne;
1161 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1162 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1163 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1164
1165 // Output known-0 bits are only known if clear in both the LHS & RHS.
1166 KnownZero &= KnownZero2;
1167 // Output known-1 are known to be set if set in either the LHS | RHS.
1168 KnownOne |= KnownOne2;
1169 return;
1170 case ISD::XOR: {
1171 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1172 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1173 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1174 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1175
1176 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1177 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1178 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1179 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1180 KnownZero = KnownZeroOut;
1181 return;
1182 }
1183 case ISD::SELECT:
1184 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1185 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1186 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1187 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1188
1189 // Only known if known in both the LHS and RHS.
1190 KnownOne &= KnownOne2;
1191 KnownZero &= KnownZero2;
1192 return;
1193 case ISD::SELECT_CC:
1194 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1195 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1196 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1197 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1198
1199 // Only known if known in both the LHS and RHS.
1200 KnownOne &= KnownOne2;
1201 KnownZero &= KnownZero2;
1202 return;
1203 case ISD::SETCC:
1204 // If we know the result of a setcc has the top bits zero, use this info.
1205 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1206 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1207 return;
1208 case ISD::SHL:
1209 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1210 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1211 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1212 KnownZero, KnownOne, Depth+1);
1213 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1214 KnownZero <<= SA->getValue();
1215 KnownOne <<= SA->getValue();
1216 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1217 }
1218 return;
1219 case ISD::SRL:
1220 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1221 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1222 MVT::ValueType VT = Op.getValueType();
1223 unsigned ShAmt = SA->getValue();
1224
1225 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1226 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1227 KnownZero, KnownOne, Depth+1);
1228 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1229 KnownZero &= TypeMask;
1230 KnownOne &= TypeMask;
1231 KnownZero >>= ShAmt;
1232 KnownOne >>= ShAmt;
1233
1234 uint64_t HighBits = (1ULL << ShAmt)-1;
1235 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1236 KnownZero |= HighBits; // High bits known zero.
1237 }
1238 return;
1239 case ISD::SRA:
1240 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1241 MVT::ValueType VT = Op.getValueType();
1242 unsigned ShAmt = SA->getValue();
1243
1244 // Compute the new bits that are at the top now.
1245 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1246
1247 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1248 // If any of the demanded bits are produced by the sign extension, we also
1249 // demand the input sign bit.
1250 uint64_t HighBits = (1ULL << ShAmt)-1;
1251 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1252 if (HighBits & Mask)
1253 InDemandedMask |= MVT::getIntVTSignBit(VT);
1254
1255 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1256 Depth+1);
1257 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1258 KnownZero &= TypeMask;
1259 KnownOne &= TypeMask;
1260 KnownZero >>= ShAmt;
1261 KnownOne >>= ShAmt;
1262
1263 // Handle the sign bits.
1264 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1265 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1266
1267 if (KnownZero & SignBit) {
1268 KnownZero |= HighBits; // New bits are known zero.
1269 } else if (KnownOne & SignBit) {
1270 KnownOne |= HighBits; // New bits are known one.
1271 }
1272 }
1273 return;
1274 case ISD::SIGN_EXTEND_INREG: {
1275 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1276
1277 // Sign extension. Compute the demanded bits in the result that are not
1278 // present in the input.
1279 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1280
1281 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1282 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1283
1284 // If the sign extended bits are demanded, we know that the sign
1285 // bit is demanded.
1286 if (NewBits)
1287 InputDemandedBits |= InSignBit;
1288
1289 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1290 KnownZero, KnownOne, Depth+1);
1291 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1292
1293 // If the sign bit of the input is known set or clear, then we know the
1294 // top bits of the result.
1295 if (KnownZero & InSignBit) { // Input sign bit known clear
1296 KnownZero |= NewBits;
1297 KnownOne &= ~NewBits;
1298 } else if (KnownOne & InSignBit) { // Input sign bit known set
1299 KnownOne |= NewBits;
1300 KnownZero &= ~NewBits;
1301 } else { // Input sign bit unknown
1302 KnownZero &= ~NewBits;
1303 KnownOne &= ~NewBits;
1304 }
1305 return;
1306 }
1307 case ISD::CTTZ:
1308 case ISD::CTLZ:
1309 case ISD::CTPOP: {
1310 MVT::ValueType VT = Op.getValueType();
1311 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1312 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1313 KnownOne = 0;
1314 return;
1315 }
1316 case ISD::LOAD: {
1317 if (ISD::isZEXTLoad(Op.Val)) {
1318 LoadSDNode *LD = cast<LoadSDNode>(Op);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001319 MVT::ValueType VT = LD->getMemoryVT();
Dan Gohmanea859be2007-06-22 14:59:07 +00001320 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1321 }
1322 return;
1323 }
1324 case ISD::ZERO_EXTEND: {
1325 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1326 uint64_t NewBits = (~InMask) & Mask;
1327 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1328 KnownOne, Depth+1);
1329 KnownZero |= NewBits & Mask;
1330 KnownOne &= ~NewBits;
1331 return;
1332 }
1333 case ISD::SIGN_EXTEND: {
1334 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1335 unsigned InBits = MVT::getSizeInBits(InVT);
1336 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1337 uint64_t InSignBit = 1ULL << (InBits-1);
1338 uint64_t NewBits = (~InMask) & Mask;
1339 uint64_t InDemandedBits = Mask & InMask;
1340
1341 // If any of the sign extended bits are demanded, we know that the sign
1342 // bit is demanded.
1343 if (NewBits & Mask)
1344 InDemandedBits |= InSignBit;
1345
1346 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1347 KnownOne, Depth+1);
1348 // If the sign bit is known zero or one, the top bits match.
1349 if (KnownZero & InSignBit) {
1350 KnownZero |= NewBits;
1351 KnownOne &= ~NewBits;
1352 } else if (KnownOne & InSignBit) {
1353 KnownOne |= NewBits;
1354 KnownZero &= ~NewBits;
1355 } else { // Otherwise, top bits aren't known.
1356 KnownOne &= ~NewBits;
1357 KnownZero &= ~NewBits;
1358 }
1359 return;
1360 }
1361 case ISD::ANY_EXTEND: {
1362 MVT::ValueType VT = Op.getOperand(0).getValueType();
1363 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1364 KnownZero, KnownOne, Depth+1);
1365 return;
1366 }
1367 case ISD::TRUNCATE: {
1368 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1369 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1370 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1371 KnownZero &= OutMask;
1372 KnownOne &= OutMask;
1373 break;
1374 }
1375 case ISD::AssertZext: {
1376 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1377 uint64_t InMask = MVT::getIntVTBitMask(VT);
1378 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1379 KnownOne, Depth+1);
1380 KnownZero |= (~InMask) & Mask;
1381 return;
1382 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001383 case ISD::FGETSIGN:
1384 // All bits are zero except the low bit.
1385 KnownZero = MVT::getIntVTBitMask(Op.getValueType()) ^ 1;
1386 return;
1387
Dan Gohmanea859be2007-06-22 14:59:07 +00001388 case ISD::ADD: {
1389 // If either the LHS or the RHS are Zero, the result is zero.
1390 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1391 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1392 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1393 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1394
1395 // Output known-0 bits are known if clear or set in both the low clear bits
1396 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1397 // low 3 bits clear.
1398 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1399 CountTrailingZeros_64(~KnownZero2));
1400
1401 KnownZero = (1ULL << KnownZeroOut) - 1;
1402 KnownOne = 0;
1403 return;
1404 }
1405 case ISD::SUB: {
1406 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1407 if (!CLHS) return;
1408
1409 // We know that the top bits of C-X are clear if X contains less bits
1410 // than C (i.e. no wrap-around can happen). For example, 20-X is
1411 // positive if we can prove that X is >= 0 and < 16.
1412 MVT::ValueType VT = CLHS->getValueType(0);
1413 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1414 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1415 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1416 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1417 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1418
1419 // If all of the MaskV bits are known to be zero, then we know the output
1420 // top bits are zero, because we now know that the output is from [0-C].
1421 if ((KnownZero & MaskV) == MaskV) {
1422 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1423 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1424 KnownOne = 0; // No one bits known.
1425 } else {
1426 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1427 }
1428 }
1429 return;
1430 }
1431 default:
1432 // Allow the target to implement this method for its nodes.
1433 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1434 case ISD::INTRINSIC_WO_CHAIN:
1435 case ISD::INTRINSIC_W_CHAIN:
1436 case ISD::INTRINSIC_VOID:
1437 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1438 }
1439 return;
1440 }
1441}
1442
1443/// ComputeNumSignBits - Return the number of times the sign bit of the
1444/// register is replicated into the other bits. We know that at least 1 bit
1445/// is always equal to the sign bit (itself), but other cases can give us
1446/// information. For example, immediately after an "SRA X, 2", we know that
1447/// the top 3 bits are all equal to each other, so we return 3.
1448unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1449 MVT::ValueType VT = Op.getValueType();
1450 assert(MVT::isInteger(VT) && "Invalid VT!");
1451 unsigned VTBits = MVT::getSizeInBits(VT);
1452 unsigned Tmp, Tmp2;
1453
1454 if (Depth == 6)
1455 return 1; // Limit search depth.
1456
1457 switch (Op.getOpcode()) {
1458 default: break;
1459 case ISD::AssertSext:
1460 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1461 return VTBits-Tmp+1;
1462 case ISD::AssertZext:
1463 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1464 return VTBits-Tmp;
1465
1466 case ISD::Constant: {
1467 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1468 // If negative, invert the bits, then look at it.
1469 if (Val & MVT::getIntVTSignBit(VT))
1470 Val = ~Val;
1471
1472 // Shift the bits so they are the leading bits in the int64_t.
1473 Val <<= 64-VTBits;
1474
1475 // Return # leading zeros. We use 'min' here in case Val was zero before
1476 // shifting. We don't want to return '64' as for an i32 "0".
1477 return std::min(VTBits, CountLeadingZeros_64(Val));
1478 }
1479
1480 case ISD::SIGN_EXTEND:
1481 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1482 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1483
1484 case ISD::SIGN_EXTEND_INREG:
1485 // Max of the input and what this extends.
1486 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1487 Tmp = VTBits-Tmp+1;
1488
1489 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1490 return std::max(Tmp, Tmp2);
1491
1492 case ISD::SRA:
1493 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1494 // SRA X, C -> adds C sign bits.
1495 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1496 Tmp += C->getValue();
1497 if (Tmp > VTBits) Tmp = VTBits;
1498 }
1499 return Tmp;
1500 case ISD::SHL:
1501 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1502 // shl destroys sign bits.
1503 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1504 if (C->getValue() >= VTBits || // Bad shift.
1505 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1506 return Tmp - C->getValue();
1507 }
1508 break;
1509 case ISD::AND:
1510 case ISD::OR:
1511 case ISD::XOR: // NOT is handled here.
1512 // Logical binary ops preserve the number of sign bits.
1513 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1514 if (Tmp == 1) return 1; // Early out.
1515 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1516 return std::min(Tmp, Tmp2);
1517
1518 case ISD::SELECT:
1519 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1520 if (Tmp == 1) return 1; // Early out.
1521 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1522 return std::min(Tmp, Tmp2);
1523
1524 case ISD::SETCC:
1525 // If setcc returns 0/-1, all bits are sign bits.
1526 if (TLI.getSetCCResultContents() ==
1527 TargetLowering::ZeroOrNegativeOneSetCCResult)
1528 return VTBits;
1529 break;
1530 case ISD::ROTL:
1531 case ISD::ROTR:
1532 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1533 unsigned RotAmt = C->getValue() & (VTBits-1);
1534
1535 // Handle rotate right by N like a rotate left by 32-N.
1536 if (Op.getOpcode() == ISD::ROTR)
1537 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1538
1539 // If we aren't rotating out all of the known-in sign bits, return the
1540 // number that are left. This handles rotl(sext(x), 1) for example.
1541 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1542 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1543 }
1544 break;
1545 case ISD::ADD:
1546 // Add can have at most one carry bit. Thus we know that the output
1547 // is, at worst, one more bit than the inputs.
1548 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1549 if (Tmp == 1) return 1; // Early out.
1550
1551 // Special case decrementing a value (ADD X, -1):
1552 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1553 if (CRHS->isAllOnesValue()) {
1554 uint64_t KnownZero, KnownOne;
1555 uint64_t Mask = MVT::getIntVTBitMask(VT);
1556 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1557
1558 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1559 // sign bits set.
1560 if ((KnownZero|1) == Mask)
1561 return VTBits;
1562
1563 // If we are subtracting one from a positive number, there is no carry
1564 // out of the result.
1565 if (KnownZero & MVT::getIntVTSignBit(VT))
1566 return Tmp;
1567 }
1568
1569 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1570 if (Tmp2 == 1) return 1;
1571 return std::min(Tmp, Tmp2)-1;
1572 break;
1573
1574 case ISD::SUB:
1575 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1576 if (Tmp2 == 1) return 1;
1577
1578 // Handle NEG.
1579 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1580 if (CLHS->getValue() == 0) {
1581 uint64_t KnownZero, KnownOne;
1582 uint64_t Mask = MVT::getIntVTBitMask(VT);
1583 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1584 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1585 // sign bits set.
1586 if ((KnownZero|1) == Mask)
1587 return VTBits;
1588
1589 // If the input is known to be positive (the sign bit is known clear),
1590 // the output of the NEG has the same number of sign bits as the input.
1591 if (KnownZero & MVT::getIntVTSignBit(VT))
1592 return Tmp2;
1593
1594 // Otherwise, we treat this like a SUB.
1595 }
1596
1597 // Sub can have at most one carry bit. Thus we know that the output
1598 // is, at worst, one more bit than the inputs.
1599 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1600 if (Tmp == 1) return 1; // Early out.
1601 return std::min(Tmp, Tmp2)-1;
1602 break;
1603 case ISD::TRUNCATE:
1604 // FIXME: it's tricky to do anything useful for this, but it is an important
1605 // case for targets like X86.
1606 break;
1607 }
1608
1609 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1610 if (Op.getOpcode() == ISD::LOAD) {
1611 LoadSDNode *LD = cast<LoadSDNode>(Op);
1612 unsigned ExtType = LD->getExtensionType();
1613 switch (ExtType) {
1614 default: break;
1615 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001616 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001617 return VTBits-Tmp+1;
1618 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001619 Tmp = MVT::getSizeInBits(LD->getMemoryVT());
Dan Gohmanea859be2007-06-22 14:59:07 +00001620 return VTBits-Tmp;
1621 }
1622 }
1623
1624 // Allow the target to implement this method for its nodes.
1625 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1626 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1627 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1628 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1629 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1630 if (NumBits > 1) return NumBits;
1631 }
1632
1633 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1634 // use this information.
1635 uint64_t KnownZero, KnownOne;
1636 uint64_t Mask = MVT::getIntVTBitMask(VT);
1637 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1638
1639 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1640 if (KnownZero & SignBit) { // SignBit is 0
1641 Mask = KnownZero;
1642 } else if (KnownOne & SignBit) { // SignBit is 1;
1643 Mask = KnownOne;
1644 } else {
1645 // Nothing known.
1646 return 1;
1647 }
1648
1649 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1650 // the number of identical bits in the top of the input value.
1651 Mask ^= ~0ULL;
1652 Mask <<= 64-VTBits;
1653 // Return # leading zeros. We use 'min' here in case Val was zero before
1654 // shifting. We don't want to return '64' as for an i32 "0".
1655 return std::min(VTBits, CountLeadingZeros_64(Mask));
1656}
1657
Chris Lattner51dabfb2006-10-14 00:41:01 +00001658
Evan Chenga844bde2008-02-02 04:07:54 +00001659bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1660 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1661 if (!GA) return false;
1662 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1663 if (!GV) return false;
1664 MachineModuleInfo *MMI = getMachineModuleInfo();
1665 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1666}
1667
1668
Chris Lattnerc3aae252005-01-07 07:46:32 +00001669/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001670///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001671SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001672 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001673 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001674 void *IP = 0;
1675 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1676 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001677 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001678 CSEMap.InsertNode(N, IP);
1679
1680 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001681 return SDOperand(N, 0);
1682}
1683
Chris Lattnerc3aae252005-01-07 07:46:32 +00001684SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1685 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001686 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001687 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001688 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1689 uint64_t Val = C->getValue();
1690 switch (Opcode) {
1691 default: break;
1692 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001693 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001694 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1695 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001696 case ISD::UINT_TO_FP:
1697 case ISD::SINT_TO_FP: {
1698 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001699 // No compile time operations on this type.
1700 if (VT==MVT::ppcf128)
1701 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00001702 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001703 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001704 MVT::getSizeInBits(Operand.getValueType()),
1705 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001706 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001707 return getConstantFP(apf, VT);
1708 }
Chris Lattner94683772005-12-23 05:30:37 +00001709 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001710 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001711 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001712 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001713 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001714 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001715 case ISD::BSWAP:
1716 switch(VT) {
1717 default: assert(0 && "Invalid bswap!"); break;
1718 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1719 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1720 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1721 }
1722 break;
1723 case ISD::CTPOP:
1724 switch(VT) {
1725 default: assert(0 && "Invalid ctpop!"); break;
1726 case MVT::i1: return getConstant(Val != 0, VT);
1727 case MVT::i8:
1728 Tmp1 = (unsigned)Val & 0xFF;
1729 return getConstant(CountPopulation_32(Tmp1), VT);
1730 case MVT::i16:
1731 Tmp1 = (unsigned)Val & 0xFFFF;
1732 return getConstant(CountPopulation_32(Tmp1), VT);
1733 case MVT::i32:
1734 return getConstant(CountPopulation_32((unsigned)Val), VT);
1735 case MVT::i64:
1736 return getConstant(CountPopulation_64(Val), VT);
1737 }
1738 case ISD::CTLZ:
1739 switch(VT) {
1740 default: assert(0 && "Invalid ctlz!"); break;
1741 case MVT::i1: return getConstant(Val == 0, VT);
1742 case MVT::i8:
1743 Tmp1 = (unsigned)Val & 0xFF;
1744 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1745 case MVT::i16:
1746 Tmp1 = (unsigned)Val & 0xFFFF;
1747 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1748 case MVT::i32:
1749 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1750 case MVT::i64:
1751 return getConstant(CountLeadingZeros_64(Val), VT);
1752 }
1753 case ISD::CTTZ:
1754 switch(VT) {
1755 default: assert(0 && "Invalid cttz!"); break;
1756 case MVT::i1: return getConstant(Val == 0, VT);
1757 case MVT::i8:
1758 Tmp1 = (unsigned)Val | 0x100;
1759 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1760 case MVT::i16:
1761 Tmp1 = (unsigned)Val | 0x10000;
1762 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1763 case MVT::i32:
1764 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1765 case MVT::i64:
1766 return getConstant(CountTrailingZeros_64(Val), VT);
1767 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001768 }
1769 }
1770
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001771 // Constant fold unary operations with a floating point constant operand.
1772 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1773 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001774 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001775 switch (Opcode) {
1776 case ISD::FNEG:
1777 V.changeSign();
1778 return getConstantFP(V, VT);
1779 case ISD::FABS:
1780 V.clearSign();
1781 return getConstantFP(V, VT);
1782 case ISD::FP_ROUND:
1783 case ISD::FP_EXTEND:
1784 // This can return overflow, underflow, or inexact; we don't care.
1785 // FIXME need to be more flexible about rounding mode.
1786 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
1787 VT==MVT::f64 ? APFloat::IEEEdouble :
1788 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1789 VT==MVT::f128 ? APFloat::IEEEquad :
1790 APFloat::Bogus,
1791 APFloat::rmNearestTiesToEven);
1792 return getConstantFP(V, VT);
1793 case ISD::FP_TO_SINT:
1794 case ISD::FP_TO_UINT: {
1795 integerPart x;
1796 assert(integerPartWidth >= 64);
1797 // FIXME need to be more flexible about rounding mode.
1798 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1799 Opcode==ISD::FP_TO_SINT,
1800 APFloat::rmTowardZero);
1801 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1802 break;
1803 return getConstant(x, VT);
1804 }
1805 case ISD::BIT_CONVERT:
1806 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1807 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1808 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1809 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001810 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001811 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001812 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001813 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001814
1815 unsigned OpOpcode = Operand.Val->getOpcode();
1816 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001817 case ISD::TokenFactor:
1818 return Operand; // Factor of one node? No factor.
Chris Lattner0bd48932008-01-17 07:00:52 +00001819 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001820 case ISD::FP_EXTEND:
1821 assert(MVT::isFloatingPoint(VT) &&
1822 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001823 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001824 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00001825 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001826 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1827 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001828 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001829 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1830 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001831 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1832 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1833 break;
1834 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001835 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1836 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001837 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001838 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1839 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001840 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001841 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001842 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001843 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001844 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1845 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001846 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsaf47b112007-10-16 09:56:48 +00001847 assert(MVT::getSizeInBits(Operand.getValueType()) < MVT::getSizeInBits(VT)
1848 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001849 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1850 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1851 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1852 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001853 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001854 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1855 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001856 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsaf47b112007-10-16 09:56:48 +00001857 assert(MVT::getSizeInBits(Operand.getValueType()) > MVT::getSizeInBits(VT)
1858 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001859 if (OpOpcode == ISD::TRUNCATE)
1860 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001861 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1862 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001863 // If the source is smaller than the dest, we still need an extend.
Duncan Sandsaf47b112007-10-16 09:56:48 +00001864 if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1865 < MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001866 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sandsaf47b112007-10-16 09:56:48 +00001867 else if (MVT::getSizeInBits(Operand.Val->getOperand(0).getValueType())
1868 > MVT::getSizeInBits(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001869 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1870 else
1871 return Operand.Val->getOperand(0);
1872 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001873 break;
Chris Lattner35481892005-12-23 00:16:34 +00001874 case ISD::BIT_CONVERT:
1875 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001876 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001877 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001878 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001879 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1880 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001881 if (OpOpcode == ISD::UNDEF)
1882 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001883 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001884 case ISD::SCALAR_TO_VECTOR:
1885 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001886 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001887 "Illegal SCALAR_TO_VECTOR node!");
1888 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001889 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001890 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1891 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001892 Operand.Val->getOperand(0));
1893 if (OpOpcode == ISD::FNEG) // --X -> X
1894 return Operand.Val->getOperand(0);
1895 break;
1896 case ISD::FABS:
1897 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1898 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1899 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001900 }
1901
Chris Lattner43247a12005-08-25 19:12:10 +00001902 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001903 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001904 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001905 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001906 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001907 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001908 void *IP = 0;
1909 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1910 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001911 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001912 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001913 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001914 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001915 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001916 AllNodes.push_back(N);
1917 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001918}
1919
Chris Lattner36019aa2005-04-18 03:48:41 +00001920
1921
Chris Lattnerc3aae252005-01-07 07:46:32 +00001922SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1923 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001924 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1925 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00001926 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001927 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00001928 case ISD::TokenFactor:
1929 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1930 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001931 // Fold trivial token factors.
1932 if (N1.getOpcode() == ISD::EntryToken) return N2;
1933 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00001934 break;
Chris Lattner76365122005-01-16 02:23:22 +00001935 case ISD::AND:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001936 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1937 N1.getValueType() == VT && "Binary operator types must match!");
1938 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1939 // worth handling here.
1940 if (N2C && N2C->getValue() == 0)
1941 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00001942 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
1943 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001944 break;
Chris Lattner76365122005-01-16 02:23:22 +00001945 case ISD::OR:
1946 case ISD::XOR:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001947 assert(MVT::isInteger(VT) && N1.getValueType() == N2.getValueType() &&
1948 N1.getValueType() == VT && "Binary operator types must match!");
1949 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1950 // worth handling here.
1951 if (N2C && N2C->getValue() == 0)
1952 return N1;
1953 break;
Chris Lattner76365122005-01-16 02:23:22 +00001954 case ISD::UDIV:
1955 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001956 case ISD::MULHU:
1957 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001958 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1959 // fall through
1960 case ISD::ADD:
1961 case ISD::SUB:
1962 case ISD::MUL:
1963 case ISD::SDIV:
1964 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001965 case ISD::FADD:
1966 case ISD::FSUB:
1967 case ISD::FMUL:
1968 case ISD::FDIV:
1969 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001970 assert(N1.getValueType() == N2.getValueType() &&
1971 N1.getValueType() == VT && "Binary operator types must match!");
1972 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001973 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1974 assert(N1.getValueType() == VT &&
1975 MVT::isFloatingPoint(N1.getValueType()) &&
1976 MVT::isFloatingPoint(N2.getValueType()) &&
1977 "Invalid FCOPYSIGN!");
1978 break;
Chris Lattner76365122005-01-16 02:23:22 +00001979 case ISD::SHL:
1980 case ISD::SRA:
1981 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001982 case ISD::ROTL:
1983 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001984 assert(VT == N1.getValueType() &&
1985 "Shift operators return type must be the same as their first arg");
1986 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001987 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001988 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001989 case ISD::FP_ROUND_INREG: {
1990 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1991 assert(VT == N1.getValueType() && "Not an inreg round!");
1992 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1993 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00001994 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
1995 "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00001996 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00001997 break;
1998 }
Chris Lattner0bd48932008-01-17 07:00:52 +00001999 case ISD::FP_ROUND:
2000 assert(MVT::isFloatingPoint(VT) &&
2001 MVT::isFloatingPoint(N1.getValueType()) &&
2002 MVT::getSizeInBits(VT) <= MVT::getSizeInBits(N1.getValueType()) &&
2003 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002004 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002005 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002006 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002007 case ISD::AssertZext: {
2008 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2009 assert(VT == N1.getValueType() && "Not an inreg extend!");
2010 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2011 "Cannot *_EXTEND_INREG FP types");
2012 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2013 "Not extending!");
2014 break;
2015 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002016 case ISD::SIGN_EXTEND_INREG: {
2017 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2018 assert(VT == N1.getValueType() && "Not an inreg extend!");
2019 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
2020 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsaf47b112007-10-16 09:56:48 +00002021 assert(MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(VT) &&
2022 "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002023 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002024
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002025 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002026 int64_t Val = N1C->getValue();
2027 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
2028 Val <<= 64-FromBits;
2029 Val >>= 64-FromBits;
2030 return getConstant(Val, VT);
2031 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002032 break;
2033 }
2034 case ISD::EXTRACT_VECTOR_ELT:
2035 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2036
2037 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2038 // expanding copies of large vectors from registers.
2039 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2040 N1.getNumOperands() > 0) {
2041 unsigned Factor =
2042 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2043 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2044 N1.getOperand(N2C->getValue() / Factor),
2045 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2046 }
2047
2048 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2049 // expanding large vector constants.
2050 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2051 return N1.getOperand(N2C->getValue());
2052
2053 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2054 // operations are lowered to scalars.
2055 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2056 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2057 if (IEC == N2C)
2058 return N1.getOperand(1);
2059 else
2060 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2061 }
2062 break;
2063 case ISD::EXTRACT_ELEMENT:
2064 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002065
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002066 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2067 // 64-bit integers into 32-bit parts. Instead of building the extract of
2068 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2069 if (N1.getOpcode() == ISD::BUILD_PAIR)
2070 return N1.getOperand(N2C->getValue());
2071
2072 // EXTRACT_ELEMENT of a constant int is also very common.
2073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2074 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2075 return getConstant(C->getValue() >> Shift, VT);
2076 }
2077 break;
2078 }
2079
2080 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002081 if (N2C) {
2082 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
2083 switch (Opcode) {
2084 case ISD::ADD: return getConstant(C1 + C2, VT);
2085 case ISD::SUB: return getConstant(C1 - C2, VT);
2086 case ISD::MUL: return getConstant(C1 * C2, VT);
2087 case ISD::UDIV:
2088 if (C2) return getConstant(C1 / C2, VT);
2089 break;
2090 case ISD::UREM :
2091 if (C2) return getConstant(C1 % C2, VT);
2092 break;
2093 case ISD::SDIV :
2094 if (C2) return getConstant(N1C->getSignExtended() /
2095 N2C->getSignExtended(), VT);
2096 break;
2097 case ISD::SREM :
2098 if (C2) return getConstant(N1C->getSignExtended() %
2099 N2C->getSignExtended(), VT);
2100 break;
2101 case ISD::AND : return getConstant(C1 & C2, VT);
2102 case ISD::OR : return getConstant(C1 | C2, VT);
2103 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002104 case ISD::SHL : return getConstant(C1 << C2, VT);
2105 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00002106 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00002107 case ISD::ROTL :
2108 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
2109 VT);
2110 case ISD::ROTR :
2111 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
2112 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002113 default: break;
2114 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002115 } else { // Cannonicalize constant to RHS if commutative
2116 if (isCommutativeBinOp(Opcode)) {
2117 std::swap(N1C, N2C);
2118 std::swap(N1, N2);
2119 }
2120 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002121 }
2122
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002123 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002124 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2125 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002126 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002127 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2128 // Cannonicalize constant to RHS if commutative
2129 std::swap(N1CFP, N2CFP);
2130 std::swap(N1, N2);
2131 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002132 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2133 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002134 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002135 case ISD::FADD:
2136 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002137 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002138 return getConstantFP(V1, VT);
2139 break;
2140 case ISD::FSUB:
2141 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2142 if (s!=APFloat::opInvalidOp)
2143 return getConstantFP(V1, VT);
2144 break;
2145 case ISD::FMUL:
2146 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2147 if (s!=APFloat::opInvalidOp)
2148 return getConstantFP(V1, VT);
2149 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002150 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002151 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2152 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2153 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002154 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002155 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002156 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2157 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2158 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002159 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002160 case ISD::FCOPYSIGN:
2161 V1.copySign(V2);
2162 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002163 default: break;
2164 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002165 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002166 }
Chris Lattner62b57722006-04-20 05:39:12 +00002167
2168 // Canonicalize an UNDEF to the RHS, even over a constant.
2169 if (N1.getOpcode() == ISD::UNDEF) {
2170 if (isCommutativeBinOp(Opcode)) {
2171 std::swap(N1, N2);
2172 } else {
2173 switch (Opcode) {
2174 case ISD::FP_ROUND_INREG:
2175 case ISD::SIGN_EXTEND_INREG:
2176 case ISD::SUB:
2177 case ISD::FSUB:
2178 case ISD::FDIV:
2179 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002180 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002181 return N1; // fold op(undef, arg2) -> undef
2182 case ISD::UDIV:
2183 case ISD::SDIV:
2184 case ISD::UREM:
2185 case ISD::SREM:
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(undef, arg2) -> 0
2190 // For vectors, we can't easily build an all zero vector, just return
2191 // the LHS.
2192 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002193 }
2194 }
2195 }
2196
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002197 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002198 if (N2.getOpcode() == ISD::UNDEF) {
2199 switch (Opcode) {
2200 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002201 case ISD::ADDC:
2202 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002203 case ISD::SUB:
2204 case ISD::FADD:
2205 case ISD::FSUB:
2206 case ISD::FMUL:
2207 case ISD::FDIV:
2208 case ISD::FREM:
2209 case ISD::UDIV:
2210 case ISD::SDIV:
2211 case ISD::UREM:
2212 case ISD::SREM:
2213 case ISD::XOR:
2214 return N2; // fold op(arg1, undef) -> undef
2215 case ISD::MUL:
2216 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002217 case ISD::SRL:
2218 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002219 if (!MVT::isVector(VT))
2220 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2221 // For vectors, we can't easily build an all zero vector, just return
2222 // the LHS.
2223 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002224 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002225 if (!MVT::isVector(VT))
2226 return getConstant(MVT::getIntVTBitMask(VT), VT);
2227 // For vectors, we can't easily build an all one vector, just return
2228 // the LHS.
2229 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002230 case ISD::SRA:
2231 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002232 }
2233 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002234
Chris Lattner27e9b412005-05-11 18:57:39 +00002235 // Memoize this node if possible.
2236 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002237 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002238 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002239 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002240 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002241 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002242 void *IP = 0;
2243 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2244 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002245 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002246 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002247 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002248 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002249 }
2250
Chris Lattnerc3aae252005-01-07 07:46:32 +00002251 AllNodes.push_back(N);
2252 return SDOperand(N, 0);
2253}
2254
Chris Lattnerc3aae252005-01-07 07:46:32 +00002255SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2256 SDOperand N1, SDOperand N2, SDOperand N3) {
2257 // Perform various simplifications.
2258 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2259 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002260 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002261 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002262 // Use FoldSetCC to simplify SETCC's.
2263 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002264 if (Simp.Val) return Simp;
2265 break;
2266 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002267 case ISD::SELECT:
2268 if (N1C)
2269 if (N1C->getValue())
2270 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002271 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002272 return N3; // select false, X, Y -> Y
2273
2274 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002275 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002276 case ISD::BRCOND:
2277 if (N2C)
2278 if (N2C->getValue()) // Unconditional branch
2279 return getNode(ISD::BR, MVT::Other, N1, N3);
2280 else
2281 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002282 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002283 case ISD::VECTOR_SHUFFLE:
2284 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2285 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2286 N3.getOpcode() == ISD::BUILD_VECTOR &&
2287 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2288 "Illegal VECTOR_SHUFFLE node!");
2289 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002290 case ISD::BIT_CONVERT:
2291 // Fold bit_convert nodes from a type to themselves.
2292 if (N1.getValueType() == VT)
2293 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002294 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002295 }
2296
Chris Lattner43247a12005-08-25 19:12:10 +00002297 // Memoize node if it doesn't produce a flag.
2298 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002299 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002300 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002301 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002302 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002303 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002304 void *IP = 0;
2305 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2306 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002307 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002308 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002309 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002310 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002311 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002312 AllNodes.push_back(N);
2313 return SDOperand(N, 0);
2314}
2315
2316SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002317 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002318 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002319 SDOperand Ops[] = { N1, N2, N3, N4 };
2320 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002321}
2322
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002323SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2324 SDOperand N1, SDOperand N2, SDOperand N3,
2325 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002326 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2327 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002328}
2329
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002330SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dest,
2331 SDOperand Src, SDOperand Size,
2332 SDOperand Align,
2333 SDOperand AlwaysInline) {
2334 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2335 return getNode(ISD::MEMCPY, MVT::Other, Ops, 6);
2336}
2337
2338SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dest,
2339 SDOperand Src, SDOperand Size,
2340 SDOperand Align,
2341 SDOperand AlwaysInline) {
2342 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2343 return getNode(ISD::MEMMOVE, MVT::Other, Ops, 6);
2344}
2345
2346SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dest,
2347 SDOperand Src, SDOperand Size,
2348 SDOperand Align,
2349 SDOperand AlwaysInline) {
2350 SDOperand Ops[] = { Chain, Dest, Src, Size, Align, AlwaysInline };
2351 return getNode(ISD::MEMSET, MVT::Other, Ops, 6);
2352}
2353
Evan Cheng7038daf2005-12-10 00:37:58 +00002354SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2355 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002356 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002357 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002358 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2359 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002360 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002361 Ty = MVT::getTypeForValueType(VT);
2362 } else if (SV) {
2363 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2364 assert(PT && "Value for load must be a pointer");
2365 Ty = PT->getElementType();
2366 }
2367 assert(Ty && "Could not get type information for load");
2368 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2369 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002370 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002371 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002372 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002373 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002374 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002375 ID.AddInteger(ISD::UNINDEXED);
2376 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002377 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002378 ID.AddInteger(Alignment);
2379 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002380 void *IP = 0;
2381 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2382 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002383 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002384 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2385 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002386 CSEMap.InsertNode(N, IP);
2387 AllNodes.push_back(N);
2388 return SDOperand(N, 0);
2389}
2390
2391SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002392 SDOperand Chain, SDOperand Ptr,
2393 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002394 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002395 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002396 // If they are asking for an extending load from/to the same thing, return a
2397 // normal load.
2398 if (VT == EVT)
Duncan Sands5d868b12007-10-19 13:05:40 +00002399 return getLoad(VT, Chain, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00002400
2401 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002402 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002403 else
Duncan Sandsaf47b112007-10-16 09:56:48 +00002404 assert(MVT::getSizeInBits(EVT) < MVT::getSizeInBits(VT) &&
2405 "Should only be an extending load, not truncating!");
Evan Cheng466685d2006-10-09 20:57:25 +00002406 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2407 "Cannot sign/zero extend a FP/Vector load!");
2408 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2409 "Cannot convert from FP to Int or Int -> FP!");
2410
Dan Gohman575e2f42007-06-04 15:49:41 +00002411 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2412 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002413 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002414 Ty = MVT::getTypeForValueType(VT);
2415 } else if (SV) {
2416 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2417 assert(PT && "Value for load must be a pointer");
2418 Ty = PT->getElementType();
2419 }
2420 assert(Ty && "Could not get type information for load");
2421 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2422 }
Evan Cheng466685d2006-10-09 20:57:25 +00002423 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002424 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002425 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002426 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002427 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002428 ID.AddInteger(ISD::UNINDEXED);
2429 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002430 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002431 ID.AddInteger(Alignment);
2432 ID.AddInteger(isVolatile);
2433 void *IP = 0;
2434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2435 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002436 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002437 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002438 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002439 AllNodes.push_back(N);
2440 return SDOperand(N, 0);
2441}
2442
Evan Cheng144d8f02006-11-09 17:55:04 +00002443SDOperand
2444SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2445 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002446 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002447 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2448 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002449 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002450 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002451 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002452 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002453 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002454 ID.AddInteger(AM);
2455 ID.AddInteger(LD->getExtensionType());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002456 ID.AddInteger((unsigned int)(LD->getMemoryVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002457 ID.AddInteger(LD->getAlignment());
2458 ID.AddInteger(LD->isVolatile());
2459 void *IP = 0;
2460 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2461 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002462 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002463 LD->getExtensionType(), LD->getMemoryVT(),
Evan Cheng2caccca2006-10-17 21:14:32 +00002464 LD->getSrcValue(), LD->getSrcValueOffset(),
2465 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002466 CSEMap.InsertNode(N, IP);
2467 AllNodes.push_back(N);
2468 return SDOperand(N, 0);
2469}
2470
Jeff Cohend41b30d2006-11-05 19:31:28 +00002471SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002472 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002473 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002474 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002475
Dan Gohman575e2f42007-06-04 15:49:41 +00002476 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2477 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002478 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002479 Ty = MVT::getTypeForValueType(VT);
2480 } else if (SV) {
2481 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2482 assert(PT && "Value for store must be a pointer");
2483 Ty = PT->getElementType();
2484 }
2485 assert(Ty && "Could not get type information for store");
2486 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2487 }
Evan Chengad071e12006-10-05 22:57:11 +00002488 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002489 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002490 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002491 FoldingSetNodeID ID;
2492 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002493 ID.AddInteger(ISD::UNINDEXED);
2494 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002495 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002496 ID.AddInteger(Alignment);
2497 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002498 void *IP = 0;
2499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2500 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002501 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002502 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002503 CSEMap.InsertNode(N, IP);
2504 AllNodes.push_back(N);
2505 return SDOperand(N, 0);
2506}
2507
Jeff Cohend41b30d2006-11-05 19:31:28 +00002508SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002509 SDOperand Ptr, const Value *SV,
2510 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002511 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002512 MVT::ValueType VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002513
2514 if (VT == SVT)
2515 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002516
Duncan Sandsaf47b112007-10-16 09:56:48 +00002517 assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(SVT) &&
2518 "Not a truncation?");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002519 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2520 "Can't do FP-INT conversion!");
2521
Dan Gohman575e2f42007-06-04 15:49:41 +00002522 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2523 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002524 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002525 Ty = MVT::getTypeForValueType(VT);
2526 } else if (SV) {
2527 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2528 assert(PT && "Value for store must be a pointer");
2529 Ty = PT->getElementType();
2530 }
2531 assert(Ty && "Could not get type information for store");
2532 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2533 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002534 SDVTList VTs = getVTList(MVT::Other);
2535 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002536 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002537 FoldingSetNodeID ID;
2538 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002539 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002540 ID.AddInteger(1);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002541 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002542 ID.AddInteger(Alignment);
2543 ID.AddInteger(isVolatile);
2544 void *IP = 0;
2545 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2546 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00002547 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002548 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002549 CSEMap.InsertNode(N, IP);
2550 AllNodes.push_back(N);
2551 return SDOperand(N, 0);
2552}
2553
Evan Cheng144d8f02006-11-09 17:55:04 +00002554SDOperand
2555SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2556 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002557 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2558 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2559 "Store is already a indexed store!");
2560 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2561 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2562 FoldingSetNodeID ID;
2563 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2564 ID.AddInteger(AM);
2565 ID.AddInteger(ST->isTruncatingStore());
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002566 ID.AddInteger((unsigned int)(ST->getMemoryVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002567 ID.AddInteger(ST->getAlignment());
2568 ID.AddInteger(ST->isVolatile());
2569 void *IP = 0;
2570 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2571 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002572 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002573 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00002574 ST->getSrcValue(), ST->getSrcValueOffset(),
2575 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002576 CSEMap.InsertNode(N, IP);
2577 AllNodes.push_back(N);
2578 return SDOperand(N, 0);
2579}
2580
Nate Begemanacc398c2006-01-25 18:21:52 +00002581SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2582 SDOperand Chain, SDOperand Ptr,
2583 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002584 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002585 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002586}
2587
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002588SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002589 const SDOperand *Ops, unsigned NumOps) {
2590 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002591 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002592 case 1: return getNode(Opcode, VT, Ops[0]);
2593 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2594 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002595 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002596 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002597
Chris Lattneref847df2005-04-09 03:27:28 +00002598 switch (Opcode) {
2599 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002600 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002601 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002602 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2603 "LHS and RHS of condition must have same type!");
2604 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2605 "True and False arms of SelectCC must have same type!");
2606 assert(Ops[2].getValueType() == VT &&
2607 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002608 break;
2609 }
2610 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002611 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002612 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2613 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002614 break;
2615 }
Chris Lattneref847df2005-04-09 03:27:28 +00002616 }
2617
Chris Lattner385328c2005-05-14 07:42:29 +00002618 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002619 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002620 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002621 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002622 FoldingSetNodeID ID;
2623 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002624 void *IP = 0;
2625 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2626 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002627 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002628 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002629 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002630 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002631 }
Chris Lattneref847df2005-04-09 03:27:28 +00002632 AllNodes.push_back(N);
2633 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002634}
2635
Chris Lattner89c34632005-05-14 06:20:26 +00002636SDOperand SelectionDAG::getNode(unsigned Opcode,
2637 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002638 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002639 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2640 Ops, NumOps);
2641}
2642
2643SDOperand SelectionDAG::getNode(unsigned Opcode,
2644 const MVT::ValueType *VTs, unsigned NumVTs,
2645 const SDOperand *Ops, unsigned NumOps) {
2646 if (NumVTs == 1)
2647 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002648 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2649}
2650
2651SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2652 const SDOperand *Ops, unsigned NumOps) {
2653 if (VTList.NumVTs == 1)
2654 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002655
Chris Lattner5f056bf2005-07-10 01:55:33 +00002656 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002657 // FIXME: figure out how to safely handle things like
2658 // int foo(int x) { return 1 << (x & 255); }
2659 // int bar() { return foo(256); }
2660#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002661 case ISD::SRA_PARTS:
2662 case ISD::SRL_PARTS:
2663 case ISD::SHL_PARTS:
2664 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002665 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002666 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2667 else if (N3.getOpcode() == ISD::AND)
2668 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2669 // If the and is only masking out bits that cannot effect the shift,
2670 // eliminate the and.
2671 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2672 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2673 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2674 }
2675 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002676#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002677 }
Chris Lattner89c34632005-05-14 06:20:26 +00002678
Chris Lattner43247a12005-08-25 19:12:10 +00002679 // Memoize the node unless it returns a flag.
2680 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002681 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002682 FoldingSetNodeID ID;
2683 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002684 void *IP = 0;
2685 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2686 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002687 if (NumOps == 1)
2688 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2689 else if (NumOps == 2)
2690 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2691 else if (NumOps == 3)
2692 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2693 else
2694 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002695 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002696 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002697 if (NumOps == 1)
2698 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2699 else if (NumOps == 2)
2700 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2701 else if (NumOps == 3)
2702 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2703 else
2704 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002705 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002706 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002707 return SDOperand(N, 0);
2708}
2709
Dan Gohman08ce9762007-10-08 15:49:58 +00002710SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2711 return getNode(Opcode, VTList, 0, 0);
2712}
2713
2714SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2715 SDOperand N1) {
2716 SDOperand Ops[] = { N1 };
2717 return getNode(Opcode, VTList, Ops, 1);
2718}
2719
2720SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2721 SDOperand N1, SDOperand N2) {
2722 SDOperand Ops[] = { N1, N2 };
2723 return getNode(Opcode, VTList, Ops, 2);
2724}
2725
2726SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2727 SDOperand N1, SDOperand N2, SDOperand N3) {
2728 SDOperand Ops[] = { N1, N2, N3 };
2729 return getNode(Opcode, VTList, Ops, 3);
2730}
2731
2732SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2733 SDOperand N1, SDOperand N2, SDOperand N3,
2734 SDOperand N4) {
2735 SDOperand Ops[] = { N1, N2, N3, N4 };
2736 return getNode(Opcode, VTList, Ops, 4);
2737}
2738
2739SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2740 SDOperand N1, SDOperand N2, SDOperand N3,
2741 SDOperand N4, SDOperand N5) {
2742 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2743 return getNode(Opcode, VTList, Ops, 5);
2744}
2745
Chris Lattner70046e92006-08-15 17:46:01 +00002746SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00002747 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002748}
2749
Chris Lattner70046e92006-08-15 17:46:01 +00002750SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002751 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2752 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002753 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002754 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002755 }
2756 std::vector<MVT::ValueType> V;
2757 V.push_back(VT1);
2758 V.push_back(VT2);
2759 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002760 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002761}
Chris Lattner70046e92006-08-15 17:46:01 +00002762SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2763 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002764 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002765 E = VTList.end(); I != E; ++I) {
2766 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2767 (*I)[2] == VT3)
2768 return makeVTList(&(*I)[0], 3);
2769 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002770 std::vector<MVT::ValueType> V;
2771 V.push_back(VT1);
2772 V.push_back(VT2);
2773 V.push_back(VT3);
2774 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002775 return makeVTList(&(*VTList.begin())[0], 3);
2776}
2777
2778SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2779 switch (NumVTs) {
2780 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002781 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002782 case 2: return getVTList(VTs[0], VTs[1]);
2783 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2784 default: break;
2785 }
2786
2787 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2788 E = VTList.end(); I != E; ++I) {
2789 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2790
2791 bool NoMatch = false;
2792 for (unsigned i = 2; i != NumVTs; ++i)
2793 if (VTs[i] != (*I)[i]) {
2794 NoMatch = true;
2795 break;
2796 }
2797 if (!NoMatch)
2798 return makeVTList(&*I->begin(), NumVTs);
2799 }
2800
2801 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2802 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002803}
2804
2805
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002806/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2807/// specified operands. If the resultant node already exists in the DAG,
2808/// this does not modify the specified node, instead it returns the node that
2809/// already exists. If the resultant node does not exist in the DAG, the
2810/// input node is returned. As a degenerate case, if you specify the same
2811/// input operands as the node already has, the input node is returned.
2812SDOperand SelectionDAG::
2813UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2814 SDNode *N = InN.Val;
2815 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2816
2817 // Check to see if there is no change.
2818 if (Op == N->getOperand(0)) return InN;
2819
2820 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002821 void *InsertPos = 0;
2822 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2823 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002824
2825 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002826 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002827 RemoveNodeFromCSEMaps(N);
2828
2829 // Now we update the operands.
2830 N->OperandList[0].Val->removeUser(N);
2831 Op.Val->addUser(N);
2832 N->OperandList[0] = Op;
2833
2834 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002835 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002836 return InN;
2837}
2838
2839SDOperand SelectionDAG::
2840UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2841 SDNode *N = InN.Val;
2842 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2843
2844 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002845 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2846 return InN; // No operands changed, just return the input node.
2847
2848 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002849 void *InsertPos = 0;
2850 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2851 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002852
2853 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002854 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002855 RemoveNodeFromCSEMaps(N);
2856
2857 // Now we update the operands.
2858 if (N->OperandList[0] != Op1) {
2859 N->OperandList[0].Val->removeUser(N);
2860 Op1.Val->addUser(N);
2861 N->OperandList[0] = Op1;
2862 }
2863 if (N->OperandList[1] != Op2) {
2864 N->OperandList[1].Val->removeUser(N);
2865 Op2.Val->addUser(N);
2866 N->OperandList[1] = Op2;
2867 }
2868
2869 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002870 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002871 return InN;
2872}
2873
2874SDOperand SelectionDAG::
2875UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002876 SDOperand Ops[] = { Op1, Op2, Op3 };
2877 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002878}
2879
2880SDOperand SelectionDAG::
2881UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2882 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002883 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2884 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002885}
2886
2887SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002888UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2889 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002890 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2891 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002892}
2893
2894
2895SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002896UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002897 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002898 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002899 "Update with wrong number of operands");
2900
2901 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002902 bool AnyChange = false;
2903 for (unsigned i = 0; i != NumOps; ++i) {
2904 if (Ops[i] != N->getOperand(i)) {
2905 AnyChange = true;
2906 break;
2907 }
2908 }
2909
2910 // No operands changed, just return the input node.
2911 if (!AnyChange) return InN;
2912
2913 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002914 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002915 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002916 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002917
2918 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002919 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002920 RemoveNodeFromCSEMaps(N);
2921
2922 // Now we update the operands.
2923 for (unsigned i = 0; i != NumOps; ++i) {
2924 if (N->OperandList[i] != Ops[i]) {
2925 N->OperandList[i].Val->removeUser(N);
2926 Ops[i].Val->addUser(N);
2927 N->OperandList[i] = Ops[i];
2928 }
2929 }
2930
2931 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002932 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002933 return InN;
2934}
2935
2936
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002937/// MorphNodeTo - This frees the operands of the current node, resets the
2938/// opcode, types, and operands to the specified value. This should only be
2939/// used by the SelectionDAG class.
2940void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2941 const SDOperand *Ops, unsigned NumOps) {
2942 NodeType = Opc;
2943 ValueList = L.VTs;
2944 NumValues = L.NumVTs;
2945
2946 // Clear the operands list, updating used nodes to remove this from their
2947 // use list.
2948 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2949 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002950
2951 // If NumOps is larger than the # of operands we currently have, reallocate
2952 // the operand list.
2953 if (NumOps > NumOperands) {
2954 if (OperandsNeedDelete)
2955 delete [] OperandList;
2956 OperandList = new SDOperand[NumOps];
2957 OperandsNeedDelete = true;
2958 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002959
2960 // Assign the new operands.
2961 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002962
2963 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2964 OperandList[i] = Ops[i];
2965 SDNode *N = OperandList[i].Val;
2966 N->Uses.push_back(this);
2967 }
2968}
Chris Lattner149c58c2005-08-16 18:17:10 +00002969
2970/// SelectNodeTo - These are used for target selectors to *mutate* the
2971/// specified node to have the specified return type, Target opcode, and
2972/// operands. Note that target opcodes are stored as
2973/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002974///
2975/// Note that SelectNodeTo returns the resultant node. If there is already a
2976/// node of the specified opcode and operands, it returns that node instead of
2977/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002978SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2979 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002980 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002981 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002982 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002983 void *IP = 0;
2984 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002985 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002986
Chris Lattner7651fa42005-08-24 23:00:29 +00002987 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002988
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002989 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002990
Chris Lattner4a283e92006-08-11 18:38:11 +00002991 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002992 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002993}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002994
Evan Cheng95514ba2006-08-26 08:00:10 +00002995SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2996 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002997 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002998 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002999 SDOperand Ops[] = { Op1 };
3000
Jim Laskey583bd472006-10-27 23:46:08 +00003001 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003002 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003003 void *IP = 0;
3004 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003005 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003006
Chris Lattner149c58c2005-08-16 18:17:10 +00003007 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003008 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003009 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003010 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003011}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003012
Evan Cheng95514ba2006-08-26 08:00:10 +00003013SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3014 MVT::ValueType VT, SDOperand Op1,
3015 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003016 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003017 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003018 SDOperand Ops[] = { Op1, Op2 };
3019
Jim Laskey583bd472006-10-27 23:46:08 +00003020 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003021 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003022 void *IP = 0;
3023 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003024 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003025
Chris Lattner149c58c2005-08-16 18:17:10 +00003026 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003027
Chris Lattner63e3f142007-02-04 07:28:00 +00003028 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003029
Chris Lattnera5682852006-08-07 23:03:03 +00003030 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003031 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003032}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003033
Evan Cheng95514ba2006-08-26 08:00:10 +00003034SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3035 MVT::ValueType VT, SDOperand Op1,
3036 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003037 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003038 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003039 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003040 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003041 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003042 void *IP = 0;
3043 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003044 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003045
Chris Lattner149c58c2005-08-16 18:17:10 +00003046 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003047
Chris Lattner63e3f142007-02-04 07:28:00 +00003048 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003049
Chris Lattnera5682852006-08-07 23:03:03 +00003050 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003051 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003052}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003053
Evan Cheng95514ba2006-08-26 08:00:10 +00003054SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00003055 MVT::ValueType VT, const SDOperand *Ops,
3056 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003057 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003058 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003059 FoldingSetNodeID ID;
3060 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003061 void *IP = 0;
3062 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003063 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003064
Chris Lattner6b09a292005-08-21 18:49:33 +00003065 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003066 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003067
Chris Lattnera5682852006-08-07 23:03:03 +00003068 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003069 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003070}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003071
Evan Cheng95514ba2006-08-26 08:00:10 +00003072SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3073 MVT::ValueType VT1, MVT::ValueType VT2,
3074 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003075 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003076 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003077 SDOperand Ops[] = { Op1, Op2 };
3078 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003079 void *IP = 0;
3080 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003081 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003082
Chris Lattner0fb094f2005-11-19 01:44:53 +00003083 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003084 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003085 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003086 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003087}
3088
Evan Cheng95514ba2006-08-26 08:00:10 +00003089SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3090 MVT::ValueType VT1, MVT::ValueType VT2,
3091 SDOperand Op1, SDOperand Op2,
3092 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003093 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003094 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003095 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003096 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003097 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003098 void *IP = 0;
3099 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003100 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003101
Chris Lattner0fb094f2005-11-19 01:44:53 +00003102 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003103
Chris Lattner63e3f142007-02-04 07:28:00 +00003104 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003105 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003106 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003107}
3108
Chris Lattner0fb094f2005-11-19 01:44:53 +00003109
Evan Cheng6ae46c42006-02-09 07:15:23 +00003110/// getTargetNode - These are used for target selectors to create a new node
3111/// with specified return type(s), target opcode, and operands.
3112///
3113/// Note that getTargetNode returns the resultant node. If there is already a
3114/// node of the specified opcode and operands, it returns that node instead of
3115/// the current one.
3116SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3117 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3118}
3119SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3120 SDOperand Op1) {
3121 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3122}
3123SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3124 SDOperand Op1, SDOperand Op2) {
3125 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3126}
3127SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003128 SDOperand Op1, SDOperand Op2,
3129 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003130 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3131}
3132SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003133 const SDOperand *Ops, unsigned NumOps) {
3134 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003135}
3136SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003137 MVT::ValueType VT2) {
3138 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3139 SDOperand Op;
3140 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3141}
3142SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003143 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003144 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003145 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003146}
3147SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003148 MVT::ValueType VT2, SDOperand Op1,
3149 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003150 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003151 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003152 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003153}
3154SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003155 MVT::ValueType VT2, SDOperand Op1,
3156 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003157 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003158 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003159 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003160}
Evan Cheng694481e2006-08-27 08:08:54 +00003161SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3162 MVT::ValueType VT2,
3163 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003164 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003165 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003166}
3167SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3168 MVT::ValueType VT2, MVT::ValueType VT3,
3169 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003170 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003171 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003172 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003173}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003174SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3175 MVT::ValueType VT2, MVT::ValueType VT3,
3176 SDOperand Op1, SDOperand Op2,
3177 SDOperand Op3) {
3178 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3179 SDOperand Ops[] = { Op1, Op2, Op3 };
3180 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3181}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003182SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003183 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003184 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003185 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3186 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003187}
Evan Cheng05e69c12007-09-12 23:39:49 +00003188SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3189 MVT::ValueType VT2, MVT::ValueType VT3,
3190 MVT::ValueType VT4,
3191 const SDOperand *Ops, unsigned NumOps) {
3192 std::vector<MVT::ValueType> VTList;
3193 VTList.push_back(VT1);
3194 VTList.push_back(VT2);
3195 VTList.push_back(VT3);
3196 VTList.push_back(VT4);
3197 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3198 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3199}
Evan Cheng39305cf2007-10-05 01:10:49 +00003200SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3201 std::vector<MVT::ValueType> &ResultTys,
3202 const SDOperand *Ops, unsigned NumOps) {
3203 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3204 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3205 Ops, NumOps).Val;
3206}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003207
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003208
Evan Cheng99157a02006-08-07 22:13:29 +00003209/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003210/// This can cause recursive merging of nodes in the DAG.
3211///
Chris Lattner11d049c2008-02-03 03:35:22 +00003212/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003213///
Chris Lattner11d049c2008-02-03 03:35:22 +00003214void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003215 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003216 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003217 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003218 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003219 assert(From != To.Val && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003220
Chris Lattner8b8749f2005-08-17 19:00:20 +00003221 while (!From->use_empty()) {
3222 // Process users until they are all gone.
3223 SDNode *U = *From->use_begin();
3224
3225 // This node is about to morph, remove its old self from the CSE maps.
3226 RemoveNodeFromCSEMaps(U);
3227
Chris Lattner65113b22005-11-08 22:07:03 +00003228 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3229 I != E; ++I)
3230 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003231 From->removeUser(U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003232 *I = To;
3233 To.Val->addUser(U);
Chris Lattner8b52f212005-08-26 18:36:28 +00003234 }
3235
3236 // Now that we have modified U, add it back to the CSE maps. If it already
3237 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003238 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003239 ReplaceAllUsesWith(U, Existing, UpdateListener);
3240 // U is now dead. Inform the listener if it exists and delete it.
3241 if (UpdateListener)
3242 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003243 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003244 } else {
3245 // If the node doesn't already exist, we updated it. Inform a listener if
3246 // it exists.
3247 if (UpdateListener)
3248 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003249 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003250 }
3251}
3252
3253/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3254/// This can cause recursive merging of nodes in the DAG.
3255///
3256/// This version assumes From/To have matching types and numbers of result
3257/// values.
3258///
Chris Lattner1e111c72005-09-07 05:37:01 +00003259void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003260 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003261 assert(From != To && "Cannot replace uses of with self");
3262 assert(From->getNumValues() == To->getNumValues() &&
3263 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003264 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003265 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3266 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003267
3268 while (!From->use_empty()) {
3269 // Process users until they are all gone.
3270 SDNode *U = *From->use_begin();
3271
3272 // This node is about to morph, remove its old self from the CSE maps.
3273 RemoveNodeFromCSEMaps(U);
3274
Chris Lattner65113b22005-11-08 22:07:03 +00003275 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3276 I != E; ++I)
3277 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003278 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003279 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003280 To->addUser(U);
3281 }
3282
3283 // Now that we have modified U, add it back to the CSE maps. If it already
3284 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003285 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003286 ReplaceAllUsesWith(U, Existing, UpdateListener);
3287 // U is now dead. Inform the listener if it exists and delete it.
3288 if (UpdateListener)
3289 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003290 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003291 } else {
3292 // If the node doesn't already exist, we updated it. Inform a listener if
3293 // it exists.
3294 if (UpdateListener)
3295 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003296 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003297 }
3298}
3299
Chris Lattner8b52f212005-08-26 18:36:28 +00003300/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3301/// This can cause recursive merging of nodes in the DAG.
3302///
3303/// This version can replace From with any result values. To must match the
3304/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003305void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003306 const SDOperand *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003307 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003308 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003309 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003310
3311 while (!From->use_empty()) {
3312 // Process users until they are all gone.
3313 SDNode *U = *From->use_begin();
3314
3315 // This node is about to morph, remove its old self from the CSE maps.
3316 RemoveNodeFromCSEMaps(U);
3317
Chris Lattner65113b22005-11-08 22:07:03 +00003318 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3319 I != E; ++I)
3320 if (I->Val == From) {
3321 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003322 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003323 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003324 ToOp.Val->addUser(U);
3325 }
3326
3327 // Now that we have modified U, add it back to the CSE maps. If it already
3328 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003329 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003330 ReplaceAllUsesWith(U, Existing, UpdateListener);
3331 // U is now dead. Inform the listener if it exists and delete it.
3332 if (UpdateListener)
3333 UpdateListener->NodeDeleted(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003334 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003335 } else {
3336 // If the node doesn't already exist, we updated it. Inform a listener if
3337 // it exists.
3338 if (UpdateListener)
3339 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003340 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003341 }
3342}
3343
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003344namespace {
3345 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
3346 /// any deleted nodes from the set passed into its constructor and recursively
3347 /// notifies another update listener if specified.
3348 class ChainedSetUpdaterListener :
3349 public SelectionDAG::DAGUpdateListener {
3350 SmallSetVector<SDNode*, 16> &Set;
3351 SelectionDAG::DAGUpdateListener *Chain;
3352 public:
3353 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
3354 SelectionDAG::DAGUpdateListener *chain)
3355 : Set(set), Chain(chain) {}
3356
3357 virtual void NodeDeleted(SDNode *N) {
3358 Set.remove(N);
3359 if (Chain) Chain->NodeDeleted(N);
3360 }
3361 virtual void NodeUpdated(SDNode *N) {
3362 if (Chain) Chain->NodeUpdated(N);
3363 }
3364 };
3365}
3366
Chris Lattner012f2412006-02-17 21:58:01 +00003367/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3368/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003369/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00003370void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003371 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00003372 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003373
Chris Lattner012f2412006-02-17 21:58:01 +00003374 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00003375 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003376 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00003377 return;
3378 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003379
3380 if (From.use_empty()) return;
3381
Chris Lattnercf5640b2007-02-04 00:14:31 +00003382 // Get all of the users of From.Val. We want these in a nice,
3383 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3384 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003385
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003386 // When one of the recursive merges deletes nodes from the graph, we need to
3387 // make sure that UpdateListener is notified *and* that the node is removed
3388 // from Users if present. CSUL does this.
3389 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00003390
Chris Lattner012f2412006-02-17 21:58:01 +00003391 while (!Users.empty()) {
3392 // We know that this user uses some value of From. If it is the right
3393 // value, update it.
3394 SDNode *User = Users.back();
3395 Users.pop_back();
3396
Chris Lattner01d029b2007-10-15 06:10:22 +00003397 // Scan for an operand that matches From.
3398 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3399 for (; Op != E; ++Op)
3400 if (*Op == From) break;
3401
3402 // If there are no matches, the user must use some other result of From.
3403 if (Op == E) continue;
3404
3405 // Okay, we know this user needs to be updated. Remove its old self
3406 // from the CSE maps.
3407 RemoveNodeFromCSEMaps(User);
3408
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003409 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00003410 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003411 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003412 From.Val->removeUser(User);
3413 *Op = To;
3414 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003415 }
3416 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003417
3418 // Now that we have modified User, add it back to the CSE maps. If it
3419 // already exists there, recursively merge the results together.
3420 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003421 if (!Existing) {
3422 if (UpdateListener) UpdateListener->NodeUpdated(User);
3423 continue; // Continue on to next user.
3424 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003425
3426 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003427 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00003428 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003429 // can cause deletion of nodes that used the old value. To handle this, we
3430 // use CSUL to remove them from the Users set.
3431 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00003432
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003433 // User is now dead. Notify a listener if present.
3434 if (UpdateListener) UpdateListener->NodeDeleted(User);
Chris Lattner01d029b2007-10-15 06:10:22 +00003435 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003436 }
3437}
3438
Chris Lattner7b2880c2005-08-24 22:44:39 +00003439
Evan Chenge6f35d82006-08-01 08:20:41 +00003440/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3441/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003442unsigned SelectionDAG::AssignNodeIds() {
3443 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003444 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3445 SDNode *N = I;
3446 N->setNodeId(Id++);
3447 }
3448 return Id;
3449}
3450
Evan Chenge6f35d82006-08-01 08:20:41 +00003451/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003452/// based on their topological order. It returns the maximum id and a vector
3453/// of the SDNodes* in assigned order by reference.
3454unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003455 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003456 std::vector<unsigned> InDegree(DAGSize);
3457 std::vector<SDNode*> Sources;
3458
3459 // Use a two pass approach to avoid using a std::map which is slow.
3460 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003461 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3462 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003463 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003464 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003465 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003466 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003467 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003468 }
3469
Evan Cheng99157a02006-08-07 22:13:29 +00003470 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003471 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003472 SDNode *N = Sources.back();
3473 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003474 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003475 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3476 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003477 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003478 if (Degree == 0)
3479 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003480 }
3481 }
3482
Evan Chengc384d6c2006-08-02 22:00:34 +00003483 // Second pass, assign the actual topological order as node ids.
3484 Id = 0;
3485 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3486 TI != TE; ++TI)
3487 (*TI)->setNodeId(Id++);
3488
3489 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003490}
3491
3492
Evan Cheng091cba12006-07-27 06:39:06 +00003493
Jim Laskey58b968b2005-08-17 20:08:02 +00003494//===----------------------------------------------------------------------===//
3495// SDNode Class
3496//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003497
Chris Lattner917d2c92006-07-19 00:00:37 +00003498// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003499void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003500void UnarySDNode::ANCHOR() {}
3501void BinarySDNode::ANCHOR() {}
3502void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003503void HandleSDNode::ANCHOR() {}
3504void StringSDNode::ANCHOR() {}
3505void ConstantSDNode::ANCHOR() {}
3506void ConstantFPSDNode::ANCHOR() {}
3507void GlobalAddressSDNode::ANCHOR() {}
3508void FrameIndexSDNode::ANCHOR() {}
3509void JumpTableSDNode::ANCHOR() {}
3510void ConstantPoolSDNode::ANCHOR() {}
3511void BasicBlockSDNode::ANCHOR() {}
3512void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00003513void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003514void RegisterSDNode::ANCHOR() {}
3515void ExternalSymbolSDNode::ANCHOR() {}
3516void CondCodeSDNode::ANCHOR() {}
3517void VTSDNode::ANCHOR() {}
3518void LoadSDNode::ANCHOR() {}
3519void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003520
Chris Lattner48b85922007-02-04 02:41:42 +00003521HandleSDNode::~HandleSDNode() {
3522 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003523 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003524}
3525
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003526GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3527 MVT::ValueType VT, int o)
3528 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003529 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003530 // Thread Local
3531 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3532 // Non Thread Local
3533 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3534 getSDVTList(VT)), Offset(o) {
3535 TheGlobal = const_cast<GlobalValue*>(GA);
3536}
Chris Lattner48b85922007-02-04 02:41:42 +00003537
Dan Gohman69de1932008-02-06 22:27:42 +00003538/// getMemOperand - Return a MemOperand object describing the memory
3539/// reference performed by this load or store.
3540MemOperand LSBaseSDNode::getMemOperand() const {
3541 int Size = (MVT::getSizeInBits(getMemoryVT()) + 7) >> 3;
3542 int Flags =
3543 getOpcode() == ISD::LOAD ? MemOperand::MOLoad : MemOperand::MOStore;
3544 if (IsVolatile) Flags |= MemOperand::MOVolatile;
3545
3546 // Check if the load references a frame index, and does not have
3547 // an SV attached.
3548 const FrameIndexSDNode *FI =
3549 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
3550 if (!getSrcValue() && FI)
Dan Gohman3069b872008-02-07 18:41:25 +00003551 return MemOperand(PseudoSourceValue::getFixedStack(), Flags,
Dan Gohman69de1932008-02-06 22:27:42 +00003552 FI->getIndex(), Size, Alignment);
3553 else
3554 return MemOperand(getSrcValue(), Flags,
3555 getSrcValueOffset(), Size, Alignment);
3556}
3557
Jim Laskey583bd472006-10-27 23:46:08 +00003558/// Profile - Gather unique data for the node.
3559///
3560void SDNode::Profile(FoldingSetNodeID &ID) {
3561 AddNodeIDNode(ID, this);
3562}
3563
Chris Lattnera3255112005-11-08 23:30:28 +00003564/// getValueTypeList - Return a pointer to the specified value type.
3565///
Dan Gohman547ca532008-02-08 03:26:46 +00003566const MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003567 if (MVT::isExtendedVT(VT)) {
3568 static std::set<MVT::ValueType> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00003569 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00003570 } else {
3571 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3572 VTs[VT] = VT;
3573 return &VTs[VT];
3574 }
Chris Lattnera3255112005-11-08 23:30:28 +00003575}
Duncan Sandsaf47b112007-10-16 09:56:48 +00003576
Chris Lattner5c884562005-01-12 18:37:47 +00003577/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3578/// indicated value. This method ignores uses of other values defined by this
3579/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003580bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003581 assert(Value < getNumValues() && "Bad value!");
3582
3583 // If there is only one value, this is easy.
3584 if (getNumValues() == 1)
3585 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003586 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003587
Evan Cheng4ee62112006-02-05 06:29:23 +00003588 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003589
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003590 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003591
Chris Lattnerf83482d2006-08-16 20:59:32 +00003592 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003593 SDNode *User = *UI;
3594 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003595 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003596 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3597 if (User->getOperand(i) == TheValue) {
3598 if (NUses == 0)
3599 return false; // too many uses
3600 --NUses;
3601 }
3602 }
3603
3604 // Found exactly the right number of uses?
3605 return NUses == 0;
3606}
3607
3608
Evan Cheng33d55952007-08-02 05:29:38 +00003609/// hasAnyUseOfValue - Return true if there are any use of the indicated
3610/// value. This method ignores uses of other values defined by this operation.
3611bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3612 assert(Value < getNumValues() && "Bad value!");
3613
Dan Gohman30359592008-01-29 13:02:09 +00003614 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00003615
3616 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3617
3618 SmallPtrSet<SDNode*, 32> UsersHandled;
3619
3620 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3621 SDNode *User = *UI;
3622 if (User->getNumOperands() == 1 ||
3623 UsersHandled.insert(User)) // First time we've seen this?
3624 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3625 if (User->getOperand(i) == TheValue) {
3626 return true;
3627 }
3628 }
3629
3630 return false;
3631}
3632
3633
Evan Chenge6e97e62006-11-03 07:31:32 +00003634/// isOnlyUse - Return true if this node is the only use of N.
3635///
Evan Cheng4ee62112006-02-05 06:29:23 +00003636bool SDNode::isOnlyUse(SDNode *N) const {
3637 bool Seen = false;
3638 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3639 SDNode *User = *I;
3640 if (User == this)
3641 Seen = true;
3642 else
3643 return false;
3644 }
3645
3646 return Seen;
3647}
3648
Evan Chenge6e97e62006-11-03 07:31:32 +00003649/// isOperand - Return true if this node is an operand of N.
3650///
Evan Chengbfa284f2006-03-03 06:42:32 +00003651bool SDOperand::isOperand(SDNode *N) const {
3652 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3653 if (*this == N->getOperand(i))
3654 return true;
3655 return false;
3656}
3657
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003658bool SDNode::isOperand(SDNode *N) const {
3659 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3660 if (this == N->OperandList[i].Val)
3661 return true;
3662 return false;
3663}
Evan Cheng4ee62112006-02-05 06:29:23 +00003664
Chris Lattner572dee72008-01-16 05:49:24 +00003665/// reachesChainWithoutSideEffects - Return true if this operand (which must
3666/// be a chain) reaches the specified operand without crossing any
3667/// side-effecting instructions. In practice, this looks through token
3668/// factors and non-volatile loads. In order to remain efficient, this only
3669/// looks a couple of nodes in, it does not do an exhaustive search.
3670bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
3671 unsigned Depth) const {
3672 if (*this == Dest) return true;
3673
3674 // Don't search too deeply, we just want to be able to see through
3675 // TokenFactor's etc.
3676 if (Depth == 0) return false;
3677
3678 // If this is a token factor, all inputs to the TF happen in parallel. If any
3679 // of the operands of the TF reach dest, then we can do the xform.
3680 if (getOpcode() == ISD::TokenFactor) {
3681 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
3682 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
3683 return true;
3684 return false;
3685 }
3686
3687 // Loads don't have side effects, look through them.
3688 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
3689 if (!Ld->isVolatile())
3690 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
3691 }
3692 return false;
3693}
3694
3695
Evan Chengc5fc57d2006-11-03 03:05:24 +00003696static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003697 SmallPtrSet<SDNode *, 32> &Visited) {
3698 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003699 return;
3700
3701 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3702 SDNode *Op = N->getOperand(i).Val;
3703 if (Op == P) {
3704 found = true;
3705 return;
3706 }
3707 findPredecessor(Op, P, found, Visited);
3708 }
3709}
3710
Evan Chenge6e97e62006-11-03 07:31:32 +00003711/// isPredecessor - Return true if this node is a predecessor of N. This node
3712/// is either an operand of N or it can be reached by recursively traversing
3713/// up the operands.
3714/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003715bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003716 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003717 bool found = false;
3718 findPredecessor(N, this, found, Visited);
3719 return found;
3720}
3721
Evan Chengc5484282006-10-04 00:56:09 +00003722uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3723 assert(Num < NumOperands && "Invalid child # of SDNode!");
3724 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3725}
3726
Reid Spencer577cc322007-04-01 07:32:19 +00003727std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003728 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003729 default:
3730 if (getOpcode() < ISD::BUILTIN_OP_END)
3731 return "<<Unknown DAG Node>>";
3732 else {
Evan Cheng72261582005-12-20 06:22:03 +00003733 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003734 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003735 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00003736 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00003737
Evan Cheng72261582005-12-20 06:22:03 +00003738 TargetLowering &TLI = G->getTargetLoweringInfo();
3739 const char *Name =
3740 TLI.getTargetNodeName(getOpcode());
3741 if (Name) return Name;
3742 }
3743
3744 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003745 }
3746
Andrew Lenharth95762122005-03-31 21:24:06 +00003747 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003748 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003749 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00003750 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003751 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003752 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003753 case ISD::AssertSext: return "AssertSext";
3754 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003755
3756 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003757 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003758 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003759 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003760
3761 case ISD::Constant: return "Constant";
3762 case ISD::ConstantFP: return "ConstantFP";
3763 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003764 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003765 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003766 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003767 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003768 case ISD::RETURNADDR: return "RETURNADDR";
3769 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003770 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003771 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3772 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003773 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003774 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003775 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003776 case ISD::INTRINSIC_WO_CHAIN: {
3777 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3778 return Intrinsic::getName((Intrinsic::ID)IID);
3779 }
3780 case ISD::INTRINSIC_VOID:
3781 case ISD::INTRINSIC_W_CHAIN: {
3782 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003783 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003784 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003785
Chris Lattnerb2827b02006-03-19 00:52:58 +00003786 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003787 case ISD::TargetConstant: return "TargetConstant";
3788 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003789 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003790 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003791 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003792 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003793 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003794 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003795
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003796 case ISD::CopyToReg: return "CopyToReg";
3797 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003798 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003799 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003800 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003801 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00003802 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003803 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003804 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003805 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003806
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003807 // Unary operators
3808 case ISD::FABS: return "fabs";
3809 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003810 case ISD::FSQRT: return "fsqrt";
3811 case ISD::FSIN: return "fsin";
3812 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003813 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003814 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003815
3816 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003817 case ISD::ADD: return "add";
3818 case ISD::SUB: return "sub";
3819 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003820 case ISD::MULHU: return "mulhu";
3821 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003822 case ISD::SDIV: return "sdiv";
3823 case ISD::UDIV: return "udiv";
3824 case ISD::SREM: return "srem";
3825 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003826 case ISD::SMUL_LOHI: return "smul_lohi";
3827 case ISD::UMUL_LOHI: return "umul_lohi";
3828 case ISD::SDIVREM: return "sdivrem";
3829 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003830 case ISD::AND: return "and";
3831 case ISD::OR: return "or";
3832 case ISD::XOR: return "xor";
3833 case ISD::SHL: return "shl";
3834 case ISD::SRA: return "sra";
3835 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003836 case ISD::ROTL: return "rotl";
3837 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003838 case ISD::FADD: return "fadd";
3839 case ISD::FSUB: return "fsub";
3840 case ISD::FMUL: return "fmul";
3841 case ISD::FDIV: return "fdiv";
3842 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003843 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00003844 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003845
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003846 case ISD::SETCC: return "setcc";
3847 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003848 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003849 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003850 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003851 case ISD::CONCAT_VECTORS: return "concat_vectors";
3852 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003853 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003854 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003855 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003856 case ISD::ADDC: return "addc";
3857 case ISD::ADDE: return "adde";
3858 case ISD::SUBC: return "subc";
3859 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003860 case ISD::SHL_PARTS: return "shl_parts";
3861 case ISD::SRA_PARTS: return "sra_parts";
3862 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003863
3864 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3865 case ISD::INSERT_SUBREG: return "insert_subreg";
3866
Chris Lattner7f644642005-04-28 21:44:03 +00003867 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003868 case ISD::SIGN_EXTEND: return "sign_extend";
3869 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003870 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003871 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003872 case ISD::TRUNCATE: return "truncate";
3873 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00003874 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00003875 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003876 case ISD::FP_EXTEND: return "fp_extend";
3877
3878 case ISD::SINT_TO_FP: return "sint_to_fp";
3879 case ISD::UINT_TO_FP: return "uint_to_fp";
3880 case ISD::FP_TO_SINT: return "fp_to_sint";
3881 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003882 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003883
3884 // Control flow instructions
3885 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003886 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003887 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003888 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003889 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003890 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003891 case ISD::CALLSEQ_START: return "callseq_start";
3892 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003893
3894 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003895 case ISD::LOAD: return "load";
3896 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003897 case ISD::VAARG: return "vaarg";
3898 case ISD::VACOPY: return "vacopy";
3899 case ISD::VAEND: return "vaend";
3900 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003901 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003902 case ISD::EXTRACT_ELEMENT: return "extract_element";
3903 case ISD::BUILD_PAIR: return "build_pair";
3904 case ISD::STACKSAVE: return "stacksave";
3905 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003906 case ISD::TRAP: return "trap";
3907
Chris Lattner5a67afc2006-01-13 02:39:42 +00003908 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003909 case ISD::MEMSET: return "memset";
3910 case ISD::MEMCPY: return "memcpy";
3911 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003912
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003913 // Bit manipulation
3914 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003915 case ISD::CTPOP: return "ctpop";
3916 case ISD::CTTZ: return "cttz";
3917 case ISD::CTLZ: return "ctlz";
3918
Chris Lattner36ce6912005-11-29 06:21:05 +00003919 // Debug info
3920 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003921 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003922
Duncan Sands36397f52007-07-27 12:58:54 +00003923 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003924 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003925
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003926 case ISD::CONDCODE:
3927 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003928 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003929 case ISD::SETOEQ: return "setoeq";
3930 case ISD::SETOGT: return "setogt";
3931 case ISD::SETOGE: return "setoge";
3932 case ISD::SETOLT: return "setolt";
3933 case ISD::SETOLE: return "setole";
3934 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003935
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003936 case ISD::SETO: return "seto";
3937 case ISD::SETUO: return "setuo";
3938 case ISD::SETUEQ: return "setue";
3939 case ISD::SETUGT: return "setugt";
3940 case ISD::SETUGE: return "setuge";
3941 case ISD::SETULT: return "setult";
3942 case ISD::SETULE: return "setule";
3943 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003944
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003945 case ISD::SETEQ: return "seteq";
3946 case ISD::SETGT: return "setgt";
3947 case ISD::SETGE: return "setge";
3948 case ISD::SETLT: return "setlt";
3949 case ISD::SETLE: return "setle";
3950 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003951 }
3952 }
3953}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003954
Evan Cheng144d8f02006-11-09 17:55:04 +00003955const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003956 switch (AM) {
3957 default:
3958 return "";
3959 case ISD::PRE_INC:
3960 return "<pre-inc>";
3961 case ISD::PRE_DEC:
3962 return "<pre-dec>";
3963 case ISD::POST_INC:
3964 return "<post-inc>";
3965 case ISD::POST_DEC:
3966 return "<post-dec>";
3967 }
3968}
3969
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003970void SDNode::dump() const { dump(0); }
3971void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003972 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003973
3974 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003975 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003976 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003977 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003978 else
Bill Wendling832171c2006-12-07 20:04:42 +00003979 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003980 }
Bill Wendling832171c2006-12-07 20:04:42 +00003981 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003982
Bill Wendling832171c2006-12-07 20:04:42 +00003983 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003984 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003985 if (i) cerr << ", ";
3986 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003987 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003988 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003989 }
3990
Evan Chengce254432007-12-11 02:08:35 +00003991 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
3992 SDNode *Mask = getOperand(2).Val;
3993 cerr << "<";
3994 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
3995 if (i) cerr << ",";
3996 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
3997 cerr << "u";
3998 else
3999 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4000 }
4001 cerr << ">";
4002 }
4003
Chris Lattnerc3aae252005-01-07 07:46:32 +00004004 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004005 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004006 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004007 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4008 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4009 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4010 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4011 else {
4012 cerr << "<APFloat(";
4013 CSDN->getValueAPF().convertToAPInt().dump();
4014 cerr << ")>";
4015 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004016 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004017 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004018 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004019 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004020 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004021 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004022 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004023 else
Bill Wendling832171c2006-12-07 20:04:42 +00004024 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004025 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004026 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004027 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004028 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004029 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004030 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004031 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004032 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004033 else
Bill Wendling832171c2006-12-07 20:04:42 +00004034 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004035 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004036 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004037 else
Bill Wendling832171c2006-12-07 20:04:42 +00004038 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004039 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004040 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004041 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4042 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004043 cerr << LBB->getName() << " ";
4044 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004045 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00004046 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00004047 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004048 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004049 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004050 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004051 } else if (const ExternalSymbolSDNode *ES =
4052 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004053 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004054 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4055 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004056 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004057 else
Dan Gohman69de1932008-02-06 22:27:42 +00004058 cerr << "<null>";
4059 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4060 if (M->MO.getValue())
4061 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4062 else
4063 cerr << "<null:" << M->MO.getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00004064 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00004065 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004066 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004067 const Value *SrcValue = LD->getSrcValue();
4068 int SrcOffset = LD->getSrcValueOffset();
4069 cerr << " <";
4070 if (SrcValue)
4071 cerr << SrcValue;
4072 else
4073 cerr << "null";
4074 cerr << ":" << SrcOffset << ">";
4075
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004076 bool doExt = true;
4077 switch (LD->getExtensionType()) {
4078 default: doExt = false; break;
4079 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004080 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004081 break;
4082 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004083 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004084 break;
4085 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004086 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004087 break;
4088 }
4089 if (doExt)
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004090 cerr << MVT::getValueTypeString(LD->getMemoryVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004091
Evan Cheng144d8f02006-11-09 17:55:04 +00004092 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004093 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004094 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004095 if (LD->isVolatile())
4096 cerr << " <volatile>";
4097 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004098 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004099 const Value *SrcValue = ST->getSrcValue();
4100 int SrcOffset = ST->getSrcValueOffset();
4101 cerr << " <";
4102 if (SrcValue)
4103 cerr << SrcValue;
4104 else
4105 cerr << "null";
4106 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004107
4108 if (ST->isTruncatingStore())
4109 cerr << " <trunc "
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004110 << MVT::getValueTypeString(ST->getMemoryVT()) << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004111
4112 const char *AM = getIndexedModeName(ST->getAddressingMode());
4113 if (*AM)
4114 cerr << " " << AM;
4115 if (ST->isVolatile())
4116 cerr << " <volatile>";
4117 cerr << " alignment=" << ST->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004118 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004119}
4120
Chris Lattnerde202b32005-11-09 23:47:37 +00004121static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004122 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4123 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004124 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004125 else
Bill Wendling832171c2006-12-07 20:04:42 +00004126 cerr << "\n" << std::string(indent+2, ' ')
4127 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004128
Chris Lattnerea946cd2005-01-09 20:38:33 +00004129
Bill Wendling832171c2006-12-07 20:04:42 +00004130 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004131 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004132}
4133
Chris Lattnerc3aae252005-01-07 07:46:32 +00004134void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004135 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004136 std::vector<const SDNode*> Nodes;
4137 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4138 I != E; ++I)
4139 Nodes.push_back(I);
4140
Chris Lattner49d24712005-01-09 20:26:36 +00004141 std::sort(Nodes.begin(), Nodes.end());
4142
4143 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004144 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004145 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004146 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004147
Jim Laskey26f7fa72006-10-17 19:33:52 +00004148 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004149
Bill Wendling832171c2006-12-07 20:04:42 +00004150 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004151}
4152
Evan Chengd6594ae2006-09-12 21:00:35 +00004153const Type *ConstantPoolSDNode::getType() const {
4154 if (isMachineConstantPoolEntry())
4155 return Val.MachineCPVal->getType();
4156 return Val.ConstVal->getType();
4157}