blob: e459ae74ec831f3fb950d8cd73ad7505266e4d42 [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.
Owen Anderson4474c792008-07-01 22:34:11 +0000530 unsigned op_num = 0;
Evan Cheng130a6472006-10-12 20:34:05 +0000531 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000532 SDNode *Operand = I->getVal();
Owen Anderson4474c792008-07-01 22:34:11 +0000533 Operand->removeUser(op_num, N);
Evan Cheng130a6472006-10-12 20:34:05 +0000534
535 // Now that we removed this operand, see if there are no uses of it left.
536 if (Operand->use_empty())
537 DeadNodes.push_back(Operand);
Owen Anderson4474c792008-07-01 22:34:11 +0000538
539 op_num++;
Evan Cheng130a6472006-10-12 20:34:05 +0000540 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000541 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000542 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000543 }
Evan Cheng130a6472006-10-12 20:34:05 +0000544 N->OperandList = 0;
545 N->NumOperands = 0;
546
547 // Finally, remove N itself.
Evan Cheng130a6472006-10-12 20:34:05 +0000548 AllNodes.erase(N);
549 }
550}
551
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000552void SelectionDAG::DeleteNode(SDNode *N) {
553 assert(N->use_empty() && "Cannot delete a node that is not dead!");
554
555 // First take this out of the appropriate CSE map.
556 RemoveNodeFromCSEMaps(N);
557
Chris Lattner1e111c72005-09-07 05:37:01 +0000558 // Finally, remove uses due to operands of this node, remove from the
559 // AllNodes list, and delete the node.
560 DeleteNodeNotInCSEMaps(N);
561}
562
563void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
564
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000565 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000566 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000567
568 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000569 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000570 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000571 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000572 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000573 }
Chris Lattner65113b22005-11-08 22:07:03 +0000574 N->OperandList = 0;
575 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000576
577 delete N;
578}
579
Chris Lattner149c58c2005-08-16 18:17:10 +0000580/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
581/// correspond to it. This is useful when we're about to delete or repurpose
582/// the node. We don't want future request for structurally identical nodes
583/// to return N anymore.
584void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000585 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000586 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000587 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000588 case ISD::CONDCODE:
589 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
590 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000591 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000592 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
593 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000594 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000595 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000596 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000597 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000598 Erased =
599 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000600 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000601 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000602 MVT VT = cast<VTSDNode>(N)->getVT();
603 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000604 Erased = ExtendedValueTypeNodes.erase(VT);
605 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000606 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
607 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000608 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000609 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000610 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000611 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000612 // Remove it from the CSE Map.
613 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000614 break;
615 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000616#ifndef NDEBUG
617 // Verify that the node was actually in one of the CSE maps, unless it has a
618 // flag result (which cannot be CSE'd) or is one of the special cases that are
619 // not subject to CSE.
620 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000621 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000622 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000623 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000624 assert(0 && "Node is not in map!");
625 }
626#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000627}
628
Chris Lattner8b8749f2005-08-17 19:00:20 +0000629/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
630/// has been taken out and modified in some way. If the specified node already
631/// exists in the CSE maps, do not modify the maps, but return the existing node
632/// instead. If it doesn't exist, add it and return null.
633///
634SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
635 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000636 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000637 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000638
Chris Lattner9f8cc692005-12-19 22:21:21 +0000639 // Check that remaining values produced are not flags.
640 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
641 if (N->getValueType(i) == MVT::Flag)
642 return 0; // Never CSE anything that produces a flag.
643
Chris Lattnera5682852006-08-07 23:03:03 +0000644 SDNode *New = CSEMap.GetOrInsertNode(N);
645 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000646 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000647}
648
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000649/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
650/// were replaced with those specified. If this node is never memoized,
651/// return null, otherwise return a pointer to the slot it would take. If a
652/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000653SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
654 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000655 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000656 return 0; // Never add these nodes.
657
658 // Check that remaining values produced are not flags.
659 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
660 if (N->getValueType(i) == MVT::Flag)
661 return 0; // Never CSE anything that produces a flag.
662
Chris Lattner63e3f142007-02-04 07:28:00 +0000663 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000664 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000665 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000666 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000667}
668
669/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
670/// were replaced with those specified. If this node is never memoized,
671/// return null, otherwise return a pointer to the slot it would take. If a
672/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000673SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
674 SDOperand Op1, SDOperand Op2,
675 void *&InsertPos) {
676 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
677 return 0; // Never add these nodes.
678
679 // Check that remaining values produced are not flags.
680 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
681 if (N->getValueType(i) == MVT::Flag)
682 return 0; // Never CSE anything that produces a flag.
683
Chris Lattner63e3f142007-02-04 07:28:00 +0000684 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000685 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000686 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000687 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
688}
689
690
691/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
692/// were replaced with those specified. If this node is never memoized,
693/// return null, otherwise return a pointer to the slot it would take. If a
694/// node already exists with these operands, the slot will be non-null.
695SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000696 SDOperandPtr Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000697 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000698 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000699 return 0; // Never add these nodes.
700
701 // Check that remaining values produced are not flags.
702 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
703 if (N->getValueType(i) == MVT::Flag)
704 return 0; // Never CSE anything that produces a flag.
705
Jim Laskey583bd472006-10-27 23:46:08 +0000706 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000707 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000708
Evan Cheng9629aba2006-10-11 01:47:58 +0000709 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
710 ID.AddInteger(LD->getAddressingMode());
711 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000712 ID.AddInteger(LD->getMemoryVT().getRawBits());
Evan Cheng9629aba2006-10-11 01:47:58 +0000713 ID.AddInteger(LD->getAlignment());
714 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000715 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
716 ID.AddInteger(ST->getAddressingMode());
717 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000718 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000719 ID.AddInteger(ST->getAlignment());
720 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000721 }
Jim Laskey583bd472006-10-27 23:46:08 +0000722
Chris Lattnera5682852006-08-07 23:03:03 +0000723 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000724}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000725
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000726
Chris Lattner78ec3112003-08-11 14:57:33 +0000727SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000728 while (!AllNodes.empty()) {
729 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000730 N->SetNextInBucket(0);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000731 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000732 delete [] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000733 }
Chris Lattner65113b22005-11-08 22:07:03 +0000734 N->OperandList = 0;
735 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000736 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000737 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000738}
739
Duncan Sands83ec4b62008-06-06 12:08:01 +0000740SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000741 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000742 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000743 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000744 return getNode(ISD::AND, Op.getValueType(), Op,
745 getConstant(Imm, Op.getValueType()));
746}
747
Duncan Sands83ec4b62008-06-06 12:08:01 +0000748SDOperand SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000749 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000750 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000751}
752
Duncan Sands83ec4b62008-06-06 12:08:01 +0000753SDOperand SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
754 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000755
Evan Cheng0ff39b32008-06-30 07:31:25 +0000756 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000757 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000758 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000759
760 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000761 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000762 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000763 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000764 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000765 SDNode *N = NULL;
766 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000767 if (!VT.isVector())
Dan Gohman89081322007-12-12 22:21:26 +0000768 return SDOperand(N, 0);
769 if (!N) {
770 N = new ConstantSDNode(isT, Val, EltVT);
771 CSEMap.InsertNode(N, IP);
772 AllNodes.push_back(N);
773 }
774
775 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000776 if (VT.isVector()) {
Dan Gohman89081322007-12-12 22:21:26 +0000777 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000778 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000779 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
780 }
781 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000782}
783
Chris Lattner0bd48932008-01-17 07:00:52 +0000784SDOperand SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
785 return getConstant(Val, TLI.getPointerTy(), isTarget);
786}
787
788
Duncan Sands83ec4b62008-06-06 12:08:01 +0000789SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
790 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000791
Duncan Sands83ec4b62008-06-06 12:08:01 +0000792 MVT EltVT =
793 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000794
Chris Lattnerd8658612005-02-17 20:17:32 +0000795 // Do the map lookup using the actual bit pattern for the floating point
796 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
797 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000798 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000799 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000800 AddNodeIDNode(ID, Opc, getVTList(EltVT), (SDOperand*)0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000801 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000802 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000803 SDNode *N = NULL;
804 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000805 if (!VT.isVector())
Evan Chengc908dcd2007-06-29 21:36:04 +0000806 return SDOperand(N, 0);
807 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000808 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000809 CSEMap.InsertNode(N, IP);
810 AllNodes.push_back(N);
811 }
812
Dan Gohman7f321562007-06-25 16:23:39 +0000813 SDOperand Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000814 if (VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000815 SmallVector<SDOperand, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000816 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000817 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
818 }
819 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820}
821
Duncan Sands83ec4b62008-06-06 12:08:01 +0000822SDOperand SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
823 MVT EltVT =
824 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000825 if (EltVT==MVT::f32)
826 return getConstantFP(APFloat((float)Val), VT, isTarget);
827 else
828 return getConstantFP(APFloat(Val), VT, isTarget);
829}
830
Chris Lattnerc3aae252005-01-07 07:46:32 +0000831SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000832 MVT VT, int Offset,
Chris Lattner61b09412006-08-11 21:01:22 +0000833 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000834 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000835
836 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
837 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000838 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000839 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
840 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
841 }
842
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000843 if (GVar && GVar->isThreadLocal())
844 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
845 else
846 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000847
Jim Laskey583bd472006-10-27 23:46:08 +0000848 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000849 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000850 ID.AddPointer(GV);
851 ID.AddInteger(Offset);
852 void *IP = 0;
853 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
854 return SDOperand(E, 0);
855 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
856 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000857 AllNodes.push_back(N);
858 return SDOperand(N, 0);
859}
860
Duncan Sands83ec4b62008-06-06 12:08:01 +0000861SDOperand SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000862 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000863 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000864 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000865 ID.AddInteger(FI);
866 void *IP = 0;
867 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
868 return SDOperand(E, 0);
869 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
870 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000871 AllNodes.push_back(N);
872 return SDOperand(N, 0);
873}
874
Duncan Sands83ec4b62008-06-06 12:08:01 +0000875SDOperand SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000876 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000877 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000878 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000879 ID.AddInteger(JTI);
880 void *IP = 0;
881 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
882 return SDOperand(E, 0);
883 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
884 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000885 AllNodes.push_back(N);
886 return SDOperand(N, 0);
887}
888
Duncan Sands83ec4b62008-06-06 12:08:01 +0000889SDOperand SelectionDAG::getConstantPool(Constant *C, MVT VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000890 unsigned Alignment, int Offset,
891 bool isTarget) {
892 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000893 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000894 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000895 ID.AddInteger(Alignment);
896 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000897 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000898 void *IP = 0;
899 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
900 return SDOperand(E, 0);
901 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
902 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000903 AllNodes.push_back(N);
904 return SDOperand(N, 0);
905}
906
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907
Duncan Sands83ec4b62008-06-06 12:08:01 +0000908SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
Evan Chengd6594ae2006-09-12 21:00:35 +0000909 unsigned Alignment, int Offset,
910 bool isTarget) {
911 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000912 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000913 AddNodeIDNode(ID, Opc, getVTList(VT), (SDOperand*)0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000914 ID.AddInteger(Alignment);
915 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000916 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000917 void *IP = 0;
918 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
919 return SDOperand(E, 0);
920 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
921 CSEMap.InsertNode(N, IP);
922 AllNodes.push_back(N);
923 return SDOperand(N, 0);
924}
925
926
Chris Lattnerc3aae252005-01-07 07:46:32 +0000927SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000928 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000929 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000930 ID.AddPointer(MBB);
931 void *IP = 0;
932 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
933 return SDOperand(E, 0);
934 SDNode *N = new BasicBlockSDNode(MBB);
935 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936 AllNodes.push_back(N);
937 return SDOperand(N, 0);
938}
939
Duncan Sands276dcbd2008-03-21 09:14:45 +0000940SDOperand SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
941 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000942 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), (SDOperand*)0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +0000943 ID.AddInteger(Flags.getRawBits());
944 void *IP = 0;
945 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
946 return SDOperand(E, 0);
947 SDNode *N = new ARG_FLAGSSDNode(Flags);
948 CSEMap.InsertNode(N, IP);
949 AllNodes.push_back(N);
950 return SDOperand(N, 0);
951}
952
Duncan Sands83ec4b62008-06-06 12:08:01 +0000953SDOperand SelectionDAG::getValueType(MVT VT) {
954 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
955 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +0000956
Duncan Sands83ec4b62008-06-06 12:08:01 +0000957 SDNode *&N = VT.isExtended() ?
958 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +0000959
960 if (N) return SDOperand(N, 0);
961 N = new VTSDNode(VT);
962 AllNodes.push_back(N);
963 return SDOperand(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +0000964}
965
Duncan Sands83ec4b62008-06-06 12:08:01 +0000966SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967 SDNode *&N = ExternalSymbols[Sym];
968 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000969 N = new ExternalSymbolSDNode(false, Sym, VT);
970 AllNodes.push_back(N);
971 return SDOperand(N, 0);
972}
973
Duncan Sands83ec4b62008-06-06 12:08:01 +0000974SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000975 SDNode *&N = TargetExternalSymbols[Sym];
976 if (N) return SDOperand(N, 0);
977 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000978 AllNodes.push_back(N);
979 return SDOperand(N, 0);
980}
981
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000982SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
983 if ((unsigned)Cond >= CondCodeNodes.size())
984 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000985
Chris Lattner079a27a2005-08-09 20:40:02 +0000986 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000987 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000988 AllNodes.push_back(CondCodeNodes[Cond]);
989 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000990 return SDOperand(CondCodeNodes[Cond], 0);
991}
992
Duncan Sands83ec4b62008-06-06 12:08:01 +0000993SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000994 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +0000995 AddNodeIDNode(ID, ISD::Register, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000996 ID.AddInteger(RegNo);
997 void *IP = 0;
998 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
999 return SDOperand(E, 0);
1000 SDNode *N = new RegisterSDNode(RegNo, VT);
1001 CSEMap.InsertNode(N, IP);
1002 AllNodes.push_back(N);
1003 return SDOperand(N, 0);
1004}
1005
Dan Gohman7f460202008-06-30 20:59:49 +00001006SDOperand SelectionDAG::getDbgStopPoint(SDOperand Root,
1007 unsigned Line, unsigned Col,
1008 const CompileUnitDesc *CU) {
1009 FoldingSetNodeID ID;
1010 SDOperand Ops[] = { Root };
1011 AddNodeIDNode(ID, ISD::DBG_STOPPOINT, getVTList(MVT::Other), &Ops[0], 1);
1012 ID.AddInteger(Line);
1013 ID.AddInteger(Col);
1014 ID.AddPointer(CU);
1015 void *IP = 0;
1016 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1017 return SDOperand(E, 0);
1018 SDNode *N = new DbgStopPointSDNode(Root, Line, Col, CU);
1019 CSEMap.InsertNode(N, IP);
1020 AllNodes.push_back(N);
1021 return SDOperand(N, 0);
1022}
1023
Dan Gohman44066042008-07-01 00:05:16 +00001024SDOperand SelectionDAG::getLabel(unsigned Opcode,
1025 SDOperand Root,
1026 unsigned LabelID) {
1027 FoldingSetNodeID ID;
1028 SDOperand Ops[] = { Root };
1029 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1030 ID.AddInteger(LabelID);
1031 void *IP = 0;
1032 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1033 return SDOperand(E, 0);
1034 SDNode *N = new LabelSDNode(Opcode, Root, LabelID);
1035 CSEMap.InsertNode(N, IP);
1036 AllNodes.push_back(N);
1037 return SDOperand(N, 0);
1038}
1039
Dan Gohman69de1932008-02-06 22:27:42 +00001040SDOperand SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001041 assert((!V || isa<PointerType>(V->getType())) &&
1042 "SrcValue is not a pointer?");
1043
Jim Laskey583bd472006-10-27 23:46:08 +00001044 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001045 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), (SDOperand*)0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001046 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001047
Chris Lattner61b09412006-08-11 21:01:22 +00001048 void *IP = 0;
1049 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1050 return SDOperand(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001051
1052 SDNode *N = new SrcValueSDNode(V);
1053 CSEMap.InsertNode(N, IP);
1054 AllNodes.push_back(N);
1055 return SDOperand(N, 0);
1056}
1057
Dan Gohman36b5c132008-04-07 19:35:22 +00001058SDOperand SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001059 const Value *v = MO.getValue();
1060 assert((!v || isa<PointerType>(v->getType())) &&
1061 "SrcValue is not a pointer?");
1062
1063 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001064 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), (SDOperand*)0, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001065 ID.AddPointer(v);
1066 ID.AddInteger(MO.getFlags());
1067 ID.AddInteger(MO.getOffset());
1068 ID.AddInteger(MO.getSize());
1069 ID.AddInteger(MO.getAlignment());
1070
1071 void *IP = 0;
1072 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1073 return SDOperand(E, 0);
1074
1075 SDNode *N = new MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001076 CSEMap.InsertNode(N, IP);
1077 AllNodes.push_back(N);
1078 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001079}
1080
Chris Lattner37ce9df2007-10-15 17:47:20 +00001081/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1082/// specified value type.
Mon P Wangbee98c62008-07-02 17:07:12 +00001083SDOperand SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001084 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001085 unsigned ByteSize = VT.getSizeInBits()/8;
1086 const Type *Ty = VT.getTypeForMVT();
Mon P Wangbee98c62008-07-02 17:07:12 +00001087 unsigned StackAlign =
1088 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1089
Chris Lattner37ce9df2007-10-15 17:47:20 +00001090 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1091 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1092}
1093
Duncan Sands83ec4b62008-06-06 12:08:01 +00001094SDOperand SelectionDAG::FoldSetCC(MVT VT, SDOperand N1,
Chris Lattner51dabfb2006-10-14 00:41:01 +00001095 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096 // These setcc operations always fold.
1097 switch (Cond) {
1098 default: break;
1099 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001100 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001101 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001102 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001103
1104 case ISD::SETOEQ:
1105 case ISD::SETOGT:
1106 case ISD::SETOGE:
1107 case ISD::SETOLT:
1108 case ISD::SETOLE:
1109 case ISD::SETONE:
1110 case ISD::SETO:
1111 case ISD::SETUO:
1112 case ISD::SETUEQ:
1113 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001114 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001115 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001116 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001117
Chris Lattner67255a12005-04-07 18:14:58 +00001118 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001119 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001120 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001121 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001122
Chris Lattnerc3aae252005-01-07 07:46:32 +00001123 switch (Cond) {
1124 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001125 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1126 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001127 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1128 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1129 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1130 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1131 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1132 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1133 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1134 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001135 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001136 }
Chris Lattner67255a12005-04-07 18:14:58 +00001137 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001138 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001139 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001140 // No compile time operations on this type yet.
1141 if (N1C->getValueType(0) == MVT::ppcf128)
1142 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001143
1144 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001146 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001147 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1148 return getNode(ISD::UNDEF, VT);
1149 // fall through
1150 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1151 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1152 return getNode(ISD::UNDEF, VT);
1153 // fall through
1154 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001155 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001156 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1157 return getNode(ISD::UNDEF, VT);
1158 // fall through
1159 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1160 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1161 return getNode(ISD::UNDEF, VT);
1162 // fall through
1163 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1164 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1165 return getNode(ISD::UNDEF, VT);
1166 // fall through
1167 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001168 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001169 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1170 return getNode(ISD::UNDEF, VT);
1171 // fall through
1172 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001173 R==APFloat::cmpEqual, VT);
1174 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1175 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1176 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1177 R==APFloat::cmpEqual, VT);
1178 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1179 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1180 R==APFloat::cmpLessThan, VT);
1181 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1182 R==APFloat::cmpUnordered, VT);
1183 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1184 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 }
1186 } else {
1187 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001188 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001189 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001190 }
1191
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001192 // Could not fold it.
1193 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001194}
1195
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001196/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1197/// use this predicate to simplify operations downstream.
1198bool SelectionDAG::SignBitIsZero(SDOperand Op, unsigned Depth) const {
1199 unsigned BitWidth = Op.getValueSizeInBits();
1200 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1201}
1202
Dan Gohmanea859be2007-06-22 14:59:07 +00001203/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1204/// this predicate to simplify operations downstream. Mask is known to be zero
1205/// for bits that V cannot have.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001206bool SelectionDAG::MaskedValueIsZero(SDOperand Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001207 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001208 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001209 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1210 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1211 return (KnownZero & Mask) == Mask;
1212}
1213
1214/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1215/// known to be either zero or one and return them in the KnownZero/KnownOne
1216/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1217/// processing.
Dan Gohman977a76f2008-02-13 22:28:48 +00001218void SelectionDAG::ComputeMaskedBits(SDOperand Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001219 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001220 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001221 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001222 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001223 "Mask size mismatches value type size!");
1224
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001225 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001226 if (Depth == 6 || Mask == 0)
1227 return; // Limit search depth.
1228
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001229 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001230
1231 switch (Op.getOpcode()) {
1232 case ISD::Constant:
1233 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001234 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001235 KnownZero = ~KnownOne & Mask;
1236 return;
1237 case ISD::AND:
1238 // If either the LHS or the RHS are Zero, the result is zero.
1239 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001240 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1241 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001242 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1243 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1244
1245 // Output known-1 bits are only known if set in both the LHS & RHS.
1246 KnownOne &= KnownOne2;
1247 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1248 KnownZero |= KnownZero2;
1249 return;
1250 case ISD::OR:
1251 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001252 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1253 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001254 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1255 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1256
1257 // Output known-0 bits are only known if clear in both the LHS & RHS.
1258 KnownZero &= KnownZero2;
1259 // Output known-1 are known to be set if set in either the LHS | RHS.
1260 KnownOne |= KnownOne2;
1261 return;
1262 case ISD::XOR: {
1263 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1264 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1265 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1266 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1267
1268 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001269 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001270 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1271 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1272 KnownZero = KnownZeroOut;
1273 return;
1274 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001275 case ISD::MUL: {
1276 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1277 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1278 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1279 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1280 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1281
1282 // If low bits are zero in either operand, output low known-0 bits.
1283 // Also compute a conserative estimate for high known-0 bits.
1284 // More trickiness is possible, but this is sufficient for the
1285 // interesting case of alignment computation.
1286 KnownOne.clear();
1287 unsigned TrailZ = KnownZero.countTrailingOnes() +
1288 KnownZero2.countTrailingOnes();
1289 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001290 KnownZero2.countLeadingOnes(),
1291 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001292
1293 TrailZ = std::min(TrailZ, BitWidth);
1294 LeadZ = std::min(LeadZ, BitWidth);
1295 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1296 APInt::getHighBitsSet(BitWidth, LeadZ);
1297 KnownZero &= Mask;
1298 return;
1299 }
1300 case ISD::UDIV: {
1301 // For the purposes of computing leading zeros we can conservatively
1302 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001303 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001304 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1305 ComputeMaskedBits(Op.getOperand(0),
1306 AllOnes, KnownZero2, KnownOne2, Depth+1);
1307 unsigned LeadZ = KnownZero2.countLeadingOnes();
1308
1309 KnownOne2.clear();
1310 KnownZero2.clear();
1311 ComputeMaskedBits(Op.getOperand(1),
1312 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001313 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1314 if (RHSUnknownLeadingOnes != BitWidth)
1315 LeadZ = std::min(BitWidth,
1316 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001317
1318 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1319 return;
1320 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001321 case ISD::SELECT:
1322 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1323 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1324 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1325 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1326
1327 // Only known if known in both the LHS and RHS.
1328 KnownOne &= KnownOne2;
1329 KnownZero &= KnownZero2;
1330 return;
1331 case ISD::SELECT_CC:
1332 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1333 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1334 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1335 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1336
1337 // Only known if known in both the LHS and RHS.
1338 KnownOne &= KnownOne2;
1339 KnownZero &= KnownZero2;
1340 return;
1341 case ISD::SETCC:
1342 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001343 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1344 BitWidth > 1)
1345 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001346 return;
1347 case ISD::SHL:
1348 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1349 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001350 unsigned ShAmt = SA->getValue();
1351
1352 // If the shift count is an invalid immediate, don't do anything.
1353 if (ShAmt >= BitWidth)
1354 return;
1355
1356 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001357 KnownZero, KnownOne, Depth+1);
1358 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001359 KnownZero <<= ShAmt;
1360 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001361 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001362 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001363 }
1364 return;
1365 case ISD::SRL:
1366 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1367 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001368 unsigned ShAmt = SA->getValue();
1369
Dan Gohmand4cf9922008-02-26 18:50:50 +00001370 // If the shift count is an invalid immediate, don't do anything.
1371 if (ShAmt >= BitWidth)
1372 return;
1373
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001374 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001375 KnownZero, KnownOne, Depth+1);
1376 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001377 KnownZero = KnownZero.lshr(ShAmt);
1378 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001379
Dan Gohman72d2fd52008-02-13 22:43:25 +00001380 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001381 KnownZero |= HighBits; // High bits known zero.
1382 }
1383 return;
1384 case ISD::SRA:
1385 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001386 unsigned ShAmt = SA->getValue();
1387
Dan Gohmand4cf9922008-02-26 18:50:50 +00001388 // If the shift count is an invalid immediate, don't do anything.
1389 if (ShAmt >= BitWidth)
1390 return;
1391
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001392 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001393 // If any of the demanded bits are produced by the sign extension, we also
1394 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001395 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1396 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001397 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001398
1399 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1400 Depth+1);
1401 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001402 KnownZero = KnownZero.lshr(ShAmt);
1403 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001404
1405 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001406 APInt SignBit = APInt::getSignBit(BitWidth);
1407 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001408
Dan Gohmanca93a432008-02-20 16:30:17 +00001409 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001410 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001411 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001412 KnownOne |= HighBits; // New bits are known one.
1413 }
1414 }
1415 return;
1416 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001417 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1418 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001419
1420 // Sign extension. Compute the demanded bits in the result that are not
1421 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001422 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001423
Dan Gohman977a76f2008-02-13 22:28:48 +00001424 APInt InSignBit = APInt::getSignBit(EBits);
1425 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001426
1427 // If the sign extended bits are demanded, we know that the sign
1428 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001429 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001430 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001431 InputDemandedBits |= InSignBit;
1432
1433 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1434 KnownZero, KnownOne, Depth+1);
1435 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1436
1437 // If the sign bit of the input is known set or clear, then we know the
1438 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001439 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001440 KnownZero |= NewBits;
1441 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001442 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001443 KnownOne |= NewBits;
1444 KnownZero &= ~NewBits;
1445 } else { // Input sign bit unknown
1446 KnownZero &= ~NewBits;
1447 KnownOne &= ~NewBits;
1448 }
1449 return;
1450 }
1451 case ISD::CTTZ:
1452 case ISD::CTLZ:
1453 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001454 unsigned LowBits = Log2_32(BitWidth)+1;
1455 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001456 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001457 return;
1458 }
1459 case ISD::LOAD: {
1460 if (ISD::isZEXTLoad(Op.Val)) {
1461 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001462 MVT VT = LD->getMemoryVT();
1463 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001464 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001465 }
1466 return;
1467 }
1468 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001469 MVT InVT = Op.getOperand(0).getValueType();
1470 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001471 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1472 APInt InMask = Mask;
1473 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001474 KnownZero.trunc(InBits);
1475 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001476 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001477 KnownZero.zext(BitWidth);
1478 KnownOne.zext(BitWidth);
1479 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001480 return;
1481 }
1482 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001483 MVT InVT = Op.getOperand(0).getValueType();
1484 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001485 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001486 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1487 APInt InMask = Mask;
1488 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001489
1490 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001491 // bit is demanded. Temporarily set this bit in the mask for our callee.
1492 if (NewBits.getBoolValue())
1493 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001494
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001495 KnownZero.trunc(InBits);
1496 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001497 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1498
1499 // Note if the sign bit is known to be zero or one.
1500 bool SignBitKnownZero = KnownZero.isNegative();
1501 bool SignBitKnownOne = KnownOne.isNegative();
1502 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1503 "Sign bit can't be known to be both zero and one!");
1504
1505 // If the sign bit wasn't actually demanded by our caller, we don't
1506 // want it set in the KnownZero and KnownOne result values. Reset the
1507 // mask and reapply it to the result values.
1508 InMask = Mask;
1509 InMask.trunc(InBits);
1510 KnownZero &= InMask;
1511 KnownOne &= InMask;
1512
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001513 KnownZero.zext(BitWidth);
1514 KnownOne.zext(BitWidth);
1515
Dan Gohman977a76f2008-02-13 22:28:48 +00001516 // If the sign bit is known zero or one, the top bits match.
1517 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001518 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001519 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001520 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001521 return;
1522 }
1523 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001524 MVT InVT = Op.getOperand(0).getValueType();
1525 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001526 APInt InMask = Mask;
1527 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001528 KnownZero.trunc(InBits);
1529 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001530 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001531 KnownZero.zext(BitWidth);
1532 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001533 return;
1534 }
1535 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001536 MVT InVT = Op.getOperand(0).getValueType();
1537 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001538 APInt InMask = Mask;
1539 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001540 KnownZero.zext(InBits);
1541 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001542 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001543 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001544 KnownZero.trunc(BitWidth);
1545 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001546 break;
1547 }
1548 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001549 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1550 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001551 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1552 KnownOne, Depth+1);
1553 KnownZero |= (~InMask) & Mask;
1554 return;
1555 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001556 case ISD::FGETSIGN:
1557 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001558 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001559 return;
1560
Dan Gohman23e8b712008-04-28 17:02:21 +00001561 case ISD::SUB: {
1562 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1563 // We know that the top bits of C-X are clear if X contains less bits
1564 // than C (i.e. no wrap-around can happen). For example, 20-X is
1565 // positive if we can prove that X is >= 0 and < 16.
1566 if (CLHS->getAPIntValue().isNonNegative()) {
1567 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1568 // NLZ can't be BitWidth with no sign bit
1569 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1570 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1571 Depth+1);
1572
1573 // If all of the MaskV bits are known to be zero, then we know the
1574 // output top bits are zero, because we now know that the output is
1575 // from [0-C].
1576 if ((KnownZero2 & MaskV) == MaskV) {
1577 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1578 // Top bits known zero.
1579 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1580 }
1581 }
1582 }
1583 }
1584 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001585 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001586 // Output known-0 bits are known if clear or set in both the low clear bits
1587 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1588 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001589 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1590 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1591 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1592 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1593
1594 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1595 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1596 KnownZeroOut = std::min(KnownZeroOut,
1597 KnownZero2.countTrailingOnes());
1598
1599 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001600 return;
1601 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001602 case ISD::SREM:
1603 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1604 APInt RA = Rem->getAPIntValue();
1605 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001606 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001607 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1608 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001609
Dan Gohman23e8b712008-04-28 17:02:21 +00001610 // The sign of a remainder is equal to the sign of the first
1611 // operand (zero being positive).
1612 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1613 KnownZero2 |= ~LowBits;
1614 else if (KnownOne2[BitWidth-1])
1615 KnownOne2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001616
Dan Gohman23e8b712008-04-28 17:02:21 +00001617 KnownZero |= KnownZero2 & Mask;
1618 KnownOne |= KnownOne2 & Mask;
1619
1620 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001621 }
1622 }
1623 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001624 case ISD::UREM: {
1625 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1626 APInt RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001627 if (RA.isPowerOf2()) {
1628 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001629 APInt Mask2 = LowBits & Mask;
1630 KnownZero |= ~LowBits & Mask;
1631 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1632 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1633 break;
1634 }
1635 }
1636
1637 // Since the result is less than or equal to either operand, any leading
1638 // zero bits in either operand must also exist in the result.
1639 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1640 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1641 Depth+1);
1642 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1643 Depth+1);
1644
1645 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1646 KnownZero2.countLeadingOnes());
1647 KnownOne.clear();
1648 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1649 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001650 }
1651 default:
1652 // Allow the target to implement this method for its nodes.
1653 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1654 case ISD::INTRINSIC_WO_CHAIN:
1655 case ISD::INTRINSIC_W_CHAIN:
1656 case ISD::INTRINSIC_VOID:
1657 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1658 }
1659 return;
1660 }
1661}
1662
1663/// ComputeNumSignBits - Return the number of times the sign bit of the
1664/// register is replicated into the other bits. We know that at least 1 bit
1665/// is always equal to the sign bit (itself), but other cases can give us
1666/// information. For example, immediately after an "SRA X, 2", we know that
1667/// the top 3 bits are all equal to each other, so we return 3.
1668unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001669 MVT VT = Op.getValueType();
1670 assert(VT.isInteger() && "Invalid VT!");
1671 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001672 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001673 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001674
1675 if (Depth == 6)
1676 return 1; // Limit search depth.
1677
1678 switch (Op.getOpcode()) {
1679 default: break;
1680 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001681 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001682 return VTBits-Tmp+1;
1683 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001684 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001685 return VTBits-Tmp;
1686
1687 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001688 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1689 // If negative, return # leading ones.
1690 if (Val.isNegative())
1691 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001692
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001693 // Return # leading zeros.
1694 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001695 }
1696
1697 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001698 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001699 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1700
1701 case ISD::SIGN_EXTEND_INREG:
1702 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001703 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001704 Tmp = VTBits-Tmp+1;
1705
1706 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1707 return std::max(Tmp, Tmp2);
1708
1709 case ISD::SRA:
1710 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1711 // SRA X, C -> adds C sign bits.
1712 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1713 Tmp += C->getValue();
1714 if (Tmp > VTBits) Tmp = VTBits;
1715 }
1716 return Tmp;
1717 case ISD::SHL:
1718 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1719 // shl destroys sign bits.
1720 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1721 if (C->getValue() >= VTBits || // Bad shift.
1722 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1723 return Tmp - C->getValue();
1724 }
1725 break;
1726 case ISD::AND:
1727 case ISD::OR:
1728 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001729 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001730 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001731 if (Tmp != 1) {
1732 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1733 FirstAnswer = std::min(Tmp, Tmp2);
1734 // We computed what we know about the sign bits as our first
1735 // answer. Now proceed to the generic code that uses
1736 // ComputeMaskedBits, and pick whichever answer is better.
1737 }
1738 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001739
1740 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001741 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001742 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001743 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001744 return std::min(Tmp, Tmp2);
1745
1746 case ISD::SETCC:
1747 // If setcc returns 0/-1, all bits are sign bits.
1748 if (TLI.getSetCCResultContents() ==
1749 TargetLowering::ZeroOrNegativeOneSetCCResult)
1750 return VTBits;
1751 break;
1752 case ISD::ROTL:
1753 case ISD::ROTR:
1754 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1755 unsigned RotAmt = C->getValue() & (VTBits-1);
1756
1757 // Handle rotate right by N like a rotate left by 32-N.
1758 if (Op.getOpcode() == ISD::ROTR)
1759 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1760
1761 // If we aren't rotating out all of the known-in sign bits, return the
1762 // number that are left. This handles rotl(sext(x), 1) for example.
1763 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1764 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1765 }
1766 break;
1767 case ISD::ADD:
1768 // Add can have at most one carry bit. Thus we know that the output
1769 // is, at worst, one more bit than the inputs.
1770 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1771 if (Tmp == 1) return 1; // Early out.
1772
1773 // Special case decrementing a value (ADD X, -1):
1774 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1775 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001776 APInt KnownZero, KnownOne;
1777 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001778 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1779
1780 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1781 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001782 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001783 return VTBits;
1784
1785 // If we are subtracting one from a positive number, there is no carry
1786 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001787 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001788 return Tmp;
1789 }
1790
1791 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1792 if (Tmp2 == 1) return 1;
1793 return std::min(Tmp, Tmp2)-1;
1794 break;
1795
1796 case ISD::SUB:
1797 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1798 if (Tmp2 == 1) return 1;
1799
1800 // Handle NEG.
1801 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001802 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001803 APInt KnownZero, KnownOne;
1804 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001805 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1806 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1807 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001808 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001809 return VTBits;
1810
1811 // If the input is known to be positive (the sign bit is known clear),
1812 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001813 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001814 return Tmp2;
1815
1816 // Otherwise, we treat this like a SUB.
1817 }
1818
1819 // Sub can have at most one carry bit. Thus we know that the output
1820 // is, at worst, one more bit than the inputs.
1821 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1822 if (Tmp == 1) return 1; // Early out.
1823 return std::min(Tmp, Tmp2)-1;
1824 break;
1825 case ISD::TRUNCATE:
1826 // FIXME: it's tricky to do anything useful for this, but it is an important
1827 // case for targets like X86.
1828 break;
1829 }
1830
1831 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1832 if (Op.getOpcode() == ISD::LOAD) {
1833 LoadSDNode *LD = cast<LoadSDNode>(Op);
1834 unsigned ExtType = LD->getExtensionType();
1835 switch (ExtType) {
1836 default: break;
1837 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001838 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001839 return VTBits-Tmp+1;
1840 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001841 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001842 return VTBits-Tmp;
1843 }
1844 }
1845
1846 // Allow the target to implement this method for its nodes.
1847 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1848 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1849 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1850 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1851 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001852 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001853 }
1854
1855 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1856 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001857 APInt KnownZero, KnownOne;
1858 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001859 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1860
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001861 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001862 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001863 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001864 Mask = KnownOne;
1865 } else {
1866 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001867 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001868 }
1869
1870 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1871 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001872 Mask = ~Mask;
1873 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001874 // Return # leading zeros. We use 'min' here in case Val was zero before
1875 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001876 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001877}
1878
Chris Lattner51dabfb2006-10-14 00:41:01 +00001879
Evan Chenga844bde2008-02-02 04:07:54 +00001880bool SelectionDAG::isVerifiedDebugInfoDesc(SDOperand Op) const {
1881 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1882 if (!GA) return false;
1883 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1884 if (!GV) return false;
1885 MachineModuleInfo *MMI = getMachineModuleInfo();
1886 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1887}
1888
1889
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001890/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1891/// element of the result of the vector shuffle.
Evan Chengab262272008-06-25 20:52:59 +00001892SDOperand SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001893 MVT VT = N->getValueType(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001894 SDOperand PermMask = N->getOperand(2);
Evan Chengab262272008-06-25 20:52:59 +00001895 SDOperand Idx = PermMask.getOperand(i);
1896 if (Idx.getOpcode() == ISD::UNDEF)
1897 return getNode(ISD::UNDEF, VT.getVectorElementType());
1898 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001899 unsigned NumElems = PermMask.getNumOperands();
Evan Chengab262272008-06-25 20:52:59 +00001900 SDOperand V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
1901 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001902
1903 if (V.getOpcode() == ISD::BIT_CONVERT) {
1904 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001905 if (V.getValueType().getVectorNumElements() != NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00001906 return SDOperand();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001907 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001908 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001909 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001910 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001911 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001912 return V.getOperand(Index);
1913 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1914 return getShuffleScalarElt(V.Val, Index);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001915 return SDOperand();
1916}
1917
1918
Chris Lattnerc3aae252005-01-07 07:46:32 +00001919/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001920///
Duncan Sands83ec4b62008-06-06 12:08:01 +00001921SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001922 FoldingSetNodeID ID;
Roman Levenstein9cac5252008-04-16 16:15:27 +00001923 AddNodeIDNode(ID, Opcode, getVTList(VT), (SDOperand*)0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001924 void *IP = 0;
1925 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1926 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001927 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001928 CSEMap.InsertNode(N, IP);
1929
1930 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001931 return SDOperand(N, 0);
1932}
1933
Duncan Sands83ec4b62008-06-06 12:08:01 +00001934SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT, SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00001935 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001936 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001937 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001938 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001939 switch (Opcode) {
1940 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00001941 case ISD::SIGN_EXTEND:
1942 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001943 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00001944 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00001945 case ISD::TRUNCATE:
1946 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001947 case ISD::UINT_TO_FP:
1948 case ISD::SINT_TO_FP: {
1949 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00001950 // No compile time operations on this type.
1951 if (VT==MVT::ppcf128)
1952 break;
Dan Gohman6c231502008-02-29 01:47:35 +00001953 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
1954 (void)apf.convertFromAPInt(Val,
1955 Opcode==ISD::SINT_TO_FP,
1956 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001957 return getConstantFP(apf, VT);
1958 }
Chris Lattner94683772005-12-23 05:30:37 +00001959 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001960 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00001961 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001962 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00001963 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001964 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001965 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00001966 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001967 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00001968 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001969 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001970 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001971 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00001972 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001973 }
1974 }
1975
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001976 // Constant fold unary operations with a floating point constant operand.
1977 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1978 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00001979 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00001980 switch (Opcode) {
1981 case ISD::FNEG:
1982 V.changeSign();
1983 return getConstantFP(V, VT);
1984 case ISD::FABS:
1985 V.clearSign();
1986 return getConstantFP(V, VT);
1987 case ISD::FP_ROUND:
1988 case ISD::FP_EXTEND:
1989 // This can return overflow, underflow, or inexact; we don't care.
1990 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00001991 (void)V.convert(*MVTToAPFloatSemantics(VT),
1992 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00001993 return getConstantFP(V, VT);
1994 case ISD::FP_TO_SINT:
1995 case ISD::FP_TO_UINT: {
1996 integerPart x;
1997 assert(integerPartWidth >= 64);
1998 // FIXME need to be more flexible about rounding mode.
1999 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2000 Opcode==ISD::FP_TO_SINT,
2001 APFloat::rmTowardZero);
2002 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2003 break;
2004 return getConstant(x, VT);
2005 }
2006 case ISD::BIT_CONVERT:
2007 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2008 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2009 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2010 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002011 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002012 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002013 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002014 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002015
2016 unsigned OpOpcode = Operand.Val->getOpcode();
2017 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002018 case ISD::TokenFactor:
Duncan Sandsf9516202008-06-30 10:19:09 +00002019 return Operand; // Factor of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002020 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002021 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002022 assert(VT.isFloatingPoint() &&
2023 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002024 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002025 if (Operand.getOpcode() == ISD::UNDEF)
2026 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002027 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002028 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002029 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002030 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002031 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002032 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002033 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002034 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2035 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2036 break;
2037 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002038 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002039 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002040 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002041 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002042 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002043 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002044 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002045 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002046 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002047 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002048 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002049 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002050 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002051 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002052 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2053 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2054 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2055 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002056 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002057 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002058 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002059 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002060 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002061 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002062 if (OpOpcode == ISD::TRUNCATE)
2063 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002064 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2065 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002066 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002067 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002068 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002069 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002070 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2071 else
2072 return Operand.Val->getOperand(0);
2073 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002074 break;
Chris Lattner35481892005-12-23 00:16:34 +00002075 case ISD::BIT_CONVERT:
2076 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002077 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002078 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002079 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002080 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2081 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002082 if (OpOpcode == ISD::UNDEF)
2083 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002084 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002085 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002086 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2087 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002088 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002089 if (OpOpcode == ISD::UNDEF)
2090 return getNode(ISD::UNDEF, VT);
2091 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2092 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2093 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2094 Operand.getConstantOperandVal(1) == 0 &&
2095 Operand.getOperand(0).getValueType() == VT)
2096 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002097 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002098 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002099 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2100 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002101 Operand.Val->getOperand(0));
2102 if (OpOpcode == ISD::FNEG) // --X -> X
2103 return Operand.Val->getOperand(0);
2104 break;
2105 case ISD::FABS:
2106 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2107 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2108 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002109 }
2110
Chris Lattner43247a12005-08-25 19:12:10 +00002111 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002112 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002113 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002114 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00002115 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002116 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002117 void *IP = 0;
2118 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2119 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002120 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002121 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002122 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002123 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002124 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002125 AllNodes.push_back(N);
2126 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002127}
2128
Chris Lattner36019aa2005-04-18 03:48:41 +00002129
2130
Duncan Sands83ec4b62008-06-06 12:08:01 +00002131SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002132 SDOperand N1, SDOperand N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2134 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002135 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002136 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002137 case ISD::TokenFactor:
2138 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2139 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002140 // Fold trivial token factors.
2141 if (N1.getOpcode() == ISD::EntryToken) return N2;
2142 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002143 break;
Chris Lattner76365122005-01-16 02:23:22 +00002144 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002145 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002146 N1.getValueType() == VT && "Binary operator types must match!");
2147 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2148 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002149 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002150 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002151 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2152 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002153 break;
Chris Lattner76365122005-01-16 02:23:22 +00002154 case ISD::OR:
2155 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002156 case ISD::ADD:
2157 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002158 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002159 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002160 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2161 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002162 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002163 return N1;
2164 break;
Chris Lattner76365122005-01-16 02:23:22 +00002165 case ISD::UDIV:
2166 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002167 case ISD::MULHU:
2168 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002169 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002170 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002171 case ISD::MUL:
2172 case ISD::SDIV:
2173 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002174 case ISD::FADD:
2175 case ISD::FSUB:
2176 case ISD::FMUL:
2177 case ISD::FDIV:
2178 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002179 assert(N1.getValueType() == N2.getValueType() &&
2180 N1.getValueType() == VT && "Binary operator types must match!");
2181 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002182 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2183 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002184 N1.getValueType().isFloatingPoint() &&
2185 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002186 "Invalid FCOPYSIGN!");
2187 break;
Chris Lattner76365122005-01-16 02:23:22 +00002188 case ISD::SHL:
2189 case ISD::SRA:
2190 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002191 case ISD::ROTL:
2192 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002193 assert(VT == N1.getValueType() &&
2194 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002195 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002196 "Shifts only work on integers");
2197
2198 // Always fold shifts of i1 values so the code generator doesn't need to
2199 // handle them. Since we know the size of the shift has to be less than the
2200 // size of the value, the shift/rotate count is guaranteed to be zero.
2201 if (VT == MVT::i1)
2202 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002203 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002204 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002205 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002206 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002207 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002208 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002209 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002210 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002211 break;
2212 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002213 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002214 assert(VT.isFloatingPoint() &&
2215 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002216 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002217 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002218 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002219 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002220 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002221 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002222 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002223 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002224 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002225 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002226 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002227 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002228 break;
2229 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002230 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002231 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002232 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002233 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002234 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002235 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002236 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002237
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002238 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002239 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002240 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002241 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002242 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002243 return getConstant(Val, VT);
2244 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002245 break;
2246 }
2247 case ISD::EXTRACT_VECTOR_ELT:
2248 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2249
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002250 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2251 if (N1.getOpcode() == ISD::UNDEF)
2252 return getNode(ISD::UNDEF, VT);
2253
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002254 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2255 // expanding copies of large vectors from registers.
2256 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2257 N1.getNumOperands() > 0) {
2258 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002259 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002260 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2261 N1.getOperand(N2C->getValue() / Factor),
2262 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2263 }
2264
2265 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2266 // expanding large vector constants.
2267 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2268 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002269
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002270 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2271 // operations are lowered to scalars.
2272 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2273 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2274 if (IEC == N2C)
2275 return N1.getOperand(1);
2276 else
2277 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2278 }
2279 break;
2280 case ISD::EXTRACT_ELEMENT:
2281 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002282 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2283 (N1.getValueType().isInteger() == VT.isInteger()) &&
2284 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002285
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002286 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2287 // 64-bit integers into 32-bit parts. Instead of building the extract of
2288 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2289 if (N1.getOpcode() == ISD::BUILD_PAIR)
2290 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002291
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002292 // EXTRACT_ELEMENT of a constant int is also very common.
2293 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002294 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002295 unsigned Shift = ElementSize * N2C->getValue();
2296 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2297 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002298 }
2299 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002300 case ISD::EXTRACT_SUBVECTOR:
2301 if (N1.getValueType() == VT) // Trivial extraction.
2302 return N1;
2303 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002304 }
2305
2306 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002307 if (N2C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002308 APInt C1 = N1C->getAPIntValue(), C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002309 switch (Opcode) {
2310 case ISD::ADD: return getConstant(C1 + C2, VT);
2311 case ISD::SUB: return getConstant(C1 - C2, VT);
2312 case ISD::MUL: return getConstant(C1 * C2, VT);
2313 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002314 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002315 break;
2316 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002317 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002318 break;
2319 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002320 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002321 break;
2322 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002323 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002324 break;
2325 case ISD::AND : return getConstant(C1 & C2, VT);
2326 case ISD::OR : return getConstant(C1 | C2, VT);
2327 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002328 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002329 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2330 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2331 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2332 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002333 default: break;
2334 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002335 } else { // Cannonicalize constant to RHS if commutative
2336 if (isCommutativeBinOp(Opcode)) {
2337 std::swap(N1C, N2C);
2338 std::swap(N1, N2);
2339 }
2340 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002341 }
2342
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002343 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002344 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2345 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002346 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002347 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2348 // Cannonicalize constant to RHS if commutative
2349 std::swap(N1CFP, N2CFP);
2350 std::swap(N1, N2);
2351 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002352 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2353 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002354 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002355 case ISD::FADD:
2356 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002357 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002358 return getConstantFP(V1, VT);
2359 break;
2360 case ISD::FSUB:
2361 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2362 if (s!=APFloat::opInvalidOp)
2363 return getConstantFP(V1, VT);
2364 break;
2365 case ISD::FMUL:
2366 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2367 if (s!=APFloat::opInvalidOp)
2368 return getConstantFP(V1, VT);
2369 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002370 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002371 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2372 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2373 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002374 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002375 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002376 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2377 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2378 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002379 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002380 case ISD::FCOPYSIGN:
2381 V1.copySign(V2);
2382 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002383 default: break;
2384 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002385 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002386 }
Chris Lattner62b57722006-04-20 05:39:12 +00002387
2388 // Canonicalize an UNDEF to the RHS, even over a constant.
2389 if (N1.getOpcode() == ISD::UNDEF) {
2390 if (isCommutativeBinOp(Opcode)) {
2391 std::swap(N1, N2);
2392 } else {
2393 switch (Opcode) {
2394 case ISD::FP_ROUND_INREG:
2395 case ISD::SIGN_EXTEND_INREG:
2396 case ISD::SUB:
2397 case ISD::FSUB:
2398 case ISD::FDIV:
2399 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002400 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002401 return N1; // fold op(undef, arg2) -> undef
2402 case ISD::UDIV:
2403 case ISD::SDIV:
2404 case ISD::UREM:
2405 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002406 case ISD::SRL:
2407 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002408 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002409 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2410 // For vectors, we can't easily build an all zero vector, just return
2411 // the LHS.
2412 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002413 }
2414 }
2415 }
2416
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002417 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002418 if (N2.getOpcode() == ISD::UNDEF) {
2419 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002420 case ISD::XOR:
2421 if (N1.getOpcode() == ISD::UNDEF)
2422 // Handle undef ^ undef -> 0 special case. This is a common
2423 // idiom (misuse).
2424 return getConstant(0, VT);
2425 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002426 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002427 case ISD::ADDC:
2428 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002429 case ISD::SUB:
2430 case ISD::FADD:
2431 case ISD::FSUB:
2432 case ISD::FMUL:
2433 case ISD::FDIV:
2434 case ISD::FREM:
2435 case ISD::UDIV:
2436 case ISD::SDIV:
2437 case ISD::UREM:
2438 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002439 return N2; // fold op(arg1, undef) -> undef
2440 case ISD::MUL:
2441 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002442 case ISD::SRL:
2443 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002444 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002445 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2446 // For vectors, we can't easily build an all zero vector, just return
2447 // the LHS.
2448 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002449 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002450 if (!VT.isVector())
2451 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002452 // For vectors, we can't easily build an all one vector, just return
2453 // the LHS.
2454 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002455 case ISD::SRA:
2456 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002457 }
2458 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002459
Chris Lattner27e9b412005-05-11 18:57:39 +00002460 // Memoize this node if possible.
2461 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002462 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002463 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002464 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002465 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002466 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002467 void *IP = 0;
2468 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2469 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002470 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002471 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002472 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002473 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002474 }
2475
Chris Lattnerc3aae252005-01-07 07:46:32 +00002476 AllNodes.push_back(N);
2477 return SDOperand(N, 0);
2478}
2479
Duncan Sands83ec4b62008-06-06 12:08:01 +00002480SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattnerc3aae252005-01-07 07:46:32 +00002481 SDOperand N1, SDOperand N2, SDOperand N3) {
2482 // Perform various simplifications.
2483 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2484 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002485 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002486 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002487 // Use FoldSetCC to simplify SETCC's.
2488 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002489 if (Simp.Val) return Simp;
2490 break;
2491 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002492 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002493 if (N1C) {
2494 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002495 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002496 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002497 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002498 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002499
2500 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002501 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002502 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002503 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002504 if (N2C->getValue()) // Unconditional branch
2505 return getNode(ISD::BR, MVT::Other, N1, N3);
2506 else
2507 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002508 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002509 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002510 case ISD::VECTOR_SHUFFLE:
2511 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002512 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002513 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002514 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002515 "Illegal VECTOR_SHUFFLE node!");
2516 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002517 case ISD::BIT_CONVERT:
2518 // Fold bit_convert nodes from a type to themselves.
2519 if (N1.getValueType() == VT)
2520 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002521 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002522 }
2523
Chris Lattner43247a12005-08-25 19:12:10 +00002524 // Memoize node if it doesn't produce a flag.
2525 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002526 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002527 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002528 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002529 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002530 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002531 void *IP = 0;
2532 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2533 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002534 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002535 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002536 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002537 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002538 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002539 AllNodes.push_back(N);
2540 return SDOperand(N, 0);
2541}
2542
Duncan Sands83ec4b62008-06-06 12:08:01 +00002543SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002544 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002545 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002546 SDOperand Ops[] = { N1, N2, N3, N4 };
2547 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002548}
2549
Duncan Sands83ec4b62008-06-06 12:08:01 +00002550SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002551 SDOperand N1, SDOperand N2, SDOperand N3,
2552 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002553 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2554 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002555}
2556
Dan Gohman707e0182008-04-12 04:36:06 +00002557/// getMemsetValue - Vectorized representation of the memset value
2558/// operand.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002559static SDOperand getMemsetValue(SDOperand Value, MVT VT, SelectionDAG &DAG) {
2560 unsigned NumBits = VT.isVector() ?
2561 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002562 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002563 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002564 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002565 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002566 Val = (Val << Shift) | Val;
2567 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002568 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002569 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002570 return DAG.getConstant(Val, VT);
2571 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002572 }
Evan Chengf0df0312008-05-15 08:39:06 +00002573
2574 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2575 unsigned Shift = 8;
2576 for (unsigned i = NumBits; i > 8; i >>= 1) {
2577 Value = DAG.getNode(ISD::OR, VT,
2578 DAG.getNode(ISD::SHL, VT, Value,
2579 DAG.getConstant(Shift, MVT::i8)), Value);
2580 Shift <<= 1;
2581 }
2582
2583 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002584}
2585
Dan Gohman707e0182008-04-12 04:36:06 +00002586/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2587/// used when a memcpy is turned into a memset when the source is a constant
2588/// string ptr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002589static SDOperand getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002590 const TargetLowering &TLI,
2591 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002592 // Handle vector with all elements zero.
2593 if (Str.empty()) {
2594 if (VT.isInteger())
2595 return DAG.getConstant(0, VT);
2596 unsigned NumElts = VT.getVectorNumElements();
2597 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2598 return DAG.getNode(ISD::BIT_CONVERT, VT,
2599 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2600 }
2601
Duncan Sands83ec4b62008-06-06 12:08:01 +00002602 assert(!VT.isVector() && "Can't handle vector type here!");
2603 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002604 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002605 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002606 if (TLI.isLittleEndian())
2607 Offset = Offset + MSB - 1;
2608 for (unsigned i = 0; i != MSB; ++i) {
2609 Val = (Val << 8) | (unsigned char)Str[Offset];
2610 Offset += TLI.isLittleEndian() ? -1 : 1;
2611 }
2612 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002613}
2614
Dan Gohman707e0182008-04-12 04:36:06 +00002615/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002616///
Dan Gohman707e0182008-04-12 04:36:06 +00002617static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2618 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002619 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002620 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2621}
2622
Evan Chengf0df0312008-05-15 08:39:06 +00002623/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2624///
Evan Cheng0ff39b32008-06-30 07:31:25 +00002625static bool isMemSrcFromString(SDOperand Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002626 unsigned SrcDelta = 0;
2627 GlobalAddressSDNode *G = NULL;
2628 if (Src.getOpcode() == ISD::GlobalAddress)
2629 G = cast<GlobalAddressSDNode>(Src);
2630 else if (Src.getOpcode() == ISD::ADD &&
2631 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2632 Src.getOperand(1).getOpcode() == ISD::Constant) {
2633 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2634 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2635 }
2636 if (!G)
2637 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002638
Evan Chengf0df0312008-05-15 08:39:06 +00002639 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002640 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2641 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002642
Evan Chengf0df0312008-05-15 08:39:06 +00002643 return false;
2644}
Dan Gohman707e0182008-04-12 04:36:06 +00002645
Evan Chengf0df0312008-05-15 08:39:06 +00002646/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2647/// to replace the memset / memcpy is below the threshold. It also returns the
2648/// types of the sequence of memory ops to perform memset / memcpy.
2649static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002650bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Evan Chengf0df0312008-05-15 08:39:06 +00002651 SDOperand Dst, SDOperand Src,
2652 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002653 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002654 SelectionDAG &DAG,
2655 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002656 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002657 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002658 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002660 if (VT != MVT::iAny) {
2661 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002662 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002663 // If source is a string constant, this will require an unaligned load.
2664 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2665 if (Dst.getOpcode() != ISD::FrameIndex) {
2666 // Can't change destination alignment. It requires a unaligned store.
2667 if (AllowUnalign)
2668 VT = MVT::iAny;
2669 } else {
2670 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2671 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2672 if (MFI->isFixedObjectIndex(FI)) {
2673 // Can't change destination alignment. It requires a unaligned store.
2674 if (AllowUnalign)
2675 VT = MVT::iAny;
2676 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002677 // Give the stack frame object a larger alignment if needed.
2678 if (MFI->getObjectAlignment(FI) < NewAlign)
2679 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002680 Align = NewAlign;
2681 }
2682 }
2683 }
2684 }
2685
2686 if (VT == MVT::iAny) {
2687 if (AllowUnalign) {
2688 VT = MVT::i64;
2689 } else {
2690 switch (Align & 7) {
2691 case 0: VT = MVT::i64; break;
2692 case 4: VT = MVT::i32; break;
2693 case 2: VT = MVT::i16; break;
2694 default: VT = MVT::i8; break;
2695 }
2696 }
2697
Duncan Sands83ec4b62008-06-06 12:08:01 +00002698 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002699 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002700 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2701 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002702
Duncan Sands8e4eb092008-06-08 20:54:56 +00002703 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002704 VT = LVT;
2705 }
Dan Gohman707e0182008-04-12 04:36:06 +00002706
2707 unsigned NumMemOps = 0;
2708 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002709 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002710 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002711 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002712 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002713 VT = MVT::i64;
2714 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002715 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2716 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002717 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002718 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002719 VTSize >>= 1;
2720 }
Dan Gohman707e0182008-04-12 04:36:06 +00002721 }
Dan Gohman707e0182008-04-12 04:36:06 +00002722
2723 if (++NumMemOps > Limit)
2724 return false;
2725 MemOps.push_back(VT);
2726 Size -= VTSize;
2727 }
2728
2729 return true;
2730}
2731
2732static SDOperand getMemcpyLoadsAndStores(SelectionDAG &DAG,
2733 SDOperand Chain, SDOperand Dst,
2734 SDOperand Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002735 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002736 const Value *DstSV, uint64_t DstSVOff,
2737 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002738 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2739
Dan Gohman21323f32008-05-29 19:42:22 +00002740 // Expand memcpy to a series of load and store ops if the size operand falls
2741 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002742 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002743 uint64_t Limit = -1;
2744 if (!AlwaysInline)
2745 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002746 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002747 std::string Str;
2748 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002749 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002750 Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002751 return SDOperand();
2752
Dan Gohman707e0182008-04-12 04:36:06 +00002753
Evan Cheng0ff39b32008-06-30 07:31:25 +00002754 bool isZeroStr = CopyFromStr && Str.empty();
Evan Chengf0df0312008-05-15 08:39:06 +00002755 SmallVector<SDOperand, 8> OutChains;
2756 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002757 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002758 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002759 MVT VT = MemOps[i];
2760 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002761 SDOperand Value, Store;
2762
Evan Cheng0ff39b32008-06-30 07:31:25 +00002763 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002764 // It's unlikely a store of a vector immediate can be done in a single
2765 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002766 // We also handle store a vector with all zero's.
2767 // FIXME: Handle other cases where store of vector immediate is done in
2768 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002769 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002770 Store = DAG.getStore(Chain, Value,
2771 getMemBasePlusOffset(Dst, DstOff, DAG),
2772 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002773 } else {
2774 Value = DAG.getLoad(VT, Chain,
2775 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002776 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002777 Store = DAG.getStore(Chain, Value,
2778 getMemBasePlusOffset(Dst, DstOff, DAG),
2779 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002780 }
2781 OutChains.push_back(Store);
2782 SrcOff += VTSize;
2783 DstOff += VTSize;
2784 }
2785
2786 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2787 &OutChains[0], OutChains.size());
2788}
2789
Dan Gohman21323f32008-05-29 19:42:22 +00002790static SDOperand getMemmoveLoadsAndStores(SelectionDAG &DAG,
2791 SDOperand Chain, SDOperand Dst,
2792 SDOperand Src, uint64_t Size,
2793 unsigned Align, bool AlwaysInline,
2794 const Value *DstSV, uint64_t DstSVOff,
2795 const Value *SrcSV, uint64_t SrcSVOff){
2796 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2797
2798 // Expand memmove to a series of load and store ops if the size operand falls
2799 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002800 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002801 uint64_t Limit = -1;
2802 if (!AlwaysInline)
2803 Limit = TLI.getMaxStoresPerMemmove();
2804 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002805 std::string Str;
2806 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002807 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002808 Str, CopyFromStr, DAG, TLI))
Dan Gohman21323f32008-05-29 19:42:22 +00002809 return SDOperand();
2810
Dan Gohman21323f32008-05-29 19:42:22 +00002811 uint64_t SrcOff = 0, DstOff = 0;
2812
2813 SmallVector<SDOperand, 8> LoadValues;
2814 SmallVector<SDOperand, 8> LoadChains;
2815 SmallVector<SDOperand, 8> OutChains;
2816 unsigned NumMemOps = MemOps.size();
2817 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002818 MVT VT = MemOps[i];
2819 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002820 SDOperand Value, Store;
2821
2822 Value = DAG.getLoad(VT, Chain,
2823 getMemBasePlusOffset(Src, SrcOff, DAG),
2824 SrcSV, SrcSVOff + SrcOff, false, Align);
2825 LoadValues.push_back(Value);
2826 LoadChains.push_back(Value.getValue(1));
2827 SrcOff += VTSize;
2828 }
2829 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2830 &LoadChains[0], LoadChains.size());
2831 OutChains.clear();
2832 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002833 MVT VT = MemOps[i];
2834 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman21323f32008-05-29 19:42:22 +00002835 SDOperand Value, Store;
2836
2837 Store = DAG.getStore(Chain, LoadValues[i],
2838 getMemBasePlusOffset(Dst, DstOff, DAG),
2839 DstSV, DstSVOff + DstOff, false, DstAlign);
2840 OutChains.push_back(Store);
2841 DstOff += VTSize;
2842 }
2843
2844 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2845 &OutChains[0], OutChains.size());
2846}
2847
Dan Gohman707e0182008-04-12 04:36:06 +00002848static SDOperand getMemsetStores(SelectionDAG &DAG,
2849 SDOperand Chain, SDOperand Dst,
2850 SDOperand Src, uint64_t Size,
2851 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002852 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002853 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2854
2855 // Expand memset to a series of load/store ops if the size operand
2856 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002857 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002858 std::string Str;
2859 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002860 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002861 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman707e0182008-04-12 04:36:06 +00002862 return SDOperand();
2863
2864 SmallVector<SDOperand, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002865 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002866
2867 unsigned NumMemOps = MemOps.size();
2868 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002869 MVT VT = MemOps[i];
2870 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002871 SDOperand Value = getMemsetValue(Src, VT, DAG);
2872 SDOperand Store = DAG.getStore(Chain, Value,
2873 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002874 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002875 OutChains.push_back(Store);
2876 DstOff += VTSize;
2877 }
2878
2879 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2880 &OutChains[0], OutChains.size());
2881}
2882
2883SDOperand SelectionDAG::getMemcpy(SDOperand Chain, SDOperand Dst,
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002884 SDOperand Src, SDOperand Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002885 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002886 const Value *DstSV, uint64_t DstSVOff,
2887 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002888
2889 // Check to see if we should lower the memcpy to loads and stores first.
2890 // For cases within the target-specified limits, this is the best choice.
2891 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2892 if (ConstantSize) {
2893 // Memcpy with size zero? Just return the original chain.
2894 if (ConstantSize->isNullValue())
2895 return Chain;
2896
2897 SDOperand Result =
2898 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00002899 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002900 if (Result.Val)
2901 return Result;
2902 }
2903
2904 // Then check to see if we should lower the memcpy with target-specific
2905 // code. If the target chooses to do this, this is the next best.
2906 SDOperand Result =
2907 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
2908 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002909 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002910 if (Result.Val)
2911 return Result;
2912
2913 // If we really need inline code and the target declined to provide it,
2914 // use a (potentially long) sequence of loads and stores.
2915 if (AlwaysInline) {
2916 assert(ConstantSize && "AlwaysInline requires a constant size!");
2917 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
2918 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00002919 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002920 }
2921
2922 // Emit a library call.
2923 TargetLowering::ArgListTy Args;
2924 TargetLowering::ArgListEntry Entry;
2925 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2926 Entry.Node = Dst; Args.push_back(Entry);
2927 Entry.Node = Src; Args.push_back(Entry);
2928 Entry.Node = Size; Args.push_back(Entry);
2929 std::pair<SDOperand,SDOperand> CallResult =
2930 TLI.LowerCallTo(Chain, Type::VoidTy,
2931 false, false, false, CallingConv::C, false,
2932 getExternalSymbol("memcpy", TLI.getPointerTy()),
2933 Args, *this);
2934 return CallResult.second;
2935}
2936
2937SDOperand SelectionDAG::getMemmove(SDOperand Chain, SDOperand Dst,
2938 SDOperand Src, SDOperand Size,
2939 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002940 const Value *DstSV, uint64_t DstSVOff,
2941 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002942
Dan Gohman21323f32008-05-29 19:42:22 +00002943 // Check to see if we should lower the memmove to loads and stores first.
2944 // For cases within the target-specified limits, this is the best choice.
2945 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2946 if (ConstantSize) {
2947 // Memmove with size zero? Just return the original chain.
2948 if (ConstantSize->isNullValue())
2949 return Chain;
2950
2951 SDOperand Result =
2952 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
2953 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
2954 if (Result.Val)
2955 return Result;
2956 }
Dan Gohman707e0182008-04-12 04:36:06 +00002957
2958 // Then check to see if we should lower the memmove with target-specific
2959 // code. If the target chooses to do this, this is the next best.
2960 SDOperand Result =
2961 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002962 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002963 if (Result.Val)
2964 return Result;
2965
2966 // Emit a library call.
2967 TargetLowering::ArgListTy Args;
2968 TargetLowering::ArgListEntry Entry;
2969 Entry.Ty = TLI.getTargetData()->getIntPtrType();
2970 Entry.Node = Dst; Args.push_back(Entry);
2971 Entry.Node = Src; Args.push_back(Entry);
2972 Entry.Node = Size; Args.push_back(Entry);
2973 std::pair<SDOperand,SDOperand> CallResult =
2974 TLI.LowerCallTo(Chain, Type::VoidTy,
2975 false, false, false, CallingConv::C, false,
2976 getExternalSymbol("memmove", TLI.getPointerTy()),
2977 Args, *this);
2978 return CallResult.second;
2979}
2980
2981SDOperand SelectionDAG::getMemset(SDOperand Chain, SDOperand Dst,
2982 SDOperand Src, SDOperand Size,
2983 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002984 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002985
2986 // Check to see if we should lower the memset to stores first.
2987 // For cases within the target-specified limits, this is the best choice.
2988 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
2989 if (ConstantSize) {
2990 // Memset with size zero? Just return the original chain.
2991 if (ConstantSize->isNullValue())
2992 return Chain;
2993
2994 SDOperand Result =
2995 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002996 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002997 if (Result.Val)
2998 return Result;
2999 }
3000
3001 // Then check to see if we should lower the memset with target-specific
3002 // code. If the target chooses to do this, this is the next best.
3003 SDOperand Result =
3004 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003005 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003006 if (Result.Val)
3007 return Result;
3008
3009 // Emit a library call.
3010 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3011 TargetLowering::ArgListTy Args;
3012 TargetLowering::ArgListEntry Entry;
3013 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3014 Args.push_back(Entry);
3015 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003016 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003017 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3018 else
3019 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3020 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3021 Args.push_back(Entry);
3022 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3023 Args.push_back(Entry);
3024 std::pair<SDOperand,SDOperand> CallResult =
3025 TLI.LowerCallTo(Chain, Type::VoidTy,
3026 false, false, false, CallingConv::C, false,
3027 getExternalSymbol("memset", TLI.getPointerTy()),
3028 Args, *this);
3029 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003030}
3031
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003032SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003033 SDOperand Ptr, SDOperand Cmp,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003034 SDOperand Swp, const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003035 unsigned Alignment) {
3036 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003037 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3038 SDVTList VTs = getVTList(Cmp.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003039 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003040 SDOperand Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003041 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003042 void* IP = 0;
3043 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3044 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003045 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp,
Mon P Wang28873102008-06-25 08:15:39 +00003046 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003047 CSEMap.InsertNode(N, IP);
3048 AllNodes.push_back(N);
3049 return SDOperand(N, 0);
3050}
3051
3052SDOperand SelectionDAG::getAtomic(unsigned Opcode, SDOperand Chain,
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003053 SDOperand Ptr, SDOperand Val,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003054 const Value* PtrVal,
Mon P Wang28873102008-06-25 08:15:39 +00003055 unsigned Alignment) {
3056 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003057 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3058 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003059 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003060 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3061 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003062 && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003063 SDVTList VTs = getVTList(Val.getValueType(), MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003064 FoldingSetNodeID ID;
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003065 SDOperand Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003066 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003067 void* IP = 0;
3068 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3069 return SDOperand(E, 0);
Dan Gohmanfd4418f2008-06-25 16:07:49 +00003070 SDNode* N = new AtomicSDNode(Opcode, VTs, Chain, Ptr, Val,
Mon P Wang28873102008-06-25 08:15:39 +00003071 PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003072 CSEMap.InsertNode(N, IP);
3073 AllNodes.push_back(N);
3074 return SDOperand(N, 0);
3075}
3076
Duncan Sands4bdcb612008-07-02 17:40:58 +00003077/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3078/// Allowed to return something different (and simpler) if Simplify is true.
3079SDOperand SelectionDAG::getMergeValues(SDOperandPtr Ops, unsigned NumOps,
3080 bool Simplify) {
3081 if (Simplify && NumOps == 1)
3082 return Ops[0];
3083
3084 SmallVector<MVT, 4> VTs;
3085 VTs.reserve(NumOps);
3086 for (unsigned i = 0; i < NumOps; ++i)
3087 VTs.push_back(Ops[i].getValueType());
3088 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3089}
3090
Duncan Sands14ea39c2008-03-27 20:23:40 +00003091SDOperand
Duncan Sandse10efce2008-03-28 09:45:24 +00003092SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003093 MVT VT, SDOperand Chain,
Duncan Sandse10efce2008-03-28 09:45:24 +00003094 SDOperand Ptr, SDOperand Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003096 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00003097 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3098 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003099 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003100 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003101 } else if (SV) {
3102 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3103 assert(PT && "Value for load must be a pointer");
3104 Ty = PT->getElementType();
Duncan Sands14ea39c2008-03-27 20:23:40 +00003105 }
Dan Gohman575e2f42007-06-04 15:49:41 +00003106 assert(Ty && "Could not get type information for load");
3107 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3108 }
Evan Cheng466685d2006-10-09 20:57:25 +00003109
Duncan Sands14ea39c2008-03-27 20:23:40 +00003110 if (VT == EVT) {
3111 ExtType = ISD::NON_EXTLOAD;
3112 } else if (ExtType == ISD::NON_EXTLOAD) {
3113 assert(VT == EVT && "Non-extending load from different memory type!");
3114 } else {
3115 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003116 if (VT.isVector())
3117 assert(EVT == VT.getVectorElementType() && "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003118 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003119 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003120 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003121 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003122 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003123 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003124 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003125 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003126
3127 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003128 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003129 "Unindexed load with an offset!");
3130
3131 SDVTList VTs = Indexed ?
3132 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3133 SDOperand Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003134 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003135 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003136 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003137 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003138 ID.AddInteger(EVT.getRawBits());
Evan Cheng466685d2006-10-09 20:57:25 +00003139 ID.AddInteger(Alignment);
3140 ID.AddInteger(isVolatile);
3141 void *IP = 0;
3142 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3143 return SDOperand(E, 0);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003144 SDNode *N = new LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3145 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003146 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003147 AllNodes.push_back(N);
3148 return SDOperand(N, 0);
3149}
3150
Duncan Sands83ec4b62008-06-06 12:08:01 +00003151SDOperand SelectionDAG::getLoad(MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003152 SDOperand Chain, SDOperand Ptr,
3153 const Value *SV, int SVOffset,
3154 bool isVolatile, unsigned Alignment) {
3155 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003156 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3157 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003158}
3159
Duncan Sands83ec4b62008-06-06 12:08:01 +00003160SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003161 SDOperand Chain, SDOperand Ptr,
3162 const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003163 int SVOffset, MVT EVT,
Duncan Sands14ea39c2008-03-27 20:23:40 +00003164 bool isVolatile, unsigned Alignment) {
3165 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003166 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3167 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003168}
3169
Evan Cheng144d8f02006-11-09 17:55:04 +00003170SDOperand
3171SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
3172 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003173 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003174 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3175 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003176 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3177 LD->getChain(), Base, Offset, LD->getSrcValue(),
3178 LD->getSrcValueOffset(), LD->getMemoryVT(),
3179 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003180}
3181
Jeff Cohend41b30d2006-11-05 19:31:28 +00003182SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003183 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003184 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003185 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003186
Dan Gohman575e2f42007-06-04 15:49:41 +00003187 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3188 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003189 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003190 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003191 } else if (SV) {
3192 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3193 assert(PT && "Value for store must be a pointer");
3194 Ty = PT->getElementType();
3195 }
3196 assert(Ty && "Could not get type information for store");
3197 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3198 }
Evan Chengad071e12006-10-05 22:57:11 +00003199 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003200 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003201 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003202 FoldingSetNodeID ID;
3203 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003204 ID.AddInteger(ISD::UNINDEXED);
3205 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003206 ID.AddInteger(VT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003207 ID.AddInteger(Alignment);
3208 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003209 void *IP = 0;
3210 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3211 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003212 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003213 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003214 CSEMap.InsertNode(N, IP);
3215 AllNodes.push_back(N);
3216 return SDOperand(N, 0);
3217}
3218
Jeff Cohend41b30d2006-11-05 19:31:28 +00003219SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003220 SDOperand Ptr, const Value *SV,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003221 int SVOffset, MVT SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003222 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003223 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003224
3225 if (VT == SVT)
3226 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003227
Duncan Sands8e4eb092008-06-08 20:54:56 +00003228 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003229 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003230 "Can't do FP-INT conversion!");
3231
Dan Gohman575e2f42007-06-04 15:49:41 +00003232 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
3233 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00003234 if (VT != MVT::iPTR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003235 Ty = VT.getTypeForMVT();
Dan Gohman575e2f42007-06-04 15:49:41 +00003236 } else if (SV) {
3237 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
3238 assert(PT && "Value for store must be a pointer");
3239 Ty = PT->getElementType();
3240 }
3241 assert(Ty && "Could not get type information for store");
3242 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
3243 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00003244 SDVTList VTs = getVTList(MVT::Other);
3245 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00003246 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003247 FoldingSetNodeID ID;
3248 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003249 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003250 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003251 ID.AddInteger(SVT.getRawBits());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003252 ID.AddInteger(Alignment);
3253 ID.AddInteger(isVolatile);
3254 void *IP = 0;
3255 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3256 return SDOperand(E, 0);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003257 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
Evan Cheng8b2794a2006-10-13 21:14:26 +00003258 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003259 CSEMap.InsertNode(N, IP);
3260 AllNodes.push_back(N);
3261 return SDOperand(N, 0);
3262}
3263
Evan Cheng144d8f02006-11-09 17:55:04 +00003264SDOperand
3265SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
3266 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003267 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3268 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3269 "Store is already a indexed store!");
3270 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3271 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3272 FoldingSetNodeID ID;
3273 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3274 ID.AddInteger(AM);
3275 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003276 ID.AddInteger(ST->getMemoryVT().getRawBits());
Evan Cheng9109fb12006-11-05 09:30:09 +00003277 ID.AddInteger(ST->getAlignment());
3278 ID.AddInteger(ST->isVolatile());
3279 void *IP = 0;
3280 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3281 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003282 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003283 ST->isTruncatingStore(), ST->getMemoryVT(),
Evan Cheng9109fb12006-11-05 09:30:09 +00003284 ST->getSrcValue(), ST->getSrcValueOffset(),
3285 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003286 CSEMap.InsertNode(N, IP);
3287 AllNodes.push_back(N);
3288 return SDOperand(N, 0);
3289}
3290
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291SDOperand SelectionDAG::getVAArg(MVT VT,
Nate Begemanacc398c2006-01-25 18:21:52 +00003292 SDOperand Chain, SDOperand Ptr,
3293 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003294 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003295 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003296}
3297
Duncan Sands83ec4b62008-06-06 12:08:01 +00003298SDOperand SelectionDAG::getNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003299 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003300 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003301 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003302 case 1: return getNode(Opcode, VT, Ops[0]);
3303 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3304 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003305 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003306 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003307
Chris Lattneref847df2005-04-09 03:27:28 +00003308 switch (Opcode) {
3309 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003310 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003311 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003312 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3313 "LHS and RHS of condition must have same type!");
3314 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3315 "True and False arms of SelectCC must have same type!");
3316 assert(Ops[2].getValueType() == VT &&
3317 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003318 break;
3319 }
3320 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003321 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003322 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3323 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003324 break;
3325 }
Chris Lattneref847df2005-04-09 03:27:28 +00003326 }
3327
Chris Lattner385328c2005-05-14 07:42:29 +00003328 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003329 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003330 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003331 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003332 FoldingSetNodeID ID;
3333 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003334 void *IP = 0;
3335 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3336 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00003337 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003338 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003339 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00003340 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003341 }
Chris Lattneref847df2005-04-09 03:27:28 +00003342 AllNodes.push_back(N);
3343 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003344}
3345
Chris Lattner89c34632005-05-14 06:20:26 +00003346SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003347 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003348 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003349 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3350 Ops, NumOps);
3351}
3352
3353SDOperand SelectionDAG::getNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003354 const MVT *VTs, unsigned NumVTs,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003355 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003356 if (NumVTs == 1)
3357 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003358 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3359}
3360
3361SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003362 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003363 if (VTList.NumVTs == 1)
3364 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003365
Chris Lattner5f056bf2005-07-10 01:55:33 +00003366 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003367 // FIXME: figure out how to safely handle things like
3368 // int foo(int x) { return 1 << (x & 255); }
3369 // int bar() { return foo(256); }
3370#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003371 case ISD::SRA_PARTS:
3372 case ISD::SRL_PARTS:
3373 case ISD::SHL_PARTS:
3374 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003375 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003376 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3377 else if (N3.getOpcode() == ISD::AND)
3378 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3379 // If the and is only masking out bits that cannot effect the shift,
3380 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003381 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003382 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3383 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3384 }
3385 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003386#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003387 }
Chris Lattner89c34632005-05-14 06:20:26 +00003388
Chris Lattner43247a12005-08-25 19:12:10 +00003389 // Memoize the node unless it returns a flag.
3390 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003391 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003392 FoldingSetNodeID ID;
3393 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003394 void *IP = 0;
3395 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3396 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00003397 if (NumOps == 1)
3398 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3399 else if (NumOps == 2)
3400 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3401 else if (NumOps == 3)
3402 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3403 else
3404 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003405 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003406 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00003407 if (NumOps == 1)
3408 N = new UnarySDNode(Opcode, VTList, Ops[0]);
3409 else if (NumOps == 2)
3410 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3411 else if (NumOps == 3)
3412 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3413 else
3414 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003415 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003416 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00003417 return SDOperand(N, 0);
3418}
3419
Dan Gohman08ce9762007-10-08 15:49:58 +00003420SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003421 return getNode(Opcode, VTList, (SDOperand*)0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003422}
3423
3424SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3425 SDOperand N1) {
3426 SDOperand Ops[] = { N1 };
3427 return getNode(Opcode, VTList, Ops, 1);
3428}
3429
3430SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3431 SDOperand N1, SDOperand N2) {
3432 SDOperand Ops[] = { N1, N2 };
3433 return getNode(Opcode, VTList, Ops, 2);
3434}
3435
3436SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3437 SDOperand N1, SDOperand N2, SDOperand N3) {
3438 SDOperand Ops[] = { N1, N2, N3 };
3439 return getNode(Opcode, VTList, Ops, 3);
3440}
3441
3442SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3443 SDOperand N1, SDOperand N2, SDOperand N3,
3444 SDOperand N4) {
3445 SDOperand Ops[] = { N1, N2, N3, N4 };
3446 return getNode(Opcode, VTList, Ops, 4);
3447}
3448
3449SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3450 SDOperand N1, SDOperand N2, SDOperand N3,
3451 SDOperand N4, SDOperand N5) {
3452 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
3453 return getNode(Opcode, VTList, Ops, 5);
3454}
3455
Duncan Sands83ec4b62008-06-06 12:08:01 +00003456SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003457 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003458}
3459
Duncan Sands83ec4b62008-06-06 12:08:01 +00003460SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
3461 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattnera3255112005-11-08 23:30:28 +00003462 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00003463 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00003464 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003465 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003466 std::vector<MVT> V;
Chris Lattnera3255112005-11-08 23:30:28 +00003467 V.push_back(VT1);
3468 V.push_back(VT2);
3469 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003470 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00003471}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003472SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2,
3473 MVT VT3) {
3474 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003475 E = VTList.end(); I != E; ++I) {
3476 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
3477 (*I)[2] == VT3)
3478 return makeVTList(&(*I)[0], 3);
3479 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003480 std::vector<MVT> V;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003481 V.push_back(VT1);
3482 V.push_back(VT2);
3483 V.push_back(VT3);
3484 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00003485 return makeVTList(&(*VTList.begin())[0], 3);
3486}
3487
Duncan Sands83ec4b62008-06-06 12:08:01 +00003488SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003489 switch (NumVTs) {
3490 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003491 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003492 case 2: return getVTList(VTs[0], VTs[1]);
3493 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3494 default: break;
3495 }
3496
Duncan Sands83ec4b62008-06-06 12:08:01 +00003497 for (std::list<std::vector<MVT> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00003498 E = VTList.end(); I != E; ++I) {
3499 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
3500
3501 bool NoMatch = false;
3502 for (unsigned i = 2; i != NumVTs; ++i)
3503 if (VTs[i] != (*I)[i]) {
3504 NoMatch = true;
3505 break;
3506 }
3507 if (!NoMatch)
3508 return makeVTList(&*I->begin(), NumVTs);
3509 }
3510
Duncan Sands83ec4b62008-06-06 12:08:01 +00003511 VTList.push_front(std::vector<MVT>(VTs, VTs+NumVTs));
Chris Lattner70046e92006-08-15 17:46:01 +00003512 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003513}
3514
3515
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003516/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3517/// specified operands. If the resultant node already exists in the DAG,
3518/// this does not modify the specified node, instead it returns the node that
3519/// already exists. If the resultant node does not exist in the DAG, the
3520/// input node is returned. As a degenerate case, if you specify the same
3521/// input operands as the node already has, the input node is returned.
3522SDOperand SelectionDAG::
3523UpdateNodeOperands(SDOperand InN, SDOperand Op) {
3524 SDNode *N = InN.Val;
3525 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3526
3527 // Check to see if there is no change.
3528 if (Op == N->getOperand(0)) return InN;
3529
3530 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003531 void *InsertPos = 0;
3532 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
3533 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003534
3535 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003536 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003537 RemoveNodeFromCSEMaps(N);
3538
3539 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003540 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003541 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003542 N->OperandList[0].setUser(N);
3543 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003544
3545 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003546 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003547 return InN;
3548}
3549
3550SDOperand SelectionDAG::
3551UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
3552 SDNode *N = InN.Val;
3553 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3554
3555 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003556 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3557 return InN; // No operands changed, just return the input node.
3558
3559 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003560 void *InsertPos = 0;
3561 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
3562 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003563
3564 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003565 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003566 RemoveNodeFromCSEMaps(N);
3567
3568 // Now we update the operands.
3569 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003570 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003571 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003572 N->OperandList[0].setUser(N);
3573 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003574 }
3575 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003576 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003577 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003578 N->OperandList[1].setUser(N);
3579 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003580 }
3581
3582 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003583 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003584 return InN;
3585}
3586
3587SDOperand SelectionDAG::
3588UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003589 SDOperand Ops[] = { Op1, Op2, Op3 };
3590 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003591}
3592
3593SDOperand SelectionDAG::
3594UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3595 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003596 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
3597 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003598}
3599
3600SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00003601UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
3602 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003603 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
3604 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003605}
3606
Chris Lattner809ec112006-01-28 10:09:25 +00003607SDOperand SelectionDAG::
Roman Levenstein9cac5252008-04-16 16:15:27 +00003608UpdateNodeOperands(SDOperand InN, SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003609 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003610 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003611 "Update with wrong number of operands");
3612
3613 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003614 bool AnyChange = false;
3615 for (unsigned i = 0; i != NumOps; ++i) {
3616 if (Ops[i] != N->getOperand(i)) {
3617 AnyChange = true;
3618 break;
3619 }
3620 }
3621
3622 // No operands changed, just return the input node.
3623 if (!AnyChange) return InN;
3624
3625 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003626 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003627 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00003628 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003629
Dan Gohman7ceda162008-05-02 00:05:03 +00003630 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003631 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003632 RemoveNodeFromCSEMaps(N);
3633
3634 // Now we update the operands.
3635 for (unsigned i = 0; i != NumOps; ++i) {
3636 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003637 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003638 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003639 N->OperandList[i].setUser(N);
3640 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003641 }
3642 }
3643
3644 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003645 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003646 return InN;
3647}
3648
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003649/// MorphNodeTo - This frees the operands of the current node, resets the
3650/// opcode, types, and operands to the specified value. This should only be
3651/// used by the SelectionDAG class.
3652void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
Dan Gohman35b31be2008-04-17 23:02:12 +00003653 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003654 NodeType = Opc;
3655 ValueList = L.VTs;
3656 NumValues = L.NumVTs;
3657
3658 // Clear the operands list, updating used nodes to remove this from their
3659 // use list.
3660 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003661 I->getVal()->removeUser(std::distance(op_begin(), I), this);
Chris Lattner63e3f142007-02-04 07:28:00 +00003662
3663 // If NumOps is larger than the # of operands we currently have, reallocate
3664 // the operand list.
3665 if (NumOps > NumOperands) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003666 if (OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +00003667 delete [] OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003668 }
Roman Levenstein9cac5252008-04-16 16:15:27 +00003669 OperandList = new SDUse[NumOps];
Chris Lattner63e3f142007-02-04 07:28:00 +00003670 OperandsNeedDelete = true;
3671 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003672
3673 // Assign the new operands.
3674 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003675
3676 for (unsigned i = 0, e = NumOps; i != e; ++i) {
3677 OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003678 OperandList[i].setUser(this);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003679 SDNode *N = OperandList[i].getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003680 N->addUser(i, this);
3681 ++N->UsesSize;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003682 }
3683}
Chris Lattner149c58c2005-08-16 18:17:10 +00003684
3685/// SelectNodeTo - These are used for target selectors to *mutate* the
3686/// specified node to have the specified return type, Target opcode, and
3687/// operands. Note that target opcodes are stored as
3688/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003689///
3690/// Note that SelectNodeTo returns the resultant node. If there is already a
3691/// node of the specified opcode and operands, it returns that node instead of
3692/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00003693SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003694 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003695 SDVTList VTs = getVTList(VT);
Dan Gohmancd920d92008-07-02 23:23:19 +00003696 return SelectNodeTo(N, TargetOpc, VTs, (SDOperand*)0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003697}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003698
Evan Cheng95514ba2006-08-26 08:00:10 +00003699SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003700 MVT VT, SDOperand Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003701 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003702 SDOperand Ops[] = { Op1 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003703 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003704}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003705
Evan Cheng95514ba2006-08-26 08:00:10 +00003706SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003707 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003708 SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003709 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003710 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003711 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003712}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003713
Evan Cheng95514ba2006-08-26 08:00:10 +00003714SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003715 MVT VT, SDOperand Op1,
Evan Cheng95514ba2006-08-26 08:00:10 +00003716 SDOperand Op2, SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003717 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00003718 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003719 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003720}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003721
Evan Cheng95514ba2006-08-26 08:00:10 +00003722SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003723 MVT VT, SDOperandPtr Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003724 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003725 SDVTList VTs = getVTList(VT);
Dan Gohmancd920d92008-07-02 23:23:19 +00003726 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3727}
3728
3729SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3730 MVT VT1, MVT VT2, SDOperandPtr Ops,
3731 unsigned NumOps) {
3732 SDVTList VTs = getVTList(VT1, VT2);
3733 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3734}
3735
3736SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3737 MVT VT1, MVT VT2) {
3738 SDVTList VTs = getVTList(VT1, VT2);
3739 return SelectNodeTo(N, TargetOpc, VTs, (SDOperand *)0, 0);
3740}
3741
3742SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3743 MVT VT1, MVT VT2, MVT VT3, SDOperandPtr Ops,
3744 unsigned NumOps) {
3745 SDVTList VTs = getVTList(VT1, VT2, VT3);
3746 return SelectNodeTo(N, TargetOpc, VTs, Ops, NumOps);
3747}
3748
3749SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3750 MVT VT1, MVT VT2,
3751 SDOperand Op1) {
3752 SDVTList VTs = getVTList(VT1, VT2);
3753 SDOperand Ops[] = { Op1 };
3754 return SelectNodeTo(N, TargetOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003755}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003756
Evan Cheng95514ba2006-08-26 08:00:10 +00003757SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003758 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003759 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003760 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003761 SDOperand Ops[] = { Op1, Op2 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003762 return SelectNodeTo(N, TargetOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003763}
3764
Evan Cheng95514ba2006-08-26 08:00:10 +00003765SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003766 MVT VT1, MVT VT2,
Evan Cheng95514ba2006-08-26 08:00:10 +00003767 SDOperand Op1, SDOperand Op2,
3768 SDOperand Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003769 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003770 SDOperand Ops[] = { Op1, Op2, Op3 };
Dan Gohmancd920d92008-07-02 23:23:19 +00003771 return SelectNodeTo(N, TargetOpc, VTs, Ops, 3);
3772}
3773
3774SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3775 SDVTList VTs, SDOperandPtr Ops,
3776 unsigned NumOps) {
3777 // If an identical node already exists, use it.
Jim Laskey583bd472006-10-27 23:46:08 +00003778 FoldingSetNodeID ID;
Dan Gohmancd920d92008-07-02 23:23:19 +00003779 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003780 void *IP = 0;
3781 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003782 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003783
Chris Lattner0fb094f2005-11-19 01:44:53 +00003784 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003785
Dan Gohmancd920d92008-07-02 23:23:19 +00003786 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003787 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003788 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003789}
3790
Chris Lattner0fb094f2005-11-19 01:44:53 +00003791
Evan Cheng6ae46c42006-02-09 07:15:23 +00003792/// getTargetNode - These are used for target selectors to create a new node
3793/// with specified return type(s), target opcode, and operands.
3794///
3795/// Note that getTargetNode returns the resultant node. If there is already a
3796/// node of the specified opcode and operands, it returns that node instead of
3797/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003798SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003799 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3800}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003801SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDOperand Op1) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003802 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3803}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003804SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003805 SDOperand Op1, SDOperand Op2) {
3806 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3807}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003808SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003809 SDOperand Op1, SDOperand Op2,
3810 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003811 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3812}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003813SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003814 SDOperandPtr Ops, unsigned NumOps) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003815 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003816}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003817SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
3818 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003819 SDOperand Op;
3820 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3821}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003822SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3823 MVT VT2, SDOperand Op1) {
3824 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003825 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003826}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003827SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3828 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003829 SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003830 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003831 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003832 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003833}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003834SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3835 MVT VT2, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00003836 SDOperand Op2, SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003837 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003838 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003839 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003840}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003841SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003842 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003843 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003844 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003845}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003846SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003847 SDOperand Op1, SDOperand Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003848 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003849 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003850 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003851}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003852SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003853 SDOperand Op1, SDOperand Op2,
3854 SDOperand Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003855 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003856 SDOperand Ops[] = { Op1, Op2, Op3 };
3857 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3858}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003859SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003860 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003861 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Evan Cheng694481e2006-08-27 08:08:54 +00003862 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003863}
Duncan Sands83ec4b62008-06-06 12:08:01 +00003864SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
3865 MVT VT2, MVT VT3, MVT VT4,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003866 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003867 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00003868 VTList.push_back(VT1);
3869 VTList.push_back(VT2);
3870 VTList.push_back(VT3);
3871 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003872 const MVT *VTs = getNodeValueTypes(VTList);
Evan Cheng05e69c12007-09-12 23:39:49 +00003873 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3874}
Evan Cheng39305cf2007-10-05 01:10:49 +00003875SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003876 std::vector<MVT> &ResultTys,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003877 SDOperandPtr Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003878 const MVT *VTs = getNodeValueTypes(ResultTys);
Evan Cheng39305cf2007-10-05 01:10:49 +00003879 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3880 Ops, NumOps).Val;
3881}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003882
Evan Cheng08b11732008-03-22 01:55:50 +00003883/// getNodeIfExists - Get the specified node if it's already available, or
3884/// else return NULL.
3885SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003886 SDOperandPtr Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00003887 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3888 FoldingSetNodeID ID;
3889 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3890 void *IP = 0;
3891 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3892 return E;
3893 }
3894 return NULL;
3895}
3896
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003897
Evan Cheng99157a02006-08-07 22:13:29 +00003898/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003899/// This can cause recursive merging of nodes in the DAG.
3900///
Chris Lattner11d049c2008-02-03 03:35:22 +00003901/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00003902///
Chris Lattner11d049c2008-02-03 03:35:22 +00003903void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003904 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003905 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00003906 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00003907 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003908 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00003909
Chris Lattner8b8749f2005-08-17 19:00:20 +00003910 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003911 SDNode::use_iterator UI = From->use_begin();
3912 SDNode *U = UI->getUser();
3913
Chris Lattner8b8749f2005-08-17 19:00:20 +00003914 // This node is about to morph, remove its old self from the CSE maps.
3915 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003916 int operandNum = 0;
3917 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3918 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003919 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003920 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00003921 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003922 I->setUser(U);
3923 To.Val->addUser(operandNum, U);
3924 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003925
3926 // Now that we have modified U, add it back to the CSE maps. If it already
3927 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003928 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003929 ReplaceAllUsesWith(U, Existing, UpdateListener);
3930 // U is now dead. Inform the listener if it exists and delete it.
3931 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003932 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003933 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003934 } else {
3935 // If the node doesn't already exist, we updated it. Inform a listener if
3936 // it exists.
3937 if (UpdateListener)
3938 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003939 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003940 }
3941}
3942
3943/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3944/// This can cause recursive merging of nodes in the DAG.
3945///
3946/// This version assumes From/To have matching types and numbers of result
3947/// values.
3948///
Chris Lattner1e111c72005-09-07 05:37:01 +00003949void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003950 DAGUpdateListener *UpdateListener) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003951 assert(From != To && "Cannot replace uses of with self");
3952 assert(From->getNumValues() == To->getNumValues() &&
3953 "Cannot use this version of ReplaceAllUsesWith!");
Chris Lattner11d049c2008-02-03 03:35:22 +00003954 if (From->getNumValues() == 1) // If possible, use the faster version.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003955 return ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0),
3956 UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00003957
3958 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003959 SDNode::use_iterator UI = From->use_begin();
3960 SDNode *U = UI->getUser();
3961
Chris Lattner8b52f212005-08-26 18:36:28 +00003962 // This node is about to morph, remove its old self from the CSE maps.
3963 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00003964 int operandNum = 0;
3965 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
3966 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00003967 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00003968 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00003969 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003970 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00003971 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00003972
Chris Lattner8b8749f2005-08-17 19:00:20 +00003973 // Now that we have modified U, add it back to the CSE maps. If it already
3974 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003975 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003976 ReplaceAllUsesWith(U, Existing, UpdateListener);
3977 // U is now dead. Inform the listener if it exists and delete it.
3978 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00003979 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00003980 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003981 } else {
3982 // If the node doesn't already exist, we updated it. Inform a listener if
3983 // it exists.
3984 if (UpdateListener)
3985 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00003986 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003987 }
3988}
3989
Chris Lattner8b52f212005-08-26 18:36:28 +00003990/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3991/// This can cause recursive merging of nodes in the DAG.
3992///
3993/// This version can replace From with any result values. To must match the
3994/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003995void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Roman Levenstein9cac5252008-04-16 16:15:27 +00003996 SDOperandPtr To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003997 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00003998 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003999 return ReplaceAllUsesWith(SDOperand(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004000
4001 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004002 SDNode::use_iterator UI = From->use_begin();
4003 SDNode *U = UI->getUser();
4004
Chris Lattner7b2880c2005-08-24 22:44:39 +00004005 // This node is about to morph, remove its old self from the CSE maps.
4006 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004007 int operandNum = 0;
4008 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4009 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004010 if (I->getVal() == From) {
4011 const SDOperand &ToOp = To[I->getSDOperand().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004012 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004013 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004014 I->setUser(U);
4015 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004016 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004017
Chris Lattner7b2880c2005-08-24 22:44:39 +00004018 // Now that we have modified U, add it back to the CSE maps. If it already
4019 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004020 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004021 ReplaceAllUsesWith(U, Existing, UpdateListener);
4022 // U is now dead. Inform the listener if it exists and delete it.
4023 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004024 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004025 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004026 } else {
4027 // If the node doesn't already exist, we updated it. Inform a listener if
4028 // it exists.
4029 if (UpdateListener)
4030 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004031 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004032 }
4033}
4034
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004035namespace {
4036 /// ChainedSetUpdaterListener - This class is a DAGUpdateListener that removes
4037 /// any deleted nodes from the set passed into its constructor and recursively
4038 /// notifies another update listener if specified.
4039 class ChainedSetUpdaterListener :
4040 public SelectionDAG::DAGUpdateListener {
4041 SmallSetVector<SDNode*, 16> &Set;
4042 SelectionDAG::DAGUpdateListener *Chain;
4043 public:
4044 ChainedSetUpdaterListener(SmallSetVector<SDNode*, 16> &set,
4045 SelectionDAG::DAGUpdateListener *chain)
4046 : Set(set), Chain(chain) {}
Roman Levensteindc1adac2008-04-07 10:06:32 +00004047
Duncan Sandsedfcf592008-06-11 11:42:12 +00004048 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004049 Set.remove(N);
Duncan Sandsedfcf592008-06-11 11:42:12 +00004050 if (Chain) Chain->NodeDeleted(N, E);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004051 }
4052 virtual void NodeUpdated(SDNode *N) {
4053 if (Chain) Chain->NodeUpdated(N);
4054 }
4055 };
4056}
4057
Chris Lattner012f2412006-02-17 21:58:01 +00004058/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4059/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004060/// handled the same way as for ReplaceAllUsesWith.
Chris Lattner012f2412006-02-17 21:58:01 +00004061void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004062 DAGUpdateListener *UpdateListener){
Chris Lattner012f2412006-02-17 21:58:01 +00004063 assert(From != To && "Cannot replace a value with itself");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004064
Chris Lattner012f2412006-02-17 21:58:01 +00004065 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004066 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004067 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004068 return;
4069 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004070
4071 if (From.use_empty()) return;
4072
Chris Lattnercf5640b2007-02-04 00:14:31 +00004073 // Get all of the users of From.Val. We want these in a nice,
4074 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004075 SmallSetVector<SDNode*, 16> Users;
4076 for (SDNode::use_iterator UI = From.Val->use_begin(),
4077 E = From.Val->use_end(); UI != E; ++UI) {
4078 SDNode *User = UI->getUser();
Dan Gohmancd920d92008-07-02 23:23:19 +00004079 Users.insert(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004080 }
Chris Lattner012f2412006-02-17 21:58:01 +00004081
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004082 // When one of the recursive merges deletes nodes from the graph, we need to
4083 // make sure that UpdateListener is notified *and* that the node is removed
4084 // from Users if present. CSUL does this.
4085 ChainedSetUpdaterListener CSUL(Users, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004086
Chris Lattner012f2412006-02-17 21:58:01 +00004087 while (!Users.empty()) {
4088 // We know that this user uses some value of From. If it is the right
4089 // value, update it.
4090 SDNode *User = Users.back();
4091 Users.pop_back();
4092
Chris Lattner01d029b2007-10-15 06:10:22 +00004093 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004094 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004095 for (; Op != E; ++Op)
4096 if (*Op == From) break;
4097
4098 // If there are no matches, the user must use some other result of From.
4099 if (Op == E) continue;
4100
4101 // Okay, we know this user needs to be updated. Remove its old self
4102 // from the CSE maps.
4103 RemoveNodeFromCSEMaps(User);
4104
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004105 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004106 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004107 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004108 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004109 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004110 Op->setUser(User);
4111 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004112 }
4113 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004114
4115 // Now that we have modified User, add it back to the CSE maps. If it
4116 // already exists there, recursively merge the results together.
4117 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004118 if (!Existing) {
4119 if (UpdateListener) UpdateListener->NodeUpdated(User);
4120 continue; // Continue on to next user.
4121 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004122
4123 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004124 // to replace the dead one with the existing one. This can cause
Chris Lattner01d029b2007-10-15 06:10:22 +00004125 // recursive merging of other unrelated nodes down the line. The merging
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004126 // can cause deletion of nodes that used the old value. To handle this, we
4127 // use CSUL to remove them from the Users set.
4128 ReplaceAllUsesWith(User, Existing, &CSUL);
Chris Lattner01d029b2007-10-15 06:10:22 +00004129
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004130 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004131 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004132 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004133 }
4134}
4135
Evan Chenge6f35d82006-08-01 08:20:41 +00004136/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
4137/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00004138unsigned SelectionDAG::AssignNodeIds() {
4139 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00004140 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
4141 SDNode *N = I;
4142 N->setNodeId(Id++);
4143 }
4144 return Id;
4145}
4146
Evan Chenge6f35d82006-08-01 08:20:41 +00004147/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004148/// based on their topological order. It returns the maximum id and a vector
4149/// of the SDNodes* in assigned order by reference.
4150unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004151 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004152 std::vector<unsigned> InDegree(DAGSize);
4153 std::vector<SDNode*> Sources;
4154
4155 // Use a two pass approach to avoid using a std::map which is slow.
4156 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004157 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4158 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004159 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004160 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004161 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004162 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004163 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004164 }
4165
Evan Cheng99157a02006-08-07 22:13:29 +00004166 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00004167 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004168 SDNode *N = Sources.back();
4169 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004170 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004171 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004172 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004173 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004174 if (Degree == 0)
4175 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004176 }
4177 }
4178
Evan Chengc384d6c2006-08-02 22:00:34 +00004179 // Second pass, assign the actual topological order as node ids.
4180 Id = 0;
4181 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4182 TI != TE; ++TI)
4183 (*TI)->setNodeId(Id++);
4184
4185 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004186}
4187
4188
Evan Cheng091cba12006-07-27 06:39:06 +00004189
Jim Laskey58b968b2005-08-17 20:08:02 +00004190//===----------------------------------------------------------------------===//
4191// SDNode Class
4192//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004193
Chris Lattner917d2c92006-07-19 00:00:37 +00004194// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004195void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004196void UnarySDNode::ANCHOR() {}
4197void BinarySDNode::ANCHOR() {}
4198void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004199void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004200void ConstantSDNode::ANCHOR() {}
4201void ConstantFPSDNode::ANCHOR() {}
4202void GlobalAddressSDNode::ANCHOR() {}
4203void FrameIndexSDNode::ANCHOR() {}
4204void JumpTableSDNode::ANCHOR() {}
4205void ConstantPoolSDNode::ANCHOR() {}
4206void BasicBlockSDNode::ANCHOR() {}
4207void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004208void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004209void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004210void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004211void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004212void ExternalSymbolSDNode::ANCHOR() {}
4213void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004214void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004215void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004216void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004217void LoadSDNode::ANCHOR() {}
4218void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004219void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004220
Chris Lattner48b85922007-02-04 02:41:42 +00004221HandleSDNode::~HandleSDNode() {
4222 SDVTList VTs = { 0, 0 };
Dan Gohman35b31be2008-04-17 23:02:12 +00004223 MorphNodeTo(ISD::HANDLENODE, VTs, SDOperandPtr(), 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00004224}
4225
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004226GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004227 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004228 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004229 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004230 // Thread Local
4231 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4232 // Non Thread Local
4233 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4234 getSDVTList(VT)), Offset(o) {
4235 TheGlobal = const_cast<GlobalValue*>(GA);
4236}
Chris Lattner48b85922007-02-04 02:41:42 +00004237
Dan Gohman36b5c132008-04-07 19:35:22 +00004238/// getMemOperand - Return a MachineMemOperand object describing the memory
Mon P Wang28873102008-06-25 08:15:39 +00004239/// reference performed by this atomic.
4240MachineMemOperand AtomicSDNode::getMemOperand() const {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004241 int Size = (getValueType(0).getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004242 int Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4243 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4244
4245 // Check if the atomic references a frame index
4246 const FrameIndexSDNode *FI =
4247 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4248 if (!getSrcValue() && FI)
4249 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
4250 FI->getIndex(), Size, getAlignment());
4251 else
4252 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4253 Size, getAlignment());
4254}
4255
4256/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman69de1932008-02-06 22:27:42 +00004257/// reference performed by this load or store.
Dan Gohman36b5c132008-04-07 19:35:22 +00004258MachineMemOperand LSBaseSDNode::getMemOperand() const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004259 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Dan Gohman69de1932008-02-06 22:27:42 +00004260 int Flags =
Dan Gohman36b5c132008-04-07 19:35:22 +00004261 getOpcode() == ISD::LOAD ? MachineMemOperand::MOLoad :
4262 MachineMemOperand::MOStore;
Mon P Wang28873102008-06-25 08:15:39 +00004263 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Dan Gohman69de1932008-02-06 22:27:42 +00004264
4265 // Check if the load references a frame index, and does not have
4266 // an SV attached.
4267 const FrameIndexSDNode *FI =
4268 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4269 if (!getSrcValue() && FI)
Dan Gohman36b5c132008-04-07 19:35:22 +00004270 return MachineMemOperand(PseudoSourceValue::getFixedStack(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004271 FI->getIndex(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004272 else
Dan Gohman36b5c132008-04-07 19:35:22 +00004273 return MachineMemOperand(getSrcValue(), Flags,
Mon P Wang28873102008-06-25 08:15:39 +00004274 getSrcValueOffset(), Size, getAlignment());
Dan Gohman69de1932008-02-06 22:27:42 +00004275}
4276
Jim Laskey583bd472006-10-27 23:46:08 +00004277/// Profile - Gather unique data for the node.
4278///
4279void SDNode::Profile(FoldingSetNodeID &ID) {
4280 AddNodeIDNode(ID, this);
4281}
4282
Chris Lattnera3255112005-11-08 23:30:28 +00004283/// getValueTypeList - Return a pointer to the specified value type.
4284///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004285const MVT *SDNode::getValueTypeList(MVT VT) {
4286 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004287 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004288 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004289 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004290 static MVT VTs[MVT::LAST_VALUETYPE];
4291 VTs[VT.getSimpleVT()] = VT;
4292 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004293 }
Chris Lattnera3255112005-11-08 23:30:28 +00004294}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004295
Chris Lattner5c884562005-01-12 18:37:47 +00004296/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4297/// indicated value. This method ignores uses of other values defined by this
4298/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004299bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004300 assert(Value < getNumValues() && "Bad value!");
4301
4302 // If there is only one value, this is easy.
4303 if (getNumValues() == 1)
4304 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00004305 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00004306
Evan Cheng4ee62112006-02-05 06:29:23 +00004307 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00004308
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004309 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00004310
Roman Levensteindc1adac2008-04-07 10:06:32 +00004311 // TODO: Only iterate over uses of a given value of the node
4312 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4313 if (*UI == TheValue) {
4314 if (NUses == 0)
4315 return false;
4316 --NUses;
4317 }
Chris Lattner5c884562005-01-12 18:37:47 +00004318 }
4319
4320 // Found exactly the right number of uses?
4321 return NUses == 0;
4322}
4323
4324
Evan Cheng33d55952007-08-02 05:29:38 +00004325/// hasAnyUseOfValue - Return true if there are any use of the indicated
4326/// value. This method ignores uses of other values defined by this operation.
4327bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4328 assert(Value < getNumValues() && "Bad value!");
4329
Dan Gohman30359592008-01-29 13:02:09 +00004330 if (use_empty()) return false;
Evan Cheng33d55952007-08-02 05:29:38 +00004331
4332 SDOperand TheValue(const_cast<SDNode *>(this), Value);
4333
4334 SmallPtrSet<SDNode*, 32> UsersHandled;
4335
Roman Levensteindc1adac2008-04-07 10:06:32 +00004336 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
4337 SDNode *User = UI->getUser();
Evan Cheng33d55952007-08-02 05:29:38 +00004338 if (User->getNumOperands() == 1 ||
4339 UsersHandled.insert(User)) // First time we've seen this?
4340 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
4341 if (User->getOperand(i) == TheValue) {
4342 return true;
4343 }
4344 }
4345
4346 return false;
4347}
4348
4349
Evan Cheng917be682008-03-04 00:41:45 +00004350/// isOnlyUseOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004351///
Evan Cheng917be682008-03-04 00:41:45 +00004352bool SDNode::isOnlyUseOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004353 bool Seen = false;
4354 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004355 SDNode *User = I->getUser();
Evan Cheng4ee62112006-02-05 06:29:23 +00004356 if (User == this)
4357 Seen = true;
4358 else
4359 return false;
4360 }
4361
4362 return Seen;
4363}
4364
Evan Chenge6e97e62006-11-03 07:31:32 +00004365/// isOperand - Return true if this node is an operand of N.
4366///
Roman Levenstein9cac5252008-04-16 16:15:27 +00004367bool SDOperand::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004368 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4369 if (*this == N->getOperand(i))
4370 return true;
4371 return false;
4372}
4373
Evan Cheng917be682008-03-04 00:41:45 +00004374bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004375 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004376 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004377 return true;
4378 return false;
4379}
Evan Cheng4ee62112006-02-05 06:29:23 +00004380
Chris Lattner572dee72008-01-16 05:49:24 +00004381/// reachesChainWithoutSideEffects - Return true if this operand (which must
4382/// be a chain) reaches the specified operand without crossing any
4383/// side-effecting instructions. In practice, this looks through token
4384/// factors and non-volatile loads. In order to remain efficient, this only
4385/// looks a couple of nodes in, it does not do an exhaustive search.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004386bool SDOperand::reachesChainWithoutSideEffects(SDOperand Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004387 unsigned Depth) const {
4388 if (*this == Dest) return true;
4389
4390 // Don't search too deeply, we just want to be able to see through
4391 // TokenFactor's etc.
4392 if (Depth == 0) return false;
4393
4394 // If this is a token factor, all inputs to the TF happen in parallel. If any
4395 // of the operands of the TF reach dest, then we can do the xform.
4396 if (getOpcode() == ISD::TokenFactor) {
4397 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4398 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4399 return true;
4400 return false;
4401 }
4402
4403 // Loads don't have side effects, look through them.
4404 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4405 if (!Ld->isVolatile())
4406 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4407 }
4408 return false;
4409}
4410
4411
Evan Chengc5fc57d2006-11-03 03:05:24 +00004412static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004413 SmallPtrSet<SDNode *, 32> &Visited) {
4414 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004415 return;
4416
4417 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4418 SDNode *Op = N->getOperand(i).Val;
4419 if (Op == P) {
4420 found = true;
4421 return;
4422 }
4423 findPredecessor(Op, P, found, Visited);
4424 }
4425}
4426
Evan Cheng917be682008-03-04 00:41:45 +00004427/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004428/// is either an operand of N or it can be reached by recursively traversing
4429/// up the operands.
4430/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004431bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004432 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004433 bool found = false;
4434 findPredecessor(N, this, found, Visited);
4435 return found;
4436}
4437
Evan Chengc5484282006-10-04 00:56:09 +00004438uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4439 assert(Num < NumOperands && "Invalid child # of SDNode!");
4440 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4441}
4442
Reid Spencer577cc322007-04-01 07:32:19 +00004443std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004444 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004445 default:
4446 if (getOpcode() < ISD::BUILTIN_OP_END)
4447 return "<<Unknown DAG Node>>";
4448 else {
Evan Cheng72261582005-12-20 06:22:03 +00004449 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004450 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00004451 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
Chris Lattner349c4952008-01-07 03:13:06 +00004452 return TII->get(getOpcode()-ISD::BUILTIN_OP_END).getName();
Evan Cheng115c0362005-12-19 23:11:49 +00004453
Evan Cheng72261582005-12-20 06:22:03 +00004454 TargetLowering &TLI = G->getTargetLoweringInfo();
4455 const char *Name =
4456 TLI.getTargetNodeName(getOpcode());
4457 if (Name) return Name;
4458 }
4459
4460 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004461 }
4462
Evan Cheng27b7db52008-03-08 00:58:38 +00004463 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004464 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004465 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4466 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4467 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004468 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4469 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4470 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004471 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004472 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4473 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4474 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4475 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4476 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004477 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004478 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004479 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004480 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004481 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004482 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004483 case ISD::AssertSext: return "AssertSext";
4484 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004485
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004486 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004487 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004488 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004489 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004490
4491 case ISD::Constant: return "Constant";
4492 case ISD::ConstantFP: return "ConstantFP";
4493 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004494 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004495 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004496 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004497 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004498 case ISD::RETURNADDR: return "RETURNADDR";
4499 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004500 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004501 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4502 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004503 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004504 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004505 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004506 case ISD::INTRINSIC_WO_CHAIN: {
4507 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4508 return Intrinsic::getName((Intrinsic::ID)IID);
4509 }
4510 case ISD::INTRINSIC_VOID:
4511 case ISD::INTRINSIC_W_CHAIN: {
4512 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004513 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004514 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004515
Chris Lattnerb2827b02006-03-19 00:52:58 +00004516 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004517 case ISD::TargetConstant: return "TargetConstant";
4518 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004519 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004520 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004521 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004522 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004523 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004524 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004525
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004526 case ISD::CopyToReg: return "CopyToReg";
4527 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004528 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004529 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004530 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004531 case ISD::DBG_LABEL: return "dbg_label";
4532 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004533 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004534 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004535 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004536 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004537
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004538 // Unary operators
4539 case ISD::FABS: return "fabs";
4540 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004541 case ISD::FSQRT: return "fsqrt";
4542 case ISD::FSIN: return "fsin";
4543 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004544 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004545 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004546
4547 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004548 case ISD::ADD: return "add";
4549 case ISD::SUB: return "sub";
4550 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004551 case ISD::MULHU: return "mulhu";
4552 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004553 case ISD::SDIV: return "sdiv";
4554 case ISD::UDIV: return "udiv";
4555 case ISD::SREM: return "srem";
4556 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004557 case ISD::SMUL_LOHI: return "smul_lohi";
4558 case ISD::UMUL_LOHI: return "umul_lohi";
4559 case ISD::SDIVREM: return "sdivrem";
4560 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004561 case ISD::AND: return "and";
4562 case ISD::OR: return "or";
4563 case ISD::XOR: return "xor";
4564 case ISD::SHL: return "shl";
4565 case ISD::SRA: return "sra";
4566 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004567 case ISD::ROTL: return "rotl";
4568 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004569 case ISD::FADD: return "fadd";
4570 case ISD::FSUB: return "fsub";
4571 case ISD::FMUL: return "fmul";
4572 case ISD::FDIV: return "fdiv";
4573 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004574 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004575 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004576
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004577 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004578 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004579 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004580 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004581 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004582 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004583 case ISD::CONCAT_VECTORS: return "concat_vectors";
4584 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004585 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004586 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004587 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004588 case ISD::ADDC: return "addc";
4589 case ISD::ADDE: return "adde";
4590 case ISD::SUBC: return "subc";
4591 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004592 case ISD::SHL_PARTS: return "shl_parts";
4593 case ISD::SRA_PARTS: return "sra_parts";
4594 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004595
4596 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4597 case ISD::INSERT_SUBREG: return "insert_subreg";
4598
Chris Lattner7f644642005-04-28 21:44:03 +00004599 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004600 case ISD::SIGN_EXTEND: return "sign_extend";
4601 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004602 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004603 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004604 case ISD::TRUNCATE: return "truncate";
4605 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004606 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004607 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004608 case ISD::FP_EXTEND: return "fp_extend";
4609
4610 case ISD::SINT_TO_FP: return "sint_to_fp";
4611 case ISD::UINT_TO_FP: return "uint_to_fp";
4612 case ISD::FP_TO_SINT: return "fp_to_sint";
4613 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004614 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004615
4616 // Control flow instructions
4617 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004618 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004619 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004620 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004621 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004622 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004623 case ISD::CALLSEQ_START: return "callseq_start";
4624 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004625
4626 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004627 case ISD::LOAD: return "load";
4628 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004629 case ISD::VAARG: return "vaarg";
4630 case ISD::VACOPY: return "vacopy";
4631 case ISD::VAEND: return "vaend";
4632 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004633 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004634 case ISD::EXTRACT_ELEMENT: return "extract_element";
4635 case ISD::BUILD_PAIR: return "build_pair";
4636 case ISD::STACKSAVE: return "stacksave";
4637 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004638 case ISD::TRAP: return "trap";
4639
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004640 // Bit manipulation
4641 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004642 case ISD::CTPOP: return "ctpop";
4643 case ISD::CTTZ: return "cttz";
4644 case ISD::CTLZ: return "ctlz";
4645
Chris Lattner36ce6912005-11-29 06:21:05 +00004646 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004647 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004648 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004649
Duncan Sands36397f52007-07-27 12:58:54 +00004650 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004651 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004652
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004653 case ISD::CONDCODE:
4654 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004655 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004656 case ISD::SETOEQ: return "setoeq";
4657 case ISD::SETOGT: return "setogt";
4658 case ISD::SETOGE: return "setoge";
4659 case ISD::SETOLT: return "setolt";
4660 case ISD::SETOLE: return "setole";
4661 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004662
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004663 case ISD::SETO: return "seto";
4664 case ISD::SETUO: return "setuo";
4665 case ISD::SETUEQ: return "setue";
4666 case ISD::SETUGT: return "setugt";
4667 case ISD::SETUGE: return "setuge";
4668 case ISD::SETULT: return "setult";
4669 case ISD::SETULE: return "setule";
4670 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004671
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004672 case ISD::SETEQ: return "seteq";
4673 case ISD::SETGT: return "setgt";
4674 case ISD::SETGE: return "setge";
4675 case ISD::SETLT: return "setlt";
4676 case ISD::SETLE: return "setle";
4677 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004678 }
4679 }
4680}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004681
Evan Cheng144d8f02006-11-09 17:55:04 +00004682const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004683 switch (AM) {
4684 default:
4685 return "";
4686 case ISD::PRE_INC:
4687 return "<pre-inc>";
4688 case ISD::PRE_DEC:
4689 return "<pre-dec>";
4690 case ISD::POST_INC:
4691 return "<post-inc>";
4692 case ISD::POST_DEC:
4693 return "<post-dec>";
4694 }
4695}
4696
Duncan Sands276dcbd2008-03-21 09:14:45 +00004697std::string ISD::ArgFlagsTy::getArgFlagsString() {
4698 std::string S = "< ";
4699
4700 if (isZExt())
4701 S += "zext ";
4702 if (isSExt())
4703 S += "sext ";
4704 if (isInReg())
4705 S += "inreg ";
4706 if (isSRet())
4707 S += "sret ";
4708 if (isByVal())
4709 S += "byval ";
4710 if (isNest())
4711 S += "nest ";
4712 if (getByValAlign())
4713 S += "byval-align:" + utostr(getByValAlign()) + " ";
4714 if (getOrigAlign())
4715 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4716 if (getByValSize())
4717 S += "byval-size:" + utostr(getByValSize()) + " ";
4718 return S + ">";
4719}
4720
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004721void SDNode::dump() const { dump(0); }
4722void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00004723 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004724
4725 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004726 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004727 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00004728 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004729 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00004730 cerr << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004731 }
Bill Wendling832171c2006-12-07 20:04:42 +00004732 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004733
Bill Wendling832171c2006-12-07 20:04:42 +00004734 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004735 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00004736 if (i) cerr << ", ";
4737 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004738 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00004739 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004740 }
4741
Evan Chengce254432007-12-11 02:08:35 +00004742 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
4743 SDNode *Mask = getOperand(2).Val;
4744 cerr << "<";
4745 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
4746 if (i) cerr << ",";
4747 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
4748 cerr << "u";
4749 else
4750 cerr << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
4751 }
4752 cerr << ">";
4753 }
4754
Chris Lattnerc3aae252005-01-07 07:46:32 +00004755 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004756 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004757 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004758 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
4759 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
4760 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
4761 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
4762 else {
4763 cerr << "<APFloat(";
4764 CSDN->getValueAPF().convertToAPInt().dump();
4765 cerr << ")>";
4766 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00004767 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00004768 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00004769 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00004770 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00004771 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00004772 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004773 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00004774 else
Bill Wendling832171c2006-12-07 20:04:42 +00004775 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004776 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004777 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00004778 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004779 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004780 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00004781 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00004782 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00004783 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00004784 else
Bill Wendling832171c2006-12-07 20:04:42 +00004785 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00004786 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00004787 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00004788 else
Bill Wendling832171c2006-12-07 20:04:42 +00004789 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00004790 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004791 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004792 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
4793 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00004794 cerr << LBB->getName() << " ";
4795 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00004796 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00004797 if (G && R->getReg() &&
4798 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +00004799 cerr << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00004800 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00004801 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00004802 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004803 } else if (const ExternalSymbolSDNode *ES =
4804 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00004805 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004806 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
4807 if (M->getValue())
Dan Gohman69de1932008-02-06 22:27:42 +00004808 cerr << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004809 else
Dan Gohman69de1932008-02-06 22:27:42 +00004810 cerr << "<null>";
4811 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
4812 if (M->MO.getValue())
4813 cerr << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
4814 else
4815 cerr << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004816 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
4817 cerr << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00004818 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004819 cerr << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00004820 }
4821 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00004822 const Value *SrcValue = LD->getSrcValue();
4823 int SrcOffset = LD->getSrcValueOffset();
4824 cerr << " <";
4825 if (SrcValue)
4826 cerr << SrcValue;
4827 else
4828 cerr << "null";
4829 cerr << ":" << SrcOffset << ">";
4830
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004831 bool doExt = true;
4832 switch (LD->getExtensionType()) {
4833 default: doExt = false; break;
4834 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004835 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004836 break;
4837 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004838 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004839 break;
4840 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00004841 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004842 break;
4843 }
4844 if (doExt)
Duncan Sands83ec4b62008-06-06 12:08:01 +00004845 cerr << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00004846
Evan Cheng144d8f02006-11-09 17:55:04 +00004847 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00004848 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00004849 cerr << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00004850 if (LD->isVolatile())
4851 cerr << " <volatile>";
4852 cerr << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00004853 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00004854 const Value *SrcValue = ST->getSrcValue();
4855 int SrcOffset = ST->getSrcValueOffset();
4856 cerr << " <";
4857 if (SrcValue)
4858 cerr << SrcValue;
4859 else
4860 cerr << "null";
4861 cerr << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004862
4863 if (ST->isTruncatingStore())
4864 cerr << " <trunc "
Duncan Sands83ec4b62008-06-06 12:08:01 +00004865 << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00004866
4867 const char *AM = getIndexedModeName(ST->getAddressingMode());
4868 if (*AM)
4869 cerr << " " << AM;
4870 if (ST->isVolatile())
4871 cerr << " <volatile>";
4872 cerr << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00004873 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
4874 const Value *SrcValue = AT->getSrcValue();
4875 int SrcOffset = AT->getSrcValueOffset();
4876 cerr << " <";
4877 if (SrcValue)
4878 cerr << SrcValue;
4879 else
4880 cerr << "null";
4881 cerr << ":" << SrcOffset << ">";
4882 if (AT->isVolatile())
4883 cerr << " <volatile>";
4884 cerr << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004885 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00004886}
4887
Chris Lattnerde202b32005-11-09 23:47:37 +00004888static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004889 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4890 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004891 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004892 else
Bill Wendling832171c2006-12-07 20:04:42 +00004893 cerr << "\n" << std::string(indent+2, ' ')
4894 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004895
Chris Lattnerea946cd2005-01-09 20:38:33 +00004896
Bill Wendling832171c2006-12-07 20:04:42 +00004897 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004898 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004899}
4900
Chris Lattnerc3aae252005-01-07 07:46:32 +00004901void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00004902 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00004903 std::vector<const SDNode*> Nodes;
4904 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
4905 I != E; ++I)
4906 Nodes.push_back(I);
4907
Chris Lattner49d24712005-01-09 20:26:36 +00004908 std::sort(Nodes.begin(), Nodes.end());
4909
4910 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00004911 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004912 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004913 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00004914
Jim Laskey26f7fa72006-10-17 19:33:52 +00004915 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00004916
Bill Wendling832171c2006-12-07 20:04:42 +00004917 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004918}
4919
Evan Chengd6594ae2006-09-12 21:00:35 +00004920const Type *ConstantPoolSDNode::getType() const {
4921 if (isMachineConstantPoolEntry())
4922 return Val.MachineCPVal->getType();
4923 return Val.ConstVal->getType();
4924}