blob: 17a5fa7d84630b8e9c72e500100ef0d4246686ee [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"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000027#include "llvm/Support/MathExtras.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000029#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000030#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000033#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000034#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000035#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000036#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000037#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000038#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000039#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattner0b3e5252006-08-15 19:11:05 +000042/// makeVTList - Return an instance of the SDVTList struct initialized with the
43/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000044static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000045 SDVTList Res = {VTs, NumVTs};
46 return Res;
47}
48
Duncan Sands83ec4b62008-06-06 12:08:01 +000049static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
50 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000051 default: assert(0 && "Unknown FP format");
52 case MVT::f32: return &APFloat::IEEEsingle;
53 case MVT::f64: return &APFloat::IEEEdouble;
54 case MVT::f80: return &APFloat::x87DoubleExtended;
55 case MVT::f128: return &APFloat::IEEEquad;
56 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
57 }
58}
59
Chris Lattnerf8dc0612008-02-03 06:49:24 +000060SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
61
Jim Laskey58b968b2005-08-17 20:08:02 +000062//===----------------------------------------------------------------------===//
63// ConstantFPSDNode Class
64//===----------------------------------------------------------------------===//
65
66/// isExactlyValue - We don't rely on operator== working on double values, as
67/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
68/// As such, this method can be used to do an exact bit-for-bit comparison of
69/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000070bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000071 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000072}
73
Duncan Sands83ec4b62008-06-06 12:08:01 +000074bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000075 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000076 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000077
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000078 // PPC long double cannot be converted to any other type.
79 if (VT == MVT::ppcf128 ||
80 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000081 return false;
82
Dale Johannesenf04afdb2007-08-30 00:23:21 +000083 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000085 return Val2.convert(*MVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000087}
88
Jim Laskey58b968b2005-08-17 20:08:02 +000089//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000090// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000092
Evan Chenga8df1662006-03-27 06:58:47 +000093/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000094/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000095bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000096 // Look through a bit convert.
97 if (N->getOpcode() == ISD::BIT_CONVERT)
98 N = N->getOperand(0).Val;
99
Evan Chenga8df1662006-03-27 06:58:47 +0000100 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000101
102 unsigned i = 0, e = N->getNumOperands();
103
104 // Skip over all of the undef values.
105 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
106 ++i;
107
108 // Do not accept an all-undef vector.
109 if (i == e) return false;
110
111 // Do not accept build_vectors that aren't all constants or which have non-~0
112 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000113 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000114 if (isa<ConstantSDNode>(NotZero)) {
115 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
116 return false;
117 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000118 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
119 convertToAPInt().isAllOnesValue())
120 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000121 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000122 return false;
123
124 // Okay, we have at least one ~0 value, check to see if the rest match or are
125 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000126 for (++i; i != e; ++i)
127 if (N->getOperand(i) != NotZero &&
128 N->getOperand(i).getOpcode() != ISD::UNDEF)
129 return false;
130 return true;
131}
132
133
Evan Cheng4a147842006-03-26 09:50:58 +0000134/// isBuildVectorAllZeros - Return true if the specified node is a
135/// BUILD_VECTOR where all of the elements are 0 or undef.
136bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000137 // Look through a bit convert.
138 if (N->getOpcode() == ISD::BIT_CONVERT)
139 N = N->getOperand(0).Val;
140
Evan Cheng4a147842006-03-26 09:50:58 +0000141 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000142
143 unsigned i = 0, e = N->getNumOperands();
144
145 // Skip over all of the undef values.
146 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
147 ++i;
148
149 // Do not accept an all-undef vector.
150 if (i == e) return false;
151
152 // Do not accept build_vectors that aren't all constants or which have non-~0
153 // elements.
154 SDOperand Zero = N->getOperand(i);
155 if (isa<ConstantSDNode>(Zero)) {
156 if (!cast<ConstantSDNode>(Zero)->isNullValue())
157 return false;
158 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000159 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000160 return false;
161 } else
162 return false;
163
164 // Okay, we have at least one ~0 value, check to see if the rest match or are
165 // undefs.
166 for (++i; i != e; ++i)
167 if (N->getOperand(i) != Zero &&
168 N->getOperand(i).getOpcode() != ISD::UNDEF)
169 return false;
170 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000171}
172
Evan Chengefec7512008-02-18 23:04:32 +0000173/// isScalarToVector - Return true if the specified node is a
174/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
175/// element is not an undef.
176bool ISD::isScalarToVector(const SDNode *N) {
177 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
178 return true;
179
180 if (N->getOpcode() != ISD::BUILD_VECTOR)
181 return false;
182 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
183 return false;
184 unsigned NumElems = N->getNumOperands();
185 for (unsigned i = 1; i < NumElems; ++i) {
186 SDOperand V = N->getOperand(i);
187 if (V.getOpcode() != ISD::UNDEF)
188 return false;
189 }
190 return true;
191}
192
193
Evan Chengbb81d972008-01-31 09:59:15 +0000194/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000195/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000196bool ISD::isDebugLabel(const SDNode *N) {
197 SDOperand Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000198 if (N->getOpcode() == ISD::DBG_LABEL)
199 return true;
200 if (N->isTargetOpcode() &&
201 N->getTargetOpcode() == TargetInstrInfo::DBG_LABEL)
202 return true;
203 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000204}
205
Chris Lattnerc3aae252005-01-07 07:46:32 +0000206/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
207/// when given the operation for (X op Y).
208ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
209 // To perform this operation, we just need to swap the L and G bits of the
210 // operation.
211 unsigned OldL = (Operation >> 2) & 1;
212 unsigned OldG = (Operation >> 1) & 1;
213 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
214 (OldL << 1) | // New G bit
215 (OldG << 2)); // New L bit.
216}
217
218/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
219/// 'op' is a valid SetCC operation.
220ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
221 unsigned Operation = Op;
222 if (isInteger)
223 Operation ^= 7; // Flip L, G, E bits, but not U.
224 else
225 Operation ^= 15; // Flip all of the condition bits.
226 if (Operation > ISD::SETTRUE2)
227 Operation &= ~8; // Don't let N and U bits get set.
228 return ISD::CondCode(Operation);
229}
230
231
232/// isSignedOp - For an integer comparison, return 1 if the comparison is a
233/// signed operation and 2 if the result is an unsigned comparison. Return zero
234/// if the operation does not depend on the sign of the input (setne and seteq).
235static int isSignedOp(ISD::CondCode Opcode) {
236 switch (Opcode) {
237 default: assert(0 && "Illegal integer setcc operation!");
238 case ISD::SETEQ:
239 case ISD::SETNE: return 0;
240 case ISD::SETLT:
241 case ISD::SETLE:
242 case ISD::SETGT:
243 case ISD::SETGE: return 1;
244 case ISD::SETULT:
245 case ISD::SETULE:
246 case ISD::SETUGT:
247 case ISD::SETUGE: return 2;
248 }
249}
250
251/// getSetCCOrOperation - Return the result of a logical OR between different
252/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
253/// returns SETCC_INVALID if it is not possible to represent the resultant
254/// comparison.
255ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
256 bool isInteger) {
257 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
258 // Cannot fold a signed integer setcc with an unsigned integer setcc.
259 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattnerc3aae252005-01-07 07:46:32 +0000263 // If the N and U bits get set then the resultant comparison DOES suddenly
264 // care about orderedness, and is true when ordered.
265 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000266 Op &= ~16; // Clear the U bit if the N bit is set.
267
268 // Canonicalize illegal integer setcc's.
269 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
270 Op = ISD::SETNE;
271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 return ISD::CondCode(Op);
273}
274
275/// getSetCCAndOperation - Return the result of a logical AND between different
276/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
277/// function returns zero if it is not possible to represent the resultant
278/// comparison.
279ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
280 bool isInteger) {
281 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
282 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000283 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000284
285 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000286 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
287
288 // Canonicalize illegal integer setcc's.
289 if (isInteger) {
290 switch (Result) {
291 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000292 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000293 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000294 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
295 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
296 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000297 }
298 }
299
300 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301}
302
Chris Lattnerb48da392005-01-23 04:39:44 +0000303const TargetMachine &SelectionDAG::getTarget() const {
304 return TLI.getTargetMachine();
305}
306
Jim Laskey58b968b2005-08-17 20:08:02 +0000307//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000308// SDNode Profile Support
309//===----------------------------------------------------------------------===//
310
Jim Laskeydef69b92006-10-27 23:52:51 +0000311/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
312///
Jim Laskey583bd472006-10-27 23:46:08 +0000313static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
314 ID.AddInteger(OpC);
315}
316
317/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
318/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000319static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000320 ID.AddPointer(VTList.VTs);
321}
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOperands(FoldingSetNodeID &ID,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000326 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000327 for (; NumOps; --NumOps, ++Ops) {
328 ID.AddPointer(Ops->Val);
329 ID.AddInteger(Ops->ResNo);
330 }
Jim Laskey583bd472006-10-27 23:46:08 +0000331}
332
Jim Laskey583bd472006-10-27 23:46:08 +0000333static void AddNodeIDNode(FoldingSetNodeID &ID,
334 unsigned short OpC, SDVTList VTList,
Dan Gohman35b31be2008-04-17 23:02:12 +0000335 SDOperandPtr OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000336 AddNodeIDOpcode(ID, OpC);
337 AddNodeIDValueTypes(ID, VTList);
338 AddNodeIDOperands(ID, OpList, N);
339}
340
Roman Levenstein9cac5252008-04-16 16:15:27 +0000341
Jim Laskeydef69b92006-10-27 23:52:51 +0000342/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
343/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000344static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
345 AddNodeIDOpcode(ID, N->getOpcode());
346 // Add the return value info.
347 AddNodeIDValueTypes(ID, N->getVTList());
348 // Add the operand info.
349 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
350
351 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000352 switch (N->getOpcode()) {
353 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000354 case ISD::ARG_FLAGS:
355 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
356 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000357 case ISD::TargetConstant:
358 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000359 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000360 break;
361 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000362 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000363 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000364 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000365 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000367 case ISD::GlobalAddress:
368 case ISD::TargetGlobalTLSAddress:
369 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
371 ID.AddPointer(GA->getGlobal());
372 ID.AddInteger(GA->getOffset());
373 break;
374 }
375 case ISD::BasicBlock:
376 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
377 break;
378 case ISD::Register:
379 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
380 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000381 case ISD::DBG_STOPPOINT: {
382 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
383 ID.AddInteger(DSP->getLine());
384 ID.AddInteger(DSP->getColumn());
385 ID.AddPointer(DSP->getCompileUnit());
386 break;
387 }
Dan Gohman44066042008-07-01 00:05:16 +0000388 case ISD::DBG_LABEL:
389 case ISD::EH_LABEL:
390 ID.AddInteger(cast<LabelSDNode>(N)->getLabelID());
391 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000392 case ISD::SRCVALUE:
393 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
394 break;
395 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000396 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohman69de1932008-02-06 22:27:42 +0000397 ID.AddPointer(MO.getValue());
398 ID.AddInteger(MO.getFlags());
399 ID.AddInteger(MO.getOffset());
400 ID.AddInteger(MO.getSize());
401 ID.AddInteger(MO.getAlignment());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000402 break;
403 }
404 case ISD::FrameIndex:
405 case ISD::TargetFrameIndex:
406 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
407 break;
408 case ISD::JumpTable:
409 case ISD::TargetJumpTable:
410 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
411 break;
412 case ISD::ConstantPool:
413 case ISD::TargetConstantPool: {
414 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
415 ID.AddInteger(CP->getAlignment());
416 ID.AddInteger(CP->getOffset());
417 if (CP->isMachineConstantPoolEntry())
418 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
419 else
420 ID.AddPointer(CP->getConstVal());
421 break;
422 }
423 case ISD::LOAD: {
424 LoadSDNode *LD = cast<LoadSDNode>(N);
425 ID.AddInteger(LD->getAddressingMode());
426 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000427 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000428 ID.AddInteger(LD->getAlignment());
429 ID.AddInteger(LD->isVolatile());
430 break;
431 }
432 case ISD::STORE: {
433 StoreSDNode *ST = cast<StoreSDNode>(N);
434 ID.AddInteger(ST->getAddressingMode());
435 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000436 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000437 ID.AddInteger(ST->getAlignment());
438 ID.AddInteger(ST->isVolatile());
439 break;
440 }
Mon P Wang28873102008-06-25 08:15:39 +0000441 case ISD::ATOMIC_CMP_SWAP:
442 case ISD::ATOMIC_LOAD_ADD:
443 case ISD::ATOMIC_SWAP:
444 case ISD::ATOMIC_LOAD_SUB:
445 case ISD::ATOMIC_LOAD_AND:
446 case ISD::ATOMIC_LOAD_OR:
447 case ISD::ATOMIC_LOAD_XOR:
448 case ISD::ATOMIC_LOAD_NAND:
449 case ISD::ATOMIC_LOAD_MIN:
450 case ISD::ATOMIC_LOAD_MAX:
451 case ISD::ATOMIC_LOAD_UMIN:
452 case ISD::ATOMIC_LOAD_UMAX: {
453 AtomicSDNode *AT = cast<AtomicSDNode>(N);
454 ID.AddInteger(AT->getAlignment());
455 ID.AddInteger(AT->isVolatile());
456 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000457 }
Mon P Wang28873102008-06-25 08:15:39 +0000458 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000459}
460
461//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000462// SelectionDAG Class
463//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000464
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000465/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000466/// SelectionDAG.
467void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000468 // Create a dummy node (which is not added to allnodes), that adds a reference
469 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000470 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000471
Chris Lattner190a4182006-08-04 17:45:20 +0000472 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000473
Chris Lattner190a4182006-08-04 17:45:20 +0000474 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000475 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000476 if (I->use_empty())
477 DeadNodes.push_back(I);
478
479 // Process the worklist, deleting the nodes and adding their uses to the
480 // worklist.
481 while (!DeadNodes.empty()) {
482 SDNode *N = DeadNodes.back();
483 DeadNodes.pop_back();
484
485 // Take the node out of the appropriate CSE map.
486 RemoveNodeFromCSEMaps(N);
487
488 // Next, brutally remove the operand list. This is safe to do, as there are
489 // no cycles in the graph.
490 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000491 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000492 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000493
494 // Now that we removed this operand, see if there are no uses of it left.
495 if (Operand->use_empty())
496 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000497 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000498 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000499 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000500 }
Chris Lattner190a4182006-08-04 17:45:20 +0000501 N->OperandList = 0;
502 N->NumOperands = 0;
503
504 // Finally, remove N itself.
505 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000506 }
507
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000508 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000509 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000510}
511
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000512void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Evan Cheng130a6472006-10-12 20:34:05 +0000513 SmallVector<SDNode*, 16> DeadNodes;
514 DeadNodes.push_back(N);
515
516 // Process the worklist, deleting the nodes and adding their uses to the
517 // worklist.
518 while (!DeadNodes.empty()) {
519 SDNode *N = DeadNodes.back();
520 DeadNodes.pop_back();
521
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000522 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +0000523 UpdateListener->NodeDeleted(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000524
Evan Cheng130a6472006-10-12 20:34:05 +0000525 // Take the node out of the appropriate CSE map.
526 RemoveNodeFromCSEMaps(N);
527
528 // Next, brutally remove the operand list. This is safe to do, as there are
529 // no cycles in the graph.
530 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000531 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000532 Operand->removeUser(std::distance(N->op_begin(), I), N);
Evan Cheng130a6472006-10-12 20:34:05 +0000533
534 // Now that we removed this operand, see if there are no uses of it left.
535 if (Operand->use_empty())
536 DeadNodes.push_back(Operand);
537 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000538 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000539 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000540 }
Evan Cheng130a6472006-10-12 20:34:05 +0000541 N->OperandList = 0;
542 N->NumOperands = 0;
543
544 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000545 AllNodes.erase(N);
546 }
547}
548
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000549void SelectionDAG::DeleteNode(SDNode *N) {
550 assert(N->use_empty() && "Cannot delete a node that is not dead!");
551
552 // First take this out of the appropriate CSE map.
553 RemoveNodeFromCSEMaps(N);
554
Chris Lattner1e111c72005-09-07 05:37:01 +0000555 // Finally, remove uses due to operands of this node, remove from the
556 // AllNodes list, and delete the node.
557 DeleteNodeNotInCSEMaps(N);
558}
559
560void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
561
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000562 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000563 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000564
565 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000566 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000567 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000568 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000569 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000570 }
Chris Lattner65113b22005-11-08 22:07:03 +0000571 N->OperandList = 0;
572 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000573
574 delete N;
575}
576
Chris Lattner149c58c2005-08-16 18:17:10 +0000577/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
578/// correspond to it. This is useful when we're about to delete or repurpose
579/// the node. We don't want future request for structurally identical nodes
580/// to return N anymore.
581void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000582 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000583 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000584 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000585 case ISD::CONDCODE:
586 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
587 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000588 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000589 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
590 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000591 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000592 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000593 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000594 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000595 Erased =
596 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000597 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000598 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000599 MVT VT = cast<VTSDNode>(N)->getVT();
600 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000601 Erased = ExtendedValueTypeNodes.erase(VT);
602 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000603 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
604 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000605 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000606 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000607 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000608 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000609 // Remove it from the CSE Map.
610 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000611 break;
612 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000613#ifndef NDEBUG
614 // Verify that the node was actually in one of the CSE maps, unless it has a
615 // flag result (which cannot be CSE'd) or is one of the special cases that are
616 // not subject to CSE.
617 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000618 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000619 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000620 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000621 assert(0 && "Node is not in map!");
622 }
623#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000624}
625
Chris Lattner8b8749f2005-08-17 19:00:20 +0000626/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
627/// has been taken out and modified in some way. If the specified node already
628/// exists in the CSE maps, do not modify the maps, but return the existing node
629/// instead. If it doesn't exist, add it and return null.
630///
631SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
632 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000633 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000634 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000635
Chris Lattner9f8cc692005-12-19 22:21:21 +0000636 // Check that remaining values produced are not flags.
637 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
638 if (N->getValueType(i) == MVT::Flag)
639 return 0; // Never CSE anything that produces a flag.
640
Chris Lattnera5682852006-08-07 23:03:03 +0000641 SDNode *New = CSEMap.GetOrInsertNode(N);
642 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000643 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000644}
645
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000646/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
647/// were replaced with those specified. If this node is never memoized,
648/// return null, otherwise return a pointer to the slot it would take. If a
649/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000650SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
651 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000652 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000653 return 0; // Never add these nodes.
654
655 // Check that remaining values produced are not flags.
656 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
657 if (N->getValueType(i) == MVT::Flag)
658 return 0; // Never CSE anything that produces a flag.
659
Chris Lattner63e3f142007-02-04 07:28:00 +0000660 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000661 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000662 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000663 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000664}
665
666/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
667/// were replaced with those specified. If this node is never memoized,
668/// return null, otherwise return a pointer to the slot it would take. If a
669/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000670SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
671 SDOperand Op1, SDOperand Op2,
672 void *&InsertPos) {
673 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
674 return 0; // Never add these nodes.
675
676 // Check that remaining values produced are not flags.
677 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
678 if (N->getValueType(i) == MVT::Flag)
679 return 0; // Never CSE anything that produces a flag.
680
Chris Lattner63e3f142007-02-04 07:28:00 +0000681 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000682 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000683 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000684 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
685}
686
687
688/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
689/// were replaced with those specified. If this node is never memoized,
690/// return null, otherwise return a pointer to the slot it would take. If a
691/// node already exists with these operands, the slot will be non-null.
692SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000693 SDOperandPtr Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000694 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000695 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000696 return 0; // Never add these nodes.
697
698 // Check that remaining values produced are not flags.
699 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
700 if (N->getValueType(i) == MVT::Flag)
701 return 0; // Never CSE anything that produces a flag.
702
Jim Laskey583bd472006-10-27 23:46:08 +0000703 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000704 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000705
Evan Cheng9629aba2006-10-11 01:47:58 +0000706 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
707 ID.AddInteger(LD->getAddressingMode());
708 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000709 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000710 ID.AddInteger(LD->getAlignment());
711 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000712 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
713 ID.AddInteger(ST->getAddressingMode());
714 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000715 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000716 ID.AddInteger(ST->getAlignment());
717 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000718 }
Jim Laskey583bd472006-10-27 23:46:08 +0000719
Chris Lattnera5682852006-08-07 23:03:03 +0000720 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000721}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000722
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000723
Chris Lattner78ec3112003-08-11 14:57:33 +0000724SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000725 while (!AllNodes.empty()) {
726 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000727 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000728 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000729 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000730 }
Chris Lattner65113b22005-11-08 22:07:03 +0000731 N->OperandList = 0;
732 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000733 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000734 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000735}
736
Duncan Sands83ec4b62008-06-06 12:08:01 +0000737SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000738 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000739 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000740 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000741 return getNode(ISD::AND, Op.getValueType(), Op,
742 getConstant(Imm, Op.getValueType()));
743}
744
Duncan Sands83ec4b62008-06-06 12:08:01 +0000745SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000746 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000747 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000748}
749
Duncan Sands83ec4b62008-06-06 12:08:01 +0000750SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
751 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000752
Evan Cheng0ff39b32008-06-30 07:31:25 +0000753 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000754 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000755 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000756
757 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000758 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000759 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000760 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000761 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000762 SDNode *N = NULL;
763 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000764 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000765 return SDOperand(N, 0);
766 if (!N) {
767 N = new ConstantSDNode(isT, Val, EltVT);
768 CSEMap.InsertNode(N, IP);
769 AllNodes.push_back(N);
770 }
771
772 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000773 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000774 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000775 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000776 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
777 }
778 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000779}
780
Chris Lattner0bd48932008-01-17 07:00:52 +0000781SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
782 return getConstant(Val, TLI.getPointerTy(), isTarget);
783}
784
785
Duncan Sands83ec4b62008-06-06 12:08:01 +0000786SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
787 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000788
Duncan Sands83ec4b62008-06-06 12:08:01 +0000789 MVT EltVT =
790 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000791
Chris Lattnerd8658612005-02-17 20:17:32 +0000792 // Do the map lookup using the actual bit pattern for the floating point
793 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
794 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000795 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000796 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000797 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000798 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000799 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000800 SDNode *N = NULL;
801 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000802 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000803 return SDOperand(N, 0);
804 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000805 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000806 CSEMap.InsertNode(N, IP);
807 AllNodes.push_back(N);
808 }
809
Dan Gohman7f321562007-06-25 16:23:39 +0000810 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000811 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000812 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000813 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000814 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
815 }
816 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000817}
818
Duncan Sands83ec4b62008-06-06 12:08:01 +0000819SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
820 MVT EltVT =
821 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000822 if (EltVT==MVT::f32)
823 return getConstantFP(APFloat((float)Val), VT, isTarget);
824 else
825 return getConstantFP(APFloat(Val), VT, isTarget);
826}
827
Chris Lattnerc3aae252005-01-07 07:46:32 +0000828SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000829 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000830 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000831 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000832
833 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
834 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000835 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000836 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
837 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
838 }
839
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000840 if (GVar && GVar->isThreadLocal())
841 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
842 else
843 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000844
Jim Laskey583bd472006-10-27 23:46:08 +0000845 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000846 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000847 ID.AddPointer(GV);
848 ID.AddInteger(Offset);
849 void *IP = 0;
850 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
851 return SDOperand(E, 0);
852 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
853 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000854 AllNodes.push_back(N);
855 return SDOperand(N, 0);
856}
857
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000859 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000860 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000861 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000862 ID.AddInteger(FI);
863 void *IP = 0;
864 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
865 return SDOperand(E, 0);
866 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
867 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000868 AllNodes.push_back(N);
869 return SDOperand(N, 0);
870}
871
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000873 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000874 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000875 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000876 ID.AddInteger(JTI);
877 void *IP = 0;
878 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
879 return SDOperand(E, 0);
880 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
881 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000882 AllNodes.push_back(N);
883 return SDOperand(N, 0);
884}
885
Duncan Sands83ec4b62008-06-06 12:08:01 +0000886SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000887 unsigned Alignment, int Offset,
888 bool isTarget) {
889 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000890 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000891 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000892 ID.AddInteger(Alignment);
893 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000894 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000895 void *IP = 0;
896 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
897 return SDOperand(E, 0);
898 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
899 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000900 AllNodes.push_back(N);
901 return SDOperand(N, 0);
902}
903
Chris Lattnerc3aae252005-01-07 07:46:32 +0000904
Duncan Sands83ec4b62008-06-06 12:08:01 +0000905SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000906 unsigned Alignment, int Offset,
907 bool isTarget) {
908 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000909 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000910 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000911 ID.AddInteger(Alignment);
912 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000913 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000914 void *IP = 0;
915 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
916 return SDOperand(E, 0);
917 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
918 CSEMap.InsertNode(N, IP);
919 AllNodes.push_back(N);
920 return SDOperand(N, 0);
921}
922
923
Chris Lattnerc3aae252005-01-07 07:46:32 +0000924SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000925 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000926 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000927 ID.AddPointer(MBB);
928 void *IP = 0;
929 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
930 return SDOperand(E, 0);
931 SDNode *N = new BasicBlockSDNode(MBB);
932 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 AllNodes.push_back(N);
934 return SDOperand(N, 0);
935}
936
Duncan Sands276dcbd2008-03-21 09:14:45 +0000937SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
938 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000939 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000940 ID.AddInteger(Flags.getRawBits());
941 void *IP = 0;
942 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
943 return SDOperand(E, 0);
944 SDNode *N = new ARG_FLAGSSDNode(Flags);
945 CSEMap.InsertNode(N, IP);
946 AllNodes.push_back(N);
947 return SDOperand(N, 0);
948}
949
Duncan Sands83ec4b62008-06-06 12:08:01 +0000950SDOperand SelectionDAG::getValueType(MVT VT) {
951 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
952 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000953
Duncan Sands83ec4b62008-06-06 12:08:01 +0000954 SDNode *&N = VT.isExtended() ?
955 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000956
957 if (N) return SDOperand(N, 0);
958 N = new VTSDNode(VT);
959 AllNodes.push_back(N);
960 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000961}
962
Duncan Sands83ec4b62008-06-06 12:08:01 +0000963SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000964 SDNode *&N = ExternalSymbols[Sym];
965 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000966 N = new ExternalSymbolSDNode(false, Sym, VT);
967 AllNodes.push_back(N);
968 return SDOperand(N, 0);
969}
970
Duncan Sands83ec4b62008-06-06 12:08:01 +0000971SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000972 SDNode *&N = TargetExternalSymbols[Sym];
973 if (N) return SDOperand(N, 0);
974 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000975 AllNodes.push_back(N);
976 return SDOperand(N, 0);
977}
978
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000979SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
980 if ((unsigned)Cond >= CondCodeNodes.size())
981 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000982
Chris Lattner079a27a2005-08-09 20:40:02 +0000983 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000984 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000985 AllNodes.push_back(CondCodeNodes[Cond]);
986 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000987 return SDOperand(CondCodeNodes[Cond], 0);
988}
989
Duncan Sands83ec4b62008-06-06 12:08:01 +0000990SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000991 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000992 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000993 ID.AddInteger(RegNo);
994 void *IP = 0;
995 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
996 return SDOperand(E, 0);
997 SDNode *N = new RegisterSDNode(RegNo, VT);
998 CSEMap.InsertNode(N, IP);
999 AllNodes.push_back(N);
1000 return SDOperand(N, 0);
1001}
1002
Dan Gohman7f460202008-06-30 20:59:49 +00001003SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1004 unsigned Line, unsigned Col,
1005 const CompileUnitDesc *CU) {
1006 FoldingSetNodeID ID;
1007 SDOperand Ops[] = { Root };
1008 AddNodeIDNode(ID, ISD::DBG_STOPPOINT, getVTList(MVT::Other), &Ops[0], 1);
1009 ID.AddInteger(Line);
1010 ID.AddInteger(Col);
1011 ID.AddPointer(CU);
1012 void *IP = 0;
1013 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1014 return SDOperand(E, 0);
1015 SDNode *N = new DbgStopPointSDNode(Root, Line, Col, CU);
1016 CSEMap.InsertNode(N, IP);
1017 AllNodes.push_back(N);
1018 return SDOperand(N, 0);
1019}
1020
Dan Gohman44066042008-07-01 00:05:16 +00001021SDOperand SelectionDAG::getLabel(unsigned Opcode,
1022 SDOperand Root,
1023 unsigned LabelID) {
1024 FoldingSetNodeID ID;
1025 SDOperand Ops[] = { Root };
1026 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1027 ID.AddInteger(LabelID);
1028 void *IP = 0;
1029 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1030 return SDOperand(E, 0);
1031 SDNode *N = new LabelSDNode(Opcode, Root, LabelID);
1032 CSEMap.InsertNode(N, IP);
1033 AllNodes.push_back(N);
1034 return SDOperand(N, 0);
1035}
1036
Dan Gohman69de1932008-02-06 22:27:42 +00001037SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001038 assert((!V || isa<PointerType>(V->getType())) &&
1039 "SrcValue is not a pointer?");
1040
Jim Laskey583bd472006-10-27 23:46:08 +00001041 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001042 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001043 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001044
Chris Lattner61b09412006-08-11 21:01:22 +00001045 void *IP = 0;
1046 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1047 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001048
1049 SDNode *N = new SrcValueSDNode(V);
1050 CSEMap.InsertNode(N, IP);
1051 AllNodes.push_back(N);
1052 return SDOperand(N, 0);
1053}
1054
Dan Gohman36b5c132008-04-07 19:35:22 +00001055SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001056 const Value *v = MO.getValue();
1057 assert((!v || isa<PointerType>(v->getType())) &&
1058 "SrcValue is not a pointer?");
1059
1060 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001061 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001062 ID.AddPointer(v);
1063 ID.AddInteger(MO.getFlags());
1064 ID.AddInteger(MO.getOffset());
1065 ID.AddInteger(MO.getSize());
1066 ID.AddInteger(MO.getAlignment());
1067
1068 void *IP = 0;
1069 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1070 return SDOperand(E, 0);
1071
1072 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001073 CSEMap.InsertNode(N, IP);
1074 AllNodes.push_back(N);
1075 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001076}
1077
Chris Lattner37ce9df2007-10-15 17:47:20 +00001078/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1079/// specified value type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001080SDOperand SelectionDAG::CreateStackTemporary(MVT VT) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001081 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001082 unsigned ByteSize = VT.getSizeInBits()/8;
1083 const Type *Ty = VT.getTypeForMVT();
Chris Lattner37ce9df2007-10-15 17:47:20 +00001084 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
1085 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1086 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1087}
1088
1089
Duncan Sands83ec4b62008-06-06 12:08:01 +00001090SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001091 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092 // These setcc operations always fold.
1093 switch (Cond) {
1094 default: break;
1095 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001096 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001097 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001098 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001099
1100 case ISD::SETOEQ:
1101 case ISD::SETOGT:
1102 case ISD::SETOGE:
1103 case ISD::SETOLT:
1104 case ISD::SETOLE:
1105 case ISD::SETONE:
1106 case ISD::SETO:
1107 case ISD::SETUO:
1108 case ISD::SETUEQ:
1109 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001110 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001111 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001112 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001113
Chris Lattner67255a12005-04-07 18:14:58 +00001114 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001115 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001116 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001117 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001118
Chris Lattnerc3aae252005-01-07 07:46:32 +00001119 switch (Cond) {
1120 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001121 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1122 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001123 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1124 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1125 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1126 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1127 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1128 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1129 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1130 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001131 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001132 }
Chris Lattner67255a12005-04-07 18:14:58 +00001133 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001134 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001135 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001136 // No compile time operations on this type yet.
1137 if (N1C->getValueType(0) == MVT::ppcf128)
1138 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001139
1140 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001141 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001142 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001143 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1144 return getNode(ISD::UNDEF, VT);
1145 // fall through
1146 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1147 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1148 return getNode(ISD::UNDEF, VT);
1149 // fall through
1150 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001151 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001152 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1153 return getNode(ISD::UNDEF, VT);
1154 // fall through
1155 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1156 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1157 return getNode(ISD::UNDEF, VT);
1158 // fall through
1159 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1160 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1161 return getNode(ISD::UNDEF, VT);
1162 // fall through
1163 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001164 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001165 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1166 return getNode(ISD::UNDEF, VT);
1167 // fall through
1168 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001169 R==APFloat::cmpEqual, VT);
1170 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1171 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1172 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1173 R==APFloat::cmpEqual, VT);
1174 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1175 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1176 R==APFloat::cmpLessThan, VT);
1177 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1178 R==APFloat::cmpUnordered, VT);
1179 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1180 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001181 }
1182 } else {
1183 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001184 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001186 }
1187
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001188 // Could not fold it.
1189 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001190}
1191
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001192/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1193/// use this predicate to simplify operations downstream.
1194bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1195 unsigned BitWidth = Op.getValueSizeInBits();
1196 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1197}
1198
Dan Gohmanea859be2007-06-22 14:59:07 +00001199/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1200/// this predicate to simplify operations downstream. Mask is known to be zero
1201/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001202bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001203 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001204 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001205 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1206 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1207 return (KnownZero & Mask) == Mask;
1208}
1209
1210/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1211/// known to be either zero or one and return them in the KnownZero/KnownOne
1212/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1213/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001214void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001215 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001216 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001217 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001218 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001219 "Mask size mismatches value type size!");
1220
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001221 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001222 if (Depth == 6 || Mask == 0)
1223 return; // Limit search depth.
1224
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001225 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001226
1227 switch (Op.getOpcode()) {
1228 case ISD::Constant:
1229 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001230 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001231 KnownZero = ~KnownOne & Mask;
1232 return;
1233 case ISD::AND:
1234 // If either the LHS or the RHS are Zero, the result is zero.
1235 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001236 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1237 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001238 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1239 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1240
1241 // Output known-1 bits are only known if set in both the LHS & RHS.
1242 KnownOne &= KnownOne2;
1243 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1244 KnownZero |= KnownZero2;
1245 return;
1246 case ISD::OR:
1247 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001248 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1249 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001250 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1251 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1252
1253 // Output known-0 bits are only known if clear in both the LHS & RHS.
1254 KnownZero &= KnownZero2;
1255 // Output known-1 are known to be set if set in either the LHS | RHS.
1256 KnownOne |= KnownOne2;
1257 return;
1258 case ISD::XOR: {
1259 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1260 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1261 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1262 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1263
1264 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001265 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001266 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1267 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1268 KnownZero = KnownZeroOut;
1269 return;
1270 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001271 case ISD::MUL: {
1272 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1273 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1274 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1275 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1276 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1277
1278 // If low bits are zero in either operand, output low known-0 bits.
1279 // Also compute a conserative estimate for high known-0 bits.
1280 // More trickiness is possible, but this is sufficient for the
1281 // interesting case of alignment computation.
1282 KnownOne.clear();
1283 unsigned TrailZ = KnownZero.countTrailingOnes() +
1284 KnownZero2.countTrailingOnes();
1285 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001286 KnownZero2.countLeadingOnes(),
1287 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001288
1289 TrailZ = std::min(TrailZ, BitWidth);
1290 LeadZ = std::min(LeadZ, BitWidth);
1291 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1292 APInt::getHighBitsSet(BitWidth, LeadZ);
1293 KnownZero &= Mask;
1294 return;
1295 }
1296 case ISD::UDIV: {
1297 // For the purposes of computing leading zeros we can conservatively
1298 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001299 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001300 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1301 ComputeMaskedBits(Op.getOperand(0),
1302 AllOnes, KnownZero2, KnownOne2, Depth+1);
1303 unsigned LeadZ = KnownZero2.countLeadingOnes();
1304
1305 KnownOne2.clear();
1306 KnownZero2.clear();
1307 ComputeMaskedBits(Op.getOperand(1),
1308 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001309 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1310 if (RHSUnknownLeadingOnes != BitWidth)
1311 LeadZ = std::min(BitWidth,
1312 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001313
1314 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1315 return;
1316 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001317 case ISD::SELECT:
1318 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1319 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1320 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1321 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1322
1323 // Only known if known in both the LHS and RHS.
1324 KnownOne &= KnownOne2;
1325 KnownZero &= KnownZero2;
1326 return;
1327 case ISD::SELECT_CC:
1328 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1329 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1330 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1331 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1332
1333 // Only known if known in both the LHS and RHS.
1334 KnownOne &= KnownOne2;
1335 KnownZero &= KnownZero2;
1336 return;
1337 case ISD::SETCC:
1338 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001339 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1340 BitWidth > 1)
1341 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001342 return;
1343 case ISD::SHL:
1344 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1345 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001346 unsigned ShAmt = SA->getValue();
1347
1348 // If the shift count is an invalid immediate, don't do anything.
1349 if (ShAmt >= BitWidth)
1350 return;
1351
1352 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001353 KnownZero, KnownOne, Depth+1);
1354 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001355 KnownZero <<= ShAmt;
1356 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001357 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001358 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001359 }
1360 return;
1361 case ISD::SRL:
1362 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1363 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001364 unsigned ShAmt = SA->getValue();
1365
Dan Gohmand4cf9922008-02-26 18:50:50 +00001366 // If the shift count is an invalid immediate, don't do anything.
1367 if (ShAmt >= BitWidth)
1368 return;
1369
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001370 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001371 KnownZero, KnownOne, Depth+1);
1372 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001373 KnownZero = KnownZero.lshr(ShAmt);
1374 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001375
Dan Gohman72d2fd52008-02-13 22:43:25 +00001376 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001377 KnownZero |= HighBits; // High bits known zero.
1378 }
1379 return;
1380 case ISD::SRA:
1381 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001382 unsigned ShAmt = SA->getValue();
1383
Dan Gohmand4cf9922008-02-26 18:50:50 +00001384 // If the shift count is an invalid immediate, don't do anything.
1385 if (ShAmt >= BitWidth)
1386 return;
1387
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001388 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001389 // If any of the demanded bits are produced by the sign extension, we also
1390 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001391 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1392 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001393 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001394
1395 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1396 Depth+1);
1397 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001398 KnownZero = KnownZero.lshr(ShAmt);
1399 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001400
1401 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001402 APInt SignBit = APInt::getSignBit(BitWidth);
1403 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001404
Dan Gohmanca93a432008-02-20 16:30:17 +00001405 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001406 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001407 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001408 KnownOne |= HighBits; // New bits are known one.
1409 }
1410 }
1411 return;
1412 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001413 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1414 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001415
1416 // Sign extension. Compute the demanded bits in the result that are not
1417 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001418 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001419
Dan Gohman977a76f2008-02-13 22:28:48 +00001420 APInt InSignBit = APInt::getSignBit(EBits);
1421 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001422
1423 // If the sign extended bits are demanded, we know that the sign
1424 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001425 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001426 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001427 InputDemandedBits |= InSignBit;
1428
1429 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1430 KnownZero, KnownOne, Depth+1);
1431 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1432
1433 // If the sign bit of the input is known set or clear, then we know the
1434 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001435 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001436 KnownZero |= NewBits;
1437 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001438 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001439 KnownOne |= NewBits;
1440 KnownZero &= ~NewBits;
1441 } else { // Input sign bit unknown
1442 KnownZero &= ~NewBits;
1443 KnownOne &= ~NewBits;
1444 }
1445 return;
1446 }
1447 case ISD::CTTZ:
1448 case ISD::CTLZ:
1449 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001450 unsigned LowBits = Log2_32(BitWidth)+1;
1451 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001452 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001453 return;
1454 }
1455 case ISD::LOAD: {
1456 if (ISD::isZEXTLoad(Op.Val)) {
1457 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 MVT VT = LD->getMemoryVT();
1459 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001460 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001461 }
1462 return;
1463 }
1464 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001465 MVT InVT = Op.getOperand(0).getValueType();
1466 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001467 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1468 APInt InMask = Mask;
1469 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001470 KnownZero.trunc(InBits);
1471 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001472 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001473 KnownZero.zext(BitWidth);
1474 KnownOne.zext(BitWidth);
1475 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001476 return;
1477 }
1478 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001479 MVT InVT = Op.getOperand(0).getValueType();
1480 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001481 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001482 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1483 APInt InMask = Mask;
1484 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001485
1486 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001487 // bit is demanded. Temporarily set this bit in the mask for our callee.
1488 if (NewBits.getBoolValue())
1489 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001490
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001491 KnownZero.trunc(InBits);
1492 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001493 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1494
1495 // Note if the sign bit is known to be zero or one.
1496 bool SignBitKnownZero = KnownZero.isNegative();
1497 bool SignBitKnownOne = KnownOne.isNegative();
1498 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1499 "Sign bit can't be known to be both zero and one!");
1500
1501 // If the sign bit wasn't actually demanded by our caller, we don't
1502 // want it set in the KnownZero and KnownOne result values. Reset the
1503 // mask and reapply it to the result values.
1504 InMask = Mask;
1505 InMask.trunc(InBits);
1506 KnownZero &= InMask;
1507 KnownOne &= InMask;
1508
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001509 KnownZero.zext(BitWidth);
1510 KnownOne.zext(BitWidth);
1511
Dan Gohman977a76f2008-02-13 22:28:48 +00001512 // If the sign bit is known zero or one, the top bits match.
1513 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001514 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001515 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001516 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001517 return;
1518 }
1519 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001520 MVT InVT = Op.getOperand(0).getValueType();
1521 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001522 APInt InMask = Mask;
1523 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001524 KnownZero.trunc(InBits);
1525 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001526 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001527 KnownZero.zext(BitWidth);
1528 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001529 return;
1530 }
1531 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001532 MVT InVT = Op.getOperand(0).getValueType();
1533 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001534 APInt InMask = Mask;
1535 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001536 KnownZero.zext(InBits);
1537 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001538 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001539 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001540 KnownZero.trunc(BitWidth);
1541 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001542 break;
1543 }
1544 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001545 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1546 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001547 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1548 KnownOne, Depth+1);
1549 KnownZero |= (~InMask) & Mask;
1550 return;
1551 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001552 case ISD::FGETSIGN:
1553 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001554 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001555 return;
1556
Dan Gohman23e8b712008-04-28 17:02:21 +00001557 case ISD::SUB: {
1558 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1559 // We know that the top bits of C-X are clear if X contains less bits
1560 // than C (i.e. no wrap-around can happen). For example, 20-X is
1561 // positive if we can prove that X is >= 0 and < 16.
1562 if (CLHS->getAPIntValue().isNonNegative()) {
1563 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1564 // NLZ can't be BitWidth with no sign bit
1565 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1566 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1567 Depth+1);
1568
1569 // If all of the MaskV bits are known to be zero, then we know the
1570 // output top bits are zero, because we now know that the output is
1571 // from [0-C].
1572 if ((KnownZero2 & MaskV) == MaskV) {
1573 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1574 // Top bits known zero.
1575 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1576 }
1577 }
1578 }
1579 }
1580 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001581 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001582 // Output known-0 bits are known if clear or set in both the low clear bits
1583 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1584 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001585 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1586 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1587 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1588 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1589
1590 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1591 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1592 KnownZeroOut = std::min(KnownZeroOut,
1593 KnownZero2.countTrailingOnes());
1594
1595 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001596 return;
1597 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001598 case ISD::SREM:
1599 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1600 APInt RA = Rem->getAPIntValue();
1601 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001602 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001603 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1604 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001605
Dan Gohman23e8b712008-04-28 17:02:21 +00001606 // The sign of a remainder is equal to the sign of the first
1607 // operand (zero being positive).
1608 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1609 KnownZero2 |= ~LowBits;
1610 else if (KnownOne2[BitWidth-1])
1611 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001612
Dan Gohman23e8b712008-04-28 17:02:21 +00001613 KnownZero |= KnownZero2 & Mask;
1614 KnownOne |= KnownOne2 & Mask;
1615
1616 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001617 }
1618 }
1619 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001620 case ISD::UREM: {
1621 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1622 APInt RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001623 if (RA.isPowerOf2()) {
1624 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001625 APInt Mask2 = LowBits & Mask;
1626 KnownZero |= ~LowBits & Mask;
1627 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1628 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1629 break;
1630 }
1631 }
1632
1633 // Since the result is less than or equal to either operand, any leading
1634 // zero bits in either operand must also exist in the result.
1635 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1636 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1637 Depth+1);
1638 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1639 Depth+1);
1640
1641 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1642 KnownZero2.countLeadingOnes());
1643 KnownOne.clear();
1644 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1645 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001646 }
1647 default:
1648 // Allow the target to implement this method for its nodes.
1649 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1650 case ISD::INTRINSIC_WO_CHAIN:
1651 case ISD::INTRINSIC_W_CHAIN:
1652 case ISD::INTRINSIC_VOID:
1653 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1654 }
1655 return;
1656 }
1657}
1658
1659/// ComputeNumSignBits - Return the number of times the sign bit of the
1660/// register is replicated into the other bits. We know that at least 1 bit
1661/// is always equal to the sign bit (itself), but other cases can give us
1662/// information. For example, immediately after an "SRA X, 2", we know that
1663/// the top 3 bits are all equal to each other, so we return 3.
1664unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001665 MVT VT = Op.getValueType();
1666 assert(VT.isInteger() && "Invalid VT!");
1667 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001668 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001669 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001670
1671 if (Depth == 6)
1672 return 1; // Limit search depth.
1673
1674 switch (Op.getOpcode()) {
1675 default: break;
1676 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001677 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001678 return VTBits-Tmp+1;
1679 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001680 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001681 return VTBits-Tmp;
1682
1683 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001684 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1685 // If negative, return # leading ones.
1686 if (Val.isNegative())
1687 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001688
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001689 // Return # leading zeros.
1690 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001691 }
1692
1693 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001694 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001695 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1696
1697 case ISD::SIGN_EXTEND_INREG:
1698 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001699 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001700 Tmp = VTBits-Tmp+1;
1701
1702 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1703 return std::max(Tmp, Tmp2);
1704
1705 case ISD::SRA:
1706 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1707 // SRA X, C -> adds C sign bits.
1708 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1709 Tmp += C->getValue();
1710 if (Tmp > VTBits) Tmp = VTBits;
1711 }
1712 return Tmp;
1713 case ISD::SHL:
1714 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1715 // shl destroys sign bits.
1716 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1717 if (C->getValue() >= VTBits || // Bad shift.
1718 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1719 return Tmp - C->getValue();
1720 }
1721 break;
1722 case ISD::AND:
1723 case ISD::OR:
1724 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001725 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001726 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001727 if (Tmp != 1) {
1728 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1729 FirstAnswer = std::min(Tmp, Tmp2);
1730 // We computed what we know about the sign bits as our first
1731 // answer. Now proceed to the generic code that uses
1732 // ComputeMaskedBits, and pick whichever answer is better.
1733 }
1734 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001735
1736 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001737 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001738 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001739 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001740 return std::min(Tmp, Tmp2);
1741
1742 case ISD::SETCC:
1743 // If setcc returns 0/-1, all bits are sign bits.
1744 if (TLI.getSetCCResultContents() ==
1745 TargetLowering::ZeroOrNegativeOneSetCCResult)
1746 return VTBits;
1747 break;
1748 case ISD::ROTL:
1749 case ISD::ROTR:
1750 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1751 unsigned RotAmt = C->getValue() & (VTBits-1);
1752
1753 // Handle rotate right by N like a rotate left by 32-N.
1754 if (Op.getOpcode() == ISD::ROTR)
1755 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1756
1757 // If we aren't rotating out all of the known-in sign bits, return the
1758 // number that are left. This handles rotl(sext(x), 1) for example.
1759 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1760 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1761 }
1762 break;
1763 case ISD::ADD:
1764 // Add can have at most one carry bit. Thus we know that the output
1765 // is, at worst, one more bit than the inputs.
1766 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1767 if (Tmp == 1) return 1; // Early out.
1768
1769 // Special case decrementing a value (ADD X, -1):
1770 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1771 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001772 APInt KnownZero, KnownOne;
1773 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001774 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1775
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 we are subtracting one from a positive number, there is no carry
1782 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001783 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001784 return Tmp;
1785 }
1786
1787 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1788 if (Tmp2 == 1) return 1;
1789 return std::min(Tmp, Tmp2)-1;
1790 break;
1791
1792 case ISD::SUB:
1793 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1794 if (Tmp2 == 1) return 1;
1795
1796 // Handle NEG.
1797 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001798 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001799 APInt KnownZero, KnownOne;
1800 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001801 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1802 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1803 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001804 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001805 return VTBits;
1806
1807 // If the input is known to be positive (the sign bit is known clear),
1808 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001809 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001810 return Tmp2;
1811
1812 // Otherwise, we treat this like a SUB.
1813 }
1814
1815 // Sub can have at most one carry bit. Thus we know that the output
1816 // is, at worst, one more bit than the inputs.
1817 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1818 if (Tmp == 1) return 1; // Early out.
1819 return std::min(Tmp, Tmp2)-1;
1820 break;
1821 case ISD::TRUNCATE:
1822 // FIXME: it's tricky to do anything useful for this, but it is an important
1823 // case for targets like X86.
1824 break;
1825 }
1826
1827 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1828 if (Op.getOpcode() == ISD::LOAD) {
1829 LoadSDNode *LD = cast<LoadSDNode>(Op);
1830 unsigned ExtType = LD->getExtensionType();
1831 switch (ExtType) {
1832 default: break;
1833 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001834 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 return VTBits-Tmp+1;
1836 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001837 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001838 return VTBits-Tmp;
1839 }
1840 }
1841
1842 // Allow the target to implement this method for its nodes.
1843 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1844 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1845 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1846 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1847 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001848 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001849 }
1850
1851 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1852 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001853 APInt KnownZero, KnownOne;
1854 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001855 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1856
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001857 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001858 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001859 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001860 Mask = KnownOne;
1861 } else {
1862 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001863 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001864 }
1865
1866 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1867 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001868 Mask = ~Mask;
1869 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001870 // Return # leading zeros. We use 'min' here in case Val was zero before
1871 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001872 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001873}
1874
Chris Lattner51dabfb2006-10-14 00:41:01 +00001875
Evan Chenga844bde2008-02-02 04:07:54 +00001876bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1877 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1878 if (!GA) return false;
1879 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1880 if (!GV) return false;
1881 MachineModuleInfo *MMI = getMachineModuleInfo();
1882 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1883}
1884
1885
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001886/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1887/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001888SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001889 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001890 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001891 SDOperand Idx = PermMask.getOperand(i);
1892 if (Idx.getOpcode() == ISD::UNDEF)
1893 return getNode(ISD::UNDEF, VT.getVectorElementType());
1894 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001895 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001896 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1897 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001898
1899 if (V.getOpcode() == ISD::BIT_CONVERT) {
1900 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001901 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001902 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001903 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001904 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001905 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001906 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001907 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001908 return V.getOperand(Index);
1909 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1910 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001911 return SDOperand();
1912}
1913
1914
Chris Lattnerc3aae252005-01-07 07:46:32 +00001915/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001916///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001917SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001918 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001919 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001920 void *IP = 0;
1921 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1922 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001923 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001924 CSEMap.InsertNode(N, IP);
1925
1926 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001927 return SDOperand(N, 0);
1928}
1929
Duncan Sands83ec4b62008-06-06 12:08:01 +00001930SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001931 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001933 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001934 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001935 switch (Opcode) {
1936 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001937 case ISD::SIGN_EXTEND:
1938 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001939 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001940 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001941 case ISD::TRUNCATE:
1942 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001943 case ISD::UINT_TO_FP:
1944 case ISD::SINT_TO_FP: {
1945 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001946 // No compile time operations on this type.
1947 if (VT==MVT::ppcf128)
1948 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001949 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1950 (void)apf.convertFromAPInt(Val,
1951 Opcode==ISD::SINT_TO_FP,
1952 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001953 return getConstantFP(apf, VT);
1954 }
Chris Lattner94683772005-12-23 05:30:37 +00001955 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001956 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001957 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001958 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001959 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001960 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001961 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001962 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001963 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001964 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001965 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001966 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001967 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001968 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001969 }
1970 }
1971
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001972 // Constant fold unary operations with a floating point constant operand.
1973 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1974 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001975 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001976 switch (Opcode) {
1977 case ISD::FNEG:
1978 V.changeSign();
1979 return getConstantFP(V, VT);
1980 case ISD::FABS:
1981 V.clearSign();
1982 return getConstantFP(V, VT);
1983 case ISD::FP_ROUND:
1984 case ISD::FP_EXTEND:
1985 // This can return overflow, underflow, or inexact; we don't care.
1986 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001987 (void)V.convert(*MVTToAPFloatSemantics(VT),
1988 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001989 return getConstantFP(V, VT);
1990 case ISD::FP_TO_SINT:
1991 case ISD::FP_TO_UINT: {
1992 integerPart x;
1993 assert(integerPartWidth >= 64);
1994 // FIXME need to be more flexible about rounding mode.
1995 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1996 Opcode==ISD::FP_TO_SINT,
1997 APFloat::rmTowardZero);
1998 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1999 break;
2000 return getConstant(x, VT);
2001 }
2002 case ISD::BIT_CONVERT:
2003 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2004 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2005 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2006 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002007 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002008 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002009 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002010 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002011
2012 unsigned OpOpcode = Operand.Val->getOpcode();
2013 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002014 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002015 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002016 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002017 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002018 assert(VT.isFloatingPoint() &&
2019 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002020 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002021 if (Operand.getOpcode() == ISD::UNDEF)
2022 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002023 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002024 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002025 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002026 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002027 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002028 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002029 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002030 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2031 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2032 break;
2033 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002034 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002035 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002036 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002037 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002038 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002039 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002040 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002041 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002042 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002043 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002044 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002045 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002046 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002047 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002048 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2049 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2050 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2051 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002052 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002053 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002054 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002055 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002056 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002057 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002058 if (OpOpcode == ISD::TRUNCATE)
2059 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002060 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2061 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002062 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002063 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002064 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002065 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002066 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2067 else
2068 return Operand.Val->getOperand(0);
2069 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002070 break;
Chris Lattner35481892005-12-23 00:16:34 +00002071 case ISD::BIT_CONVERT:
2072 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002073 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002074 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002075 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002076 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2077 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002078 if (OpOpcode == ISD::UNDEF)
2079 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002080 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002081 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002082 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2083 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002084 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002085 if (OpOpcode == ISD::UNDEF)
2086 return getNode(ISD::UNDEF, VT);
2087 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2088 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2089 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2090 Operand.getConstantOperandVal(1) == 0 &&
2091 Operand.getOperand(0).getValueType() == VT)
2092 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002093 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002094 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002095 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2096 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002097 Operand.Val->getOperand(0));
2098 if (OpOpcode == ISD::FNEG) // --X -> X
2099 return Operand.Val->getOperand(0);
2100 break;
2101 case ISD::FABS:
2102 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2103 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2104 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002105 }
2106
Chris Lattner43247a12005-08-25 19:12:10 +00002107 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002108 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002109 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002110 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002111 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002112 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002113 void *IP = 0;
2114 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2115 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002116 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002117 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002118 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002119 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002120 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002121 AllNodes.push_back(N);
2122 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002123}
2124
Chris Lattner36019aa2005-04-18 03:48:41 +00002125
2126
Duncan Sands83ec4b62008-06-06 12:08:01 +00002127SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002128 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002129 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2130 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002131 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002132 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002133 case ISD::TokenFactor:
2134 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2135 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002136 // Fold trivial token factors.
2137 if (N1.getOpcode() == ISD::EntryToken) return N2;
2138 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002139 break;
Chris Lattner76365122005-01-16 02:23:22 +00002140 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002141 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002142 N1.getValueType() == VT && "Binary operator types must match!");
2143 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2144 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002145 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002146 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002147 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2148 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002149 break;
Chris Lattner76365122005-01-16 02:23:22 +00002150 case ISD::OR:
2151 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002152 case ISD::ADD:
2153 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002154 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002155 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002156 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2157 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002158 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002159 return N1;
2160 break;
Chris Lattner76365122005-01-16 02:23:22 +00002161 case ISD::UDIV:
2162 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002163 case ISD::MULHU:
2164 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002165 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002166 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002167 case ISD::MUL:
2168 case ISD::SDIV:
2169 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002170 case ISD::FADD:
2171 case ISD::FSUB:
2172 case ISD::FMUL:
2173 case ISD::FDIV:
2174 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002175 assert(N1.getValueType() == N2.getValueType() &&
2176 N1.getValueType() == VT && "Binary operator types must match!");
2177 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002178 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2179 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002180 N1.getValueType().isFloatingPoint() &&
2181 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002182 "Invalid FCOPYSIGN!");
2183 break;
Chris Lattner76365122005-01-16 02:23:22 +00002184 case ISD::SHL:
2185 case ISD::SRA:
2186 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002187 case ISD::ROTL:
2188 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002189 assert(VT == N1.getValueType() &&
2190 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002191 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner068a81e2005-01-17 17:15:02 +00002192 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00002193 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002194 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002195 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002196 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002197 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002198 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002199 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002200 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002201 break;
2202 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002203 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002204 assert(VT.isFloatingPoint() &&
2205 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002206 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002207 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002208 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002209 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002210 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002211 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002212 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002213 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002214 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002215 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002216 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002217 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002218 break;
2219 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002220 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002221 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002222 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002223 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002224 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002225 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002226 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002227
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002228 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002229 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002230 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002231 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002232 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002233 return getConstant(Val, VT);
2234 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002235 break;
2236 }
2237 case ISD::EXTRACT_VECTOR_ELT:
2238 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2239
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002240 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2241 if (N1.getOpcode() == ISD::UNDEF)
2242 return getNode(ISD::UNDEF, VT);
2243
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002244 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2245 // expanding copies of large vectors from registers.
2246 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2247 N1.getNumOperands() > 0) {
2248 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002249 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002250 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2251 N1.getOperand(N2C->getValue() / Factor),
2252 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2253 }
2254
2255 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2256 // expanding large vector constants.
2257 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2258 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002259
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002260 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2261 // operations are lowered to scalars.
2262 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2263 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2264 if (IEC == N2C)
2265 return N1.getOperand(1);
2266 else
2267 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2268 }
2269 break;
2270 case ISD::EXTRACT_ELEMENT:
2271 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002272 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2273 (N1.getValueType().isInteger() == VT.isInteger()) &&
2274 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002275
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002276 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2277 // 64-bit integers into 32-bit parts. Instead of building the extract of
2278 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2279 if (N1.getOpcode() == ISD::BUILD_PAIR)
2280 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002281
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002282 // EXTRACT_ELEMENT of a constant int is also very common.
2283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002284 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002285 unsigned Shift = ElementSize * N2C->getValue();
2286 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2287 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002288 }
2289 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002290 case ISD::EXTRACT_SUBVECTOR:
2291 if (N1.getValueType() == VT) // Trivial extraction.
2292 return N1;
2293 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002294 }
2295
2296 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002297 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002298 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002299 switch (Opcode) {
2300 case ISD::ADD: return getConstant(C1 + C2, VT);
2301 case ISD::SUB: return getConstant(C1 - C2, VT);
2302 case ISD::MUL: return getConstant(C1 * C2, VT);
2303 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002304 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002305 break;
2306 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002307 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002308 break;
2309 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002310 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002311 break;
2312 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002313 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002314 break;
2315 case ISD::AND : return getConstant(C1 & C2, VT);
2316 case ISD::OR : return getConstant(C1 | C2, VT);
2317 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002318 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002319 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2320 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2321 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2322 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002323 default: break;
2324 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002325 } else { // Cannonicalize constant to RHS if commutative
2326 if (isCommutativeBinOp(Opcode)) {
2327 std::swap(N1C, N2C);
2328 std::swap(N1, N2);
2329 }
2330 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002331 }
2332
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002333 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002334 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2335 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002336 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002337 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2338 // Cannonicalize constant to RHS if commutative
2339 std::swap(N1CFP, N2CFP);
2340 std::swap(N1, N2);
2341 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002342 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2343 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002344 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002345 case ISD::FADD:
2346 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002347 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002348 return getConstantFP(V1, VT);
2349 break;
2350 case ISD::FSUB:
2351 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2352 if (s!=APFloat::opInvalidOp)
2353 return getConstantFP(V1, VT);
2354 break;
2355 case ISD::FMUL:
2356 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2357 if (s!=APFloat::opInvalidOp)
2358 return getConstantFP(V1, VT);
2359 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002360 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002361 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2362 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2363 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002364 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002365 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002366 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2367 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2368 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002369 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002370 case ISD::FCOPYSIGN:
2371 V1.copySign(V2);
2372 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002373 default: break;
2374 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002375 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002376 }
Chris Lattner62b57722006-04-20 05:39:12 +00002377
2378 // Canonicalize an UNDEF to the RHS, even over a constant.
2379 if (N1.getOpcode() == ISD::UNDEF) {
2380 if (isCommutativeBinOp(Opcode)) {
2381 std::swap(N1, N2);
2382 } else {
2383 switch (Opcode) {
2384 case ISD::FP_ROUND_INREG:
2385 case ISD::SIGN_EXTEND_INREG:
2386 case ISD::SUB:
2387 case ISD::FSUB:
2388 case ISD::FDIV:
2389 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002390 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002391 return N1; // fold op(undef, arg2) -> undef
2392 case ISD::UDIV:
2393 case ISD::SDIV:
2394 case ISD::UREM:
2395 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002396 case ISD::SRL:
2397 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002398 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002399 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2400 // For vectors, we can't easily build an all zero vector, just return
2401 // the LHS.
2402 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002403 }
2404 }
2405 }
2406
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002407 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002408 if (N2.getOpcode() == ISD::UNDEF) {
2409 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002410 case ISD::XOR:
2411 if (N1.getOpcode() == ISD::UNDEF)
2412 // Handle undef ^ undef -> 0 special case. This is a common
2413 // idiom (misuse).
2414 return getConstant(0, VT);
2415 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002416 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002417 case ISD::ADDC:
2418 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002419 case ISD::SUB:
2420 case ISD::FADD:
2421 case ISD::FSUB:
2422 case ISD::FMUL:
2423 case ISD::FDIV:
2424 case ISD::FREM:
2425 case ISD::UDIV:
2426 case ISD::SDIV:
2427 case ISD::UREM:
2428 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002429 return N2; // fold op(arg1, undef) -> undef
2430 case ISD::MUL:
2431 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002432 case ISD::SRL:
2433 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002434 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002435 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2436 // For vectors, we can't easily build an all zero vector, just return
2437 // the LHS.
2438 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002439 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002440 if (!VT.isVector())
2441 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002442 // For vectors, we can't easily build an all one vector, just return
2443 // the LHS.
2444 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002445 case ISD::SRA:
2446 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002447 }
2448 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002449
Chris Lattner27e9b412005-05-11 18:57:39 +00002450 // Memoize this node if possible.
2451 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002452 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002453 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002454 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002455 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002456 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002457 void *IP = 0;
2458 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2459 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002460 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002461 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002462 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002463 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002464 }
2465
Chris Lattnerc3aae252005-01-07 07:46:32 +00002466 AllNodes.push_back(N);
2467 return SDOperand(N, 0);
2468}
2469
Duncan Sands83ec4b62008-06-06 12:08:01 +00002470SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002471 SDOperand N1, SDOperand N2, SDOperand N3) {
2472 // Perform various simplifications.
2473 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2474 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002475 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002476 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002477 // Use FoldSetCC to simplify SETCC's.
2478 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002479 if (Simp.Val) return Simp;
2480 break;
2481 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002482 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002483 if (N1C) {
2484 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002485 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002486 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002487 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002488 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002489
2490 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002491 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002492 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002493 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002494 if (N2C->getValue()) // Unconditional branch
2495 return getNode(ISD::BR, MVT::Other, N1, N3);
2496 else
2497 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002498 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002499 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002500 case ISD::VECTOR_SHUFFLE:
2501 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002502 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002503 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002504 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002505 "Illegal VECTOR_SHUFFLE node!");
2506 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002507 case ISD::BIT_CONVERT:
2508 // Fold bit_convert nodes from a type to themselves.
2509 if (N1.getValueType() == VT)
2510 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002511 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002512 }
2513
Chris Lattner43247a12005-08-25 19:12:10 +00002514 // Memoize node if it doesn't produce a flag.
2515 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002516 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002517 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002518 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002519 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002520 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002521 void *IP = 0;
2522 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2523 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002524 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002525 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002526 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002527 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002528 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002529 AllNodes.push_back(N);
2530 return SDOperand(N, 0);
2531}
2532
Duncan Sands83ec4b62008-06-06 12:08:01 +00002533SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002534 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002535 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002536 SDOperand Ops[] = { N1, N2, N3, N4 };
2537 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002538}
2539
Duncan Sands83ec4b62008-06-06 12:08:01 +00002540SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002541 SDOperand N1, SDOperand N2, SDOperand N3,
2542 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002543 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2544 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002545}
2546
Dan Gohman707e0182008-04-12 04:36:06 +00002547/// getMemsetValue - Vectorized representation of the memset value
2548/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002549static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2550 unsigned NumBits = VT.isVector() ?
2551 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002552 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002553 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002554 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002555 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002556 Val = (Val << Shift) | Val;
2557 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002558 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002559 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002560 return DAG.getConstant(Val, VT);
2561 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002562 }
Evan Chengf0df0312008-05-15 08:39:06 +00002563
2564 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2565 unsigned Shift = 8;
2566 for (unsigned i = NumBits; i > 8; i >>= 1) {
2567 Value = DAG.getNode(ISD::OR, VT,
2568 DAG.getNode(ISD::SHL, VT, Value,
2569 DAG.getConstant(Shift, MVT::i8)), Value);
2570 Shift <<= 1;
2571 }
2572
2573 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002574}
2575
Dan Gohman707e0182008-04-12 04:36:06 +00002576/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2577/// used when a memcpy is turned into a memset when the source is a constant
2578/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002579static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002580 const TargetLowering &TLI,
2581 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002582 // Handle vector with all elements zero.
2583 if (Str.empty()) {
2584 if (VT.isInteger())
2585 return DAG.getConstant(0, VT);
2586 unsigned NumElts = VT.getVectorNumElements();
2587 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2588 return DAG.getNode(ISD::BIT_CONVERT, VT,
2589 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2590 }
2591
Duncan Sands83ec4b62008-06-06 12:08:01 +00002592 assert(!VT.isVector() && "Can't handle vector type here!");
2593 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002594 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002595 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002596 if (TLI.isLittleEndian())
2597 Offset = Offset + MSB - 1;
2598 for (unsigned i = 0; i != MSB; ++i) {
2599 Val = (Val << 8) | (unsigned char)Str[Offset];
2600 Offset += TLI.isLittleEndian() ? -1 : 1;
2601 }
2602 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002603}
2604
Dan Gohman707e0182008-04-12 04:36:06 +00002605/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002606///
Dan Gohman707e0182008-04-12 04:36:06 +00002607static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2608 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002609 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002610 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2611}
2612
Evan Chengf0df0312008-05-15 08:39:06 +00002613/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2614///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002615static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002616 unsigned SrcDelta = 0;
2617 GlobalAddressSDNode *G = NULL;
2618 if (Src.getOpcode() == ISD::GlobalAddress)
2619 G = cast<GlobalAddressSDNode>(Src);
2620 else if (Src.getOpcode() == ISD::ADD &&
2621 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2622 Src.getOperand(1).getOpcode() == ISD::Constant) {
2623 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2624 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2625 }
2626 if (!G)
2627 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002628
Evan Chengf0df0312008-05-15 08:39:06 +00002629 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002630 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2631 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002632
Evan Chengf0df0312008-05-15 08:39:06 +00002633 return false;
2634}
Dan Gohman707e0182008-04-12 04:36:06 +00002635
Evan Chengf0df0312008-05-15 08:39:06 +00002636/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2637/// to replace the memset / memcpy is below the threshold. It also returns the
2638/// types of the sequence of memory ops to perform memset / memcpy.
2639static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002640bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002641 SDOperand Dst, SDOperand Src,
2642 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002643 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002644 SelectionDAG &DAG,
2645 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002646 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002647 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002648 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002650 if (VT != MVT::iAny) {
2651 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002652 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002653 // If source is a string constant, this will require an unaligned load.
2654 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2655 if (Dst.getOpcode() != ISD::FrameIndex) {
2656 // Can't change destination alignment. It requires a unaligned store.
2657 if (AllowUnalign)
2658 VT = MVT::iAny;
2659 } else {
2660 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2661 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2662 if (MFI->isFixedObjectIndex(FI)) {
2663 // Can't change destination alignment. It requires a unaligned store.
2664 if (AllowUnalign)
2665 VT = MVT::iAny;
2666 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002667 // Give the stack frame object a larger alignment if needed.
2668 if (MFI->getObjectAlignment(FI) < NewAlign)
2669 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002670 Align = NewAlign;
2671 }
2672 }
2673 }
2674 }
2675
2676 if (VT == MVT::iAny) {
2677 if (AllowUnalign) {
2678 VT = MVT::i64;
2679 } else {
2680 switch (Align & 7) {
2681 case 0: VT = MVT::i64; break;
2682 case 4: VT = MVT::i32; break;
2683 case 2: VT = MVT::i16; break;
2684 default: VT = MVT::i8; break;
2685 }
2686 }
2687
Duncan Sands83ec4b62008-06-06 12:08:01 +00002688 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002689 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002690 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2691 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002692
Duncan Sands8e4eb092008-06-08 20:54:56 +00002693 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002694 VT = LVT;
2695 }
Dan Gohman707e0182008-04-12 04:36:06 +00002696
2697 unsigned NumMemOps = 0;
2698 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002699 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002700 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002701 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002702 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002703 VT = MVT::i64;
2704 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002705 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2706 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002707 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002708 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002709 VTSize >>= 1;
2710 }
Dan Gohman707e0182008-04-12 04:36:06 +00002711 }
Dan Gohman707e0182008-04-12 04:36:06 +00002712
2713 if (++NumMemOps > Limit)
2714 return false;
2715 MemOps.push_back(VT);
2716 Size -= VTSize;
2717 }
2718
2719 return true;
2720}
2721
2722static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2723 SDOperand Chain, SDOperand Dst,
2724 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002725 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002726 const Value *DstSV, uint64_t DstSVOff,
2727 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002728 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2729
Dan Gohman21323f32008-05-29 19:42:22 +00002730 // Expand memcpy to a series of load and store ops if the size operand falls
2731 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002732 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002733 uint64_t Limit = -1;
2734 if (!AlwaysInline)
2735 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002736 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002737 std::string Str;
2738 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002739 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002740 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002741 return SDOperand();
2742
Dan Gohman707e0182008-04-12 04:36:06 +00002743
Evan Cheng0ff39b32008-06-30 07:31:25 +00002744 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002745 SmallVector<SDOperand, 8> OutChains;
2746 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002747 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002748 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002749 MVT VT = MemOps[i];
2750 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002751 SDOperand Value, Store;
2752
Evan Cheng0ff39b32008-06-30 07:31:25 +00002753 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002754 // It's unlikely a store of a vector immediate can be done in a single
2755 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002756 // We also handle store a vector with all zero's.
2757 // FIXME: Handle other cases where store of vector immediate is done in
2758 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002759 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002760 Store = DAG.getStore(Chain, Value,
2761 getMemBasePlusOffset(Dst, DstOff, DAG),
2762 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002763 } else {
2764 Value = DAG.getLoad(VT, Chain,
2765 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002766 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002767 Store = DAG.getStore(Chain, Value,
2768 getMemBasePlusOffset(Dst, DstOff, DAG),
2769 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002770 }
2771 OutChains.push_back(Store);
2772 SrcOff += VTSize;
2773 DstOff += VTSize;
2774 }
2775
2776 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2777 &OutChains[0], OutChains.size());
2778}
2779
Dan Gohman21323f32008-05-29 19:42:22 +00002780static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2781 SDOperand Chain, SDOperand Dst,
2782 SDOperand Src, uint64_t Size,
2783 unsigned Align, bool AlwaysInline,
2784 const Value *DstSV, uint64_t DstSVOff,
2785 const Value *SrcSV, uint64_t SrcSVOff){
2786 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2787
2788 // Expand memmove to a series of load and store ops if the size operand falls
2789 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002790 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002791 uint64_t Limit = -1;
2792 if (!AlwaysInline)
2793 Limit = TLI.getMaxStoresPerMemmove();
2794 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002795 std::string Str;
2796 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002797 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002798 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002799 return SDOperand();
2800
Dan Gohman21323f32008-05-29 19:42:22 +00002801 uint64_t SrcOff = 0, DstOff = 0;
2802
2803 SmallVector<SDOperand, 8> LoadValues;
2804 SmallVector<SDOperand, 8> LoadChains;
2805 SmallVector<SDOperand, 8> OutChains;
2806 unsigned NumMemOps = MemOps.size();
2807 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002808 MVT VT = MemOps[i];
2809 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002810 SDOperand Value, Store;
2811
2812 Value = DAG.getLoad(VT, Chain,
2813 getMemBasePlusOffset(Src, SrcOff, DAG),
2814 SrcSV, SrcSVOff + SrcOff, false, Align);
2815 LoadValues.push_back(Value);
2816 LoadChains.push_back(Value.getValue(1));
2817 SrcOff += VTSize;
2818 }
2819 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2820 &LoadChains[0], LoadChains.size());
2821 OutChains.clear();
2822 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002823 MVT VT = MemOps[i];
2824 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002825 SDOperand Value, Store;
2826
2827 Store = DAG.getStore(Chain, LoadValues[i],
2828 getMemBasePlusOffset(Dst, DstOff, DAG),
2829 DstSV, DstSVOff + DstOff, false, DstAlign);
2830 OutChains.push_back(Store);
2831 DstOff += VTSize;
2832 }
2833
2834 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2835 &OutChains[0], OutChains.size());
2836}
2837
Dan Gohman707e0182008-04-12 04:36:06 +00002838static SDOperand getMemsetStores(SelectionDAG &DAG,
2839 SDOperand Chain, SDOperand Dst,
2840 SDOperand Src, uint64_t Size,
2841 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002842 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002843 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2844
2845 // Expand memset to a series of load/store ops if the size operand
2846 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002847 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002848 std::string Str;
2849 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002850 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002851 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002852 return SDOperand();
2853
2854 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002855 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002856
2857 unsigned NumMemOps = MemOps.size();
2858 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002859 MVT VT = MemOps[i];
2860 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002861 SDOperand Value = getMemsetValue(Src, VT, DAG);
2862 SDOperand Store = DAG.getStore(Chain, Value,
2863 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002864 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002865 OutChains.push_back(Store);
2866 DstOff += VTSize;
2867 }
2868
2869 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2870 &OutChains[0], OutChains.size());
2871}
2872
2873SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002874 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002875 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002876 const Value *DstSV, uint64_t DstSVOff,
2877 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002878
2879 // Check to see if we should lower the memcpy to loads and stores first.
2880 // For cases within the target-specified limits, this is the best choice.
2881 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2882 if (ConstantSize) {
2883 // Memcpy with size zero? Just return the original chain.
2884 if (ConstantSize->isNullValue())
2885 return Chain;
2886
2887 SDOperand Result =
2888 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002889 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002890 if (Result.Val)
2891 return Result;
2892 }
2893
2894 // Then check to see if we should lower the memcpy with target-specific
2895 // code. If the target chooses to do this, this is the next best.
2896 SDOperand Result =
2897 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2898 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002899 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002900 if (Result.Val)
2901 return Result;
2902
2903 // If we really need inline code and the target declined to provide it,
2904 // use a (potentially long) sequence of loads and stores.
2905 if (AlwaysInline) {
2906 assert(ConstantSize && "AlwaysInline requires a constant size!");
2907 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2908 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002909 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002910 }
2911
2912 // Emit a library call.
2913 TargetLowering::ArgListTy Args;
2914 TargetLowering::ArgListEntry Entry;
2915 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2916 Entry.Node = Dst; Args.push_back(Entry);
2917 Entry.Node = Src; Args.push_back(Entry);
2918 Entry.Node = Size; Args.push_back(Entry);
2919 std::pair<SDOperand,SDOperand> CallResult =
2920 TLI.LowerCallTo(Chain, Type::VoidTy,
2921 false, false, false, CallingConv::C, false,
2922 getExternalSymbol("memcpy", TLI.getPointerTy()),
2923 Args, *this);
2924 return CallResult.second;
2925}
2926
2927SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2928 SDOperand Src, SDOperand Size,
2929 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002930 const Value *DstSV, uint64_t DstSVOff,
2931 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002932
Dan Gohman21323f32008-05-29 19:42:22 +00002933 // Check to see if we should lower the memmove to loads and stores first.
2934 // For cases within the target-specified limits, this is the best choice.
2935 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2936 if (ConstantSize) {
2937 // Memmove with size zero? Just return the original chain.
2938 if (ConstantSize->isNullValue())
2939 return Chain;
2940
2941 SDOperand Result =
2942 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2943 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2944 if (Result.Val)
2945 return Result;
2946 }
Dan Gohman707e0182008-04-12 04:36:06 +00002947
2948 // Then check to see if we should lower the memmove with target-specific
2949 // code. If the target chooses to do this, this is the next best.
2950 SDOperand Result =
2951 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002952 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002953 if (Result.Val)
2954 return Result;
2955
2956 // Emit a library call.
2957 TargetLowering::ArgListTy Args;
2958 TargetLowering::ArgListEntry Entry;
2959 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2960 Entry.Node = Dst; Args.push_back(Entry);
2961 Entry.Node = Src; Args.push_back(Entry);
2962 Entry.Node = Size; Args.push_back(Entry);
2963 std::pair<SDOperand,SDOperand> CallResult =
2964 TLI.LowerCallTo(Chain, Type::VoidTy,
2965 false, false, false, CallingConv::C, false,
2966 getExternalSymbol("memmove", TLI.getPointerTy()),
2967 Args, *this);
2968 return CallResult.second;
2969}
2970
2971SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2972 SDOperand Src, SDOperand Size,
2973 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002974 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002975
2976 // Check to see if we should lower the memset to stores first.
2977 // For cases within the target-specified limits, this is the best choice.
2978 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2979 if (ConstantSize) {
2980 // Memset with size zero? Just return the original chain.
2981 if (ConstantSize->isNullValue())
2982 return Chain;
2983
2984 SDOperand Result =
2985 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002986 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002987 if (Result.Val)
2988 return Result;
2989 }
2990
2991 // Then check to see if we should lower the memset with target-specific
2992 // code. If the target chooses to do this, this is the next best.
2993 SDOperand Result =
2994 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002995 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002996 if (Result.Val)
2997 return Result;
2998
2999 // Emit a library call.
3000 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3001 TargetLowering::ArgListTy Args;
3002 TargetLowering::ArgListEntry Entry;
3003 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3004 Args.push_back(Entry);
3005 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003006 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003007 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3008 else
3009 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3010 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3011 Args.push_back(Entry);
3012 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3013 Args.push_back(Entry);
3014 std::pair<SDOperand,SDOperand> CallResult =
3015 TLI.LowerCallTo(Chain, Type::VoidTy,
3016 false, false, false, CallingConv::C, false,
3017 getExternalSymbol("memset", TLI.getPointerTy()),
3018 Args, *this);
3019 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003020}
3021
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003022SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003023 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003024 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003025 unsigned Alignment) {
3026 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003027 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3028 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003029 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003030 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003031 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003032 void* IP = 0;
3033 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3034 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003035 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp,
Mon P Wang28873102008-06-25 08:15:39 +00003036 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003037 CSEMap.InsertNode(N, IP);
3038 AllNodes.push_back(N);
3039 return SDOperand(N, 0);
3040}
3041
3042SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003043 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003044 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003045 unsigned Alignment) {
3046 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003047 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3048 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003049 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003050 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3051 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003052 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003053 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003054 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003055 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003056 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003057 void* IP = 0;
3058 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3059 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003060 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val,
Mon P Wang28873102008-06-25 08:15:39 +00003061 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003062 CSEMap.InsertNode(N, IP);
3063 AllNodes.push_back(N);
3064 return SDOperand(N, 0);
3065}
3066
Duncan Sands14ea39c2008-03-27 20:23:40 +00003067SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003068SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003069 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003070 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003071 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003072 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003073 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3074 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003075 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003076 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003077 } else if (SV) {
3078 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3079 assert(PT && "Value for load must be a pointer");
3080 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003081 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003082 assert(Ty && "Could not get type information for load");
3083 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3084 }
Evan Cheng466685d2006-10-09 20:57:25 +00003085
Duncan Sands14ea39c2008-03-27 20:23:40 +00003086 if (VT == EVT) {
3087 ExtType = ISD::NON_EXTLOAD;
3088 } else if (ExtType == ISD::NON_EXTLOAD) {
3089 assert(VT == EVT && "Non-extending load from different memory type!");
3090 } else {
3091 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003092 if (VT.isVector())
3093 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003094 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003095 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003096 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003097 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003098 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003099 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003100 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003101 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003102
3103 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003104 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003105 "Unindexed load with an offset!");
3106
3107 SDVTList VTs = Indexed ?
3108 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3109 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003110 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003111 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003112 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003113 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003114 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003115 ID.AddInteger(Alignment);
3116 ID.AddInteger(isVolatile);
3117 void *IP = 0;
3118 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3119 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003120 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3121 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003122 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003123 AllNodes.push_back(N);
3124 return SDOperand(N, 0);
3125}
3126
Duncan Sands83ec4b62008-06-06 12:08:01 +00003127SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003128 SDOperand Chain, SDOperand Ptr,
3129 const Value *SV, int SVOffset,
3130 bool isVolatile, unsigned Alignment) {
3131 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003132 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3133 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003134}
3135
Duncan Sands83ec4b62008-06-06 12:08:01 +00003136SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003137 SDOperand Chain, SDOperand Ptr,
3138 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003139 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003140 bool isVolatile, unsigned Alignment) {
3141 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003142 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3143 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003144}
3145
Evan Cheng144d8f02006-11-09 17:55:04 +00003146SDOperand
3147SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3148 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003149 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003150 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3151 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003152 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3153 LD->getChain(), Base, Offset, LD->getSrcValue(),
3154 LD->getSrcValueOffset(), LD->getMemoryVT(),
3155 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003156}
3157
Jeff Cohend41b30d2006-11-05 19:31:28 +00003158SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003159 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003160 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003161 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003162
Dan Gohman575e2f42007-06-04 15:49:41 +00003163 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3164 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003165 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003166 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003167 } else if (SV) {
3168 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3169 assert(PT && "Value for store must be a pointer");
3170 Ty = PT->getElementType();
3171 }
3172 assert(Ty && "Could not get type information for store");
3173 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3174 }
Evan Chengad071e12006-10-05 22:57:11 +00003175 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003176 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003177 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003178 FoldingSetNodeID ID;
3179 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003180 ID.AddInteger(ISD::UNINDEXED);
3181 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003182 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003183 ID.AddInteger(Alignment);
3184 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003185 void *IP = 0;
3186 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3187 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003188 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003189 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003190 CSEMap.InsertNode(N, IP);
3191 AllNodes.push_back(N);
3192 return SDOperand(N, 0);
3193}
3194
Jeff Cohend41b30d2006-11-05 19:31:28 +00003195SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003196 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003197 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003198 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003199 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003200
3201 if (VT == SVT)
3202 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003203
Duncan Sands8e4eb092008-06-08 20:54:56 +00003204 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003205 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003206 "Can't do FP-INT conversion!");
3207
Dan Gohman575e2f42007-06-04 15:49:41 +00003208 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3209 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003210 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003211 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003212 } else if (SV) {
3213 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3214 assert(PT && "Value for store must be a pointer");
3215 Ty = PT->getElementType();
3216 }
3217 assert(Ty && "Could not get type information for store");
3218 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3219 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003220 SDVTList VTs = getVTList(MVT::Other);
3221 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003222 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003223 FoldingSetNodeID ID;
3224 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003225 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003226 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003227 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003228 ID.AddInteger(Alignment);
3229 ID.AddInteger(isVolatile);
3230 void *IP = 0;
3231 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3232 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003233 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003234 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003235 CSEMap.InsertNode(N, IP);
3236 AllNodes.push_back(N);
3237 return SDOperand(N, 0);
3238}
3239
Evan Cheng144d8f02006-11-09 17:55:04 +00003240SDOperand
3241SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3242 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003243 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3244 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3245 "Store is already a indexed store!");
3246 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3247 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3248 FoldingSetNodeID ID;
3249 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3250 ID.AddInteger(AM);
3251 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003252 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003253 ID.AddInteger(ST->getAlignment());
3254 ID.AddInteger(ST->isVolatile());
3255 void *IP = 0;
3256 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3257 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003258 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003259 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003260 ST->getSrcValue(), ST->getSrcValueOffset(),
3261 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003262 CSEMap.InsertNode(N, IP);
3263 AllNodes.push_back(N);
3264 return SDOperand(N, 0);
3265}
3266
Duncan Sands83ec4b62008-06-06 12:08:01 +00003267SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003268 SDOperand Chain, SDOperand Ptr,
3269 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003270 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003271 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003272}
3273
Duncan Sands83ec4b62008-06-06 12:08:01 +00003274SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003275 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003276 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003277 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003278 case 1: return getNode(Opcode, VT, Ops[0]);
3279 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3280 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003281 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003282 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003283
Chris Lattneref847df2005-04-09 03:27:28 +00003284 switch (Opcode) {
3285 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003286 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003287 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003288 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3289 "LHS and RHS of condition must have same type!");
3290 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3291 "True and False arms of SelectCC must have same type!");
3292 assert(Ops[2].getValueType() == VT &&
3293 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003294 break;
3295 }
3296 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003297 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003298 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3299 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003300 break;
3301 }
Chris Lattneref847df2005-04-09 03:27:28 +00003302 }
3303
Chris Lattner385328c2005-05-14 07:42:29 +00003304 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003305 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003306 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003307 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003308 FoldingSetNodeID ID;
3309 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003310 void *IP = 0;
3311 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3312 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003313 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003314 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003315 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003316 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003317 }
Chris Lattneref847df2005-04-09 03:27:28 +00003318 AllNodes.push_back(N);
3319 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003320}
3321
Chris Lattner89c34632005-05-14 06:20:26 +00003322SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003323 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003324 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003325 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3326 Ops, NumOps);
3327}
3328
3329SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003330 const MVT *VTs, unsigned NumVTs,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003331 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003332 if (NumVTs == 1)
3333 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003334 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3335}
3336
3337SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003338 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003339 if (VTList.NumVTs == 1)
3340 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003341
Chris Lattner5f056bf2005-07-10 01:55:33 +00003342 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003343 // FIXME: figure out how to safely handle things like
3344 // int foo(int x) { return 1 << (x & 255); }
3345 // int bar() { return foo(256); }
3346#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003347 case ISD::SRA_PARTS:
3348 case ISD::SRL_PARTS:
3349 case ISD::SHL_PARTS:
3350 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003351 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003352 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3353 else if (N3.getOpcode() == ISD::AND)
3354 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3355 // If the and is only masking out bits that cannot effect the shift,
3356 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003357 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003358 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3359 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3360 }
3361 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003362#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003363 }
Chris Lattner89c34632005-05-14 06:20:26 +00003364
Chris Lattner43247a12005-08-25 19:12:10 +00003365 // Memoize the node unless it returns a flag.
3366 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003367 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003368 FoldingSetNodeID ID;
3369 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003370 void *IP = 0;
3371 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3372 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003373 if (NumOps == 1)
3374 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3375 else if (NumOps == 2)
3376 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3377 else if (NumOps == 3)
3378 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3379 else
3380 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003381 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003382 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003383 if (NumOps == 1)
3384 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3385 else if (NumOps == 2)
3386 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3387 else if (NumOps == 3)
3388 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3389 else
3390 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003391 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003392 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003393 return SDOperand(N, 0);
3394}
3395
Dan Gohman08ce9762007-10-08 15:49:58 +00003396SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003397 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003398}
3399
3400SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3401 SDOperand N1) {
3402 SDOperand Ops[] = { N1 };
3403 return getNode(Opcode, VTList, Ops, 1);
3404}
3405
3406SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3407 SDOperand N1, SDOperand N2) {
3408 SDOperand Ops[] = { N1, N2 };
3409 return getNode(Opcode, VTList, Ops, 2);
3410}
3411
3412SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3413 SDOperand N1, SDOperand N2, SDOperand N3) {
3414 SDOperand Ops[] = { N1, N2, N3 };
3415 return getNode(Opcode, VTList, Ops, 3);
3416}
3417
3418SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3419 SDOperand N1, SDOperand N2, SDOperand N3,
3420 SDOperand N4) {
3421 SDOperand Ops[] = { N1, N2, N3, N4 };
3422 return getNode(Opcode, VTList, Ops, 4);
3423}
3424
3425SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3426 SDOperand N1, SDOperand N2, SDOperand N3,
3427 SDOperand N4, SDOperand N5) {
3428 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3429 return getNode(Opcode, VTList, Ops, 5);
3430}
3431
Duncan Sands83ec4b62008-06-06 12:08:01 +00003432SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003433 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003434}
3435
Duncan Sands83ec4b62008-06-06 12:08:01 +00003436SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3437 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003438 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003439 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003440 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003441 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003442 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003443 V.push_back(VT1);
3444 V.push_back(VT2);
3445 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003446 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003447}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003448SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3449 MVT VT3) {
3450 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003451 E = VTList.end(); I != E; ++I) {
3452 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3453 (*I)[2] == VT3)
3454 return makeVTList(&(*I)[0], 3);
3455 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003456 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003457 V.push_back(VT1);
3458 V.push_back(VT2);
3459 V.push_back(VT3);
3460 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003461 return makeVTList(&(*VTList.begin())[0], 3);
3462}
3463
Duncan Sands83ec4b62008-06-06 12:08:01 +00003464SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003465 switch (NumVTs) {
3466 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003467 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003468 case 2: return getVTList(VTs[0], VTs[1]);
3469 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3470 default: break;
3471 }
3472
Duncan Sands83ec4b62008-06-06 12:08:01 +00003473 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003474 E = VTList.end(); I != E; ++I) {
3475 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3476
3477 bool NoMatch = false;
3478 for (unsigned i = 2; i != NumVTs; ++i)
3479 if (VTs[i] != (*I)[i]) {
3480 NoMatch = true;
3481 break;
3482 }
3483 if (!NoMatch)
3484 return makeVTList(&*I->begin(), NumVTs);
3485 }
3486
Duncan Sands83ec4b62008-06-06 12:08:01 +00003487 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003488 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003489}
3490
3491
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003492/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3493/// specified operands. If the resultant node already exists in the DAG,
3494/// this does not modify the specified node, instead it returns the node that
3495/// already exists. If the resultant node does not exist in the DAG, the
3496/// input node is returned. As a degenerate case, if you specify the same
3497/// input operands as the node already has, the input node is returned.
3498SDOperand SelectionDAG::
3499UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3500 SDNode *N = InN.Val;
3501 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3502
3503 // Check to see if there is no change.
3504 if (Op == N->getOperand(0)) return InN;
3505
3506 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003507 void *InsertPos = 0;
3508 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3509 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003510
3511 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003512 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003513 RemoveNodeFromCSEMaps(N);
3514
3515 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003516 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003517 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003518 N->OperandList[0].setUser(N);
3519 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003520
3521 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003522 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003523 return InN;
3524}
3525
3526SDOperand SelectionDAG::
3527UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3528 SDNode *N = InN.Val;
3529 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3530
3531 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003532 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3533 return InN; // No operands changed, just return the input node.
3534
3535 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003536 void *InsertPos = 0;
3537 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3538 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003539
3540 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003541 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003542 RemoveNodeFromCSEMaps(N);
3543
3544 // Now we update the operands.
3545 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003546 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003547 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003548 N->OperandList[0].setUser(N);
3549 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003550 }
3551 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003552 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003553 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003554 N->OperandList[1].setUser(N);
3555 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003556 }
3557
3558 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003559 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003560 return InN;
3561}
3562
3563SDOperand SelectionDAG::
3564UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003565 SDOperand Ops[] = { Op1, Op2, Op3 };
3566 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003567}
3568
3569SDOperand SelectionDAG::
3570UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3571 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003572 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3573 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003574}
3575
3576SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003577UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3578 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003579 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3580 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003581}
3582
Chris Lattner809ec112006-01-28 10:09:25 +00003583SDOperand SelectionDAG::
Roman Levenstein9cac5252008-04-16 16:15:27 +00003584UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003585 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003586 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003587 "Update with wrong number of operands");
3588
3589 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003590 bool AnyChange = false;
3591 for (unsigned i = 0; i != NumOps; ++i) {
3592 if (Ops[i] != N->getOperand(i)) {
3593 AnyChange = true;
3594 break;
3595 }
3596 }
3597
3598 // No operands changed, just return the input node.
3599 if (!AnyChange) return InN;
3600
3601 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003602 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003603 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003604 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003605
Dan Gohman7ceda162008-05-02 00:05:03 +00003606 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003607 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003608 RemoveNodeFromCSEMaps(N);
3609
3610 // Now we update the operands.
3611 for (unsigned i = 0; i != NumOps; ++i) {
3612 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003613 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003614 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003615 N->OperandList[i].setUser(N);
3616 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003617 }
3618 }
3619
3620 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003621 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003622 return InN;
3623}
3624
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003625/// MorphNodeTo - This frees the operands of the current node, resets the
3626/// opcode, types, and operands to the specified value. This should only be
3627/// used by the SelectionDAG class.
3628void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman35b31be2008-04-17 23:02:12 +00003629 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003630 NodeType = Opc;
3631 ValueList = L.VTs;
3632 NumValues = L.NumVTs;
3633
3634 // Clear the operands list, updating used nodes to remove this from their
3635 // use list.
3636 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003637 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003638
3639 // If NumOps is larger than the # of operands we currently have, reallocate
3640 // the operand list.
3641 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003642 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003643 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003644 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003645 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003646 OperandsNeedDelete = true;
3647 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003648
3649 // Assign the new operands.
3650 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003651
3652 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3653 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003654 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003655 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003656 N->addUser(i, this);
3657 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003658 }
3659}
Chris Lattner149c58c2005-08-16 18:17:10 +00003660
3661/// SelectNodeTo - These are used for target selectors to *mutate* the
3662/// specified node to have the specified return type, Target opcode, and
3663/// operands. Note that target opcodes are stored as
3664/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003665///
3666/// Note that SelectNodeTo returns the resultant node. If there is already a
3667/// node of the specified opcode and operands, it returns that node instead of
3668/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003669SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003670 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003671 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003672 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00003673 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00003674 void *IP = 0;
3675 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003676 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00003677
Chris Lattner7651fa42005-08-24 23:00:29 +00003678 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003679
Dan Gohman35b31be2008-04-17 23:02:12 +00003680 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, SDOperandPtr(), 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003681
Chris Lattner4a283e92006-08-11 18:38:11 +00003682 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003683 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00003684}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003685
Evan Cheng95514ba2006-08-26 08:00:10 +00003686SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003687 MVT VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003688 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003689 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003690 SDOperand Ops[] = { Op1 };
3691
Jim Laskey583bd472006-10-27 23:46:08 +00003692 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003693 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003694 void *IP = 0;
3695 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003696 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003697
Chris Lattner149c58c2005-08-16 18:17:10 +00003698 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003699 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00003700 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00003701 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003702}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003703
Evan Cheng95514ba2006-08-26 08:00:10 +00003704SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003705 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003706 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003707 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003708 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003709 SDOperand Ops[] = { Op1, Op2 };
3710
Jim Laskey583bd472006-10-27 23:46:08 +00003711 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003712 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003713 void *IP = 0;
3714 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003715 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003716
Chris Lattner149c58c2005-08-16 18:17:10 +00003717 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003718
Chris Lattner63e3f142007-02-04 07:28:00 +00003719 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003720
Chris Lattnera5682852006-08-07 23:03:03 +00003721 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003722 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003723}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003724
Evan Cheng95514ba2006-08-26 08:00:10 +00003725SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003726 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003727 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003728 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003729 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003730 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003731 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003732 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003733 void *IP = 0;
3734 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003735 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003736
Chris Lattner149c58c2005-08-16 18:17:10 +00003737 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003738
Chris Lattner63e3f142007-02-04 07:28:00 +00003739 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003740
Chris Lattnera5682852006-08-07 23:03:03 +00003741 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003742 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00003743}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003744
Evan Cheng95514ba2006-08-26 08:00:10 +00003745SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003746 MVT VT, SDOperandPtr Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003747 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003748 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003749 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00003750 FoldingSetNodeID ID;
3751 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003752 void *IP = 0;
3753 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003754 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00003755
Chris Lattner6b09a292005-08-21 18:49:33 +00003756 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003757 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003758
Chris Lattnera5682852006-08-07 23:03:03 +00003759 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003760 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003761}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003762
Evan Cheng95514ba2006-08-26 08:00:10 +00003763SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003764 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003765 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003766 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00003767 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003768 SDOperand Ops[] = { Op1, Op2 };
3769 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003770 void *IP = 0;
3771 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003772 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003773
Chris Lattner0fb094f2005-11-19 01:44:53 +00003774 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00003775 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003776 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003777 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003778}
3779
Evan Cheng95514ba2006-08-26 08:00:10 +00003780SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003781 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003782 SDOperand Op1, SDOperand Op2,
3783 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003784 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003785 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003786 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003787 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003788 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003789 void *IP = 0;
3790 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003791 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003792
Chris Lattner0fb094f2005-11-19 01:44:53 +00003793 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003794
Chris Lattner63e3f142007-02-04 07:28:00 +00003795 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003796 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003797 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003798}
3799
Chris Lattner0fb094f2005-11-19 01:44:53 +00003800
Evan Cheng6ae46c42006-02-09 07:15:23 +00003801/// getTargetNode - These are used for target selectors to create a new node
3802/// with specified return type(s), target opcode, and operands.
3803///
3804/// Note that getTargetNode returns the resultant node. If there is already a
3805/// node of the specified opcode and operands, it returns that node instead of
3806/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003807SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003808 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3809}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003810SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003811 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3812}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003813SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003814 SDOperand Op1, SDOperand Op2) {
3815 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3816}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003817SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003818 SDOperand Op1, SDOperand Op2,
3819 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003820 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3821}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003822SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003823 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003824 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003825}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003826SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3827 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003828 SDOperand Op;
3829 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3830}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003831SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3832 MVT VT2, SDOperand Op1) {
3833 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003834 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003835}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003836SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3837 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003838 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003839 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003840 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003841 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003842}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003843SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3844 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003845 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003846 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003847 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003848 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003849}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003850SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003851 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003852 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003853 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003854}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003855SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003856 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003857 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003858 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003859 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003860}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003861SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003862 SDOperand Op1, SDOperand Op2,
3863 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003864 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003865 SDOperand Ops[] = { Op1, Op2, Op3 };
3866 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3867}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003868SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003869 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003870 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003871 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003872}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003873SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3874 MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003875 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003876 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003877 VTList.push_back(VT1);
3878 VTList.push_back(VT2);
3879 VTList.push_back(VT3);
3880 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003881 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003882 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3883}
Evan Cheng39305cf2007-10-05 01:10:49 +00003884SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003885 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003886 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003887 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003888 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3889 Ops, NumOps).Val;
3890}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003891
Evan Cheng08b11732008-03-22 01:55:50 +00003892/// getNodeIfExists - Get the specified node if it's already available, or
3893/// else return NULL.
3894SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003895 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003896 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3897 FoldingSetNodeID ID;
3898 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3899 void *IP = 0;
3900 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3901 return E;
3902 }
3903 return NULL;
3904}
3905
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003906
Evan Cheng99157a02006-08-07 22:13:29 +00003907/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003908/// This can cause recursive merging of nodes in the DAG.
3909///
Chris Lattner11d049c2008-02-03 03:35:22 +00003910/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003911///
Chris Lattner11d049c2008-02-03 03:35:22 +00003912void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003913 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003914 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003915 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003916 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003917 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003918
Chris Lattner8b8749f2005-08-17 19:00:20 +00003919 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003920 SDNode::use_iterator UI = From->use_begin();
3921 SDNode *U = UI->getUser();
3922
Chris Lattner8b8749f2005-08-17 19:00:20 +00003923 // This node is about to morph, remove its old self from the CSE maps.
3924 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003925 int operandNum = 0;
3926 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3927 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003928 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003929 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003930 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003931 I->setUser(U);
3932 To.Val->addUser(operandNum, U);
3933 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003934
3935 // Now that we have modified U, add it back to the CSE maps. If it already
3936 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003937 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003938 ReplaceAllUsesWith(U, Existing, UpdateListener);
3939 // U is now dead. Inform the listener if it exists and delete it.
3940 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003941 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003942 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003943 } else {
3944 // If the node doesn't already exist, we updated it. Inform a listener if
3945 // it exists.
3946 if (UpdateListener)
3947 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003948 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003949 }
3950}
3951
3952/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3953/// This can cause recursive merging of nodes in the DAG.
3954///
3955/// This version assumes From/To have matching types and numbers of result
3956/// values.
3957///
Chris Lattner1e111c72005-09-07 05:37:01 +00003958void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003959 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003960 assert(From != To && "Cannot replace uses of with self");
3961 assert(From->getNumValues() == To->getNumValues() &&
3962 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003963 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003964 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3965 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003966
3967 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003968 SDNode::use_iterator UI = From->use_begin();
3969 SDNode *U = UI->getUser();
3970
Chris Lattner8b52f212005-08-26 18:36:28 +00003971 // This node is about to morph, remove its old self from the CSE maps.
3972 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003973 int operandNum = 0;
3974 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3975 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003976 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003977 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003978 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003979 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003980 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003981
Chris Lattner8b8749f2005-08-17 19:00:20 +00003982 // Now that we have modified U, add it back to the CSE maps. If it already
3983 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003984 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003985 ReplaceAllUsesWith(U, Existing, UpdateListener);
3986 // U is now dead. Inform the listener if it exists and delete it.
3987 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003988 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003989 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003990 } else {
3991 // If the node doesn't already exist, we updated it. Inform a listener if
3992 // it exists.
3993 if (UpdateListener)
3994 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003995 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003996 }
3997}
3998
Chris Lattner8b52f212005-08-26 18:36:28 +00003999/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4000/// This can cause recursive merging of nodes in the DAG.
4001///
4002/// This version can replace From with any result values. To must match the
4003/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004004void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein9cac5252008-04-16 16:15:27 +00004005 SDOperandPtr To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004006 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004007 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004008 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004009
4010 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004011 SDNode::use_iterator UI = From->use_begin();
4012 SDNode *U = UI->getUser();
4013
Chris Lattner7b2880c2005-08-24 22:44:39 +00004014 // This node is about to morph, remove its old self from the CSE maps.
4015 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004016 int operandNum = 0;
4017 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4018 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004019 if (I->getVal() == From) {
4020 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004021 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004022 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004023 I->setUser(U);
4024 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004025 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004026
Chris Lattner7b2880c2005-08-24 22:44:39 +00004027 // Now that we have modified U, add it back to the CSE maps. If it already
4028 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004029 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004030 ReplaceAllUsesWith(U, Existing, UpdateListener);
4031 // U is now dead. Inform the listener if it exists and delete it.
4032 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004033 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004034 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004035 } else {
4036 // If the node doesn't already exist, we updated it. Inform a listener if
4037 // it exists.
4038 if (UpdateListener)
4039 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004040 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004041 }
4042}
4043
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004044namespace {
4045 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4046 /// any deleted nodes from the set passed into its constructor and recursively
4047 /// notifies another update listener if specified.
4048 class ChainedSetUpdaterListener :
4049 public SelectionDAG::DAGUpdateListener {
4050 SmallSetVector<SDNode*, 16> &Set;
4051 SelectionDAG::DAGUpdateListener *Chain;
4052 public:
4053 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4054 SelectionDAG::DAGUpdateListener *chain)
4055 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004056
Duncan Sandsedfcf592008-06-11 11:42:12 +00004057 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004058 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004059 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004060 }
4061 virtual void NodeUpdated(SDNode *N) {
4062 if (Chain) Chain->NodeUpdated(N);
4063 }
4064 };
4065}
4066
Chris Lattner012f2412006-02-17 21:58:01 +00004067/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4068/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004069/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004070void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004071 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004072 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004073
Chris Lattner012f2412006-02-17 21:58:01 +00004074 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004075 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004076 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004077 return;
4078 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004079
4080 if (From.use_empty()) return;
4081
Chris Lattnercf5640b2007-02-04 00:14:31 +00004082 // Get all of the users of From.Val. We want these in a nice,
4083 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004084 SmallSetVector<SDNode*, 16> Users;
4085 for (SDNode::use_iterator UI = From.Val->use_begin(),
4086 E = From.Val->use_end(); UI != E; ++UI) {
4087 SDNode *User = UI->getUser();
4088 if (!Users.count(User))
4089 Users.insert(User);
4090 }
Chris Lattner012f2412006-02-17 21:58:01 +00004091
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004092 // When one of the recursive merges deletes nodes from the graph, we need to
4093 // make sure that UpdateListener is notified *and* that the node is removed
4094 // from Users if present. CSUL does this.
4095 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004096
Chris Lattner012f2412006-02-17 21:58:01 +00004097 while (!Users.empty()) {
4098 // We know that this user uses some value of From. If it is the right
4099 // value, update it.
4100 SDNode *User = Users.back();
4101 Users.pop_back();
4102
Chris Lattner01d029b2007-10-15 06:10:22 +00004103 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004104 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004105 for (; Op != E; ++Op)
4106 if (*Op == From) break;
4107
4108 // If there are no matches, the user must use some other result of From.
4109 if (Op == E) continue;
4110
4111 // Okay, we know this user needs to be updated. Remove its old self
4112 // from the CSE maps.
4113 RemoveNodeFromCSEMaps(User);
4114
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004115 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004116 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004117 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004118 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004119 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004120 Op->setUser(User);
4121 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004122 }
4123 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004124
4125 // Now that we have modified User, add it back to the CSE maps. If it
4126 // already exists there, recursively merge the results together.
4127 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004128 if (!Existing) {
4129 if (UpdateListener) UpdateListener->NodeUpdated(User);
4130 continue; // Continue on to next user.
4131 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004132
4133 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004134 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004135 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004136 // can cause deletion of nodes that used the old value. To handle this, we
4137 // use CSUL to remove them from the Users set.
4138 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004139
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004140 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004141 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004142 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004143 }
4144}
4145
Evan Chenge6f35d82006-08-01 08:20:41 +00004146/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4147/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004148unsigned SelectionDAG::AssignNodeIds() {
4149 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004150 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4151 SDNode *N = I;
4152 N->setNodeId(Id++);
4153 }
4154 return Id;
4155}
4156
Evan Chenge6f35d82006-08-01 08:20:41 +00004157/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004158/// based on their topological order. It returns the maximum id and a vector
4159/// of the SDNodes* in assigned order by reference.
4160unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004161 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004162 std::vector<unsigned> InDegree(DAGSize);
4163 std::vector<SDNode*> Sources;
4164
4165 // Use a two pass approach to avoid using a std::map which is slow.
4166 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004167 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4168 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004169 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004170 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004171 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004172 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004173 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004174 }
4175
Evan Cheng99157a02006-08-07 22:13:29 +00004176 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004177 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004178 SDNode *N = Sources.back();
4179 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004180 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004181 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004182 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004183 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004184 if (Degree == 0)
4185 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004186 }
4187 }
4188
Evan Chengc384d6c2006-08-02 22:00:34 +00004189 // Second pass, assign the actual topological order as node ids.
4190 Id = 0;
4191 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4192 TI != TE; ++TI)
4193 (*TI)->setNodeId(Id++);
4194
4195 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004196}
4197
4198
Evan Cheng091cba12006-07-27 06:39:06 +00004199
Jim Laskey58b968b2005-08-17 20:08:02 +00004200//===----------------------------------------------------------------------===//
4201// SDNode Class
4202//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004203
Chris Lattner917d2c92006-07-19 00:00:37 +00004204// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004205void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004206void UnarySDNode::ANCHOR() {}
4207void BinarySDNode::ANCHOR() {}
4208void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004209void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004210void ConstantSDNode::ANCHOR() {}
4211void ConstantFPSDNode::ANCHOR() {}
4212void GlobalAddressSDNode::ANCHOR() {}
4213void FrameIndexSDNode::ANCHOR() {}
4214void JumpTableSDNode::ANCHOR() {}
4215void ConstantPoolSDNode::ANCHOR() {}
4216void BasicBlockSDNode::ANCHOR() {}
4217void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004218void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004219void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004220void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004221void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004222void ExternalSymbolSDNode::ANCHOR() {}
4223void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004224void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004225void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004226void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004227void LoadSDNode::ANCHOR() {}
4228void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004229void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004230
Chris Lattner48b85922007-02-04 02:41:42 +00004231HandleSDNode::~HandleSDNode() {
4232 SDVTList VTs = { 0, 0 };
Dan Gohman35b31be2008-04-17 23:02:12 +00004233 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00004234}
4235
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004236GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004237 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004238 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004239 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004240 // Thread Local
4241 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4242 // Non Thread Local
4243 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4244 getSDVTList(VT)), Offset(o) {
4245 TheGlobal = const_cast<GlobalValue*>(GA);
4246}
Chris Lattner48b85922007-02-04 02:41:42 +00004247
Dan Gohman36b5c132008-04-07 19:35:22 +00004248/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004249/// reference performed by this atomic.
4250MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004251 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004252 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4253 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4254
4255 // Check if the atomic references a frame index
4256 const FrameIndexSDNode *FI =
4257 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4258 if (!getSrcValue() && FI)
4259 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4260 FI->getIndex(), Size, getAlignment());
4261 else
4262 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4263 Size, getAlignment());
4264}
4265
4266/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004267/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004268MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004269 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004270 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004271 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4272 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004273 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004274
4275 // Check if the load references a frame index, and does not have
4276 // an SV attached.
4277 const FrameIndexSDNode *FI =
4278 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4279 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004280 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004281 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004282 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004283 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004284 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004285}
4286
Jim Laskey583bd472006-10-27 23:46:08 +00004287/// Profile - Gather unique data for the node.
4288///
4289void SDNode::Profile(FoldingSetNodeID &ID) {
4290 AddNodeIDNode(ID, this);
4291}
4292
Chris Lattnera3255112005-11-08 23:30:28 +00004293/// getValueTypeList - Return a pointer to the specified value type.
4294///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004295const MVT *SDNode::getValueTypeList(MVT VT) {
4296 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004297 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004298 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004299 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004300 static MVT VTs[MVT::LAST_VALUETYPE];
4301 VTs[VT.getSimpleVT()] = VT;
4302 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004303 }
Chris Lattnera3255112005-11-08 23:30:28 +00004304}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004305
Chris Lattner5c884562005-01-12 18:37:47 +00004306/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4307/// indicated value. This method ignores uses of other values defined by this
4308/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004309bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004310 assert(Value < getNumValues() && "Bad value!");
4311
4312 // If there is only one value, this is easy.
4313 if (getNumValues() == 1)
4314 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004315 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004316
Evan Cheng4ee62112006-02-05 06:29:23 +00004317 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004318
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004319 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004320
Roman Levensteindc1adac2008-04-07 10:06:32 +00004321 // TODO: Only iterate over uses of a given value of the node
4322 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4323 if (*UI == TheValue) {
4324 if (NUses == 0)
4325 return false;
4326 --NUses;
4327 }
Chris Lattner5c884562005-01-12 18:37:47 +00004328 }
4329
4330 // Found exactly the right number of uses?
4331 return NUses == 0;
4332}
4333
4334
Evan Cheng33d55952007-08-02 05:29:38 +00004335/// hasAnyUseOfValue - Return true if there are any use of the indicated
4336/// value. This method ignores uses of other values defined by this operation.
4337bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4338 assert(Value < getNumValues() && "Bad value!");
4339
Dan Gohman30359592008-01-29 13:02:09 +00004340 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004341
4342 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4343
4344 SmallPtrSet<SDNode*, 32> UsersHandled;
4345
Roman Levensteindc1adac2008-04-07 10:06:32 +00004346 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4347 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004348 if (User->getNumOperands() == 1 ||
4349 UsersHandled.insert(User)) // First time we've seen this?
4350 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4351 if (User->getOperand(i) == TheValue) {
4352 return true;
4353 }
4354 }
4355
4356 return false;
4357}
4358
4359
Evan Cheng917be682008-03-04 00:41:45 +00004360/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004361///
Evan Cheng917be682008-03-04 00:41:45 +00004362bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004363 bool Seen = false;
4364 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004365 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004366 if (User == this)
4367 Seen = true;
4368 else
4369 return false;
4370 }
4371
4372 return Seen;
4373}
4374
Evan Chenge6e97e62006-11-03 07:31:32 +00004375/// isOperand - Return true if this node is an operand of N.
4376///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004377bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004378 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4379 if (*this == N->getOperand(i))
4380 return true;
4381 return false;
4382}
4383
Evan Cheng917be682008-03-04 00:41:45 +00004384bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004385 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004386 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004387 return true;
4388 return false;
4389}
Evan Cheng4ee62112006-02-05 06:29:23 +00004390
Chris Lattner572dee72008-01-16 05:49:24 +00004391/// reachesChainWithoutSideEffects - Return true if this operand (which must
4392/// be a chain) reaches the specified operand without crossing any
4393/// side-effecting instructions. In practice, this looks through token
4394/// factors and non-volatile loads. In order to remain efficient, this only
4395/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004396bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004397 unsigned Depth) const {
4398 if (*this == Dest) return true;
4399
4400 // Don't search too deeply, we just want to be able to see through
4401 // TokenFactor's etc.
4402 if (Depth == 0) return false;
4403
4404 // If this is a token factor, all inputs to the TF happen in parallel. If any
4405 // of the operands of the TF reach dest, then we can do the xform.
4406 if (getOpcode() == ISD::TokenFactor) {
4407 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4408 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4409 return true;
4410 return false;
4411 }
4412
4413 // Loads don't have side effects, look through them.
4414 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4415 if (!Ld->isVolatile())
4416 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4417 }
4418 return false;
4419}
4420
4421
Evan Chengc5fc57d2006-11-03 03:05:24 +00004422static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004423 SmallPtrSet<SDNode *, 32> &Visited) {
4424 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004425 return;
4426
4427 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4428 SDNode *Op = N->getOperand(i).Val;
4429 if (Op == P) {
4430 found = true;
4431 return;
4432 }
4433 findPredecessor(Op, P, found, Visited);
4434 }
4435}
4436
Evan Cheng917be682008-03-04 00:41:45 +00004437/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004438/// is either an operand of N or it can be reached by recursively traversing
4439/// up the operands.
4440/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004441bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004442 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004443 bool found = false;
4444 findPredecessor(N, this, found, Visited);
4445 return found;
4446}
4447
Evan Chengc5484282006-10-04 00:56:09 +00004448uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4449 assert(Num < NumOperands && "Invalid child # of SDNode!");
4450 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4451}
4452
Reid Spencer577cc322007-04-01 07:32:19 +00004453std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004454 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004455 default:
4456 if (getOpcode() < ISD::BUILTIN_OP_END)
4457 return "<<Unknown DAG Node>>";
4458 else {
Evan Cheng72261582005-12-20 06:22:03 +00004459 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004460 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004461 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004462 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004463
Evan Cheng72261582005-12-20 06:22:03 +00004464 TargetLowering &TLI = G->getTargetLoweringInfo();
4465 const char *Name =
4466 TLI.getTargetNodeName(getOpcode());
4467 if (Name) return Name;
4468 }
4469
4470 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004471 }
4472
Evan Cheng27b7db52008-03-08 00:58:38 +00004473 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004474 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004475 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4476 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4477 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004478 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4479 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4480 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004481 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004482 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4483 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4484 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4485 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4486 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004487 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004488 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004489 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004490 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004491 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004492 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004493 case ISD::AssertSext: return "AssertSext";
4494 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004495
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004496 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004497 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004498 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004499 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004500
4501 case ISD::Constant: return "Constant";
4502 case ISD::ConstantFP: return "ConstantFP";
4503 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004504 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004505 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004506 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004507 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004508 case ISD::RETURNADDR: return "RETURNADDR";
4509 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004510 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004511 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4512 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004513 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004514 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004515 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004516 case ISD::INTRINSIC_WO_CHAIN: {
4517 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4518 return Intrinsic::getName((Intrinsic::ID)IID);
4519 }
4520 case ISD::INTRINSIC_VOID:
4521 case ISD::INTRINSIC_W_CHAIN: {
4522 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004523 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004524 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004525
Chris Lattnerb2827b02006-03-19 00:52:58 +00004526 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004527 case ISD::TargetConstant: return "TargetConstant";
4528 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004529 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004530 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004531 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004532 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004533 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004534 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004535
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004536 case ISD::CopyToReg: return "CopyToReg";
4537 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004538 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004539 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004540 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004541 case ISD::DBG_LABEL: return "dbg_label";
4542 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004543 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004544 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004545 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004546 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004547
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004548 // Unary operators
4549 case ISD::FABS: return "fabs";
4550 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004551 case ISD::FSQRT: return "fsqrt";
4552 case ISD::FSIN: return "fsin";
4553 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004554 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004555 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004556
4557 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004558 case ISD::ADD: return "add";
4559 case ISD::SUB: return "sub";
4560 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004561 case ISD::MULHU: return "mulhu";
4562 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004563 case ISD::SDIV: return "sdiv";
4564 case ISD::UDIV: return "udiv";
4565 case ISD::SREM: return "srem";
4566 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004567 case ISD::SMUL_LOHI: return "smul_lohi";
4568 case ISD::UMUL_LOHI: return "umul_lohi";
4569 case ISD::SDIVREM: return "sdivrem";
4570 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004571 case ISD::AND: return "and";
4572 case ISD::OR: return "or";
4573 case ISD::XOR: return "xor";
4574 case ISD::SHL: return "shl";
4575 case ISD::SRA: return "sra";
4576 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004577 case ISD::ROTL: return "rotl";
4578 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004579 case ISD::FADD: return "fadd";
4580 case ISD::FSUB: return "fsub";
4581 case ISD::FMUL: return "fmul";
4582 case ISD::FDIV: return "fdiv";
4583 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004584 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004585 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004586
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004587 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004588 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004589 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004590 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004591 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004592 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004593 case ISD::CONCAT_VECTORS: return "concat_vectors";
4594 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004595 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004596 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004597 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004598 case ISD::ADDC: return "addc";
4599 case ISD::ADDE: return "adde";
4600 case ISD::SUBC: return "subc";
4601 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004602 case ISD::SHL_PARTS: return "shl_parts";
4603 case ISD::SRA_PARTS: return "sra_parts";
4604 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004605
4606 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4607 case ISD::INSERT_SUBREG: return "insert_subreg";
4608
Chris Lattner7f644642005-04-28 21:44:03 +00004609 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004610 case ISD::SIGN_EXTEND: return "sign_extend";
4611 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004612 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004613 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004614 case ISD::TRUNCATE: return "truncate";
4615 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004616 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004617 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004618 case ISD::FP_EXTEND: return "fp_extend";
4619
4620 case ISD::SINT_TO_FP: return "sint_to_fp";
4621 case ISD::UINT_TO_FP: return "uint_to_fp";
4622 case ISD::FP_TO_SINT: return "fp_to_sint";
4623 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004624 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004625
4626 // Control flow instructions
4627 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004628 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004629 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004630 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004631 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004632 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004633 case ISD::CALLSEQ_START: return "callseq_start";
4634 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004635
4636 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004637 case ISD::LOAD: return "load";
4638 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004639 case ISD::VAARG: return "vaarg";
4640 case ISD::VACOPY: return "vacopy";
4641 case ISD::VAEND: return "vaend";
4642 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004643 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004644 case ISD::EXTRACT_ELEMENT: return "extract_element";
4645 case ISD::BUILD_PAIR: return "build_pair";
4646 case ISD::STACKSAVE: return "stacksave";
4647 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004648 case ISD::TRAP: return "trap";
4649
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004650 // Bit manipulation
4651 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004652 case ISD::CTPOP: return "ctpop";
4653 case ISD::CTTZ: return "cttz";
4654 case ISD::CTLZ: return "ctlz";
4655
Chris Lattner36ce6912005-11-29 06:21:05 +00004656 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004657 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004658 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004659
Duncan Sands36397f52007-07-27 12:58:54 +00004660 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004661 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004662
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004663 case ISD::CONDCODE:
4664 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004665 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004666 case ISD::SETOEQ: return "setoeq";
4667 case ISD::SETOGT: return "setogt";
4668 case ISD::SETOGE: return "setoge";
4669 case ISD::SETOLT: return "setolt";
4670 case ISD::SETOLE: return "setole";
4671 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004672
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004673 case ISD::SETO: return "seto";
4674 case ISD::SETUO: return "setuo";
4675 case ISD::SETUEQ: return "setue";
4676 case ISD::SETUGT: return "setugt";
4677 case ISD::SETUGE: return "setuge";
4678 case ISD::SETULT: return "setult";
4679 case ISD::SETULE: return "setule";
4680 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004681
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004682 case ISD::SETEQ: return "seteq";
4683 case ISD::SETGT: return "setgt";
4684 case ISD::SETGE: return "setge";
4685 case ISD::SETLT: return "setlt";
4686 case ISD::SETLE: return "setle";
4687 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004688 }
4689 }
4690}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004691
Evan Cheng144d8f02006-11-09 17:55:04 +00004692const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004693 switch (AM) {
4694 default:
4695 return "";
4696 case ISD::PRE_INC:
4697 return "<pre-inc>";
4698 case ISD::PRE_DEC:
4699 return "<pre-dec>";
4700 case ISD::POST_INC:
4701 return "<post-inc>";
4702 case ISD::POST_DEC:
4703 return "<post-dec>";
4704 }
4705}
4706
Duncan Sands276dcbd2008-03-21 09:14:45 +00004707std::string ISD::ArgFlagsTy::getArgFlagsString() {
4708 std::string S = "< ";
4709
4710 if (isZExt())
4711 S += "zext ";
4712 if (isSExt())
4713 S += "sext ";
4714 if (isInReg())
4715 S += "inreg ";
4716 if (isSRet())
4717 S += "sret ";
4718 if (isByVal())
4719 S += "byval ";
4720 if (isNest())
4721 S += "nest ";
4722 if (getByValAlign())
4723 S += "byval-align:" + utostr(getByValAlign()) + " ";
4724 if (getOrigAlign())
4725 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4726 if (getByValSize())
4727 S += "byval-size:" + utostr(getByValSize()) + " ";
4728 return S + ">";
4729}
4730
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004731void SDNode::dump() const { dump(0); }
4732void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004733 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004734
4735 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004736 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004737 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004738 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004739 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004740 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004741 }
Bill Wendling832171c2006-12-07 20:04:42 +00004742 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004743
Bill Wendling832171c2006-12-07 20:04:42 +00004744 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004745 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004746 if (i) cerr << ", ";
4747 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004748 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004749 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004750 }
4751
Evan Chengce254432007-12-11 02:08:35 +00004752 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4753 SDNode *Mask = getOperand(2).Val;
4754 cerr << "<";
4755 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4756 if (i) cerr << ",";
4757 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4758 cerr << "u";
4759 else
4760 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4761 }
4762 cerr << ">";
4763 }
4764
Chris Lattnerc3aae252005-01-07 07:46:32 +00004765 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004766 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004767 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004768 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4769 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4770 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4771 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4772 else {
4773 cerr << "<APFloat(";
4774 CSDN->getValueAPF().convertToAPInt().dump();
4775 cerr << ")>";
4776 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004777 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004778 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004779 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004780 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004781 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004782 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004783 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004784 else
Bill Wendling832171c2006-12-07 20:04:42 +00004785 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004786 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004787 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004788 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004789 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004790 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004791 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004792 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004793 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004794 else
Bill Wendling832171c2006-12-07 20:04:42 +00004795 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004796 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004797 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004798 else
Bill Wendling832171c2006-12-07 20:04:42 +00004799 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004800 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004801 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004802 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4803 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004804 cerr << LBB->getName() << " ";
4805 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004806 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004807 if (G && R->getReg() &&
4808 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004809 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004810 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004811 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004812 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004813 } else if (const ExternalSymbolSDNode *ES =
4814 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004815 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004816 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4817 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004818 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004819 else
Dan Gohman69de1932008-02-06 22:27:42 +00004820 cerr << "<null>";
4821 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4822 if (M->MO.getValue())
4823 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4824 else
4825 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004826 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4827 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004828 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004829 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004830 }
4831 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004832 const Value *SrcValue = LD->getSrcValue();
4833 int SrcOffset = LD->getSrcValueOffset();
4834 cerr << " <";
4835 if (SrcValue)
4836 cerr << SrcValue;
4837 else
4838 cerr << "null";
4839 cerr << ":" << SrcOffset << ">";
4840
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004841 bool doExt = true;
4842 switch (LD->getExtensionType()) {
4843 default: doExt = false; break;
4844 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004845 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004846 break;
4847 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004848 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004849 break;
4850 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004851 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004852 break;
4853 }
4854 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004855 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004856
Evan Cheng144d8f02006-11-09 17:55:04 +00004857 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004858 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004859 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004860 if (LD->isVolatile())
4861 cerr << " <volatile>";
4862 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004863 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004864 const Value *SrcValue = ST->getSrcValue();
4865 int SrcOffset = ST->getSrcValueOffset();
4866 cerr << " <";
4867 if (SrcValue)
4868 cerr << SrcValue;
4869 else
4870 cerr << "null";
4871 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004872
4873 if (ST->isTruncatingStore())
4874 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004875 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004876
4877 const char *AM = getIndexedModeName(ST->getAddressingMode());
4878 if (*AM)
4879 cerr << " " << AM;
4880 if (ST->isVolatile())
4881 cerr << " <volatile>";
4882 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004883 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4884 const Value *SrcValue = AT->getSrcValue();
4885 int SrcOffset = AT->getSrcValueOffset();
4886 cerr << " <";
4887 if (SrcValue)
4888 cerr << SrcValue;
4889 else
4890 cerr << "null";
4891 cerr << ":" << SrcOffset << ">";
4892 if (AT->isVolatile())
4893 cerr << " <volatile>";
4894 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004895 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004896}
4897
Chris Lattnerde202b32005-11-09 23:47:37 +00004898static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004899 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4900 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004901 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004902 else
Bill Wendling832171c2006-12-07 20:04:42 +00004903 cerr << "\n" << std::string(indent+2, ' ')
4904 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004905
Chris Lattnerea946cd2005-01-09 20:38:33 +00004906
Bill Wendling832171c2006-12-07 20:04:42 +00004907 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004908 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004909}
4910
Chris Lattnerc3aae252005-01-07 07:46:32 +00004911void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004912 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004913 std::vector<const SDNode*> Nodes;
4914 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4915 I != E; ++I)
4916 Nodes.push_back(I);
4917
Chris Lattner49d24712005-01-09 20:26:36 +00004918 std::sort(Nodes.begin(), Nodes.end());
4919
4920 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004921 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004922 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004923 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004924
Jim Laskey26f7fa72006-10-17 19:33:52 +00004925 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004926
Bill Wendling832171c2006-12-07 20:04:42 +00004927 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004928}
4929
Evan Chengd6594ae2006-09-12 21:00:35 +00004930const Type *ConstantPoolSDNode::getType() const {
4931 if (isMachineConstantPoolEntry())
4932 return Val.MachineCPVal->getType();
4933 return Val.ConstVal->getType();
4934}