blob: 78281aa63263866e1e891794b6cca71b4f538bd1 [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//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000015#include "llvm/GlobalAlias.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"
Dan Gohman707e0182008-04-12 04:36:06 +000020#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000026#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000032#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000033#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000034#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000035#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000036#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000037#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000038#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000039using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Chris Lattner0b3e5252006-08-15 19:11:05 +000041/// makeVTList - Return an instance of the SDVTList struct initialized with the
42/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000043static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000044 SDVTList Res = {VTs, NumVTs};
45 return Res;
46}
47
Duncan Sands83ec4b62008-06-06 12:08:01 +000048static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
49 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000050 default: assert(0 && "Unknown FP format");
51 case MVT::f32: return &APFloat::IEEEsingle;
52 case MVT::f64: return &APFloat::IEEEdouble;
53 case MVT::f80: return &APFloat::x87DoubleExtended;
54 case MVT::f128: return &APFloat::IEEEquad;
55 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
56 }
57}
58
Chris Lattnerf8dc0612008-02-03 06:49:24 +000059SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
60
Jim Laskey58b968b2005-08-17 20:08:02 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000069bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000070 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000071}
72
Duncan Sands83ec4b62008-06-06 12:08:01 +000073bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000074 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000075 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000076
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000077 // PPC long double cannot be converted to any other type.
78 if (VT == MVT::ppcf128 ||
79 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000080 return false;
81
Dale Johannesenf04afdb2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000084 return Val2.convert(*MVTToAPFloatSemantics(VT),
85 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000086}
87
Jim Laskey58b968b2005-08-17 20:08:02 +000088//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000089// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000091
Evan Chenga8df1662006-03-27 06:58:47 +000092/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000093/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000094bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000095 // Look through a bit convert.
96 if (N->getOpcode() == ISD::BIT_CONVERT)
97 N = N->getOperand(0).Val;
98
Evan Chenga8df1662006-03-27 06:58:47 +000099 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000100
101 unsigned i = 0, e = N->getNumOperands();
102
103 // Skip over all of the undef values.
104 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
105 ++i;
106
107 // Do not accept an all-undef vector.
108 if (i == e) return false;
109
110 // Do not accept build_vectors that aren't all constants or which have non-~0
111 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000112 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000113 if (isa<ConstantSDNode>(NotZero)) {
114 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
115 return false;
116 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000117 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
118 convertToAPInt().isAllOnesValue())
119 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000120 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000121 return false;
122
123 // Okay, we have at least one ~0 value, check to see if the rest match or are
124 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000125 for (++i; i != e; ++i)
126 if (N->getOperand(i) != NotZero &&
127 N->getOperand(i).getOpcode() != ISD::UNDEF)
128 return false;
129 return true;
130}
131
132
Evan Cheng4a147842006-03-26 09:50:58 +0000133/// isBuildVectorAllZeros - Return true if the specified node is a
134/// BUILD_VECTOR where all of the elements are 0 or undef.
135bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000136 // Look through a bit convert.
137 if (N->getOpcode() == ISD::BIT_CONVERT)
138 N = N->getOperand(0).Val;
139
Evan Cheng4a147842006-03-26 09:50:58 +0000140 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000141
142 unsigned i = 0, e = N->getNumOperands();
143
144 // Skip over all of the undef values.
145 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
146 ++i;
147
148 // Do not accept an all-undef vector.
149 if (i == e) return false;
150
151 // Do not accept build_vectors that aren't all constants or which have non-~0
152 // elements.
153 SDOperand Zero = N->getOperand(i);
154 if (isa<ConstantSDNode>(Zero)) {
155 if (!cast<ConstantSDNode>(Zero)->isNullValue())
156 return false;
157 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000158 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000159 return false;
160 } else
161 return false;
162
163 // Okay, we have at least one ~0 value, check to see if the rest match or are
164 // undefs.
165 for (++i; i != e; ++i)
166 if (N->getOperand(i) != Zero &&
167 N->getOperand(i).getOpcode() != ISD::UNDEF)
168 return false;
169 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000170}
171
Evan Chengefec7512008-02-18 23:04:32 +0000172/// isScalarToVector - Return true if the specified node is a
173/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
174/// element is not an undef.
175bool ISD::isScalarToVector(const SDNode *N) {
176 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
177 return true;
178
179 if (N->getOpcode() != ISD::BUILD_VECTOR)
180 return false;
181 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
182 return false;
183 unsigned NumElems = N->getNumOperands();
184 for (unsigned i = 1; i < NumElems; ++i) {
185 SDOperand V = N->getOperand(i);
186 if (V.getOpcode() != ISD::UNDEF)
187 return false;
188 }
189 return true;
190}
191
192
Evan Chengbb81d972008-01-31 09:59:15 +0000193/// isDebugLabel - Return true if the specified node represents a debug
Evan Chengfc718542008-02-04 23:10:38 +0000194/// label (i.e. ISD::LABEL or TargetInstrInfo::LABEL node and third operand
Evan Chengbb81d972008-01-31 09:59:15 +0000195/// is 0).
196bool ISD::isDebugLabel(const SDNode *N) {
197 SDOperand Zero;
198 if (N->getOpcode() == ISD::LABEL)
199 Zero = N->getOperand(2);
200 else if (N->isTargetOpcode() &&
201 N->getTargetOpcode() == TargetInstrInfo::LABEL)
202 // Chain moved to last operand.
203 Zero = N->getOperand(1);
204 else
205 return false;
206 return isa<ConstantSDNode>(Zero) && cast<ConstantSDNode>(Zero)->isNullValue();
207}
208
Chris Lattnerc3aae252005-01-07 07:46:32 +0000209/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
210/// when given the operation for (X op Y).
211ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
212 // To perform this operation, we just need to swap the L and G bits of the
213 // operation.
214 unsigned OldL = (Operation >> 2) & 1;
215 unsigned OldG = (Operation >> 1) & 1;
216 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
217 (OldL << 1) | // New G bit
218 (OldG << 2)); // New L bit.
219}
220
221/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
222/// 'op' is a valid SetCC operation.
223ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
224 unsigned Operation = Op;
225 if (isInteger)
226 Operation ^= 7; // Flip L, G, E bits, but not U.
227 else
228 Operation ^= 15; // Flip all of the condition bits.
229 if (Operation > ISD::SETTRUE2)
230 Operation &= ~8; // Don't let N and U bits get set.
231 return ISD::CondCode(Operation);
232}
233
234
235/// isSignedOp - For an integer comparison, return 1 if the comparison is a
236/// signed operation and 2 if the result is an unsigned comparison. Return zero
237/// if the operation does not depend on the sign of the input (setne and seteq).
238static int isSignedOp(ISD::CondCode Opcode) {
239 switch (Opcode) {
240 default: assert(0 && "Illegal integer setcc operation!");
241 case ISD::SETEQ:
242 case ISD::SETNE: return 0;
243 case ISD::SETLT:
244 case ISD::SETLE:
245 case ISD::SETGT:
246 case ISD::SETGE: return 1;
247 case ISD::SETULT:
248 case ISD::SETULE:
249 case ISD::SETUGT:
250 case ISD::SETUGE: return 2;
251 }
252}
253
254/// getSetCCOrOperation - Return the result of a logical OR between different
255/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
256/// returns SETCC_INVALID if it is not possible to represent the resultant
257/// comparison.
258ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
259 bool isInteger) {
260 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
261 // Cannot fold a signed integer setcc with an unsigned integer setcc.
262 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattnerc3aae252005-01-07 07:46:32 +0000264 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000265
Chris Lattnerc3aae252005-01-07 07:46:32 +0000266 // If the N and U bits get set then the resultant comparison DOES suddenly
267 // care about orderedness, and is true when ordered.
268 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000269 Op &= ~16; // Clear the U bit if the N bit is set.
270
271 // Canonicalize illegal integer setcc's.
272 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
273 Op = ISD::SETNE;
274
Chris Lattnerc3aae252005-01-07 07:46:32 +0000275 return ISD::CondCode(Op);
276}
277
278/// getSetCCAndOperation - Return the result of a logical AND between different
279/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
280/// function returns zero if it is not possible to represent the resultant
281/// comparison.
282ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
283 bool isInteger) {
284 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
285 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000286 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000287
288 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000289 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
290
291 // Canonicalize illegal integer setcc's.
292 if (isInteger) {
293 switch (Result) {
294 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000295 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000296 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000297 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
298 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
299 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000300 }
301 }
302
303 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000304}
305
Chris Lattnerb48da392005-01-23 04:39:44 +0000306const TargetMachine &SelectionDAG::getTarget() const {
307 return TLI.getTargetMachine();
308}
309
Jim Laskey58b968b2005-08-17 20:08:02 +0000310//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000311// SDNode Profile Support
312//===----------------------------------------------------------------------===//
313
Jim Laskeydef69b92006-10-27 23:52:51 +0000314/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
315///
Jim Laskey583bd472006-10-27 23:46:08 +0000316static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
317 ID.AddInteger(OpC);
318}
319
320/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
321/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000322static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000323 ID.AddPointer(VTList.VTs);
324}
325
Jim Laskeydef69b92006-10-27 23:52:51 +0000326/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
327///
Jim Laskey583bd472006-10-27 23:46:08 +0000328static void AddNodeIDOperands(FoldingSetNodeID &ID,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000329 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000330 for (; NumOps; --NumOps, ++Ops) {
331 ID.AddPointer(Ops->Val);
332 ID.AddInteger(Ops->ResNo);
333 }
Jim Laskey583bd472006-10-27 23:46:08 +0000334}
335
Jim Laskey583bd472006-10-27 23:46:08 +0000336static void AddNodeIDNode(FoldingSetNodeID &ID,
337 unsigned short OpC, SDVTList VTList,
Dan Gohman35b31be2008-04-17 23:02:12 +0000338 SDOperandPtr OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000339 AddNodeIDOpcode(ID, OpC);
340 AddNodeIDValueTypes(ID, VTList);
341 AddNodeIDOperands(ID, OpList, N);
342}
343
Roman Levenstein9cac5252008-04-16 16:15:27 +0000344
Jim Laskeydef69b92006-10-27 23:52:51 +0000345/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
346/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000347static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
348 AddNodeIDOpcode(ID, N->getOpcode());
349 // Add the return value info.
350 AddNodeIDValueTypes(ID, N->getVTList());
351 // Add the operand info.
352 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
353
354 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000355 switch (N->getOpcode()) {
356 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000357 case ISD::ARG_FLAGS:
358 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
359 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000360 case ISD::TargetConstant:
361 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000362 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000363 break;
364 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000365 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000366 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000368 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000370 case ISD::GlobalAddress:
371 case ISD::TargetGlobalTLSAddress:
372 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
374 ID.AddPointer(GA->getGlobal());
375 ID.AddInteger(GA->getOffset());
376 break;
377 }
378 case ISD::BasicBlock:
379 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
380 break;
381 case ISD::Register:
382 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
383 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000384 case ISD::SRCVALUE:
385 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
386 break;
387 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000388 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000389 ID.AddPointer(MO.getValue());
390 ID.AddInteger(MO.getFlags());
391 ID.AddInteger(MO.getOffset());
392 ID.AddInteger(MO.getSize());
393 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000394 break;
395 }
396 case ISD::FrameIndex:
397 case ISD::TargetFrameIndex:
398 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
399 break;
400 case ISD::JumpTable:
401 case ISD::TargetJumpTable:
402 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
403 break;
404 case ISD::ConstantPool:
405 case ISD::TargetConstantPool: {
406 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
407 ID.AddInteger(CP->getAlignment());
408 ID.AddInteger(CP->getOffset());
409 if (CP->isMachineConstantPoolEntry())
410 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
411 else
412 ID.AddPointer(CP->getConstVal());
413 break;
414 }
415 case ISD::LOAD: {
416 LoadSDNode *LD = cast<LoadSDNode>(N);
417 ID.AddInteger(LD->getAddressingMode());
418 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000419 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000420 ID.AddInteger(LD->getAlignment());
421 ID.AddInteger(LD->isVolatile());
422 break;
423 }
424 case ISD::STORE: {
425 StoreSDNode *ST = cast<StoreSDNode>(N);
426 ID.AddInteger(ST->getAddressingMode());
427 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000428 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000429 ID.AddInteger(ST->getAlignment());
430 ID.AddInteger(ST->isVolatile());
431 break;
432 }
Mon P Wang28873102008-06-25 08:15:39 +0000433 case ISD::ATOMIC_CMP_SWAP:
434 case ISD::ATOMIC_LOAD_ADD:
435 case ISD::ATOMIC_SWAP:
436 case ISD::ATOMIC_LOAD_SUB:
437 case ISD::ATOMIC_LOAD_AND:
438 case ISD::ATOMIC_LOAD_OR:
439 case ISD::ATOMIC_LOAD_XOR:
440 case ISD::ATOMIC_LOAD_NAND:
441 case ISD::ATOMIC_LOAD_MIN:
442 case ISD::ATOMIC_LOAD_MAX:
443 case ISD::ATOMIC_LOAD_UMIN:
444 case ISD::ATOMIC_LOAD_UMAX: {
445 AtomicSDNode *AT = cast<AtomicSDNode>(N);
446 ID.AddInteger(AT->getAlignment());
447 ID.AddInteger(AT->isVolatile());
448 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000449 }
Mon P Wang28873102008-06-25 08:15:39 +0000450 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000451}
452
453//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000454// SelectionDAG Class
455//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000456
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000457/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000458/// SelectionDAG.
459void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000460 // Create a dummy node (which is not added to allnodes), that adds a reference
461 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000462 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000463
Chris Lattner190a4182006-08-04 17:45:20 +0000464 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000465
Chris Lattner190a4182006-08-04 17:45:20 +0000466 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000467 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000468 if (I->use_empty())
469 DeadNodes.push_back(I);
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
477 // Take the node out of the appropriate CSE map.
478 RemoveNodeFromCSEMaps(N);
479
480 // Next, brutally remove the operand list. This is safe to do, as there are
481 // no cycles in the graph.
482 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000483 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000484 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000485
486 // Now that we removed this operand, see if there are no uses of it left.
487 if (Operand->use_empty())
488 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000489 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000490 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000491 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000492 }
Chris Lattner190a4182006-08-04 17:45:20 +0000493 N->OperandList = 0;
494 N->NumOperands = 0;
495
496 // Finally, remove N itself.
497 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000498 }
499
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000500 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000501 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000502}
503
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000504void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000505 SmallVector<SDNode*, 16> DeadNodes;
506 DeadNodes.push_back(N);
507
508 // Process the worklist, deleting the nodes and adding their uses to the
509 // worklist.
510 while (!DeadNodes.empty()) {
511 SDNode *N = DeadNodes.back();
512 DeadNodes.pop_back();
513
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000514 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +0000515 UpdateListener->NodeDeleted(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000516
Evan Cheng130a6472006-10-12 20:34:05 +0000517 // Take the node out of the appropriate CSE map.
518 RemoveNodeFromCSEMaps(N);
519
520 // Next, brutally remove the operand list. This is safe to do, as there are
521 // no cycles in the graph.
522 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000523 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000524 Operand->removeUser(std::distance(N->op_begin(), I), N);
Evan Cheng130a6472006-10-12 20:34:05 +0000525
526 // Now that we removed this operand, see if there are no uses of it left.
527 if (Operand->use_empty())
528 DeadNodes.push_back(Operand);
529 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000530 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000531 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000532 }
Evan Cheng130a6472006-10-12 20:34:05 +0000533 N->OperandList = 0;
534 N->NumOperands = 0;
535
536 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000537 AllNodes.erase(N);
538 }
539}
540
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000541void SelectionDAG::DeleteNode(SDNode *N) {
542 assert(N->use_empty() && "Cannot delete a node that is not dead!");
543
544 // First take this out of the appropriate CSE map.
545 RemoveNodeFromCSEMaps(N);
546
Chris Lattner1e111c72005-09-07 05:37:01 +0000547 // Finally, remove uses due to operands of this node, remove from the
548 // AllNodes list, and delete the node.
549 DeleteNodeNotInCSEMaps(N);
550}
551
552void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
553
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000554 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000555 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000556
557 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000558 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000559 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000560 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000561 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000562 }
Chris Lattner65113b22005-11-08 22:07:03 +0000563 N->OperandList = 0;
564 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000565
566 delete N;
567}
568
Chris Lattner149c58c2005-08-16 18:17:10 +0000569/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
570/// correspond to it. This is useful when we're about to delete or repurpose
571/// the node. We don't want future request for structurally identical nodes
572/// to return N anymore.
573void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000574 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000575 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000576 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000577 case ISD::STRING:
578 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
579 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000580 case ISD::CONDCODE:
581 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
582 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000583 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000584 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
585 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000586 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000587 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000588 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000589 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000590 Erased =
591 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000592 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000593 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000594 MVT VT = cast<VTSDNode>(N)->getVT();
595 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000596 Erased = ExtendedValueTypeNodes.erase(VT);
597 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000598 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
599 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000600 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000601 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000602 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000603 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000604 // Remove it from the CSE Map.
605 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000606 break;
607 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000608#ifndef NDEBUG
609 // Verify that the node was actually in one of the CSE maps, unless it has a
610 // flag result (which cannot be CSE'd) or is one of the special cases that are
611 // not subject to CSE.
612 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000613 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000614 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000615 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000616 assert(0 && "Node is not in map!");
617 }
618#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000619}
620
Chris Lattner8b8749f2005-08-17 19:00:20 +0000621/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
622/// has been taken out and modified in some way. If the specified node already
623/// exists in the CSE maps, do not modify the maps, but return the existing node
624/// instead. If it doesn't exist, add it and return null.
625///
626SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
627 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000628 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000629 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000630
Chris Lattner9f8cc692005-12-19 22:21:21 +0000631 // Check that remaining values produced are not flags.
632 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
633 if (N->getValueType(i) == MVT::Flag)
634 return 0; // Never CSE anything that produces a flag.
635
Chris Lattnera5682852006-08-07 23:03:03 +0000636 SDNode *New = CSEMap.GetOrInsertNode(N);
637 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000638 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000639}
640
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000641/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
642/// were replaced with those specified. If this node is never memoized,
643/// return null, otherwise return a pointer to the slot it would take. If a
644/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000645SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
646 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000647 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000648 return 0; // Never add these nodes.
649
650 // Check that remaining values produced are not flags.
651 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
652 if (N->getValueType(i) == MVT::Flag)
653 return 0; // Never CSE anything that produces a flag.
654
Chris Lattner63e3f142007-02-04 07:28:00 +0000655 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000656 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000657 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000658 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000659}
660
661/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
662/// were replaced with those specified. If this node is never memoized,
663/// return null, otherwise return a pointer to the slot it would take. If a
664/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000665SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
666 SDOperand Op1, SDOperand Op2,
667 void *&InsertPos) {
668 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
669 return 0; // Never add these nodes.
670
671 // Check that remaining values produced are not flags.
672 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
673 if (N->getValueType(i) == MVT::Flag)
674 return 0; // Never CSE anything that produces a flag.
675
Chris Lattner63e3f142007-02-04 07:28:00 +0000676 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000677 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000678 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000679 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
680}
681
682
683/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
684/// were replaced with those specified. If this node is never memoized,
685/// return null, otherwise return a pointer to the slot it would take. If a
686/// node already exists with these operands, the slot will be non-null.
687SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000688 SDOperandPtr Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000689 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000690 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000691 return 0; // Never add these nodes.
692
693 // Check that remaining values produced are not flags.
694 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
695 if (N->getValueType(i) == MVT::Flag)
696 return 0; // Never CSE anything that produces a flag.
697
Jim Laskey583bd472006-10-27 23:46:08 +0000698 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000699 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000700
Evan Cheng9629aba2006-10-11 01:47:58 +0000701 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
702 ID.AddInteger(LD->getAddressingMode());
703 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000704 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000705 ID.AddInteger(LD->getAlignment());
706 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000707 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
708 ID.AddInteger(ST->getAddressingMode());
709 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000710 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000711 ID.AddInteger(ST->getAlignment());
712 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000713 }
Jim Laskey583bd472006-10-27 23:46:08 +0000714
Chris Lattnera5682852006-08-07 23:03:03 +0000715 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000716}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000717
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000718
Chris Lattner78ec3112003-08-11 14:57:33 +0000719SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000720 while (!AllNodes.empty()) {
721 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000722 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000723 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000724 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000725 }
Chris Lattner65113b22005-11-08 22:07:03 +0000726 N->OperandList = 0;
727 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000728 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000729 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000730}
731
Duncan Sands83ec4b62008-06-06 12:08:01 +0000732SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000733 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000734 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000735 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000736 return getNode(ISD::AND, Op.getValueType(), Op,
737 getConstant(Imm, Op.getValueType()));
738}
739
Chris Lattner36ce6912005-11-29 06:21:05 +0000740SDOperand SelectionDAG::getString(const std::string &Val) {
741 StringSDNode *&N = StringNodes[Val];
742 if (!N) {
743 N = new StringSDNode(Val);
744 AllNodes.push_back(N);
745 }
746 return SDOperand(N, 0);
747}
748
Duncan Sands83ec4b62008-06-06 12:08:01 +0000749SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
750 MVT EltVT =
751 VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohman6394b092008-02-08 22:59:30 +0000752
Duncan Sands83ec4b62008-06-06 12:08:01 +0000753 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000754}
755
Duncan Sands83ec4b62008-06-06 12:08:01 +0000756SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
757 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000758
Duncan Sands83ec4b62008-06-06 12:08:01 +0000759 MVT EltVT =
760 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000761
Duncan Sands83ec4b62008-06-06 12:08:01 +0000762 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000763 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000764
765 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000766 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000767 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000768 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000769 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000770 SDNode *N = NULL;
771 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000772 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000773 return SDOperand(N, 0);
774 if (!N) {
775 N = new ConstantSDNode(isT, Val, EltVT);
776 CSEMap.InsertNode(N, IP);
777 AllNodes.push_back(N);
778 }
779
780 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000781 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000782 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000783 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000784 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
785 }
786 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000787}
788
Chris Lattner0bd48932008-01-17 07:00:52 +0000789SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
790 return getConstant(Val, TLI.getPointerTy(), isTarget);
791}
792
793
Duncan Sands83ec4b62008-06-06 12:08:01 +0000794SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
795 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000796
Duncan Sands83ec4b62008-06-06 12:08:01 +0000797 MVT EltVT =
798 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000799
Chris Lattnerd8658612005-02-17 20:17:32 +0000800 // Do the map lookup using the actual bit pattern for the floating point
801 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
802 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000803 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000804 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000805 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000806 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000807 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000808 SDNode *N = NULL;
809 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000810 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000811 return SDOperand(N, 0);
812 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000813 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000814 CSEMap.InsertNode(N, IP);
815 AllNodes.push_back(N);
816 }
817
Dan Gohman7f321562007-06-25 16:23:39 +0000818 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000819 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000820 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000821 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000822 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
823 }
824 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000825}
826
Duncan Sands83ec4b62008-06-06 12:08:01 +0000827SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
828 MVT EltVT =
829 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000830 if (EltVT==MVT::f32)
831 return getConstantFP(APFloat((float)Val), VT, isTarget);
832 else
833 return getConstantFP(APFloat(Val), VT, isTarget);
834}
835
Chris Lattnerc3aae252005-01-07 07:46:32 +0000836SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000837 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000838 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000839 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000840
841 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
842 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000843 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000844 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
845 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
846 }
847
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000848 if (GVar && GVar->isThreadLocal())
849 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
850 else
851 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000852
Jim Laskey583bd472006-10-27 23:46:08 +0000853 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000854 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000855 ID.AddPointer(GV);
856 ID.AddInteger(Offset);
857 void *IP = 0;
858 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
859 return SDOperand(E, 0);
860 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
861 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000862 AllNodes.push_back(N);
863 return SDOperand(N, 0);
864}
865
Duncan Sands83ec4b62008-06-06 12:08:01 +0000866SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000867 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000868 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000869 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000870 ID.AddInteger(FI);
871 void *IP = 0;
872 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
873 return SDOperand(E, 0);
874 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
875 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000876 AllNodes.push_back(N);
877 return SDOperand(N, 0);
878}
879
Duncan Sands83ec4b62008-06-06 12:08:01 +0000880SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000881 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000882 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000883 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000884 ID.AddInteger(JTI);
885 void *IP = 0;
886 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
887 return SDOperand(E, 0);
888 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
889 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000890 AllNodes.push_back(N);
891 return SDOperand(N, 0);
892}
893
Duncan Sands83ec4b62008-06-06 12:08:01 +0000894SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000895 unsigned Alignment, int Offset,
896 bool isTarget) {
897 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000898 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000899 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000900 ID.AddInteger(Alignment);
901 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000902 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000903 void *IP = 0;
904 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
905 return SDOperand(E, 0);
906 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
907 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000908 AllNodes.push_back(N);
909 return SDOperand(N, 0);
910}
911
Chris Lattnerc3aae252005-01-07 07:46:32 +0000912
Duncan Sands83ec4b62008-06-06 12:08:01 +0000913SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000914 unsigned Alignment, int Offset,
915 bool isTarget) {
916 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000917 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000918 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000919 ID.AddInteger(Alignment);
920 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000921 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000922 void *IP = 0;
923 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
924 return SDOperand(E, 0);
925 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
926 CSEMap.InsertNode(N, IP);
927 AllNodes.push_back(N);
928 return SDOperand(N, 0);
929}
930
931
Chris Lattnerc3aae252005-01-07 07:46:32 +0000932SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000933 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000934 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000935 ID.AddPointer(MBB);
936 void *IP = 0;
937 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
938 return SDOperand(E, 0);
939 SDNode *N = new BasicBlockSDNode(MBB);
940 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941 AllNodes.push_back(N);
942 return SDOperand(N, 0);
943}
944
Duncan Sands276dcbd2008-03-21 09:14:45 +0000945SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
946 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000947 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000948 ID.AddInteger(Flags.getRawBits());
949 void *IP = 0;
950 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
951 return SDOperand(E, 0);
952 SDNode *N = new ARG_FLAGSSDNode(Flags);
953 CSEMap.InsertNode(N, IP);
954 AllNodes.push_back(N);
955 return SDOperand(N, 0);
956}
957
Duncan Sands83ec4b62008-06-06 12:08:01 +0000958SDOperand SelectionDAG::getValueType(MVT VT) {
959 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
960 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000961
Duncan Sands83ec4b62008-06-06 12:08:01 +0000962 SDNode *&N = VT.isExtended() ?
963 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000964
965 if (N) return SDOperand(N, 0);
966 N = new VTSDNode(VT);
967 AllNodes.push_back(N);
968 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000969}
970
Duncan Sands83ec4b62008-06-06 12:08:01 +0000971SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000972 SDNode *&N = ExternalSymbols[Sym];
973 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000974 N = new ExternalSymbolSDNode(false, Sym, VT);
975 AllNodes.push_back(N);
976 return SDOperand(N, 0);
977}
978
Duncan Sands83ec4b62008-06-06 12:08:01 +0000979SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000980 SDNode *&N = TargetExternalSymbols[Sym];
981 if (N) return SDOperand(N, 0);
982 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983 AllNodes.push_back(N);
984 return SDOperand(N, 0);
985}
986
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000987SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
988 if ((unsigned)Cond >= CondCodeNodes.size())
989 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000990
Chris Lattner079a27a2005-08-09 20:40:02 +0000991 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000992 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000993 AllNodes.push_back(CondCodeNodes[Cond]);
994 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000995 return SDOperand(CondCodeNodes[Cond], 0);
996}
997
Duncan Sands83ec4b62008-06-06 12:08:01 +0000998SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000999 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001000 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001001 ID.AddInteger(RegNo);
1002 void *IP = 0;
1003 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1004 return SDOperand(E, 0);
1005 SDNode *N = new RegisterSDNode(RegNo, VT);
1006 CSEMap.InsertNode(N, IP);
1007 AllNodes.push_back(N);
1008 return SDOperand(N, 0);
1009}
1010
Dan Gohman69de1932008-02-06 22:27:42 +00001011SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001012 assert((!V || isa<PointerType>(V->getType())) &&
1013 "SrcValue is not a pointer?");
1014
Jim Laskey583bd472006-10-27 23:46:08 +00001015 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001016 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001017 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001018
Chris Lattner61b09412006-08-11 21:01:22 +00001019 void *IP = 0;
1020 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1021 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001022
1023 SDNode *N = new SrcValueSDNode(V);
1024 CSEMap.InsertNode(N, IP);
1025 AllNodes.push_back(N);
1026 return SDOperand(N, 0);
1027}
1028
Dan Gohman36b5c132008-04-07 19:35:22 +00001029SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001030 const Value *v = MO.getValue();
1031 assert((!v || isa<PointerType>(v->getType())) &&
1032 "SrcValue is not a pointer?");
1033
1034 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001035 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001036 ID.AddPointer(v);
1037 ID.AddInteger(MO.getFlags());
1038 ID.AddInteger(MO.getOffset());
1039 ID.AddInteger(MO.getSize());
1040 ID.AddInteger(MO.getAlignment());
1041
1042 void *IP = 0;
1043 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1044 return SDOperand(E, 0);
1045
1046 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001047 CSEMap.InsertNode(N, IP);
1048 AllNodes.push_back(N);
1049 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001050}
1051
Chris Lattner37ce9df2007-10-15 17:47:20 +00001052/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1053/// specified value type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001054SDOperand SelectionDAG::CreateStackTemporary(MVT VT) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001055 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001056 unsigned ByteSize = VT.getSizeInBits()/8;
1057 const Type *Ty = VT.getTypeForMVT();
Chris Lattner37ce9df2007-10-15 17:47:20 +00001058 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1059 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1060 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1061}
1062
1063
Duncan Sands83ec4b62008-06-06 12:08:01 +00001064SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001065 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066 // These setcc operations always fold.
1067 switch (Cond) {
1068 default: break;
1069 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001070 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001072 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001073
1074 case ISD::SETOEQ:
1075 case ISD::SETOGT:
1076 case ISD::SETOGE:
1077 case ISD::SETOLT:
1078 case ISD::SETOLE:
1079 case ISD::SETONE:
1080 case ISD::SETO:
1081 case ISD::SETUO:
1082 case ISD::SETUEQ:
1083 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001084 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001085 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001087
Chris Lattner67255a12005-04-07 18:14:58 +00001088 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001089 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001090 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001091 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001092
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 switch (Cond) {
1094 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001095 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1096 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001097 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1098 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1099 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1100 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1101 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1102 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1103 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1104 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106 }
Chris Lattner67255a12005-04-07 18:14:58 +00001107 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001108 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001109 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001110 // No compile time operations on this type yet.
1111 if (N1C->getValueType(0) == MVT::ppcf128)
1112 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001113
1114 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001115 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001116 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001117 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1118 return getNode(ISD::UNDEF, VT);
1119 // fall through
1120 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1121 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1122 return getNode(ISD::UNDEF, VT);
1123 // fall through
1124 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001125 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001126 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1127 return getNode(ISD::UNDEF, VT);
1128 // fall through
1129 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1130 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1131 return getNode(ISD::UNDEF, VT);
1132 // fall through
1133 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1134 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1135 return getNode(ISD::UNDEF, VT);
1136 // fall through
1137 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001138 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001139 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1140 return getNode(ISD::UNDEF, VT);
1141 // fall through
1142 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001143 R==APFloat::cmpEqual, VT);
1144 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1145 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1146 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1147 R==APFloat::cmpEqual, VT);
1148 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1149 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1150 R==APFloat::cmpLessThan, VT);
1151 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1152 R==APFloat::cmpUnordered, VT);
1153 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1154 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001155 }
1156 } else {
1157 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001158 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001159 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001160 }
1161
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001162 // Could not fold it.
1163 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001164}
1165
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001166/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1167/// use this predicate to simplify operations downstream.
1168bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1169 unsigned BitWidth = Op.getValueSizeInBits();
1170 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1171}
1172
Dan Gohmanea859be2007-06-22 14:59:07 +00001173/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1174/// this predicate to simplify operations downstream. Mask is known to be zero
1175/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001176bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001177 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001178 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001179 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1180 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1181 return (KnownZero & Mask) == Mask;
1182}
1183
1184/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1185/// known to be either zero or one and return them in the KnownZero/KnownOne
1186/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1187/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001188void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001189 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001190 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001191 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001192 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001193 "Mask size mismatches value type size!");
1194
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001195 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001196 if (Depth == 6 || Mask == 0)
1197 return; // Limit search depth.
1198
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001199 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001200
1201 switch (Op.getOpcode()) {
1202 case ISD::Constant:
1203 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001204 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001205 KnownZero = ~KnownOne & Mask;
1206 return;
1207 case ISD::AND:
1208 // If either the LHS or the RHS are Zero, the result is zero.
1209 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001210 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1211 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001212 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1213 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1214
1215 // Output known-1 bits are only known if set in both the LHS & RHS.
1216 KnownOne &= KnownOne2;
1217 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1218 KnownZero |= KnownZero2;
1219 return;
1220 case ISD::OR:
1221 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001222 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1223 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001224 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1225 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1226
1227 // Output known-0 bits are only known if clear in both the LHS & RHS.
1228 KnownZero &= KnownZero2;
1229 // Output known-1 are known to be set if set in either the LHS | RHS.
1230 KnownOne |= KnownOne2;
1231 return;
1232 case ISD::XOR: {
1233 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1234 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1235 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1236 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1237
1238 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001239 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001240 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1241 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1242 KnownZero = KnownZeroOut;
1243 return;
1244 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001245 case ISD::MUL: {
1246 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1247 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1248 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1249 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1250 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1251
1252 // If low bits are zero in either operand, output low known-0 bits.
1253 // Also compute a conserative estimate for high known-0 bits.
1254 // More trickiness is possible, but this is sufficient for the
1255 // interesting case of alignment computation.
1256 KnownOne.clear();
1257 unsigned TrailZ = KnownZero.countTrailingOnes() +
1258 KnownZero2.countTrailingOnes();
1259 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001260 KnownZero2.countLeadingOnes(),
1261 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001262
1263 TrailZ = std::min(TrailZ, BitWidth);
1264 LeadZ = std::min(LeadZ, BitWidth);
1265 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1266 APInt::getHighBitsSet(BitWidth, LeadZ);
1267 KnownZero &= Mask;
1268 return;
1269 }
1270 case ISD::UDIV: {
1271 // For the purposes of computing leading zeros we can conservatively
1272 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001273 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001274 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1275 ComputeMaskedBits(Op.getOperand(0),
1276 AllOnes, KnownZero2, KnownOne2, Depth+1);
1277 unsigned LeadZ = KnownZero2.countLeadingOnes();
1278
1279 KnownOne2.clear();
1280 KnownZero2.clear();
1281 ComputeMaskedBits(Op.getOperand(1),
1282 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001283 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1284 if (RHSUnknownLeadingOnes != BitWidth)
1285 LeadZ = std::min(BitWidth,
1286 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001287
1288 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1289 return;
1290 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001291 case ISD::SELECT:
1292 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1293 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1294 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1295 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1296
1297 // Only known if known in both the LHS and RHS.
1298 KnownOne &= KnownOne2;
1299 KnownZero &= KnownZero2;
1300 return;
1301 case ISD::SELECT_CC:
1302 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1303 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1304 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1305 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1306
1307 // Only known if known in both the LHS and RHS.
1308 KnownOne &= KnownOne2;
1309 KnownZero &= KnownZero2;
1310 return;
1311 case ISD::SETCC:
1312 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001313 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1314 BitWidth > 1)
1315 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001316 return;
1317 case ISD::SHL:
1318 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1319 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001320 unsigned ShAmt = SA->getValue();
1321
1322 // If the shift count is an invalid immediate, don't do anything.
1323 if (ShAmt >= BitWidth)
1324 return;
1325
1326 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001327 KnownZero, KnownOne, Depth+1);
1328 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001329 KnownZero <<= ShAmt;
1330 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001331 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001332 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001333 }
1334 return;
1335 case ISD::SRL:
1336 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1337 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001338 unsigned ShAmt = SA->getValue();
1339
Dan Gohmand4cf9922008-02-26 18:50:50 +00001340 // If the shift count is an invalid immediate, don't do anything.
1341 if (ShAmt >= BitWidth)
1342 return;
1343
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001344 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001345 KnownZero, KnownOne, Depth+1);
1346 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001347 KnownZero = KnownZero.lshr(ShAmt);
1348 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001349
Dan Gohman72d2fd52008-02-13 22:43:25 +00001350 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001351 KnownZero |= HighBits; // High bits known zero.
1352 }
1353 return;
1354 case ISD::SRA:
1355 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001356 unsigned ShAmt = SA->getValue();
1357
Dan Gohmand4cf9922008-02-26 18:50:50 +00001358 // If the shift count is an invalid immediate, don't do anything.
1359 if (ShAmt >= BitWidth)
1360 return;
1361
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001362 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001363 // If any of the demanded bits are produced by the sign extension, we also
1364 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001365 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1366 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001367 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001368
1369 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1370 Depth+1);
1371 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001372 KnownZero = KnownZero.lshr(ShAmt);
1373 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001374
1375 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001376 APInt SignBit = APInt::getSignBit(BitWidth);
1377 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001378
Dan Gohmanca93a432008-02-20 16:30:17 +00001379 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001380 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001381 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001382 KnownOne |= HighBits; // New bits are known one.
1383 }
1384 }
1385 return;
1386 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001387 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1388 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001389
1390 // Sign extension. Compute the demanded bits in the result that are not
1391 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001392 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001393
Dan Gohman977a76f2008-02-13 22:28:48 +00001394 APInt InSignBit = APInt::getSignBit(EBits);
1395 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001396
1397 // If the sign extended bits are demanded, we know that the sign
1398 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001399 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001400 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001401 InputDemandedBits |= InSignBit;
1402
1403 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1404 KnownZero, KnownOne, Depth+1);
1405 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1406
1407 // If the sign bit of the input is known set or clear, then we know the
1408 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001409 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001410 KnownZero |= NewBits;
1411 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001412 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001413 KnownOne |= NewBits;
1414 KnownZero &= ~NewBits;
1415 } else { // Input sign bit unknown
1416 KnownZero &= ~NewBits;
1417 KnownOne &= ~NewBits;
1418 }
1419 return;
1420 }
1421 case ISD::CTTZ:
1422 case ISD::CTLZ:
1423 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001424 unsigned LowBits = Log2_32(BitWidth)+1;
1425 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001426 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001427 return;
1428 }
1429 case ISD::LOAD: {
1430 if (ISD::isZEXTLoad(Op.Val)) {
1431 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001432 MVT VT = LD->getMemoryVT();
1433 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001434 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001435 }
1436 return;
1437 }
1438 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001439 MVT InVT = Op.getOperand(0).getValueType();
1440 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001441 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1442 APInt InMask = Mask;
1443 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001444 KnownZero.trunc(InBits);
1445 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001446 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001447 KnownZero.zext(BitWidth);
1448 KnownOne.zext(BitWidth);
1449 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001450 return;
1451 }
1452 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001453 MVT InVT = Op.getOperand(0).getValueType();
1454 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001455 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001456 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1457 APInt InMask = Mask;
1458 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001459
1460 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001461 // bit is demanded. Temporarily set this bit in the mask for our callee.
1462 if (NewBits.getBoolValue())
1463 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001464
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001465 KnownZero.trunc(InBits);
1466 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001467 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1468
1469 // Note if the sign bit is known to be zero or one.
1470 bool SignBitKnownZero = KnownZero.isNegative();
1471 bool SignBitKnownOne = KnownOne.isNegative();
1472 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1473 "Sign bit can't be known to be both zero and one!");
1474
1475 // If the sign bit wasn't actually demanded by our caller, we don't
1476 // want it set in the KnownZero and KnownOne result values. Reset the
1477 // mask and reapply it to the result values.
1478 InMask = Mask;
1479 InMask.trunc(InBits);
1480 KnownZero &= InMask;
1481 KnownOne &= InMask;
1482
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001483 KnownZero.zext(BitWidth);
1484 KnownOne.zext(BitWidth);
1485
Dan Gohman977a76f2008-02-13 22:28:48 +00001486 // If the sign bit is known zero or one, the top bits match.
1487 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001488 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001489 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001490 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001491 return;
1492 }
1493 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001494 MVT InVT = Op.getOperand(0).getValueType();
1495 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001496 APInt InMask = Mask;
1497 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001498 KnownZero.trunc(InBits);
1499 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001500 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001501 KnownZero.zext(BitWidth);
1502 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001503 return;
1504 }
1505 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001506 MVT InVT = Op.getOperand(0).getValueType();
1507 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001508 APInt InMask = Mask;
1509 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001510 KnownZero.zext(InBits);
1511 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001512 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001513 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001514 KnownZero.trunc(BitWidth);
1515 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001516 break;
1517 }
1518 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001519 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1520 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001521 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1522 KnownOne, Depth+1);
1523 KnownZero |= (~InMask) & Mask;
1524 return;
1525 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001526 case ISD::FGETSIGN:
1527 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001528 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001529 return;
1530
Dan Gohman23e8b712008-04-28 17:02:21 +00001531 case ISD::SUB: {
1532 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1533 // We know that the top bits of C-X are clear if X contains less bits
1534 // than C (i.e. no wrap-around can happen). For example, 20-X is
1535 // positive if we can prove that X is >= 0 and < 16.
1536 if (CLHS->getAPIntValue().isNonNegative()) {
1537 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1538 // NLZ can't be BitWidth with no sign bit
1539 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1540 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1541 Depth+1);
1542
1543 // If all of the MaskV bits are known to be zero, then we know the
1544 // output top bits are zero, because we now know that the output is
1545 // from [0-C].
1546 if ((KnownZero2 & MaskV) == MaskV) {
1547 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1548 // Top bits known zero.
1549 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1550 }
1551 }
1552 }
1553 }
1554 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001555 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001556 // Output known-0 bits are known if clear or set in both the low clear bits
1557 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1558 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001559 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1560 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1561 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1562 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1563
1564 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1565 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1566 KnownZeroOut = std::min(KnownZeroOut,
1567 KnownZero2.countTrailingOnes());
1568
1569 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001570 return;
1571 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001572 case ISD::SREM:
1573 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1574 APInt RA = Rem->getAPIntValue();
1575 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001576 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001577 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1578 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001579
Dan Gohman23e8b712008-04-28 17:02:21 +00001580 // The sign of a remainder is equal to the sign of the first
1581 // operand (zero being positive).
1582 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1583 KnownZero2 |= ~LowBits;
1584 else if (KnownOne2[BitWidth-1])
1585 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001586
Dan Gohman23e8b712008-04-28 17:02:21 +00001587 KnownZero |= KnownZero2 & Mask;
1588 KnownOne |= KnownOne2 & Mask;
1589
1590 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001591 }
1592 }
1593 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001594 case ISD::UREM: {
1595 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1596 APInt RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001597 if (RA.isPowerOf2()) {
1598 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001599 APInt Mask2 = LowBits & Mask;
1600 KnownZero |= ~LowBits & Mask;
1601 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1602 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1603 break;
1604 }
1605 }
1606
1607 // Since the result is less than or equal to either operand, any leading
1608 // zero bits in either operand must also exist in the result.
1609 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1610 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1611 Depth+1);
1612 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1613 Depth+1);
1614
1615 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1616 KnownZero2.countLeadingOnes());
1617 KnownOne.clear();
1618 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1619 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001620 }
1621 default:
1622 // Allow the target to implement this method for its nodes.
1623 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1624 case ISD::INTRINSIC_WO_CHAIN:
1625 case ISD::INTRINSIC_W_CHAIN:
1626 case ISD::INTRINSIC_VOID:
1627 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1628 }
1629 return;
1630 }
1631}
1632
1633/// ComputeNumSignBits - Return the number of times the sign bit of the
1634/// register is replicated into the other bits. We know that at least 1 bit
1635/// is always equal to the sign bit (itself), but other cases can give us
1636/// information. For example, immediately after an "SRA X, 2", we know that
1637/// the top 3 bits are all equal to each other, so we return 3.
1638unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001639 MVT VT = Op.getValueType();
1640 assert(VT.isInteger() && "Invalid VT!");
1641 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001642 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001643 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001644
1645 if (Depth == 6)
1646 return 1; // Limit search depth.
1647
1648 switch (Op.getOpcode()) {
1649 default: break;
1650 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001651 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001652 return VTBits-Tmp+1;
1653 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001654 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001655 return VTBits-Tmp;
1656
1657 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001658 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1659 // If negative, return # leading ones.
1660 if (Val.isNegative())
1661 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001662
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001663 // Return # leading zeros.
1664 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001665 }
1666
1667 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001668 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001669 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1670
1671 case ISD::SIGN_EXTEND_INREG:
1672 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001673 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001674 Tmp = VTBits-Tmp+1;
1675
1676 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1677 return std::max(Tmp, Tmp2);
1678
1679 case ISD::SRA:
1680 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1681 // SRA X, C -> adds C sign bits.
1682 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1683 Tmp += C->getValue();
1684 if (Tmp > VTBits) Tmp = VTBits;
1685 }
1686 return Tmp;
1687 case ISD::SHL:
1688 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1689 // shl destroys sign bits.
1690 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1691 if (C->getValue() >= VTBits || // Bad shift.
1692 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1693 return Tmp - C->getValue();
1694 }
1695 break;
1696 case ISD::AND:
1697 case ISD::OR:
1698 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001699 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001700 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001701 if (Tmp != 1) {
1702 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1703 FirstAnswer = std::min(Tmp, Tmp2);
1704 // We computed what we know about the sign bits as our first
1705 // answer. Now proceed to the generic code that uses
1706 // ComputeMaskedBits, and pick whichever answer is better.
1707 }
1708 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001709
1710 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001711 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001712 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001713 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001714 return std::min(Tmp, Tmp2);
1715
1716 case ISD::SETCC:
1717 // If setcc returns 0/-1, all bits are sign bits.
1718 if (TLI.getSetCCResultContents() ==
1719 TargetLowering::ZeroOrNegativeOneSetCCResult)
1720 return VTBits;
1721 break;
1722 case ISD::ROTL:
1723 case ISD::ROTR:
1724 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1725 unsigned RotAmt = C->getValue() & (VTBits-1);
1726
1727 // Handle rotate right by N like a rotate left by 32-N.
1728 if (Op.getOpcode() == ISD::ROTR)
1729 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1730
1731 // If we aren't rotating out all of the known-in sign bits, return the
1732 // number that are left. This handles rotl(sext(x), 1) for example.
1733 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1734 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1735 }
1736 break;
1737 case ISD::ADD:
1738 // Add can have at most one carry bit. Thus we know that the output
1739 // is, at worst, one more bit than the inputs.
1740 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1741 if (Tmp == 1) return 1; // Early out.
1742
1743 // Special case decrementing a value (ADD X, -1):
1744 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1745 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001746 APInt KnownZero, KnownOne;
1747 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001748 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1749
1750 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1751 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001752 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001753 return VTBits;
1754
1755 // If we are subtracting one from a positive number, there is no carry
1756 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001757 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001758 return Tmp;
1759 }
1760
1761 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1762 if (Tmp2 == 1) return 1;
1763 return std::min(Tmp, Tmp2)-1;
1764 break;
1765
1766 case ISD::SUB:
1767 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1768 if (Tmp2 == 1) return 1;
1769
1770 // Handle NEG.
1771 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001772 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001773 APInt KnownZero, KnownOne;
1774 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001775 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1776 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1777 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001778 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001779 return VTBits;
1780
1781 // If the input is known to be positive (the sign bit is known clear),
1782 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001783 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001784 return Tmp2;
1785
1786 // Otherwise, we treat this like a SUB.
1787 }
1788
1789 // Sub can have at most one carry bit. Thus we know that the output
1790 // is, at worst, one more bit than the inputs.
1791 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1792 if (Tmp == 1) return 1; // Early out.
1793 return std::min(Tmp, Tmp2)-1;
1794 break;
1795 case ISD::TRUNCATE:
1796 // FIXME: it's tricky to do anything useful for this, but it is an important
1797 // case for targets like X86.
1798 break;
1799 }
1800
1801 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1802 if (Op.getOpcode() == ISD::LOAD) {
1803 LoadSDNode *LD = cast<LoadSDNode>(Op);
1804 unsigned ExtType = LD->getExtensionType();
1805 switch (ExtType) {
1806 default: break;
1807 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001808 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001809 return VTBits-Tmp+1;
1810 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001811 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001812 return VTBits-Tmp;
1813 }
1814 }
1815
1816 // Allow the target to implement this method for its nodes.
1817 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1818 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1819 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1820 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1821 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001822 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001823 }
1824
1825 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1826 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001827 APInt KnownZero, KnownOne;
1828 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001829 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1830
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001831 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001832 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001833 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001834 Mask = KnownOne;
1835 } else {
1836 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001837 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001838 }
1839
1840 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1841 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001842 Mask = ~Mask;
1843 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001844 // Return # leading zeros. We use 'min' here in case Val was zero before
1845 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001846 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001847}
1848
Chris Lattner51dabfb2006-10-14 00:41:01 +00001849
Evan Chenga844bde2008-02-02 04:07:54 +00001850bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1851 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1852 if (!GA) return false;
1853 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1854 if (!GV) return false;
1855 MachineModuleInfo *MMI = getMachineModuleInfo();
1856 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1857}
1858
1859
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001860/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1861/// element of the result of the vector shuffle.
1862SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned Idx) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001863 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001864 SDOperand PermMask = N->getOperand(2);
1865 unsigned NumElems = PermMask.getNumOperands();
1866 SDOperand V = (Idx < NumElems) ? N->getOperand(0) : N->getOperand(1);
1867 Idx %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001868
1869 if (V.getOpcode() == ISD::BIT_CONVERT) {
1870 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001871 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001872 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001873 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001874 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
1875 return (Idx == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001876 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001877 if (V.getOpcode() == ISD::BUILD_VECTOR)
1878 return V.getOperand(Idx);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001879 if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
1880 SDOperand Elt = PermMask.getOperand(Idx);
1881 if (Elt.getOpcode() == ISD::UNDEF)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001882 return getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001883 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Elt)->getValue());
1884 }
1885 return SDOperand();
1886}
1887
1888
Chris Lattnerc3aae252005-01-07 07:46:32 +00001889/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001890///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001891SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001892 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001893 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001894 void *IP = 0;
1895 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1896 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001897 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001898 CSEMap.InsertNode(N, IP);
1899
1900 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001901 return SDOperand(N, 0);
1902}
1903
Duncan Sands83ec4b62008-06-06 12:08:01 +00001904SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001905 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001906 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001907 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001908 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001909 switch (Opcode) {
1910 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001911 case ISD::SIGN_EXTEND:
1912 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001913 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001914 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001915 case ISD::TRUNCATE:
1916 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001917 case ISD::UINT_TO_FP:
1918 case ISD::SINT_TO_FP: {
1919 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001920 // No compile time operations on this type.
1921 if (VT==MVT::ppcf128)
1922 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001923 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1924 (void)apf.convertFromAPInt(Val,
1925 Opcode==ISD::SINT_TO_FP,
1926 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001927 return getConstantFP(apf, VT);
1928 }
Chris Lattner94683772005-12-23 05:30:37 +00001929 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001930 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001931 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001932 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001933 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001934 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001935 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001936 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001937 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001938 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001939 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001940 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001941 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001942 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001943 }
1944 }
1945
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001946 // Constant fold unary operations with a floating point constant operand.
1947 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1948 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001949 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001950 switch (Opcode) {
1951 case ISD::FNEG:
1952 V.changeSign();
1953 return getConstantFP(V, VT);
1954 case ISD::FABS:
1955 V.clearSign();
1956 return getConstantFP(V, VT);
1957 case ISD::FP_ROUND:
1958 case ISD::FP_EXTEND:
1959 // This can return overflow, underflow, or inexact; we don't care.
1960 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001961 (void)V.convert(*MVTToAPFloatSemantics(VT),
1962 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001963 return getConstantFP(V, VT);
1964 case ISD::FP_TO_SINT:
1965 case ISD::FP_TO_UINT: {
1966 integerPart x;
1967 assert(integerPartWidth >= 64);
1968 // FIXME need to be more flexible about rounding mode.
1969 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1970 Opcode==ISD::FP_TO_SINT,
1971 APFloat::rmTowardZero);
1972 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1973 break;
1974 return getConstant(x, VT);
1975 }
1976 case ISD::BIT_CONVERT:
1977 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1978 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
1979 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1980 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001981 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00001982 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001983 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001984 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001985
1986 unsigned OpOpcode = Operand.Val->getOpcode();
1987 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001988 case ISD::TokenFactor:
Dan Gohmanb91c89d2008-04-14 18:43:25 +00001989 case ISD::MERGE_VALUES:
1990 return Operand; // Factor or merge of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00001991 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00001992 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001993 assert(VT.isFloatingPoint() &&
1994 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00001995 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00001996 if (Operand.getOpcode() == ISD::UNDEF)
1997 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00001998 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00001999 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002000 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002001 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002002 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002003 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002004 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002005 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2006 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2007 break;
2008 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002009 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002010 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002011 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002012 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002013 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002014 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002015 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002016 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002017 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002018 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002019 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002020 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002021 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002022 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002023 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2024 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2025 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2026 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002027 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002028 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002029 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002030 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002031 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002032 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002033 if (OpOpcode == ISD::TRUNCATE)
2034 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002035 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2036 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002037 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002038 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002039 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002040 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002041 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2042 else
2043 return Operand.Val->getOperand(0);
2044 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002045 break;
Chris Lattner35481892005-12-23 00:16:34 +00002046 case ISD::BIT_CONVERT:
2047 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002048 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002049 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002050 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002051 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2052 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002053 if (OpOpcode == ISD::UNDEF)
2054 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002055 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002056 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002057 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2058 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002059 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002060 if (OpOpcode == ISD::UNDEF)
2061 return getNode(ISD::UNDEF, VT);
2062 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2063 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2064 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2065 Operand.getConstantOperandVal(1) == 0 &&
2066 Operand.getOperand(0).getValueType() == VT)
2067 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002068 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002069 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002070 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2071 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002072 Operand.Val->getOperand(0));
2073 if (OpOpcode == ISD::FNEG) // --X -> X
2074 return Operand.Val->getOperand(0);
2075 break;
2076 case ISD::FABS:
2077 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2078 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2079 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002080 }
2081
Chris Lattner43247a12005-08-25 19:12:10 +00002082 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002083 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002084 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002085 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002086 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002087 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002088 void *IP = 0;
2089 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2090 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002091 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002092 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002093 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002094 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002095 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002096 AllNodes.push_back(N);
2097 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002098}
2099
Chris Lattner36019aa2005-04-18 03:48:41 +00002100
2101
Duncan Sands83ec4b62008-06-06 12:08:01 +00002102SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002103 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002104 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2105 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002106 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002107 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002108 case ISD::TokenFactor:
2109 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2110 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002111 // Fold trivial token factors.
2112 if (N1.getOpcode() == ISD::EntryToken) return N2;
2113 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002114 break;
Chris Lattner76365122005-01-16 02:23:22 +00002115 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002116 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002117 N1.getValueType() == VT && "Binary operator types must match!");
2118 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2119 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002120 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002121 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002122 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2123 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002124 break;
Chris Lattner76365122005-01-16 02:23:22 +00002125 case ISD::OR:
2126 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002127 case ISD::ADD:
2128 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002129 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002130 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002131 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2132 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002133 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002134 return N1;
2135 break;
Chris Lattner76365122005-01-16 02:23:22 +00002136 case ISD::UDIV:
2137 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002138 case ISD::MULHU:
2139 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002140 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002141 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002142 case ISD::MUL:
2143 case ISD::SDIV:
2144 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002145 case ISD::FADD:
2146 case ISD::FSUB:
2147 case ISD::FMUL:
2148 case ISD::FDIV:
2149 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002150 assert(N1.getValueType() == N2.getValueType() &&
2151 N1.getValueType() == VT && "Binary operator types must match!");
2152 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002153 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2154 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002155 N1.getValueType().isFloatingPoint() &&
2156 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002157 "Invalid FCOPYSIGN!");
2158 break;
Chris Lattner76365122005-01-16 02:23:22 +00002159 case ISD::SHL:
2160 case ISD::SRA:
2161 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002162 case ISD::ROTL:
2163 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002164 assert(VT == N1.getValueType() &&
2165 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002166 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002167 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002168 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002169 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002170 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002171 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002172 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002173 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002174 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002175 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002176 break;
2177 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002178 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002179 assert(VT.isFloatingPoint() &&
2180 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002181 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002182 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002183 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002184 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002185 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002186 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002187 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002188 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002189 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002190 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002191 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002192 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002193 break;
2194 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002195 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002196 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002197 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002198 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002199 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002200 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002201 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002202
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002203 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002204 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002205 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002206 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002207 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002208 return getConstant(Val, VT);
2209 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002210 break;
2211 }
2212 case ISD::EXTRACT_VECTOR_ELT:
2213 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2214
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002215 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2216 if (N1.getOpcode() == ISD::UNDEF)
2217 return getNode(ISD::UNDEF, VT);
2218
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002219 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2220 // expanding copies of large vectors from registers.
2221 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2222 N1.getNumOperands() > 0) {
2223 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002224 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002225 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2226 N1.getOperand(N2C->getValue() / Factor),
2227 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2228 }
2229
2230 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2231 // expanding large vector constants.
2232 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2233 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002234
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002235 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2236 // operations are lowered to scalars.
2237 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2238 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2239 if (IEC == N2C)
2240 return N1.getOperand(1);
2241 else
2242 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2243 }
2244 break;
2245 case ISD::EXTRACT_ELEMENT:
2246 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002247 assert(!N1.getValueType().isVector() &&
2248 N1.getValueType().isInteger() &&
2249 !VT.isVector() && VT.isInteger() &&
Duncan Sands25eb0432008-03-12 20:30:08 +00002250 "EXTRACT_ELEMENT only applies to integers!");
2251
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002252 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2253 // 64-bit integers into 32-bit parts. Instead of building the extract of
2254 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2255 if (N1.getOpcode() == ISD::BUILD_PAIR)
2256 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002257
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002258 // EXTRACT_ELEMENT of a constant int is also very common.
2259 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002260 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002261 unsigned Shift = ElementSize * N2C->getValue();
2262 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2263 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002264 }
2265 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002266 case ISD::EXTRACT_SUBVECTOR:
2267 if (N1.getValueType() == VT) // Trivial extraction.
2268 return N1;
2269 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002270 }
2271
2272 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002273 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002274 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002275 switch (Opcode) {
2276 case ISD::ADD: return getConstant(C1 + C2, VT);
2277 case ISD::SUB: return getConstant(C1 - C2, VT);
2278 case ISD::MUL: return getConstant(C1 * C2, VT);
2279 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002280 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002281 break;
2282 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002283 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002284 break;
2285 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002286 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002287 break;
2288 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002289 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002290 break;
2291 case ISD::AND : return getConstant(C1 & C2, VT);
2292 case ISD::OR : return getConstant(C1 | C2, VT);
2293 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002294 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002295 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2296 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2297 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2298 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002299 default: break;
2300 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002301 } else { // Cannonicalize constant to RHS if commutative
2302 if (isCommutativeBinOp(Opcode)) {
2303 std::swap(N1C, N2C);
2304 std::swap(N1, N2);
2305 }
2306 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002307 }
2308
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002309 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002310 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2311 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002312 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002313 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2314 // Cannonicalize constant to RHS if commutative
2315 std::swap(N1CFP, N2CFP);
2316 std::swap(N1, N2);
2317 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002318 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2319 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002320 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002321 case ISD::FADD:
2322 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002323 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002324 return getConstantFP(V1, VT);
2325 break;
2326 case ISD::FSUB:
2327 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2328 if (s!=APFloat::opInvalidOp)
2329 return getConstantFP(V1, VT);
2330 break;
2331 case ISD::FMUL:
2332 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2333 if (s!=APFloat::opInvalidOp)
2334 return getConstantFP(V1, VT);
2335 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002336 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002337 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2338 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2339 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002340 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002341 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002342 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2343 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2344 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002345 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002346 case ISD::FCOPYSIGN:
2347 V1.copySign(V2);
2348 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002349 default: break;
2350 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002351 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002352 }
Chris Lattner62b57722006-04-20 05:39:12 +00002353
2354 // Canonicalize an UNDEF to the RHS, even over a constant.
2355 if (N1.getOpcode() == ISD::UNDEF) {
2356 if (isCommutativeBinOp(Opcode)) {
2357 std::swap(N1, N2);
2358 } else {
2359 switch (Opcode) {
2360 case ISD::FP_ROUND_INREG:
2361 case ISD::SIGN_EXTEND_INREG:
2362 case ISD::SUB:
2363 case ISD::FSUB:
2364 case ISD::FDIV:
2365 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002366 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002367 return N1; // fold op(undef, arg2) -> undef
2368 case ISD::UDIV:
2369 case ISD::SDIV:
2370 case ISD::UREM:
2371 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002372 case ISD::SRL:
2373 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002374 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002375 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2376 // For vectors, we can't easily build an all zero vector, just return
2377 // the LHS.
2378 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002379 }
2380 }
2381 }
2382
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002383 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002384 if (N2.getOpcode() == ISD::UNDEF) {
2385 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002386 case ISD::XOR:
2387 if (N1.getOpcode() == ISD::UNDEF)
2388 // Handle undef ^ undef -> 0 special case. This is a common
2389 // idiom (misuse).
2390 return getConstant(0, VT);
2391 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002392 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002393 case ISD::ADDC:
2394 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002395 case ISD::SUB:
2396 case ISD::FADD:
2397 case ISD::FSUB:
2398 case ISD::FMUL:
2399 case ISD::FDIV:
2400 case ISD::FREM:
2401 case ISD::UDIV:
2402 case ISD::SDIV:
2403 case ISD::UREM:
2404 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002405 return N2; // fold op(arg1, undef) -> undef
2406 case ISD::MUL:
2407 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002408 case ISD::SRL:
2409 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002410 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002411 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2412 // For vectors, we can't easily build an all zero vector, just return
2413 // the LHS.
2414 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002415 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002416 if (!VT.isVector())
2417 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002418 // For vectors, we can't easily build an all one vector, just return
2419 // the LHS.
2420 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002421 case ISD::SRA:
2422 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002423 }
2424 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002425
Chris Lattner27e9b412005-05-11 18:57:39 +00002426 // Memoize this node if possible.
2427 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002428 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002429 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002430 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002431 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002432 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002433 void *IP = 0;
2434 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2435 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002436 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002437 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002438 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002439 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002440 }
2441
Chris Lattnerc3aae252005-01-07 07:46:32 +00002442 AllNodes.push_back(N);
2443 return SDOperand(N, 0);
2444}
2445
Duncan Sands83ec4b62008-06-06 12:08:01 +00002446SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002447 SDOperand N1, SDOperand N2, SDOperand N3) {
2448 // Perform various simplifications.
2449 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2450 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002451 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002452 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002453 // Use FoldSetCC to simplify SETCC's.
2454 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002455 if (Simp.Val) return Simp;
2456 break;
2457 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002458 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002459 if (N1C) {
2460 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002461 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002462 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002463 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002464 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002465
2466 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002467 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002468 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002469 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002470 if (N2C->getValue()) // Unconditional branch
2471 return getNode(ISD::BR, MVT::Other, N1, N3);
2472 else
2473 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002474 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002475 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002476 case ISD::VECTOR_SHUFFLE:
2477 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002478 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002479 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002480 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002481 "Illegal VECTOR_SHUFFLE node!");
2482 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002483 case ISD::BIT_CONVERT:
2484 // Fold bit_convert nodes from a type to themselves.
2485 if (N1.getValueType() == VT)
2486 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002487 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002488 }
2489
Chris Lattner43247a12005-08-25 19:12:10 +00002490 // Memoize node if it doesn't produce a flag.
2491 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002492 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002493 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002494 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002495 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002496 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002497 void *IP = 0;
2498 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2499 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002500 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002501 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002502 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002503 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002504 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002505 AllNodes.push_back(N);
2506 return SDOperand(N, 0);
2507}
2508
Duncan Sands83ec4b62008-06-06 12:08:01 +00002509SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002510 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002511 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002512 SDOperand Ops[] = { N1, N2, N3, N4 };
2513 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002514}
2515
Duncan Sands83ec4b62008-06-06 12:08:01 +00002516SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002517 SDOperand N1, SDOperand N2, SDOperand N3,
2518 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002519 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2520 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002521}
2522
Dan Gohman707e0182008-04-12 04:36:06 +00002523/// getMemsetValue - Vectorized representation of the memset value
2524/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002525static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2526 unsigned NumBits = VT.isVector() ?
2527 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002528 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002529 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002530 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002531 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002532 Val = (Val << Shift) | Val;
2533 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002534 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002535 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002536 return DAG.getConstant(Val, VT);
2537 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002538 }
Evan Chengf0df0312008-05-15 08:39:06 +00002539
2540 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2541 unsigned Shift = 8;
2542 for (unsigned i = NumBits; i > 8; i >>= 1) {
2543 Value = DAG.getNode(ISD::OR, VT,
2544 DAG.getNode(ISD::SHL, VT, Value,
2545 DAG.getConstant(Shift, MVT::i8)), Value);
2546 Shift <<= 1;
2547 }
2548
2549 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002550}
2551
Dan Gohman707e0182008-04-12 04:36:06 +00002552/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2553/// used when a memcpy is turned into a memset when the source is a constant
2554/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002555static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002556 const TargetLowering &TLI,
2557 std::string &Str, unsigned Offset) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002558 assert(!VT.isVector() && "Can't handle vector type here!");
2559 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002560 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002561 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002562 if (TLI.isLittleEndian())
2563 Offset = Offset + MSB - 1;
2564 for (unsigned i = 0; i != MSB; ++i) {
2565 Val = (Val << 8) | (unsigned char)Str[Offset];
2566 Offset += TLI.isLittleEndian() ? -1 : 1;
2567 }
2568 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002569}
2570
Dan Gohman707e0182008-04-12 04:36:06 +00002571/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002572///
Dan Gohman707e0182008-04-12 04:36:06 +00002573static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2574 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002575 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002576 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2577}
2578
Evan Chengf0df0312008-05-15 08:39:06 +00002579/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2580///
2581static bool isMemSrcFromString(SDOperand Src, std::string &Str,
2582 uint64_t &SrcOff) {
2583 unsigned SrcDelta = 0;
2584 GlobalAddressSDNode *G = NULL;
2585 if (Src.getOpcode() == ISD::GlobalAddress)
2586 G = cast<GlobalAddressSDNode>(Src);
2587 else if (Src.getOpcode() == ISD::ADD &&
2588 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2589 Src.getOperand(1).getOpcode() == ISD::Constant) {
2590 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2591 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2592 }
2593 if (!G)
2594 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002595
Evan Chengf0df0312008-05-15 08:39:06 +00002596 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
2597 if (GV && GV->isConstant()) {
2598 Str = GV->getStringValue(false);
2599 if (!Str.empty()) {
2600 SrcOff += SrcDelta;
2601 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002602 }
2603 }
2604
Evan Chengf0df0312008-05-15 08:39:06 +00002605 return false;
2606}
Dan Gohman707e0182008-04-12 04:36:06 +00002607
Evan Chengf0df0312008-05-15 08:39:06 +00002608/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2609/// to replace the memset / memcpy is below the threshold. It also returns the
2610/// types of the sequence of memory ops to perform memset / memcpy.
2611static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002612bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002613 SDOperand Dst, SDOperand Src,
2614 unsigned Limit, uint64_t Size, unsigned &Align,
2615 SelectionDAG &DAG,
2616 const TargetLowering &TLI) {
2617 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
2618
2619 std::string Str;
2620 uint64_t SrcOff = 0;
2621 bool isSrcStr = isMemSrcFromString(Src, Str, SrcOff);
2622 bool isSrcConst = isa<ConstantSDNode>(Src);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002624 if (VT != MVT::iAny) {
2625 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002626 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002627 // If source is a string constant, this will require an unaligned load.
2628 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2629 if (Dst.getOpcode() != ISD::FrameIndex) {
2630 // Can't change destination alignment. It requires a unaligned store.
2631 if (AllowUnalign)
2632 VT = MVT::iAny;
2633 } else {
2634 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2635 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2636 if (MFI->isFixedObjectIndex(FI)) {
2637 // Can't change destination alignment. It requires a unaligned store.
2638 if (AllowUnalign)
2639 VT = MVT::iAny;
2640 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002641 // Give the stack frame object a larger alignment if needed.
2642 if (MFI->getObjectAlignment(FI) < NewAlign)
2643 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002644 Align = NewAlign;
2645 }
2646 }
2647 }
2648 }
2649
2650 if (VT == MVT::iAny) {
2651 if (AllowUnalign) {
2652 VT = MVT::i64;
2653 } else {
2654 switch (Align & 7) {
2655 case 0: VT = MVT::i64; break;
2656 case 4: VT = MVT::i32; break;
2657 case 2: VT = MVT::i16; break;
2658 default: VT = MVT::i8; break;
2659 }
2660 }
2661
Duncan Sands83ec4b62008-06-06 12:08:01 +00002662 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002663 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002664 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2665 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002666
Duncan Sands8e4eb092008-06-08 20:54:56 +00002667 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002668 VT = LVT;
2669 }
Dan Gohman707e0182008-04-12 04:36:06 +00002670
2671 unsigned NumMemOps = 0;
2672 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002673 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002674 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002675 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002676 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002677 VT = MVT::i64;
2678 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002679 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2680 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002681 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002682 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002683 VTSize >>= 1;
2684 }
Dan Gohman707e0182008-04-12 04:36:06 +00002685 }
Dan Gohman707e0182008-04-12 04:36:06 +00002686
2687 if (++NumMemOps > Limit)
2688 return false;
2689 MemOps.push_back(VT);
2690 Size -= VTSize;
2691 }
2692
2693 return true;
2694}
2695
2696static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2697 SDOperand Chain, SDOperand Dst,
2698 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002699 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002700 const Value *DstSV, uint64_t DstSVOff,
2701 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002702 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2703
Dan Gohman21323f32008-05-29 19:42:22 +00002704 // Expand memcpy to a series of load and store ops if the size operand falls
2705 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002706 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002707 uint64_t Limit = -1;
2708 if (!AlwaysInline)
2709 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002710 unsigned DstAlign = Align; // Destination alignment can change.
2711 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
2712 DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002713 return SDOperand();
2714
Dan Gohman707e0182008-04-12 04:36:06 +00002715 std::string Str;
Dan Gohman1f13c682008-04-28 17:15:20 +00002716 uint64_t SrcOff = 0, DstOff = 0;
Evan Chengf0df0312008-05-15 08:39:06 +00002717 bool CopyFromStr = isMemSrcFromString(Src, Str, SrcOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002718
Evan Chengf0df0312008-05-15 08:39:06 +00002719 SmallVector<SDOperand, 8> OutChains;
2720 unsigned NumMemOps = MemOps.size();
Dan Gohman707e0182008-04-12 04:36:06 +00002721 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002722 MVT VT = MemOps[i];
2723 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002724 SDOperand Value, Store;
2725
Duncan Sands83ec4b62008-06-06 12:08:01 +00002726 if (CopyFromStr && !VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002727 // It's unlikely a store of a vector immediate can be done in a single
2728 // instruction. It would require a load from a constantpool first.
2729 // FIXME: Handle cases where store of vector immediate is done in a
2730 // single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002731 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002732 Store = DAG.getStore(Chain, Value,
2733 getMemBasePlusOffset(Dst, DstOff, DAG),
2734 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002735 } else {
2736 Value = DAG.getLoad(VT, Chain,
2737 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002738 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002739 Store = DAG.getStore(Chain, Value,
2740 getMemBasePlusOffset(Dst, DstOff, DAG),
2741 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002742 }
2743 OutChains.push_back(Store);
2744 SrcOff += VTSize;
2745 DstOff += VTSize;
2746 }
2747
2748 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2749 &OutChains[0], OutChains.size());
2750}
2751
Dan Gohman21323f32008-05-29 19:42:22 +00002752static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2753 SDOperand Chain, SDOperand Dst,
2754 SDOperand Src, uint64_t Size,
2755 unsigned Align, bool AlwaysInline,
2756 const Value *DstSV, uint64_t DstSVOff,
2757 const Value *SrcSV, uint64_t SrcSVOff){
2758 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2759
2760 // Expand memmove to a series of load and store ops if the size operand falls
2761 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002762 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002763 uint64_t Limit = -1;
2764 if (!AlwaysInline)
2765 Limit = TLI.getMaxStoresPerMemmove();
2766 unsigned DstAlign = Align; // Destination alignment can change.
2767 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
2768 DAG, TLI))
2769 return SDOperand();
2770
Dan Gohman21323f32008-05-29 19:42:22 +00002771 uint64_t SrcOff = 0, DstOff = 0;
2772
2773 SmallVector<SDOperand, 8> LoadValues;
2774 SmallVector<SDOperand, 8> LoadChains;
2775 SmallVector<SDOperand, 8> OutChains;
2776 unsigned NumMemOps = MemOps.size();
2777 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002778 MVT VT = MemOps[i];
2779 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002780 SDOperand Value, Store;
2781
2782 Value = DAG.getLoad(VT, Chain,
2783 getMemBasePlusOffset(Src, SrcOff, DAG),
2784 SrcSV, SrcSVOff + SrcOff, false, Align);
2785 LoadValues.push_back(Value);
2786 LoadChains.push_back(Value.getValue(1));
2787 SrcOff += VTSize;
2788 }
2789 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2790 &LoadChains[0], LoadChains.size());
2791 OutChains.clear();
2792 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002793 MVT VT = MemOps[i];
2794 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002795 SDOperand Value, Store;
2796
2797 Store = DAG.getStore(Chain, LoadValues[i],
2798 getMemBasePlusOffset(Dst, DstOff, DAG),
2799 DstSV, DstSVOff + DstOff, false, DstAlign);
2800 OutChains.push_back(Store);
2801 DstOff += VTSize;
2802 }
2803
2804 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2805 &OutChains[0], OutChains.size());
2806}
2807
Dan Gohman707e0182008-04-12 04:36:06 +00002808static SDOperand getMemsetStores(SelectionDAG &DAG,
2809 SDOperand Chain, SDOperand Dst,
2810 SDOperand Src, uint64_t Size,
2811 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002812 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002813 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2814
2815 // Expand memset to a series of load/store ops if the size operand
2816 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002817 std::vector<MVT> MemOps;
Evan Chengf0df0312008-05-15 08:39:06 +00002818 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
2819 Size, Align, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002820 return SDOperand();
2821
2822 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002823 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002824
2825 unsigned NumMemOps = MemOps.size();
2826 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002827 MVT VT = MemOps[i];
2828 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002829 SDOperand Value = getMemsetValue(Src, VT, DAG);
2830 SDOperand Store = DAG.getStore(Chain, Value,
2831 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002832 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002833 OutChains.push_back(Store);
2834 DstOff += VTSize;
2835 }
2836
2837 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2838 &OutChains[0], OutChains.size());
2839}
2840
2841SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002842 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002843 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002844 const Value *DstSV, uint64_t DstSVOff,
2845 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002846
2847 // Check to see if we should lower the memcpy to loads and stores first.
2848 // For cases within the target-specified limits, this is the best choice.
2849 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2850 if (ConstantSize) {
2851 // Memcpy with size zero? Just return the original chain.
2852 if (ConstantSize->isNullValue())
2853 return Chain;
2854
2855 SDOperand Result =
2856 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002857 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002858 if (Result.Val)
2859 return Result;
2860 }
2861
2862 // Then check to see if we should lower the memcpy with target-specific
2863 // code. If the target chooses to do this, this is the next best.
2864 SDOperand Result =
2865 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2866 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002867 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002868 if (Result.Val)
2869 return Result;
2870
2871 // If we really need inline code and the target declined to provide it,
2872 // use a (potentially long) sequence of loads and stores.
2873 if (AlwaysInline) {
2874 assert(ConstantSize && "AlwaysInline requires a constant size!");
2875 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2876 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002877 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002878 }
2879
2880 // Emit a library call.
2881 TargetLowering::ArgListTy Args;
2882 TargetLowering::ArgListEntry Entry;
2883 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2884 Entry.Node = Dst; Args.push_back(Entry);
2885 Entry.Node = Src; Args.push_back(Entry);
2886 Entry.Node = Size; Args.push_back(Entry);
2887 std::pair<SDOperand,SDOperand> CallResult =
2888 TLI.LowerCallTo(Chain, Type::VoidTy,
2889 false, false, false, CallingConv::C, false,
2890 getExternalSymbol("memcpy", TLI.getPointerTy()),
2891 Args, *this);
2892 return CallResult.second;
2893}
2894
2895SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2896 SDOperand Src, SDOperand Size,
2897 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002898 const Value *DstSV, uint64_t DstSVOff,
2899 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002900
Dan Gohman21323f32008-05-29 19:42:22 +00002901 // Check to see if we should lower the memmove to loads and stores first.
2902 // For cases within the target-specified limits, this is the best choice.
2903 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2904 if (ConstantSize) {
2905 // Memmove with size zero? Just return the original chain.
2906 if (ConstantSize->isNullValue())
2907 return Chain;
2908
2909 SDOperand Result =
2910 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2911 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2912 if (Result.Val)
2913 return Result;
2914 }
Dan Gohman707e0182008-04-12 04:36:06 +00002915
2916 // Then check to see if we should lower the memmove with target-specific
2917 // code. If the target chooses to do this, this is the next best.
2918 SDOperand Result =
2919 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002920 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002921 if (Result.Val)
2922 return Result;
2923
2924 // Emit a library call.
2925 TargetLowering::ArgListTy Args;
2926 TargetLowering::ArgListEntry Entry;
2927 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2928 Entry.Node = Dst; Args.push_back(Entry);
2929 Entry.Node = Src; Args.push_back(Entry);
2930 Entry.Node = Size; Args.push_back(Entry);
2931 std::pair<SDOperand,SDOperand> CallResult =
2932 TLI.LowerCallTo(Chain, Type::VoidTy,
2933 false, false, false, CallingConv::C, false,
2934 getExternalSymbol("memmove", TLI.getPointerTy()),
2935 Args, *this);
2936 return CallResult.second;
2937}
2938
2939SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2940 SDOperand Src, SDOperand Size,
2941 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002942 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002943
2944 // Check to see if we should lower the memset to stores first.
2945 // For cases within the target-specified limits, this is the best choice.
2946 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2947 if (ConstantSize) {
2948 // Memset with size zero? Just return the original chain.
2949 if (ConstantSize->isNullValue())
2950 return Chain;
2951
2952 SDOperand Result =
2953 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002954 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002955 if (Result.Val)
2956 return Result;
2957 }
2958
2959 // Then check to see if we should lower the memset with target-specific
2960 // code. If the target chooses to do this, this is the next best.
2961 SDOperand Result =
2962 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002963 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002964 if (Result.Val)
2965 return Result;
2966
2967 // Emit a library call.
2968 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
2969 TargetLowering::ArgListTy Args;
2970 TargetLowering::ArgListEntry Entry;
2971 Entry.Node = Dst; Entry.Ty = IntPtrTy;
2972 Args.push_back(Entry);
2973 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002974 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00002975 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
2976 else
2977 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
2978 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
2979 Args.push_back(Entry);
2980 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
2981 Args.push_back(Entry);
2982 std::pair<SDOperand,SDOperand> CallResult =
2983 TLI.LowerCallTo(Chain, Type::VoidTy,
2984 false, false, false, CallingConv::C, false,
2985 getExternalSymbol("memset", TLI.getPointerTy()),
2986 Args, *this);
2987 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002988}
2989
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002990SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002991 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00002992 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00002993 unsigned Alignment) {
2994 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002995 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
2996 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002997 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00002998 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00002999 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003000 void* IP = 0;
3001 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3002 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003003 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp,
Mon P Wang28873102008-06-25 08:15:39 +00003004 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003005 CSEMap.InsertNode(N, IP);
3006 AllNodes.push_back(N);
3007 return SDOperand(N, 0);
3008}
3009
3010SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003011 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003012 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003013 unsigned Alignment) {
3014 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003015 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3016 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003017 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003018 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3019 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003020 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003021 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003022 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003023 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003024 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003025 void* IP = 0;
3026 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3027 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003028 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val,
Mon P Wang28873102008-06-25 08:15:39 +00003029 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003030 CSEMap.InsertNode(N, IP);
3031 AllNodes.push_back(N);
3032 return SDOperand(N, 0);
3033}
3034
Duncan Sands14ea39c2008-03-27 20:23:40 +00003035SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003036SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003037 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003038 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003039 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003040 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003041 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3042 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003043 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003044 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003045 } else if (SV) {
3046 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3047 assert(PT && "Value for load must be a pointer");
3048 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003049 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003050 assert(Ty && "Could not get type information for load");
3051 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3052 }
Evan Cheng466685d2006-10-09 20:57:25 +00003053
Duncan Sands14ea39c2008-03-27 20:23:40 +00003054 if (VT == EVT) {
3055 ExtType = ISD::NON_EXTLOAD;
3056 } else if (ExtType == ISD::NON_EXTLOAD) {
3057 assert(VT == EVT && "Non-extending load from different memory type!");
3058 } else {
3059 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003060 if (VT.isVector())
3061 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003062 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003063 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003064 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003065 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003066 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003067 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003068 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003069 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003070
3071 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003072 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003073 "Unindexed load with an offset!");
3074
3075 SDVTList VTs = Indexed ?
3076 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3077 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003078 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003079 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003080 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003081 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003082 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003083 ID.AddInteger(Alignment);
3084 ID.AddInteger(isVolatile);
3085 void *IP = 0;
3086 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3087 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003088 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3089 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003090 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003091 AllNodes.push_back(N);
3092 return SDOperand(N, 0);
3093}
3094
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003096 SDOperand Chain, SDOperand Ptr,
3097 const Value *SV, int SVOffset,
3098 bool isVolatile, unsigned Alignment) {
3099 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003100 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3101 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003102}
3103
Duncan Sands83ec4b62008-06-06 12:08:01 +00003104SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003105 SDOperand Chain, SDOperand Ptr,
3106 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003107 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003108 bool isVolatile, unsigned Alignment) {
3109 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003110 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3111 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003112}
3113
Evan Cheng144d8f02006-11-09 17:55:04 +00003114SDOperand
3115SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3116 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003117 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003118 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3119 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003120 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3121 LD->getChain(), Base, Offset, LD->getSrcValue(),
3122 LD->getSrcValueOffset(), LD->getMemoryVT(),
3123 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003124}
3125
Jeff Cohend41b30d2006-11-05 19:31:28 +00003126SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003127 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003128 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003129 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003130
Dan Gohman575e2f42007-06-04 15:49:41 +00003131 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3132 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003133 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003134 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003135 } else if (SV) {
3136 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3137 assert(PT && "Value for store must be a pointer");
3138 Ty = PT->getElementType();
3139 }
3140 assert(Ty && "Could not get type information for store");
3141 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3142 }
Evan Chengad071e12006-10-05 22:57:11 +00003143 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003144 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003145 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003146 FoldingSetNodeID ID;
3147 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003148 ID.AddInteger(ISD::UNINDEXED);
3149 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003150 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003151 ID.AddInteger(Alignment);
3152 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003153 void *IP = 0;
3154 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3155 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003156 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003157 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003158 CSEMap.InsertNode(N, IP);
3159 AllNodes.push_back(N);
3160 return SDOperand(N, 0);
3161}
3162
Jeff Cohend41b30d2006-11-05 19:31:28 +00003163SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003164 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003165 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003166 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003167 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003168
3169 if (VT == SVT)
3170 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003171
Duncan Sands8e4eb092008-06-08 20:54:56 +00003172 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003173 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003174 "Can't do FP-INT conversion!");
3175
Dan Gohman575e2f42007-06-04 15:49:41 +00003176 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3177 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003178 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003179 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003180 } else if (SV) {
3181 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3182 assert(PT && "Value for store must be a pointer");
3183 Ty = PT->getElementType();
3184 }
3185 assert(Ty && "Could not get type information for store");
3186 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3187 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003188 SDVTList VTs = getVTList(MVT::Other);
3189 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003190 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003191 FoldingSetNodeID ID;
3192 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003193 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003194 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003195 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003196 ID.AddInteger(Alignment);
3197 ID.AddInteger(isVolatile);
3198 void *IP = 0;
3199 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3200 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003201 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003202 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003203 CSEMap.InsertNode(N, IP);
3204 AllNodes.push_back(N);
3205 return SDOperand(N, 0);
3206}
3207
Evan Cheng144d8f02006-11-09 17:55:04 +00003208SDOperand
3209SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3210 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003211 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3212 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3213 "Store is already a indexed store!");
3214 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3215 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3216 FoldingSetNodeID ID;
3217 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3218 ID.AddInteger(AM);
3219 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003220 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003221 ID.AddInteger(ST->getAlignment());
3222 ID.AddInteger(ST->isVolatile());
3223 void *IP = 0;
3224 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3225 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003226 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003227 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003228 ST->getSrcValue(), ST->getSrcValueOffset(),
3229 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003230 CSEMap.InsertNode(N, IP);
3231 AllNodes.push_back(N);
3232 return SDOperand(N, 0);
3233}
3234
Duncan Sands83ec4b62008-06-06 12:08:01 +00003235SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003236 SDOperand Chain, SDOperand Ptr,
3237 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003238 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003239 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003240}
3241
Duncan Sands83ec4b62008-06-06 12:08:01 +00003242SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003243 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003244 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003245 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003246 case 1: return getNode(Opcode, VT, Ops[0]);
3247 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3248 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003249 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003250 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003251
Chris Lattneref847df2005-04-09 03:27:28 +00003252 switch (Opcode) {
3253 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003254 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003255 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003256 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3257 "LHS and RHS of condition must have same type!");
3258 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3259 "True and False arms of SelectCC must have same type!");
3260 assert(Ops[2].getValueType() == VT &&
3261 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003262 break;
3263 }
3264 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003265 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003266 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3267 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003268 break;
3269 }
Chris Lattneref847df2005-04-09 03:27:28 +00003270 }
3271
Chris Lattner385328c2005-05-14 07:42:29 +00003272 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003273 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003274 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003275 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003276 FoldingSetNodeID ID;
3277 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003278 void *IP = 0;
3279 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3280 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003281 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003282 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003283 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003284 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003285 }
Chris Lattneref847df2005-04-09 03:27:28 +00003286 AllNodes.push_back(N);
3287 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003288}
3289
Chris Lattner89c34632005-05-14 06:20:26 +00003290SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003292 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003293 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3294 Ops, NumOps);
3295}
3296
3297SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003298 const MVT *VTs, unsigned NumVTs,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003299 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003300 if (NumVTs == 1)
3301 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003302 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3303}
3304
3305SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003306 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003307 if (VTList.NumVTs == 1)
3308 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003309
Chris Lattner5f056bf2005-07-10 01:55:33 +00003310 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003311 // FIXME: figure out how to safely handle things like
3312 // int foo(int x) { return 1 << (x & 255); }
3313 // int bar() { return foo(256); }
3314#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003315 case ISD::SRA_PARTS:
3316 case ISD::SRL_PARTS:
3317 case ISD::SHL_PARTS:
3318 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003319 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003320 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3321 else if (N3.getOpcode() == ISD::AND)
3322 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3323 // If the and is only masking out bits that cannot effect the shift,
3324 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003325 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003326 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3327 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3328 }
3329 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003330#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003331 }
Chris Lattner89c34632005-05-14 06:20:26 +00003332
Chris Lattner43247a12005-08-25 19:12:10 +00003333 // Memoize the node unless it returns a flag.
3334 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003335 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003336 FoldingSetNodeID ID;
3337 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003338 void *IP = 0;
3339 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3340 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003341 if (NumOps == 1)
3342 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3343 else if (NumOps == 2)
3344 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3345 else if (NumOps == 3)
3346 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3347 else
3348 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003349 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003350 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003351 if (NumOps == 1)
3352 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3353 else if (NumOps == 2)
3354 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3355 else if (NumOps == 3)
3356 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3357 else
3358 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003359 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003360 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003361 return SDOperand(N, 0);
3362}
3363
Dan Gohman08ce9762007-10-08 15:49:58 +00003364SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003365 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003366}
3367
3368SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3369 SDOperand N1) {
3370 SDOperand Ops[] = { N1 };
3371 return getNode(Opcode, VTList, Ops, 1);
3372}
3373
3374SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3375 SDOperand N1, SDOperand N2) {
3376 SDOperand Ops[] = { N1, N2 };
3377 return getNode(Opcode, VTList, Ops, 2);
3378}
3379
3380SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3381 SDOperand N1, SDOperand N2, SDOperand N3) {
3382 SDOperand Ops[] = { N1, N2, N3 };
3383 return getNode(Opcode, VTList, Ops, 3);
3384}
3385
3386SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3387 SDOperand N1, SDOperand N2, SDOperand N3,
3388 SDOperand N4) {
3389 SDOperand Ops[] = { N1, N2, N3, N4 };
3390 return getNode(Opcode, VTList, Ops, 4);
3391}
3392
3393SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3394 SDOperand N1, SDOperand N2, SDOperand N3,
3395 SDOperand N4, SDOperand N5) {
3396 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3397 return getNode(Opcode, VTList, Ops, 5);
3398}
3399
Duncan Sands83ec4b62008-06-06 12:08:01 +00003400SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003401 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003402}
3403
Duncan Sands83ec4b62008-06-06 12:08:01 +00003404SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3405 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003406 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003407 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003408 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003409 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003410 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003411 V.push_back(VT1);
3412 V.push_back(VT2);
3413 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003414 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003415}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003416SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3417 MVT VT3) {
3418 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003419 E = VTList.end(); I != E; ++I) {
3420 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3421 (*I)[2] == VT3)
3422 return makeVTList(&(*I)[0], 3);
3423 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003424 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003425 V.push_back(VT1);
3426 V.push_back(VT2);
3427 V.push_back(VT3);
3428 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003429 return makeVTList(&(*VTList.begin())[0], 3);
3430}
3431
Duncan Sands83ec4b62008-06-06 12:08:01 +00003432SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003433 switch (NumVTs) {
3434 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003435 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003436 case 2: return getVTList(VTs[0], VTs[1]);
3437 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3438 default: break;
3439 }
3440
Duncan Sands83ec4b62008-06-06 12:08:01 +00003441 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003442 E = VTList.end(); I != E; ++I) {
3443 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3444
3445 bool NoMatch = false;
3446 for (unsigned i = 2; i != NumVTs; ++i)
3447 if (VTs[i] != (*I)[i]) {
3448 NoMatch = true;
3449 break;
3450 }
3451 if (!NoMatch)
3452 return makeVTList(&*I->begin(), NumVTs);
3453 }
3454
Duncan Sands83ec4b62008-06-06 12:08:01 +00003455 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003456 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003457}
3458
3459
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003460/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3461/// specified operands. If the resultant node already exists in the DAG,
3462/// this does not modify the specified node, instead it returns the node that
3463/// already exists. If the resultant node does not exist in the DAG, the
3464/// input node is returned. As a degenerate case, if you specify the same
3465/// input operands as the node already has, the input node is returned.
3466SDOperand SelectionDAG::
3467UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3468 SDNode *N = InN.Val;
3469 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3470
3471 // Check to see if there is no change.
3472 if (Op == N->getOperand(0)) return InN;
3473
3474 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003475 void *InsertPos = 0;
3476 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3477 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003478
3479 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003480 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003481 RemoveNodeFromCSEMaps(N);
3482
3483 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003484 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003485 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003486 N->OperandList[0].setUser(N);
3487 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003488
3489 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003490 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003491 return InN;
3492}
3493
3494SDOperand SelectionDAG::
3495UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3496 SDNode *N = InN.Val;
3497 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3498
3499 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003500 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3501 return InN; // No operands changed, just return the input node.
3502
3503 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003504 void *InsertPos = 0;
3505 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3506 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003507
3508 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003509 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003510 RemoveNodeFromCSEMaps(N);
3511
3512 // Now we update the operands.
3513 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003514 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003515 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003516 N->OperandList[0].setUser(N);
3517 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003518 }
3519 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003520 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003521 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003522 N->OperandList[1].setUser(N);
3523 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003524 }
3525
3526 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003527 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003528 return InN;
3529}
3530
3531SDOperand SelectionDAG::
3532UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003533 SDOperand Ops[] = { Op1, Op2, Op3 };
3534 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003535}
3536
3537SDOperand SelectionDAG::
3538UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3539 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003540 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3541 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003542}
3543
3544SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003545UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3546 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003547 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3548 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003549}
3550
Chris Lattner809ec112006-01-28 10:09:25 +00003551SDOperand SelectionDAG::
Roman Levenstein9cac5252008-04-16 16:15:27 +00003552UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003553 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003554 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003555 "Update with wrong number of operands");
3556
3557 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003558 bool AnyChange = false;
3559 for (unsigned i = 0; i != NumOps; ++i) {
3560 if (Ops[i] != N->getOperand(i)) {
3561 AnyChange = true;
3562 break;
3563 }
3564 }
3565
3566 // No operands changed, just return the input node.
3567 if (!AnyChange) return InN;
3568
3569 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003570 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003571 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003572 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003573
Dan Gohman7ceda162008-05-02 00:05:03 +00003574 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003575 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003576 RemoveNodeFromCSEMaps(N);
3577
3578 // Now we update the operands.
3579 for (unsigned i = 0; i != NumOps; ++i) {
3580 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003581 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003582 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003583 N->OperandList[i].setUser(N);
3584 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003585 }
3586 }
3587
3588 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003589 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003590 return InN;
3591}
3592
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003593/// MorphNodeTo - This frees the operands of the current node, resets the
3594/// opcode, types, and operands to the specified value. This should only be
3595/// used by the SelectionDAG class.
3596void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman35b31be2008-04-17 23:02:12 +00003597 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003598 NodeType = Opc;
3599 ValueList = L.VTs;
3600 NumValues = L.NumVTs;
3601
3602 // Clear the operands list, updating used nodes to remove this from their
3603 // use list.
3604 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003605 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003606
3607 // If NumOps is larger than the # of operands we currently have, reallocate
3608 // the operand list.
3609 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003610 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003611 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003612 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003613 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003614 OperandsNeedDelete = true;
3615 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003616
3617 // Assign the new operands.
3618 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003619
3620 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3621 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003622 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003623 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003624 N->addUser(i, this);
3625 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003626 }
3627}
Chris Lattner149c58c2005-08-16 18:17:10 +00003628
3629/// SelectNodeTo - These are used for target selectors to *mutate* the
3630/// specified node to have the specified return type, Target opcode, and
3631/// operands. Note that target opcodes are stored as
3632/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003633///
3634/// Note that SelectNodeTo returns the resultant node. If there is already a
3635/// node of the specified opcode and operands, it returns that node instead of
3636/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003637SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003638 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003639 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003640 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00003641 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003642 void *IP = 0;
3643 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003644 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003645
Chris Lattner7651fa42005-08-24 23:00:29 +00003646 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003647
Dan Gohman35b31be2008-04-17 23:02:12 +00003648 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, SDOperandPtr(), 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003649
Chris Lattner4a283e92006-08-11 18:38:11 +00003650 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003651 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003652}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003653
Evan Cheng95514ba2006-08-26 08:00:10 +00003654SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003655 MVT VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003656 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003657 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003658 SDOperand Ops[] = { Op1 };
3659
Jim Laskey583bd472006-10-27 23:46:08 +00003660 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003661 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003662 void *IP = 0;
3663 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003664 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003665
Chris Lattner149c58c2005-08-16 18:17:10 +00003666 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003667 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003668 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003669 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003670}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003671
Evan Cheng95514ba2006-08-26 08:00:10 +00003672SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003673 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003674 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003675 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003676 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003677 SDOperand Ops[] = { Op1, Op2 };
3678
Jim Laskey583bd472006-10-27 23:46:08 +00003679 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003680 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003681 void *IP = 0;
3682 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003683 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003684
Chris Lattner149c58c2005-08-16 18:17:10 +00003685 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003686
Chris Lattner63e3f142007-02-04 07:28:00 +00003687 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003688
Chris Lattnera5682852006-08-07 23:03:03 +00003689 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003690 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003691}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003692
Evan Cheng95514ba2006-08-26 08:00:10 +00003693SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003694 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003695 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003696 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003697 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003698 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003699 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003700 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003701 void *IP = 0;
3702 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003703 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003704
Chris Lattner149c58c2005-08-16 18:17:10 +00003705 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003706
Chris Lattner63e3f142007-02-04 07:28:00 +00003707 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003708
Chris Lattnera5682852006-08-07 23:03:03 +00003709 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003710 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003711}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003712
Evan Cheng95514ba2006-08-26 08:00:10 +00003713SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003714 MVT VT, SDOperandPtr Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003715 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003716 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003717 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003718 FoldingSetNodeID ID;
3719 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003720 void *IP = 0;
3721 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003722 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003723
Chris Lattner6b09a292005-08-21 18:49:33 +00003724 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003725 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003726
Chris Lattnera5682852006-08-07 23:03:03 +00003727 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003728 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003729}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003730
Evan Cheng95514ba2006-08-26 08:00:10 +00003731SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003732 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003733 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003734 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003735 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003736 SDOperand Ops[] = { Op1, Op2 };
3737 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003738 void *IP = 0;
3739 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003740 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003741
Chris Lattner0fb094f2005-11-19 01:44:53 +00003742 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003743 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003744 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003745 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003746}
3747
Evan Cheng95514ba2006-08-26 08:00:10 +00003748SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003749 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003750 SDOperand Op1, SDOperand Op2,
3751 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003752 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003753 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003754 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003755 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003756 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003757 void *IP = 0;
3758 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003759 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003760
Chris Lattner0fb094f2005-11-19 01:44:53 +00003761 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003762
Chris Lattner63e3f142007-02-04 07:28:00 +00003763 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003764 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003765 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003766}
3767
Chris Lattner0fb094f2005-11-19 01:44:53 +00003768
Evan Cheng6ae46c42006-02-09 07:15:23 +00003769/// getTargetNode - These are used for target selectors to create a new node
3770/// with specified return type(s), target opcode, and operands.
3771///
3772/// Note that getTargetNode returns the resultant node. If there is already a
3773/// node of the specified opcode and operands, it returns that node instead of
3774/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003775SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003776 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3777}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003778SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003779 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3780}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003781SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003782 SDOperand Op1, SDOperand Op2) {
3783 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3784}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003785SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003786 SDOperand Op1, SDOperand Op2,
3787 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003788 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3789}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003790SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003791 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003792 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003793}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003794SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3795 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003796 SDOperand Op;
3797 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3798}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003799SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3800 MVT VT2, SDOperand Op1) {
3801 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003802 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003803}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003804SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3805 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003806 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003807 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003808 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003809 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003810}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003811SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3812 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003813 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003814 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003815 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003816 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003817}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003818SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003819 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003820 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003821 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003822}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003823SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003824 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003825 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003826 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003827 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003828}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003830 SDOperand Op1, SDOperand Op2,
3831 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003832 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003833 SDOperand Ops[] = { Op1, Op2, Op3 };
3834 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3835}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003836SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003837 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003838 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003839 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003840}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003841SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3842 MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003843 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003844 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003845 VTList.push_back(VT1);
3846 VTList.push_back(VT2);
3847 VTList.push_back(VT3);
3848 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003849 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003850 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3851}
Evan Cheng39305cf2007-10-05 01:10:49 +00003852SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003853 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003854 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003855 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003856 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3857 Ops, NumOps).Val;
3858}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003859
Evan Cheng08b11732008-03-22 01:55:50 +00003860/// getNodeIfExists - Get the specified node if it's already available, or
3861/// else return NULL.
3862SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003863 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003864 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3865 FoldingSetNodeID ID;
3866 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3867 void *IP = 0;
3868 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3869 return E;
3870 }
3871 return NULL;
3872}
3873
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003874
Evan Cheng99157a02006-08-07 22:13:29 +00003875/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003876/// This can cause recursive merging of nodes in the DAG.
3877///
Chris Lattner11d049c2008-02-03 03:35:22 +00003878/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003879///
Chris Lattner11d049c2008-02-03 03:35:22 +00003880void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003881 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003882 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003883 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003884 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003885 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003886
Chris Lattner8b8749f2005-08-17 19:00:20 +00003887 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003888 SDNode::use_iterator UI = From->use_begin();
3889 SDNode *U = UI->getUser();
3890
Chris Lattner8b8749f2005-08-17 19:00:20 +00003891 // This node is about to morph, remove its old self from the CSE maps.
3892 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003893 int operandNum = 0;
3894 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3895 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003896 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003897 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003898 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003899 I->setUser(U);
3900 To.Val->addUser(operandNum, U);
3901 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003902
3903 // Now that we have modified U, add it back to the CSE maps. If it already
3904 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003905 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003906 ReplaceAllUsesWith(U, Existing, UpdateListener);
3907 // U is now dead. Inform the listener if it exists and delete it.
3908 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003909 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003910 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003911 } else {
3912 // If the node doesn't already exist, we updated it. Inform a listener if
3913 // it exists.
3914 if (UpdateListener)
3915 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003916 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003917 }
3918}
3919
3920/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3921/// This can cause recursive merging of nodes in the DAG.
3922///
3923/// This version assumes From/To have matching types and numbers of result
3924/// values.
3925///
Chris Lattner1e111c72005-09-07 05:37:01 +00003926void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003927 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003928 assert(From != To && "Cannot replace uses of with self");
3929 assert(From->getNumValues() == To->getNumValues() &&
3930 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003931 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003932 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3933 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003934
3935 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003936 SDNode::use_iterator UI = From->use_begin();
3937 SDNode *U = UI->getUser();
3938
Chris Lattner8b52f212005-08-26 18:36:28 +00003939 // This node is about to morph, remove its old self from the CSE maps.
3940 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003941 int operandNum = 0;
3942 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3943 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003944 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003945 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003946 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003947 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003948 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003949
Chris Lattner8b8749f2005-08-17 19:00:20 +00003950 // Now that we have modified U, add it back to the CSE maps. If it already
3951 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003952 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003953 ReplaceAllUsesWith(U, Existing, UpdateListener);
3954 // U is now dead. Inform the listener if it exists and delete it.
3955 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003956 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003957 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003958 } else {
3959 // If the node doesn't already exist, we updated it. Inform a listener if
3960 // it exists.
3961 if (UpdateListener)
3962 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003963 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003964 }
3965}
3966
Chris Lattner8b52f212005-08-26 18:36:28 +00003967/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3968/// This can cause recursive merging of nodes in the DAG.
3969///
3970/// This version can replace From with any result values. To must match the
3971/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003972void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003973 SDOperandPtr To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003974 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003975 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003976 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003977
3978 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003979 SDNode::use_iterator UI = From->use_begin();
3980 SDNode *U = UI->getUser();
3981
Chris Lattner7b2880c2005-08-24 22:44:39 +00003982 // This node is about to morph, remove its old self from the CSE maps.
3983 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003984 int operandNum = 0;
3985 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3986 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003987 if (I->getVal() == From) {
3988 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003989 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00003990 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003991 I->setUser(U);
3992 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003993 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003994
Chris Lattner7b2880c2005-08-24 22:44:39 +00003995 // Now that we have modified U, add it back to the CSE maps. If it already
3996 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003997 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003998 ReplaceAllUsesWith(U, Existing, UpdateListener);
3999 // U is now dead. Inform the listener if it exists and delete it.
4000 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004001 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004002 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004003 } else {
4004 // If the node doesn't already exist, we updated it. Inform a listener if
4005 // it exists.
4006 if (UpdateListener)
4007 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004008 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004009 }
4010}
4011
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004012namespace {
4013 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4014 /// any deleted nodes from the set passed into its constructor and recursively
4015 /// notifies another update listener if specified.
4016 class ChainedSetUpdaterListener :
4017 public SelectionDAG::DAGUpdateListener {
4018 SmallSetVector<SDNode*, 16> &Set;
4019 SelectionDAG::DAGUpdateListener *Chain;
4020 public:
4021 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4022 SelectionDAG::DAGUpdateListener *chain)
4023 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004024
Duncan Sandsedfcf592008-06-11 11:42:12 +00004025 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004026 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004027 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004028 }
4029 virtual void NodeUpdated(SDNode *N) {
4030 if (Chain) Chain->NodeUpdated(N);
4031 }
4032 };
4033}
4034
Chris Lattner012f2412006-02-17 21:58:01 +00004035/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4036/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004037/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004038void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004039 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004040 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004041
Chris Lattner012f2412006-02-17 21:58:01 +00004042 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004043 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004044 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004045 return;
4046 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004047
4048 if (From.use_empty()) return;
4049
Chris Lattnercf5640b2007-02-04 00:14:31 +00004050 // Get all of the users of From.Val. We want these in a nice,
4051 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004052 SmallSetVector<SDNode*, 16> Users;
4053 for (SDNode::use_iterator UI = From.Val->use_begin(),
4054 E = From.Val->use_end(); UI != E; ++UI) {
4055 SDNode *User = UI->getUser();
4056 if (!Users.count(User))
4057 Users.insert(User);
4058 }
Chris Lattner012f2412006-02-17 21:58:01 +00004059
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004060 // When one of the recursive merges deletes nodes from the graph, we need to
4061 // make sure that UpdateListener is notified *and* that the node is removed
4062 // from Users if present. CSUL does this.
4063 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004064
Chris Lattner012f2412006-02-17 21:58:01 +00004065 while (!Users.empty()) {
4066 // We know that this user uses some value of From. If it is the right
4067 // value, update it.
4068 SDNode *User = Users.back();
4069 Users.pop_back();
4070
Chris Lattner01d029b2007-10-15 06:10:22 +00004071 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004072 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004073 for (; Op != E; ++Op)
4074 if (*Op == From) break;
4075
4076 // If there are no matches, the user must use some other result of From.
4077 if (Op == E) continue;
4078
4079 // Okay, we know this user needs to be updated. Remove its old self
4080 // from the CSE maps.
4081 RemoveNodeFromCSEMaps(User);
4082
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004083 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004084 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004085 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004086 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004087 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004088 Op->setUser(User);
4089 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004090 }
4091 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004092
4093 // Now that we have modified User, add it back to the CSE maps. If it
4094 // already exists there, recursively merge the results together.
4095 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004096 if (!Existing) {
4097 if (UpdateListener) UpdateListener->NodeUpdated(User);
4098 continue; // Continue on to next user.
4099 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004100
4101 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004102 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004103 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004104 // can cause deletion of nodes that used the old value. To handle this, we
4105 // use CSUL to remove them from the Users set.
4106 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004107
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004108 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004109 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004110 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004111 }
4112}
4113
Evan Chenge6f35d82006-08-01 08:20:41 +00004114/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4115/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004116unsigned SelectionDAG::AssignNodeIds() {
4117 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004118 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4119 SDNode *N = I;
4120 N->setNodeId(Id++);
4121 }
4122 return Id;
4123}
4124
Evan Chenge6f35d82006-08-01 08:20:41 +00004125/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004126/// based on their topological order. It returns the maximum id and a vector
4127/// of the SDNodes* in assigned order by reference.
4128unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004129 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004130 std::vector<unsigned> InDegree(DAGSize);
4131 std::vector<SDNode*> Sources;
4132
4133 // Use a two pass approach to avoid using a std::map which is slow.
4134 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004135 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4136 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004137 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004138 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004139 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004140 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004141 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004142 }
4143
Evan Cheng99157a02006-08-07 22:13:29 +00004144 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004145 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004146 SDNode *N = Sources.back();
4147 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004148 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004149 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004150 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004151 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004152 if (Degree == 0)
4153 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004154 }
4155 }
4156
Evan Chengc384d6c2006-08-02 22:00:34 +00004157 // Second pass, assign the actual topological order as node ids.
4158 Id = 0;
4159 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4160 TI != TE; ++TI)
4161 (*TI)->setNodeId(Id++);
4162
4163 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004164}
4165
4166
Evan Cheng091cba12006-07-27 06:39:06 +00004167
Jim Laskey58b968b2005-08-17 20:08:02 +00004168//===----------------------------------------------------------------------===//
4169// SDNode Class
4170//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004171
Chris Lattner917d2c92006-07-19 00:00:37 +00004172// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004173void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004174void UnarySDNode::ANCHOR() {}
4175void BinarySDNode::ANCHOR() {}
4176void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004177void HandleSDNode::ANCHOR() {}
4178void StringSDNode::ANCHOR() {}
4179void ConstantSDNode::ANCHOR() {}
4180void ConstantFPSDNode::ANCHOR() {}
4181void GlobalAddressSDNode::ANCHOR() {}
4182void FrameIndexSDNode::ANCHOR() {}
4183void JumpTableSDNode::ANCHOR() {}
4184void ConstantPoolSDNode::ANCHOR() {}
4185void BasicBlockSDNode::ANCHOR() {}
4186void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004187void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004188void RegisterSDNode::ANCHOR() {}
4189void ExternalSymbolSDNode::ANCHOR() {}
4190void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004191void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004192void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004193void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004194void LoadSDNode::ANCHOR() {}
4195void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004196void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004197
Chris Lattner48b85922007-02-04 02:41:42 +00004198HandleSDNode::~HandleSDNode() {
4199 SDVTList VTs = { 0, 0 };
Dan Gohman35b31be2008-04-17 23:02:12 +00004200 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00004201}
4202
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004203GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004204 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004205 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004206 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004207 // Thread Local
4208 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4209 // Non Thread Local
4210 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4211 getSDVTList(VT)), Offset(o) {
4212 TheGlobal = const_cast<GlobalValue*>(GA);
4213}
Chris Lattner48b85922007-02-04 02:41:42 +00004214
Dan Gohman36b5c132008-04-07 19:35:22 +00004215/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004216/// reference performed by this atomic.
4217MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004218 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004219 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4220 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4221
4222 // Check if the atomic references a frame index
4223 const FrameIndexSDNode *FI =
4224 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4225 if (!getSrcValue() && FI)
4226 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4227 FI->getIndex(), Size, getAlignment());
4228 else
4229 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4230 Size, getAlignment());
4231}
4232
4233/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004234/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004235MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004236 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004237 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004238 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4239 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004240 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004241
4242 // Check if the load references a frame index, and does not have
4243 // an SV attached.
4244 const FrameIndexSDNode *FI =
4245 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4246 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004247 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004248 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004249 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004250 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004251 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004252}
4253
Jim Laskey583bd472006-10-27 23:46:08 +00004254/// Profile - Gather unique data for the node.
4255///
4256void SDNode::Profile(FoldingSetNodeID &ID) {
4257 AddNodeIDNode(ID, this);
4258}
4259
Chris Lattnera3255112005-11-08 23:30:28 +00004260/// getValueTypeList - Return a pointer to the specified value type.
4261///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004262const MVT *SDNode::getValueTypeList(MVT VT) {
4263 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004264 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004265 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004266 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004267 static MVT VTs[MVT::LAST_VALUETYPE];
4268 VTs[VT.getSimpleVT()] = VT;
4269 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004270 }
Chris Lattnera3255112005-11-08 23:30:28 +00004271}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004272
Chris Lattner5c884562005-01-12 18:37:47 +00004273/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4274/// indicated value. This method ignores uses of other values defined by this
4275/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004276bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004277 assert(Value < getNumValues() && "Bad value!");
4278
4279 // If there is only one value, this is easy.
4280 if (getNumValues() == 1)
4281 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004282 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004283
Evan Cheng4ee62112006-02-05 06:29:23 +00004284 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004285
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004286 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004287
Roman Levensteindc1adac2008-04-07 10:06:32 +00004288 // TODO: Only iterate over uses of a given value of the node
4289 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4290 if (*UI == TheValue) {
4291 if (NUses == 0)
4292 return false;
4293 --NUses;
4294 }
Chris Lattner5c884562005-01-12 18:37:47 +00004295 }
4296
4297 // Found exactly the right number of uses?
4298 return NUses == 0;
4299}
4300
4301
Evan Cheng33d55952007-08-02 05:29:38 +00004302/// hasAnyUseOfValue - Return true if there are any use of the indicated
4303/// value. This method ignores uses of other values defined by this operation.
4304bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4305 assert(Value < getNumValues() && "Bad value!");
4306
Dan Gohman30359592008-01-29 13:02:09 +00004307 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004308
4309 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4310
4311 SmallPtrSet<SDNode*, 32> UsersHandled;
4312
Roman Levensteindc1adac2008-04-07 10:06:32 +00004313 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4314 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004315 if (User->getNumOperands() == 1 ||
4316 UsersHandled.insert(User)) // First time we've seen this?
4317 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4318 if (User->getOperand(i) == TheValue) {
4319 return true;
4320 }
4321 }
4322
4323 return false;
4324}
4325
4326
Evan Cheng917be682008-03-04 00:41:45 +00004327/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004328///
Evan Cheng917be682008-03-04 00:41:45 +00004329bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004330 bool Seen = false;
4331 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004332 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004333 if (User == this)
4334 Seen = true;
4335 else
4336 return false;
4337 }
4338
4339 return Seen;
4340}
4341
Evan Chenge6e97e62006-11-03 07:31:32 +00004342/// isOperand - Return true if this node is an operand of N.
4343///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004344bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004345 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4346 if (*this == N->getOperand(i))
4347 return true;
4348 return false;
4349}
4350
Evan Cheng917be682008-03-04 00:41:45 +00004351bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004352 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004353 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004354 return true;
4355 return false;
4356}
Evan Cheng4ee62112006-02-05 06:29:23 +00004357
Chris Lattner572dee72008-01-16 05:49:24 +00004358/// reachesChainWithoutSideEffects - Return true if this operand (which must
4359/// be a chain) reaches the specified operand without crossing any
4360/// side-effecting instructions. In practice, this looks through token
4361/// factors and non-volatile loads. In order to remain efficient, this only
4362/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004363bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004364 unsigned Depth) const {
4365 if (*this == Dest) return true;
4366
4367 // Don't search too deeply, we just want to be able to see through
4368 // TokenFactor's etc.
4369 if (Depth == 0) return false;
4370
4371 // If this is a token factor, all inputs to the TF happen in parallel. If any
4372 // of the operands of the TF reach dest, then we can do the xform.
4373 if (getOpcode() == ISD::TokenFactor) {
4374 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4375 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4376 return true;
4377 return false;
4378 }
4379
4380 // Loads don't have side effects, look through them.
4381 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4382 if (!Ld->isVolatile())
4383 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4384 }
4385 return false;
4386}
4387
4388
Evan Chengc5fc57d2006-11-03 03:05:24 +00004389static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004390 SmallPtrSet<SDNode *, 32> &Visited) {
4391 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004392 return;
4393
4394 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4395 SDNode *Op = N->getOperand(i).Val;
4396 if (Op == P) {
4397 found = true;
4398 return;
4399 }
4400 findPredecessor(Op, P, found, Visited);
4401 }
4402}
4403
Evan Cheng917be682008-03-04 00:41:45 +00004404/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004405/// is either an operand of N or it can be reached by recursively traversing
4406/// up the operands.
4407/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004408bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004409 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004410 bool found = false;
4411 findPredecessor(N, this, found, Visited);
4412 return found;
4413}
4414
Evan Chengc5484282006-10-04 00:56:09 +00004415uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4416 assert(Num < NumOperands && "Invalid child # of SDNode!");
4417 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4418}
4419
Reid Spencer577cc322007-04-01 07:32:19 +00004420std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004421 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004422 default:
4423 if (getOpcode() < ISD::BUILTIN_OP_END)
4424 return "<<Unknown DAG Node>>";
4425 else {
Evan Cheng72261582005-12-20 06:22:03 +00004426 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004427 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004428 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004429 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004430
Evan Cheng72261582005-12-20 06:22:03 +00004431 TargetLowering &TLI = G->getTargetLoweringInfo();
4432 const char *Name =
4433 TLI.getTargetNodeName(getOpcode());
4434 if (Name) return Name;
4435 }
4436
4437 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004438 }
4439
Evan Cheng27b7db52008-03-08 00:58:38 +00004440 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004441 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004442 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4443 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4444 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004445 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4446 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4447 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004448 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004449 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4450 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4451 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4452 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4453 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004454 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004455 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004456 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004457 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004458 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004459 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004460 case ISD::AssertSext: return "AssertSext";
4461 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004462
4463 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004464 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004465 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004466 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004467 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004468
4469 case ISD::Constant: return "Constant";
4470 case ISD::ConstantFP: return "ConstantFP";
4471 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004472 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004473 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004474 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004475 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004476 case ISD::RETURNADDR: return "RETURNADDR";
4477 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004478 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004479 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4480 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004481 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004482 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004483 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004484 case ISD::INTRINSIC_WO_CHAIN: {
4485 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4486 return Intrinsic::getName((Intrinsic::ID)IID);
4487 }
4488 case ISD::INTRINSIC_VOID:
4489 case ISD::INTRINSIC_W_CHAIN: {
4490 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004491 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004492 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004493
Chris Lattnerb2827b02006-03-19 00:52:58 +00004494 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004495 case ISD::TargetConstant: return "TargetConstant";
4496 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004497 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004498 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004499 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004500 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004501 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004502 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004503
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004504 case ISD::CopyToReg: return "CopyToReg";
4505 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004506 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004507 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004508 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00004509 case ISD::LABEL: return "label";
Evan Chenga844bde2008-02-02 04:07:54 +00004510 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004511 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004512 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004513 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004514
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004515 // Unary operators
4516 case ISD::FABS: return "fabs";
4517 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004518 case ISD::FSQRT: return "fsqrt";
4519 case ISD::FSIN: return "fsin";
4520 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004521 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004522 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004523
4524 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004525 case ISD::ADD: return "add";
4526 case ISD::SUB: return "sub";
4527 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004528 case ISD::MULHU: return "mulhu";
4529 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004530 case ISD::SDIV: return "sdiv";
4531 case ISD::UDIV: return "udiv";
4532 case ISD::SREM: return "srem";
4533 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004534 case ISD::SMUL_LOHI: return "smul_lohi";
4535 case ISD::UMUL_LOHI: return "umul_lohi";
4536 case ISD::SDIVREM: return "sdivrem";
4537 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004538 case ISD::AND: return "and";
4539 case ISD::OR: return "or";
4540 case ISD::XOR: return "xor";
4541 case ISD::SHL: return "shl";
4542 case ISD::SRA: return "sra";
4543 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004544 case ISD::ROTL: return "rotl";
4545 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004546 case ISD::FADD: return "fadd";
4547 case ISD::FSUB: return "fsub";
4548 case ISD::FMUL: return "fmul";
4549 case ISD::FDIV: return "fdiv";
4550 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004551 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004552 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004553
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004554 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004555 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004556 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004557 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004558 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004559 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004560 case ISD::CONCAT_VECTORS: return "concat_vectors";
4561 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004562 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004563 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004564 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004565 case ISD::ADDC: return "addc";
4566 case ISD::ADDE: return "adde";
4567 case ISD::SUBC: return "subc";
4568 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004569 case ISD::SHL_PARTS: return "shl_parts";
4570 case ISD::SRA_PARTS: return "sra_parts";
4571 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004572
4573 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4574 case ISD::INSERT_SUBREG: return "insert_subreg";
4575
Chris Lattner7f644642005-04-28 21:44:03 +00004576 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004577 case ISD::SIGN_EXTEND: return "sign_extend";
4578 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004579 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004580 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004581 case ISD::TRUNCATE: return "truncate";
4582 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004583 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004584 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004585 case ISD::FP_EXTEND: return "fp_extend";
4586
4587 case ISD::SINT_TO_FP: return "sint_to_fp";
4588 case ISD::UINT_TO_FP: return "uint_to_fp";
4589 case ISD::FP_TO_SINT: return "fp_to_sint";
4590 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004591 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004592
4593 // Control flow instructions
4594 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004595 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004596 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004597 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004598 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004599 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004600 case ISD::CALLSEQ_START: return "callseq_start";
4601 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004602
4603 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004604 case ISD::LOAD: return "load";
4605 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004606 case ISD::VAARG: return "vaarg";
4607 case ISD::VACOPY: return "vacopy";
4608 case ISD::VAEND: return "vaend";
4609 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004610 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004611 case ISD::EXTRACT_ELEMENT: return "extract_element";
4612 case ISD::BUILD_PAIR: return "build_pair";
4613 case ISD::STACKSAVE: return "stacksave";
4614 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004615 case ISD::TRAP: return "trap";
4616
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004617 // Bit manipulation
4618 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004619 case ISD::CTPOP: return "ctpop";
4620 case ISD::CTTZ: return "cttz";
4621 case ISD::CTLZ: return "ctlz";
4622
Chris Lattner36ce6912005-11-29 06:21:05 +00004623 // Debug info
4624 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004625 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004626
Duncan Sands36397f52007-07-27 12:58:54 +00004627 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004628 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004629
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004630 case ISD::CONDCODE:
4631 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004632 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004633 case ISD::SETOEQ: return "setoeq";
4634 case ISD::SETOGT: return "setogt";
4635 case ISD::SETOGE: return "setoge";
4636 case ISD::SETOLT: return "setolt";
4637 case ISD::SETOLE: return "setole";
4638 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004639
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004640 case ISD::SETO: return "seto";
4641 case ISD::SETUO: return "setuo";
4642 case ISD::SETUEQ: return "setue";
4643 case ISD::SETUGT: return "setugt";
4644 case ISD::SETUGE: return "setuge";
4645 case ISD::SETULT: return "setult";
4646 case ISD::SETULE: return "setule";
4647 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004648
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004649 case ISD::SETEQ: return "seteq";
4650 case ISD::SETGT: return "setgt";
4651 case ISD::SETGE: return "setge";
4652 case ISD::SETLT: return "setlt";
4653 case ISD::SETLE: return "setle";
4654 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004655 }
4656 }
4657}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004658
Evan Cheng144d8f02006-11-09 17:55:04 +00004659const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004660 switch (AM) {
4661 default:
4662 return "";
4663 case ISD::PRE_INC:
4664 return "<pre-inc>";
4665 case ISD::PRE_DEC:
4666 return "<pre-dec>";
4667 case ISD::POST_INC:
4668 return "<post-inc>";
4669 case ISD::POST_DEC:
4670 return "<post-dec>";
4671 }
4672}
4673
Duncan Sands276dcbd2008-03-21 09:14:45 +00004674std::string ISD::ArgFlagsTy::getArgFlagsString() {
4675 std::string S = "< ";
4676
4677 if (isZExt())
4678 S += "zext ";
4679 if (isSExt())
4680 S += "sext ";
4681 if (isInReg())
4682 S += "inreg ";
4683 if (isSRet())
4684 S += "sret ";
4685 if (isByVal())
4686 S += "byval ";
4687 if (isNest())
4688 S += "nest ";
4689 if (getByValAlign())
4690 S += "byval-align:" + utostr(getByValAlign()) + " ";
4691 if (getOrigAlign())
4692 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4693 if (getByValSize())
4694 S += "byval-size:" + utostr(getByValSize()) + " ";
4695 return S + ">";
4696}
4697
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004698void SDNode::dump() const { dump(0); }
4699void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004700 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004701
4702 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004703 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004704 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004705 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004706 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004707 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004708 }
Bill Wendling832171c2006-12-07 20:04:42 +00004709 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004710
Bill Wendling832171c2006-12-07 20:04:42 +00004711 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004712 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004713 if (i) cerr << ", ";
4714 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004715 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004716 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004717 }
4718
Evan Chengce254432007-12-11 02:08:35 +00004719 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4720 SDNode *Mask = getOperand(2).Val;
4721 cerr << "<";
4722 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4723 if (i) cerr << ",";
4724 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4725 cerr << "u";
4726 else
4727 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4728 }
4729 cerr << ">";
4730 }
4731
Chris Lattnerc3aae252005-01-07 07:46:32 +00004732 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004733 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004734 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004735 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4736 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4737 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4738 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4739 else {
4740 cerr << "<APFloat(";
4741 CSDN->getValueAPF().convertToAPInt().dump();
4742 cerr << ")>";
4743 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004744 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004745 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004746 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004747 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004748 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004749 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004750 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004751 else
Bill Wendling832171c2006-12-07 20:04:42 +00004752 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004753 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004754 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004755 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004756 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004757 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004758 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004759 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004760 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004761 else
Bill Wendling832171c2006-12-07 20:04:42 +00004762 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004763 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004764 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004765 else
Bill Wendling832171c2006-12-07 20:04:42 +00004766 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004767 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004768 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004769 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4770 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004771 cerr << LBB->getName() << " ";
4772 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004773 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004774 if (G && R->getReg() &&
4775 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004776 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004777 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004778 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004779 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004780 } else if (const ExternalSymbolSDNode *ES =
4781 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004782 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004783 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4784 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004785 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004786 else
Dan Gohman69de1932008-02-06 22:27:42 +00004787 cerr << "<null>";
4788 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4789 if (M->MO.getValue())
4790 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4791 else
4792 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004793 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4794 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004795 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004796 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004797 }
4798 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004799 const Value *SrcValue = LD->getSrcValue();
4800 int SrcOffset = LD->getSrcValueOffset();
4801 cerr << " <";
4802 if (SrcValue)
4803 cerr << SrcValue;
4804 else
4805 cerr << "null";
4806 cerr << ":" << SrcOffset << ">";
4807
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004808 bool doExt = true;
4809 switch (LD->getExtensionType()) {
4810 default: doExt = false; break;
4811 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004812 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004813 break;
4814 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004815 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004816 break;
4817 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004818 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004819 break;
4820 }
4821 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004822 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004823
Evan Cheng144d8f02006-11-09 17:55:04 +00004824 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004825 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004826 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004827 if (LD->isVolatile())
4828 cerr << " <volatile>";
4829 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004830 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004831 const Value *SrcValue = ST->getSrcValue();
4832 int SrcOffset = ST->getSrcValueOffset();
4833 cerr << " <";
4834 if (SrcValue)
4835 cerr << SrcValue;
4836 else
4837 cerr << "null";
4838 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004839
4840 if (ST->isTruncatingStore())
4841 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004842 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004843
4844 const char *AM = getIndexedModeName(ST->getAddressingMode());
4845 if (*AM)
4846 cerr << " " << AM;
4847 if (ST->isVolatile())
4848 cerr << " <volatile>";
4849 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004850 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4851 const Value *SrcValue = AT->getSrcValue();
4852 int SrcOffset = AT->getSrcValueOffset();
4853 cerr << " <";
4854 if (SrcValue)
4855 cerr << SrcValue;
4856 else
4857 cerr << "null";
4858 cerr << ":" << SrcOffset << ">";
4859 if (AT->isVolatile())
4860 cerr << " <volatile>";
4861 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004862 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004863}
4864
Chris Lattnerde202b32005-11-09 23:47:37 +00004865static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004866 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4867 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004868 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004869 else
Bill Wendling832171c2006-12-07 20:04:42 +00004870 cerr << "\n" << std::string(indent+2, ' ')
4871 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004872
Chris Lattnerea946cd2005-01-09 20:38:33 +00004873
Bill Wendling832171c2006-12-07 20:04:42 +00004874 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004875 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004876}
4877
Chris Lattnerc3aae252005-01-07 07:46:32 +00004878void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004879 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004880 std::vector<const SDNode*> Nodes;
4881 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4882 I != E; ++I)
4883 Nodes.push_back(I);
4884
Chris Lattner49d24712005-01-09 20:26:36 +00004885 std::sort(Nodes.begin(), Nodes.end());
4886
4887 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004888 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004889 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004890 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004891
Jim Laskey26f7fa72006-10-17 19:33:52 +00004892 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004893
Bill Wendling832171c2006-12-07 20:04:42 +00004894 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004895}
4896
Evan Chengd6594ae2006-09-12 21:00:35 +00004897const Type *ConstantPoolSDNode::getType() const {
4898 if (isMachineConstantPoolEntry())
4899 return Val.MachineCPVal->getType();
4900 return Val.ConstVal->getType();
4901}