blob: bda9d934a41caaccd8fb6e35655e7744a0544121 [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"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Dan Gohman760f86f2009-01-22 21:58:43 +000030#include "llvm/Target/TargetOptions.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000033#include "llvm/Support/CommandLine.h"
Chris Lattner944fac72008-08-23 22:23:09 +000034#include "llvm/Support/MathExtras.h"
35#include "llvm/Support/raw_ostream.h"
Chris Lattner012f2412006-02-17 21:58:01 +000036#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000037#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000038#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000039#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000040#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000041#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000042#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000043using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000044
Chris Lattner0b3e5252006-08-15 19:11:05 +000045/// makeVTList - Return an instance of the SDVTList struct initialized with the
46/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000047static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000048 SDVTList Res = {VTs, NumVTs};
49 return Res;
50}
51
Duncan Sands83ec4b62008-06-06 12:08:01 +000052static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
53 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000054 default: assert(0 && "Unknown FP format");
55 case MVT::f32: return &APFloat::IEEEsingle;
56 case MVT::f64: return &APFloat::IEEEdouble;
57 case MVT::f80: return &APFloat::x87DoubleExtended;
58 case MVT::f128: return &APFloat::IEEEquad;
59 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
60 }
61}
62
Chris Lattnerf8dc0612008-02-03 06:49:24 +000063SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
64
Jim Laskey58b968b2005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
66// ConstantFPSDNode Class
67//===----------------------------------------------------------------------===//
68
69/// isExactlyValue - We don't rely on operator== working on double values, as
70/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
71/// As such, this method can be used to do an exact bit-for-bit comparison of
72/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000073bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000074 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000075}
76
Duncan Sands83ec4b62008-06-06 12:08:01 +000077bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000078 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000079 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000080
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000081 // PPC long double cannot be converted to any other type.
82 if (VT == MVT::ppcf128 ||
83 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000084 return false;
85
Dale Johannesenf04afdb2007-08-30 00:23:21 +000086 // convert modifies in place, so make a copy.
87 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000088 bool losesInfo;
89 (void) Val2.convert(*MVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
90 &losesInfo);
91 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000092}
93
Jim Laskey58b968b2005-08-17 20:08:02 +000094//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000095// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000096//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000097
Evan Chenga8df1662006-03-27 06:58:47 +000098/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000099/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +0000100bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000101 // Look through a bit convert.
102 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000103 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000104
Evan Chenga8df1662006-03-27 06:58:47 +0000105 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000106
107 unsigned i = 0, e = N->getNumOperands();
108
109 // Skip over all of the undef values.
110 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
111 ++i;
112
113 // Do not accept an all-undef vector.
114 if (i == e) return false;
115
116 // Do not accept build_vectors that aren't all constants or which have non-~0
117 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000118 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000119 if (isa<ConstantSDNode>(NotZero)) {
120 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
121 return false;
122 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000123 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen23a98552008-10-09 23:00:39 +0000124 bitcastToAPInt().isAllOnesValue())
Dan Gohman6c231502008-02-29 01:47:35 +0000125 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000126 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000127 return false;
128
129 // Okay, we have at least one ~0 value, check to see if the rest match or are
130 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000131 for (++i; i != e; ++i)
132 if (N->getOperand(i) != NotZero &&
133 N->getOperand(i).getOpcode() != ISD::UNDEF)
134 return false;
135 return true;
136}
137
138
Evan Cheng4a147842006-03-26 09:50:58 +0000139/// isBuildVectorAllZeros - Return true if the specified node is a
140/// BUILD_VECTOR where all of the elements are 0 or undef.
141bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000142 // Look through a bit convert.
143 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000144 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000145
Evan Cheng4a147842006-03-26 09:50:58 +0000146 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000147
148 unsigned i = 0, e = N->getNumOperands();
149
150 // Skip over all of the undef values.
151 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
152 ++i;
153
154 // Do not accept an all-undef vector.
155 if (i == e) return false;
156
157 // Do not accept build_vectors that aren't all constants or which have non-~0
158 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000159 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000160 if (isa<ConstantSDNode>(Zero)) {
161 if (!cast<ConstantSDNode>(Zero)->isNullValue())
162 return false;
163 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000164 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000165 return false;
166 } else
167 return false;
168
169 // Okay, we have at least one ~0 value, check to see if the rest match or are
170 // undefs.
171 for (++i; i != e; ++i)
172 if (N->getOperand(i) != Zero &&
173 N->getOperand(i).getOpcode() != ISD::UNDEF)
174 return false;
175 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000176}
177
Evan Chengefec7512008-02-18 23:04:32 +0000178/// isScalarToVector - Return true if the specified node is a
179/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
180/// element is not an undef.
181bool ISD::isScalarToVector(const SDNode *N) {
182 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
183 return true;
184
185 if (N->getOpcode() != ISD::BUILD_VECTOR)
186 return false;
187 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
188 return false;
189 unsigned NumElems = N->getNumOperands();
190 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000191 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000192 if (V.getOpcode() != ISD::UNDEF)
193 return false;
194 }
195 return true;
196}
197
198
Evan Chengbb81d972008-01-31 09:59:15 +0000199/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000200/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000201bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000202 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000203 if (N->getOpcode() == ISD::DBG_LABEL)
204 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000205 if (N->isMachineOpcode() &&
206 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000207 return true;
208 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000209}
210
Chris Lattnerc3aae252005-01-07 07:46:32 +0000211/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
212/// when given the operation for (X op Y).
213ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
214 // To perform this operation, we just need to swap the L and G bits of the
215 // operation.
216 unsigned OldL = (Operation >> 2) & 1;
217 unsigned OldG = (Operation >> 1) & 1;
218 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
219 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000220 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000221}
222
223/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
224/// 'op' is a valid SetCC operation.
225ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
226 unsigned Operation = Op;
227 if (isInteger)
228 Operation ^= 7; // Flip L, G, E bits, but not U.
229 else
230 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000231
Chris Lattnerc3aae252005-01-07 07:46:32 +0000232 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000233 Operation &= ~8; // Don't let N and U bits get set.
234
Chris Lattnerc3aae252005-01-07 07:46:32 +0000235 return ISD::CondCode(Operation);
236}
237
238
239/// isSignedOp - For an integer comparison, return 1 if the comparison is a
240/// signed operation and 2 if the result is an unsigned comparison. Return zero
241/// if the operation does not depend on the sign of the input (setne and seteq).
242static int isSignedOp(ISD::CondCode Opcode) {
243 switch (Opcode) {
244 default: assert(0 && "Illegal integer setcc operation!");
245 case ISD::SETEQ:
246 case ISD::SETNE: return 0;
247 case ISD::SETLT:
248 case ISD::SETLE:
249 case ISD::SETGT:
250 case ISD::SETGE: return 1;
251 case ISD::SETULT:
252 case ISD::SETULE:
253 case ISD::SETUGT:
254 case ISD::SETUGE: return 2;
255 }
256}
257
258/// getSetCCOrOperation - Return the result of a logical OR between different
259/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
260/// returns SETCC_INVALID if it is not possible to represent the resultant
261/// comparison.
262ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
263 bool isInteger) {
264 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
265 // Cannot fold a signed integer setcc with an unsigned integer setcc.
266 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000267
Chris Lattnerc3aae252005-01-07 07:46:32 +0000268 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000269
Chris Lattnerc3aae252005-01-07 07:46:32 +0000270 // If the N and U bits get set then the resultant comparison DOES suddenly
271 // care about orderedness, and is true when ordered.
272 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000273 Op &= ~16; // Clear the U bit if the N bit is set.
274
275 // Canonicalize illegal integer setcc's.
276 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
277 Op = ISD::SETNE;
278
Chris Lattnerc3aae252005-01-07 07:46:32 +0000279 return ISD::CondCode(Op);
280}
281
282/// getSetCCAndOperation - Return the result of a logical AND between different
283/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
284/// function returns zero if it is not possible to represent the resultant
285/// comparison.
286ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
287 bool isInteger) {
288 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
289 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000290 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000291
292 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000293 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
294
295 // Canonicalize illegal integer setcc's.
296 if (isInteger) {
297 switch (Result) {
298 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000299 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000300 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000301 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
302 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
303 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000304 }
305 }
306
307 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000308}
309
Chris Lattnerb48da392005-01-23 04:39:44 +0000310const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000311 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000312}
313
Jim Laskey58b968b2005-08-17 20:08:02 +0000314//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000315// SDNode Profile Support
316//===----------------------------------------------------------------------===//
317
Jim Laskeydef69b92006-10-27 23:52:51 +0000318/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
319///
Jim Laskey583bd472006-10-27 23:46:08 +0000320static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
321 ID.AddInteger(OpC);
322}
323
324/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
325/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000326static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000327 ID.AddPointer(VTList.VTs);
328}
329
Jim Laskeydef69b92006-10-27 23:52:51 +0000330/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
331///
Jim Laskey583bd472006-10-27 23:46:08 +0000332static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000333 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000334 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000335 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000336 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000337 }
Jim Laskey583bd472006-10-27 23:46:08 +0000338}
339
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000340/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
341///
342static void AddNodeIDOperands(FoldingSetNodeID &ID,
343 const SDUse *Ops, unsigned NumOps) {
344 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000345 ID.AddPointer(Ops->getNode());
346 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000347 }
348}
349
Jim Laskey583bd472006-10-27 23:46:08 +0000350static void AddNodeIDNode(FoldingSetNodeID &ID,
351 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000352 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000353 AddNodeIDOpcode(ID, OpC);
354 AddNodeIDValueTypes(ID, VTList);
355 AddNodeIDOperands(ID, OpList, N);
356}
357
Duncan Sands0dc40452008-10-27 15:30:53 +0000358/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
359/// the NodeID data.
360static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000361 switch (N->getOpcode()) {
362 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000363 case ISD::ARG_FLAGS:
364 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
365 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 case ISD::TargetConstant:
367 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000368 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 break;
370 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000371 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000372 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000374 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000376 case ISD::GlobalAddress:
377 case ISD::TargetGlobalTLSAddress:
378 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000379 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 ID.AddPointer(GA->getGlobal());
381 ID.AddInteger(GA->getOffset());
382 break;
383 }
384 case ISD::BasicBlock:
385 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
386 break;
387 case ISD::Register:
388 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
389 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000390 case ISD::DBG_STOPPOINT: {
391 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
392 ID.AddInteger(DSP->getLine());
393 ID.AddInteger(DSP->getColumn());
394 ID.AddPointer(DSP->getCompileUnit());
395 break;
396 }
Dan Gohman69de1932008-02-06 22:27:42 +0000397 case ISD::SRCVALUE:
398 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
399 break;
400 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000401 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000402 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000403 break;
404 }
405 case ISD::FrameIndex:
406 case ISD::TargetFrameIndex:
407 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
408 break;
409 case ISD::JumpTable:
410 case ISD::TargetJumpTable:
411 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
412 break;
413 case ISD::ConstantPool:
414 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000415 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000416 ID.AddInteger(CP->getAlignment());
417 ID.AddInteger(CP->getOffset());
418 if (CP->isMachineConstantPoolEntry())
419 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
420 else
421 ID.AddPointer(CP->getConstVal());
422 break;
423 }
Dan Gohman5eb0cec2008-09-15 19:46:03 +0000424 case ISD::CALL: {
425 const CallSDNode *Call = cast<CallSDNode>(N);
426 ID.AddInteger(Call->getCallingConv());
427 ID.AddInteger(Call->isVarArg());
428 break;
429 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000430 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 ID.AddInteger(LD->getAddressingMode());
433 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000434 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000435 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 break;
437 }
438 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000439 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 ID.AddInteger(ST->getAddressingMode());
441 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000442 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000443 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000444 break;
445 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000446 case ISD::ATOMIC_CMP_SWAP:
447 case ISD::ATOMIC_SWAP:
448 case ISD::ATOMIC_LOAD_ADD:
449 case ISD::ATOMIC_LOAD_SUB:
450 case ISD::ATOMIC_LOAD_AND:
451 case ISD::ATOMIC_LOAD_OR:
452 case ISD::ATOMIC_LOAD_XOR:
453 case ISD::ATOMIC_LOAD_NAND:
454 case ISD::ATOMIC_LOAD_MIN:
455 case ISD::ATOMIC_LOAD_MAX:
456 case ISD::ATOMIC_LOAD_UMIN:
457 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000458 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
459 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000460 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000461 }
Mon P Wang28873102008-06-25 08:15:39 +0000462 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000463}
464
Duncan Sands0dc40452008-10-27 15:30:53 +0000465/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
466/// data.
467static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
468 AddNodeIDOpcode(ID, N->getOpcode());
469 // Add the return value info.
470 AddNodeIDValueTypes(ID, N->getVTList());
471 // Add the operand info.
472 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
473
474 // Handle SDNode leafs with special info.
475 AddNodeIDCustom(ID, N);
476}
477
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000478/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
479/// the CSE map that carries both alignment and volatility information.
480///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000481static inline unsigned
482encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000483 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
484}
485
Jim Laskey583bd472006-10-27 23:46:08 +0000486//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000487// SelectionDAG Class
488//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000489
Duncan Sands0dc40452008-10-27 15:30:53 +0000490/// doNotCSE - Return true if CSE should not be performed for this node.
491static bool doNotCSE(SDNode *N) {
492 if (N->getValueType(0) == MVT::Flag)
493 return true; // Never CSE anything that produces a flag.
494
495 switch (N->getOpcode()) {
496 default: break;
497 case ISD::HANDLENODE:
498 case ISD::DBG_LABEL:
499 case ISD::DBG_STOPPOINT:
500 case ISD::EH_LABEL:
501 case ISD::DECLARE:
502 return true; // Never CSE these nodes.
503 }
504
505 // Check that remaining values produced are not flags.
506 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
507 if (N->getValueType(i) == MVT::Flag)
508 return true; // Never CSE anything that produces a flag.
509
510 return false;
511}
512
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000513/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000514/// SelectionDAG.
515void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000516 // Create a dummy node (which is not added to allnodes), that adds a reference
517 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000518 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000519
Chris Lattner190a4182006-08-04 17:45:20 +0000520 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000521
Chris Lattner190a4182006-08-04 17:45:20 +0000522 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000523 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000524 if (I->use_empty())
525 DeadNodes.push_back(I);
526
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000527 RemoveDeadNodes(DeadNodes);
528
529 // If the root changed (e.g. it was a dead load, update the root).
530 setRoot(Dummy.getValue());
531}
532
533/// RemoveDeadNodes - This method deletes the unreachable nodes in the
534/// given list, and any nodes that become unreachable as a result.
535void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
536 DAGUpdateListener *UpdateListener) {
537
Chris Lattner190a4182006-08-04 17:45:20 +0000538 // Process the worklist, deleting the nodes and adding their uses to the
539 // worklist.
540 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000541 SDNode *N = DeadNodes.pop_back_val();
Chris Lattner190a4182006-08-04 17:45:20 +0000542
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000543 if (UpdateListener)
544 UpdateListener->NodeDeleted(N, 0);
545
Chris Lattner190a4182006-08-04 17:45:20 +0000546 // Take the node out of the appropriate CSE map.
547 RemoveNodeFromCSEMaps(N);
548
549 // Next, brutally remove the operand list. This is safe to do, as there are
550 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000551 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
552 SDUse &Use = *I++;
553 SDNode *Operand = Use.getNode();
554 Use.set(SDValue());
555
Chris Lattner190a4182006-08-04 17:45:20 +0000556 // Now that we removed this operand, see if there are no uses of it left.
557 if (Operand->use_empty())
558 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000559 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000560
Dan Gohmanc5336122009-01-19 22:39:36 +0000561 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000562 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000563}
564
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000565void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000566 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000567 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000568}
569
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000570void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000571 // First take this out of the appropriate CSE map.
572 RemoveNodeFromCSEMaps(N);
573
Chris Lattner1e111c72005-09-07 05:37:01 +0000574 // Finally, remove uses due to operands of this node, remove from the
575 // AllNodes list, and delete the node.
576 DeleteNodeNotInCSEMaps(N);
577}
578
579void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000580 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
581 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000582
Dan Gohmanf06c8352008-09-30 18:30:35 +0000583 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000584 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000585
Dan Gohmanc5336122009-01-19 22:39:36 +0000586 DeallocateNode(N);
587}
588
589void SelectionDAG::DeallocateNode(SDNode *N) {
590 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000591 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000592
Dan Gohmanc5336122009-01-19 22:39:36 +0000593 // Set the opcode to DELETED_NODE to help catch bugs when node
594 // memory is reallocated.
595 N->NodeType = ISD::DELETED_NODE;
596
Dan Gohman11467282008-08-26 01:44:34 +0000597 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000598}
599
Chris Lattner149c58c2005-08-16 18:17:10 +0000600/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
601/// correspond to it. This is useful when we're about to delete or repurpose
602/// the node. We don't want future request for structurally identical nodes
603/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000604bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000605 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000606 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000607 case ISD::EntryToken:
608 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000609 return false;
610 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000611 case ISD::CONDCODE:
612 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
613 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000614 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000615 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
616 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000617 case ISD::ExternalSymbol:
618 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000619 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000620 case ISD::TargetExternalSymbol:
621 Erased =
622 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000623 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000624 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000625 MVT VT = cast<VTSDNode>(N)->getVT();
626 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000627 Erased = ExtendedValueTypeNodes.erase(VT);
628 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000629 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
630 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000631 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000632 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000633 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000634 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000635 // Remove it from the CSE Map.
636 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000637 break;
638 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000639#ifndef NDEBUG
640 // Verify that the node was actually in one of the CSE maps, unless it has a
641 // flag result (which cannot be CSE'd) or is one of the special cases that are
642 // not subject to CSE.
643 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000644 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000645 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000646 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000647 assert(0 && "Node is not in map!");
648 }
649#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000650 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000651}
652
Dan Gohman39946102009-01-25 16:29:12 +0000653/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
654/// maps and modified in place. Add it back to the CSE maps, unless an identical
655/// node already exists, in which case transfer all its users to the existing
656/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000657///
Dan Gohman39946102009-01-25 16:29:12 +0000658void
659SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
660 DAGUpdateListener *UpdateListener) {
661 // For node types that aren't CSE'd, just act as if no identical node
662 // already exists.
663 if (!doNotCSE(N)) {
664 SDNode *Existing = CSEMap.GetOrInsertNode(N);
665 if (Existing != N) {
666 // If there was already an existing matching node, use ReplaceAllUsesWith
667 // to replace the dead one with the existing one. This can cause
668 // recursive merging of other unrelated nodes down the line.
669 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Cheng71e86852008-07-08 20:06:39 +0000670
Dan Gohman39946102009-01-25 16:29:12 +0000671 // N is now dead. Inform the listener if it exists and delete it.
672 if (UpdateListener)
673 UpdateListener->NodeDeleted(N, Existing);
674 DeleteNodeNotInCSEMaps(N);
675 return;
676 }
677 }
Evan Cheng71e86852008-07-08 20:06:39 +0000678
Dan Gohman39946102009-01-25 16:29:12 +0000679 // If the node doesn't already exist, we updated it. Inform a listener if
680 // it exists.
681 if (UpdateListener)
682 UpdateListener->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000683}
684
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000685/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
686/// were replaced with those specified. If this node is never memoized,
687/// return null, otherwise return a pointer to the slot it would take. If a
688/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000689SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000690 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000691 if (doNotCSE(N))
692 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000693
Dan Gohman475871a2008-07-27 21:46:04 +0000694 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000695 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000696 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000697 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000698 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000699}
700
701/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
702/// were replaced with those specified. If this node is never memoized,
703/// return null, otherwise return a pointer to the slot it would take. If a
704/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000705SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000706 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000707 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000708 if (doNotCSE(N))
709 return 0;
710
Dan Gohman475871a2008-07-27 21:46:04 +0000711 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000712 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000713 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000714 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000715 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
716}
717
718
719/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
720/// were replaced with those specified. If this node is never memoized,
721/// return null, otherwise return a pointer to the slot it would take. If a
722/// node already exists with these operands, the slot will be non-null.
723SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000724 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000725 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000726 if (doNotCSE(N))
727 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000728
Jim Laskey583bd472006-10-27 23:46:08 +0000729 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000730 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000731 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000732 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000733}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000734
Duncan Sandsd038e042008-07-21 10:20:31 +0000735/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
736void SelectionDAG::VerifyNode(SDNode *N) {
737 switch (N->getOpcode()) {
738 default:
739 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000740 case ISD::BUILD_PAIR: {
741 MVT VT = N->getValueType(0);
742 assert(N->getNumValues() == 1 && "Too many results!");
743 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
744 "Wrong return type!");
745 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
746 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
747 "Mismatched operand types!");
748 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
749 "Wrong operand type!");
750 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
751 "Wrong return type size");
752 break;
753 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000754 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000755 assert(N->getNumValues() == 1 && "Too many results!");
756 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000757 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000758 "Wrong number of operands!");
Duncan Sands0954aef2008-10-22 09:00:33 +0000759 // FIXME: Change vector_shuffle to a variadic node with mask elements being
760 // operands of the node. Currently the mask is a BUILD_VECTOR passed as an
761 // operand, and it is not always possible to legalize it. Turning off the
762 // following checks at least makes it possible to legalize most of the time.
763// MVT EltVT = N->getValueType(0).getVectorElementType();
764// for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohmane7852d02009-01-26 04:35:06 +0000765// assert(I->getValueType() == EltVT &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000766// "Wrong operand type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000767 break;
768 }
769 }
770}
771
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000772/// getMVTAlignment - Compute the default alignment value for the
773/// given type.
774///
775unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
776 const Type *Ty = VT == MVT::iPTR ?
777 PointerType::get(Type::Int8Ty, 0) :
778 VT.getTypeForMVT();
779
780 return TLI.getTargetData()->getABITypeAlignment(Ty);
781}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000782
Dan Gohman7c3234c2008-08-27 23:52:12 +0000783SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
784 : TLI(tli), FLI(fli),
785 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000786 Root(getEntryNode()) {
787 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000788}
789
Devang Patel6e7a1612009-01-09 19:11:50 +0000790void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
791 DwarfWriter *dw) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000792 MF = &mf;
793 MMI = mmi;
Devang Patel6e7a1612009-01-09 19:11:50 +0000794 DW = dw;
Dan Gohman7c3234c2008-08-27 23:52:12 +0000795}
796
Chris Lattner78ec3112003-08-11 14:57:33 +0000797SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000798 allnodes_clear();
799}
800
801void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000802 assert(&*AllNodes.begin() == &EntryNode);
803 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000804 while (!AllNodes.empty())
805 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000806}
807
Dan Gohman7c3234c2008-08-27 23:52:12 +0000808void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000809 allnodes_clear();
810 OperandAllocator.Reset();
811 CSEMap.clear();
812
813 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000814 ExternalSymbols.clear();
815 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000816 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
817 static_cast<CondCodeSDNode*>(0));
818 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
819 static_cast<SDNode*>(0));
820
Dan Gohmane7852d02009-01-26 04:35:06 +0000821 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000822 AllNodes.push_back(&EntryNode);
823 Root = getEntryNode();
824}
825
Dan Gohman475871a2008-07-27 21:46:04 +0000826SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000827 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000828 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000829 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000830 return getNode(ISD::AND, Op.getValueType(), Op,
831 getConstant(Imm, Op.getValueType()));
832}
833
Bill Wendling6ce610f2009-01-30 22:23:15 +0000834SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, MVT VT) {
835 if (Op.getValueType() == VT) return Op;
836 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
837 VT.getSizeInBits());
838 return getNode(ISD::AND, DL, Op.getValueType(), Op,
839 getConstant(Imm, Op.getValueType()));
840}
841
Bob Wilson4c245462009-01-22 17:39:32 +0000842/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
843///
Bill Wendling41b9d272009-01-30 22:11:22 +0000844SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, MVT VT) {
845 SDValue NegOne;
846 if (VT.isVector()) {
847 MVT EltVT = VT.getVectorElementType();
848 SDValue NegOneElt = getConstant(EltVT.getIntegerVTBitMask(), EltVT);
849 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOneElt);
850 NegOne = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(), VT,
851 &NegOnes[0], NegOnes.size());
852 } else {
853 NegOne = getConstant(VT.getIntegerVTBitMask(), VT);
854 }
855
856 return getNode(ISD::XOR, DL, VT, Val, NegOne);
857}
858
Dan Gohman475871a2008-07-27 21:46:04 +0000859SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000860 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmance9bc122009-01-27 20:39:34 +0000861 assert((EltVT.getSizeInBits() >= 64 ||
862 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
863 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000864 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000865}
866
Dan Gohman475871a2008-07-27 21:46:04 +0000867SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000868 return getConstant(*ConstantInt::get(Val), VT, isT);
869}
870
871SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000873
Evan Cheng0ff39b32008-06-30 07:31:25 +0000874 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000875 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000876 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000877
878 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000879 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000880 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000881 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000882 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000883 SDNode *N = NULL;
884 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000885 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000886 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000887 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000888 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000889 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000890 CSEMap.InsertNode(N, IP);
891 AllNodes.push_back(N);
892 }
893
Dan Gohman475871a2008-07-27 21:46:04 +0000894 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000895 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000896 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000897 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000898 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
899 }
900 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000901}
902
Dan Gohman475871a2008-07-27 21:46:04 +0000903SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000904 return getConstant(Val, TLI.getPointerTy(), isTarget);
905}
906
907
Dan Gohman475871a2008-07-27 21:46:04 +0000908SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000909 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
910}
911
912SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000913 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000914
Duncan Sands83ec4b62008-06-06 12:08:01 +0000915 MVT EltVT =
916 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000917
Chris Lattnerd8658612005-02-17 20:17:32 +0000918 // Do the map lookup using the actual bit pattern for the floating point
919 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
920 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000921 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000922 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000923 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000924 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000925 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000926 SDNode *N = NULL;
927 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000928 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000929 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000930 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000931 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000932 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000933 CSEMap.InsertNode(N, IP);
934 AllNodes.push_back(N);
935 }
936
Dan Gohman475871a2008-07-27 21:46:04 +0000937 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000938 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000939 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000940 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000941 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
942 }
943 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000944}
945
Dan Gohman475871a2008-07-27 21:46:04 +0000946SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000947 MVT EltVT =
948 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000949 if (EltVT==MVT::f32)
950 return getConstantFP(APFloat((float)Val), VT, isTarget);
951 else
952 return getConstantFP(APFloat(Val), VT, isTarget);
953}
954
Dan Gohman475871a2008-07-27 21:46:04 +0000955SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Dan Gohman6520e202008-10-18 02:06:02 +0000956 MVT VT, int64_t Offset,
Dan Gohman475871a2008-07-27 21:46:04 +0000957 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000958 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000959
Dan Gohman6520e202008-10-18 02:06:02 +0000960 // Truncate (with sign-extension) the offset value to the pointer size.
Dan Gohman44013612008-10-21 03:38:42 +0000961 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +0000962 if (BitWidth < 64)
963 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
964
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000965 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
966 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000967 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000968 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000969 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000970 }
971
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000972 if (GVar && GVar->isThreadLocal())
973 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
974 else
975 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000976
Jim Laskey583bd472006-10-27 23:46:08 +0000977 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000978 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000979 ID.AddPointer(GV);
980 ID.AddInteger(Offset);
981 void *IP = 0;
982 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +0000983 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000984 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000985 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000986 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000988 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989}
990
Dan Gohman475871a2008-07-27 21:46:04 +0000991SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000992 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000993 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000994 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000995 ID.AddInteger(FI);
996 void *IP = 0;
997 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000998 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000999 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001000 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001001 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001002 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001003 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001004}
1005
Dan Gohman475871a2008-07-27 21:46:04 +00001006SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001007 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001008 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001009 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001010 ID.AddInteger(JTI);
1011 void *IP = 0;
1012 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001013 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001014 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001015 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001016 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001017 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001018 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001019}
1020
Dan Gohman475871a2008-07-27 21:46:04 +00001021SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1022 unsigned Alignment, int Offset,
1023 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +00001024 if (Alignment == 0)
1025 Alignment =
1026 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001027 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001028 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001029 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001030 ID.AddInteger(Alignment);
1031 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001032 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001033 void *IP = 0;
1034 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001035 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001036 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001037 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001038 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001039 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001040 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001041}
1042
Chris Lattnerc3aae252005-01-07 07:46:32 +00001043
Dan Gohman475871a2008-07-27 21:46:04 +00001044SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1045 unsigned Alignment, int Offset,
1046 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +00001047 if (Alignment == 0)
1048 Alignment =
1049 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001050 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001051 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001052 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001053 ID.AddInteger(Alignment);
1054 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001055 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001056 void *IP = 0;
1057 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001058 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001059 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001060 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001061 CSEMap.InsertNode(N, IP);
1062 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001063 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001064}
1065
1066
Dan Gohman475871a2008-07-27 21:46:04 +00001067SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001068 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001069 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001070 ID.AddPointer(MBB);
1071 void *IP = 0;
1072 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001073 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001074 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001075 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001076 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001078 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001079}
1080
Dale Johannesene8c17332009-01-29 00:47:48 +00001081SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl) {
1082 FoldingSetNodeID ID;
1083 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
1084 ID.AddPointer(MBB);
1085 void *IP = 0;
1086 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1087 return SDValue(E, 0);
1088 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
1089 new (N) BasicBlockSDNode(MBB, dl);
1090 CSEMap.InsertNode(N, IP);
1091 AllNodes.push_back(N);
1092 return SDValue(N, 0);
1093}
1094
Dan Gohman475871a2008-07-27 21:46:04 +00001095SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001096 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001097 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001098 ID.AddInteger(Flags.getRawBits());
1099 void *IP = 0;
1100 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001101 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001102 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001103 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001104 CSEMap.InsertNode(N, IP);
1105 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001106 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001107}
1108
Dan Gohman475871a2008-07-27 21:46:04 +00001109SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001110 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1111 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001112
Duncan Sands83ec4b62008-06-06 12:08:01 +00001113 SDNode *&N = VT.isExtended() ?
1114 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001115
Dan Gohman475871a2008-07-27 21:46:04 +00001116 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001117 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001118 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001119 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001120 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001121}
1122
Bill Wendling056292f2008-09-16 21:48:12 +00001123SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1124 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001125 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001126 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1127 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001128 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001129 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001130}
1131
Dale Johannesene8c17332009-01-29 00:47:48 +00001132SDValue SelectionDAG::getExternalSymbol(const char *Sym, DebugLoc dl, MVT VT) {
1133 SDNode *&N = ExternalSymbols[Sym];
1134 if (N) return SDValue(N, 0);
1135 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1136 new (N) ExternalSymbolSDNode(false, dl, Sym, VT);
1137 AllNodes.push_back(N);
1138 return SDValue(N, 0);
1139}
1140
Bill Wendling056292f2008-09-16 21:48:12 +00001141SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
1142 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001143 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001144 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1145 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001146 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001147 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001148}
1149
Dale Johannesene8c17332009-01-29 00:47:48 +00001150SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, DebugLoc dl,
1151 MVT VT) {
1152 SDNode *&N = TargetExternalSymbols[Sym];
1153 if (N) return SDValue(N, 0);
1154 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1155 new (N) ExternalSymbolSDNode(true, dl, Sym, VT);
1156 AllNodes.push_back(N);
1157 return SDValue(N, 0);
1158}
1159
Dan Gohman475871a2008-07-27 21:46:04 +00001160SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001161 if ((unsigned)Cond >= CondCodeNodes.size())
1162 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001163
Chris Lattner079a27a2005-08-09 20:40:02 +00001164 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001165 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001166 new (N) CondCodeSDNode(Cond);
1167 CondCodeNodes[Cond] = N;
1168 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001169 }
Dan Gohman475871a2008-07-27 21:46:04 +00001170 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001171}
1172
Mon P Wang77cdf302008-11-10 20:54:11 +00001173SDValue SelectionDAG::getConvertRndSat(MVT VT, SDValue Val, SDValue DTy,
1174 SDValue STy, SDValue Rnd, SDValue Sat,
1175 ISD::CvtCode Code) {
Mon P Wang6db08c42008-12-11 03:30:13 +00001176 // If the src and dest types are the same, no conversion is necessary.
1177 if (DTy == STy)
1178 return Val;
1179
Mon P Wang77cdf302008-11-10 20:54:11 +00001180 FoldingSetNodeID ID;
1181 void* IP = 0;
1182 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1183 return SDValue(E, 0);
1184 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1185 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1186 new (N) CvtRndSatSDNode(VT, Ops, 5, Code);
1187 CSEMap.InsertNode(N, IP);
1188 AllNodes.push_back(N);
1189 return SDValue(N, 0);
1190}
1191
Dan Gohman475871a2008-07-27 21:46:04 +00001192SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001193 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001194 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001195 ID.AddInteger(RegNo);
1196 void *IP = 0;
1197 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001198 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001199 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001200 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001201 CSEMap.InsertNode(N, IP);
1202 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001203 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001204}
1205
Dan Gohman475871a2008-07-27 21:46:04 +00001206SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Devang Patel83489bb2009-01-13 00:35:13 +00001207 unsigned Line, unsigned Col,
1208 Value *CU) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001209 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001210 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001211 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001212 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001213}
1214
Dan Gohman475871a2008-07-27 21:46:04 +00001215SDValue SelectionDAG::getLabel(unsigned Opcode,
1216 SDValue Root,
1217 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001218 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001219 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001220 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1221 ID.AddInteger(LabelID);
1222 void *IP = 0;
1223 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001224 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001225 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001226 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001227 CSEMap.InsertNode(N, IP);
1228 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001229 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001230}
1231
Dale Johannesene8c17332009-01-29 00:47:48 +00001232SDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl,
1233 SDValue Root,
1234 unsigned LabelID) {
1235 FoldingSetNodeID ID;
1236 SDValue Ops[] = { Root };
1237 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1238 ID.AddInteger(LabelID);
1239 void *IP = 0;
1240 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1241 return SDValue(E, 0);
1242 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
1243 new (N) LabelSDNode(Opcode, dl, Root, LabelID);
1244 CSEMap.InsertNode(N, IP);
1245 AllNodes.push_back(N);
1246 return SDValue(N, 0);
1247}
1248
Dan Gohman475871a2008-07-27 21:46:04 +00001249SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001250 assert((!V || isa<PointerType>(V->getType())) &&
1251 "SrcValue is not a pointer?");
1252
Jim Laskey583bd472006-10-27 23:46:08 +00001253 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001254 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001255 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001256
Chris Lattner61b09412006-08-11 21:01:22 +00001257 void *IP = 0;
1258 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001259 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001260
Dan Gohmanfed90b62008-07-28 21:51:04 +00001261 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001262 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001263 CSEMap.InsertNode(N, IP);
1264 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001265 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001266}
1267
Dan Gohman475871a2008-07-27 21:46:04 +00001268SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Cheng24ac4082008-11-24 07:09:49 +00001269#ifndef NDEBUG
Dan Gohman69de1932008-02-06 22:27:42 +00001270 const Value *v = MO.getValue();
1271 assert((!v || isa<PointerType>(v->getType())) &&
1272 "SrcValue is not a pointer?");
Evan Cheng24ac4082008-11-24 07:09:49 +00001273#endif
Dan Gohman69de1932008-02-06 22:27:42 +00001274
1275 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001276 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001277 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001278
1279 void *IP = 0;
1280 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001281 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001282
Dan Gohmanfed90b62008-07-28 21:51:04 +00001283 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001284 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001285 CSEMap.InsertNode(N, IP);
1286 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001287 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001288}
1289
Chris Lattner37ce9df2007-10-15 17:47:20 +00001290/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1291/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001292SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001293 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands9304f2c2008-12-04 18:08:40 +00001294 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001295 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001296 unsigned StackAlign =
1297 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1298
Chris Lattner37ce9df2007-10-15 17:47:20 +00001299 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1300 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1301}
1302
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001303/// CreateStackTemporary - Create a stack temporary suitable for holding
1304/// either of the specified value types.
1305SDValue SelectionDAG::CreateStackTemporary(MVT VT1, MVT VT2) {
1306 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1307 VT2.getStoreSizeInBits())/8;
1308 const Type *Ty1 = VT1.getTypeForMVT();
1309 const Type *Ty2 = VT2.getTypeForMVT();
1310 const TargetData *TD = TLI.getTargetData();
1311 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1312 TD->getPrefTypeAlignment(Ty2));
1313
1314 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1315 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1316 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1317}
1318
Dan Gohman475871a2008-07-27 21:46:04 +00001319SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1320 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 // These setcc operations always fold.
1322 switch (Cond) {
1323 default: break;
1324 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001325 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001326 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001327 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001328
1329 case ISD::SETOEQ:
1330 case ISD::SETOGT:
1331 case ISD::SETOGE:
1332 case ISD::SETOLT:
1333 case ISD::SETOLE:
1334 case ISD::SETONE:
1335 case ISD::SETO:
1336 case ISD::SETUO:
1337 case ISD::SETUEQ:
1338 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001339 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001340 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001341 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001342
Gabor Greifba36cb52008-08-28 21:40:38 +00001343 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001344 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001345 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001346 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001347
Chris Lattnerc3aae252005-01-07 07:46:32 +00001348 switch (Cond) {
1349 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001350 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1351 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001352 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1353 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1354 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1355 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1356 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1357 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1358 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1359 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001360 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 }
Chris Lattner67255a12005-04-07 18:14:58 +00001362 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001363 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1364 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001365 // No compile time operations on this type yet.
1366 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001367 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001368
1369 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001370 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001371 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001372 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1373 return getNode(ISD::UNDEF, VT);
1374 // fall through
1375 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1376 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1377 return getNode(ISD::UNDEF, VT);
1378 // fall through
1379 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001380 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001381 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1382 return getNode(ISD::UNDEF, VT);
1383 // fall through
1384 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1385 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1386 return getNode(ISD::UNDEF, VT);
1387 // fall through
1388 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1389 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1390 return getNode(ISD::UNDEF, VT);
1391 // fall through
1392 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001393 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001394 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1395 return getNode(ISD::UNDEF, VT);
1396 // fall through
1397 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001398 R==APFloat::cmpEqual, VT);
1399 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1400 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1401 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1402 R==APFloat::cmpEqual, VT);
1403 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1404 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1405 R==APFloat::cmpLessThan, VT);
1406 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1407 R==APFloat::cmpUnordered, VT);
1408 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1409 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001410 }
1411 } else {
1412 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001413 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001414 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001415 }
1416
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001417 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001418 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001419}
1420
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001421/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1422/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001423bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001424 unsigned BitWidth = Op.getValueSizeInBits();
1425 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1426}
1427
Dan Gohmanea859be2007-06-22 14:59:07 +00001428/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1429/// this predicate to simplify operations downstream. Mask is known to be zero
1430/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001431bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001432 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001433 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001434 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1435 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1436 return (KnownZero & Mask) == Mask;
1437}
1438
1439/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1440/// known to be either zero or one and return them in the KnownZero/KnownOne
1441/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1442/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001443void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001444 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001445 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001446 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001447 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001448 "Mask size mismatches value type size!");
1449
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001450 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001451 if (Depth == 6 || Mask == 0)
1452 return; // Limit search depth.
1453
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001454 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001455
1456 switch (Op.getOpcode()) {
1457 case ISD::Constant:
1458 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001459 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001460 KnownZero = ~KnownOne & Mask;
1461 return;
1462 case ISD::AND:
1463 // If either the LHS or the RHS are Zero, the result is zero.
1464 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001465 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1466 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001467 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1468 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1469
1470 // Output known-1 bits are only known if set in both the LHS & RHS.
1471 KnownOne &= KnownOne2;
1472 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1473 KnownZero |= KnownZero2;
1474 return;
1475 case ISD::OR:
1476 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001477 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1478 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001479 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1480 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1481
1482 // Output known-0 bits are only known if clear in both the LHS & RHS.
1483 KnownZero &= KnownZero2;
1484 // Output known-1 are known to be set if set in either the LHS | RHS.
1485 KnownOne |= KnownOne2;
1486 return;
1487 case ISD::XOR: {
1488 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1489 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1490 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1491 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1492
1493 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001494 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001495 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1496 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1497 KnownZero = KnownZeroOut;
1498 return;
1499 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001500 case ISD::MUL: {
1501 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1502 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1503 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1504 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1505 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1506
1507 // If low bits are zero in either operand, output low known-0 bits.
1508 // Also compute a conserative estimate for high known-0 bits.
1509 // More trickiness is possible, but this is sufficient for the
1510 // interesting case of alignment computation.
1511 KnownOne.clear();
1512 unsigned TrailZ = KnownZero.countTrailingOnes() +
1513 KnownZero2.countTrailingOnes();
1514 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001515 KnownZero2.countLeadingOnes(),
1516 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001517
1518 TrailZ = std::min(TrailZ, BitWidth);
1519 LeadZ = std::min(LeadZ, BitWidth);
1520 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1521 APInt::getHighBitsSet(BitWidth, LeadZ);
1522 KnownZero &= Mask;
1523 return;
1524 }
1525 case ISD::UDIV: {
1526 // For the purposes of computing leading zeros we can conservatively
1527 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001528 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001529 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1530 ComputeMaskedBits(Op.getOperand(0),
1531 AllOnes, KnownZero2, KnownOne2, Depth+1);
1532 unsigned LeadZ = KnownZero2.countLeadingOnes();
1533
1534 KnownOne2.clear();
1535 KnownZero2.clear();
1536 ComputeMaskedBits(Op.getOperand(1),
1537 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001538 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1539 if (RHSUnknownLeadingOnes != BitWidth)
1540 LeadZ = std::min(BitWidth,
1541 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001542
1543 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1544 return;
1545 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001546 case ISD::SELECT:
1547 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1548 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1549 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1550 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1551
1552 // Only known if known in both the LHS and RHS.
1553 KnownOne &= KnownOne2;
1554 KnownZero &= KnownZero2;
1555 return;
1556 case ISD::SELECT_CC:
1557 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1558 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1559 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1560 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1561
1562 // Only known if known in both the LHS and RHS.
1563 KnownOne &= KnownOne2;
1564 KnownZero &= KnownZero2;
1565 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001566 case ISD::SADDO:
1567 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001568 case ISD::SSUBO:
1569 case ISD::USUBO:
1570 case ISD::SMULO:
1571 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001572 if (Op.getResNo() != 1)
1573 return;
Duncan Sands03228082008-11-23 15:47:28 +00001574 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001575 case ISD::SETCC:
1576 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands03228082008-11-23 15:47:28 +00001577 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001578 BitWidth > 1)
1579 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001580 return;
1581 case ISD::SHL:
1582 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1583 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001584 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001585
1586 // If the shift count is an invalid immediate, don't do anything.
1587 if (ShAmt >= BitWidth)
1588 return;
1589
1590 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001591 KnownZero, KnownOne, Depth+1);
1592 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001593 KnownZero <<= ShAmt;
1594 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001595 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001596 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001597 }
1598 return;
1599 case ISD::SRL:
1600 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1601 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001602 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001603
Dan Gohmand4cf9922008-02-26 18:50:50 +00001604 // If the shift count is an invalid immediate, don't do anything.
1605 if (ShAmt >= BitWidth)
1606 return;
1607
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001608 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001609 KnownZero, KnownOne, Depth+1);
1610 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001611 KnownZero = KnownZero.lshr(ShAmt);
1612 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001613
Dan Gohman72d2fd52008-02-13 22:43:25 +00001614 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001615 KnownZero |= HighBits; // High bits known zero.
1616 }
1617 return;
1618 case ISD::SRA:
1619 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001620 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001621
Dan Gohmand4cf9922008-02-26 18:50:50 +00001622 // If the shift count is an invalid immediate, don't do anything.
1623 if (ShAmt >= BitWidth)
1624 return;
1625
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001626 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001627 // If any of the demanded bits are produced by the sign extension, we also
1628 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001629 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1630 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001631 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001632
1633 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1634 Depth+1);
1635 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001636 KnownZero = KnownZero.lshr(ShAmt);
1637 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001638
1639 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001640 APInt SignBit = APInt::getSignBit(BitWidth);
1641 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001642
Dan Gohmanca93a432008-02-20 16:30:17 +00001643 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001644 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001645 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001646 KnownOne |= HighBits; // New bits are known one.
1647 }
1648 }
1649 return;
1650 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001651 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1652 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001653
1654 // Sign extension. Compute the demanded bits in the result that are not
1655 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001656 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001657
Dan Gohman977a76f2008-02-13 22:28:48 +00001658 APInt InSignBit = APInt::getSignBit(EBits);
1659 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001660
1661 // If the sign extended bits are demanded, we know that the sign
1662 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001663 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001664 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001665 InputDemandedBits |= InSignBit;
1666
1667 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1668 KnownZero, KnownOne, Depth+1);
1669 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1670
1671 // If the sign bit of the input is known set or clear, then we know the
1672 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001673 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001674 KnownZero |= NewBits;
1675 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001676 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001677 KnownOne |= NewBits;
1678 KnownZero &= ~NewBits;
1679 } else { // Input sign bit unknown
1680 KnownZero &= ~NewBits;
1681 KnownOne &= ~NewBits;
1682 }
1683 return;
1684 }
1685 case ISD::CTTZ:
1686 case ISD::CTLZ:
1687 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001688 unsigned LowBits = Log2_32(BitWidth)+1;
1689 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001690 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001691 return;
1692 }
1693 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001694 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001695 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001696 MVT VT = LD->getMemoryVT();
1697 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001698 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001699 }
1700 return;
1701 }
1702 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001703 MVT InVT = Op.getOperand(0).getValueType();
1704 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001705 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1706 APInt InMask = Mask;
1707 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001708 KnownZero.trunc(InBits);
1709 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001710 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001711 KnownZero.zext(BitWidth);
1712 KnownOne.zext(BitWidth);
1713 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001714 return;
1715 }
1716 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001717 MVT InVT = Op.getOperand(0).getValueType();
1718 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001719 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001720 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1721 APInt InMask = Mask;
1722 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001723
1724 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001725 // bit is demanded. Temporarily set this bit in the mask for our callee.
1726 if (NewBits.getBoolValue())
1727 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001728
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001729 KnownZero.trunc(InBits);
1730 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001731 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1732
1733 // Note if the sign bit is known to be zero or one.
1734 bool SignBitKnownZero = KnownZero.isNegative();
1735 bool SignBitKnownOne = KnownOne.isNegative();
1736 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1737 "Sign bit can't be known to be both zero and one!");
1738
1739 // If the sign bit wasn't actually demanded by our caller, we don't
1740 // want it set in the KnownZero and KnownOne result values. Reset the
1741 // mask and reapply it to the result values.
1742 InMask = Mask;
1743 InMask.trunc(InBits);
1744 KnownZero &= InMask;
1745 KnownOne &= InMask;
1746
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001747 KnownZero.zext(BitWidth);
1748 KnownOne.zext(BitWidth);
1749
Dan Gohman977a76f2008-02-13 22:28:48 +00001750 // If the sign bit is known zero or one, the top bits match.
1751 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001752 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001753 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001754 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001755 return;
1756 }
1757 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001758 MVT InVT = Op.getOperand(0).getValueType();
1759 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001760 APInt InMask = Mask;
1761 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001762 KnownZero.trunc(InBits);
1763 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001764 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001765 KnownZero.zext(BitWidth);
1766 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001767 return;
1768 }
1769 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001770 MVT InVT = Op.getOperand(0).getValueType();
1771 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001772 APInt InMask = Mask;
1773 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001774 KnownZero.zext(InBits);
1775 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001776 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001777 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001778 KnownZero.trunc(BitWidth);
1779 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001780 break;
1781 }
1782 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001783 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1784 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001785 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1786 KnownOne, Depth+1);
1787 KnownZero |= (~InMask) & Mask;
1788 return;
1789 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001790 case ISD::FGETSIGN:
1791 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001792 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001793 return;
1794
Dan Gohman23e8b712008-04-28 17:02:21 +00001795 case ISD::SUB: {
1796 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1797 // We know that the top bits of C-X are clear if X contains less bits
1798 // than C (i.e. no wrap-around can happen). For example, 20-X is
1799 // positive if we can prove that X is >= 0 and < 16.
1800 if (CLHS->getAPIntValue().isNonNegative()) {
1801 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1802 // NLZ can't be BitWidth with no sign bit
1803 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1804 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1805 Depth+1);
1806
1807 // If all of the MaskV bits are known to be zero, then we know the
1808 // output top bits are zero, because we now know that the output is
1809 // from [0-C].
1810 if ((KnownZero2 & MaskV) == MaskV) {
1811 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1812 // Top bits known zero.
1813 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1814 }
1815 }
1816 }
1817 }
1818 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001819 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001820 // Output known-0 bits are known if clear or set in both the low clear bits
1821 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1822 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001823 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1824 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1825 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1826 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1827
1828 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1829 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1830 KnownZeroOut = std::min(KnownZeroOut,
1831 KnownZero2.countTrailingOnes());
1832
1833 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001834 return;
1835 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001836 case ISD::SREM:
1837 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001838 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001839 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001840 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001841 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1842 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001843
Dan Gohmana60832b2008-08-13 23:12:35 +00001844 // If the sign bit of the first operand is zero, the sign bit of
1845 // the result is zero. If the first operand has no one bits below
1846 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001847 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1848 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001849
Dan Gohman23e8b712008-04-28 17:02:21 +00001850 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001851
1852 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001853 }
1854 }
1855 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001856 case ISD::UREM: {
1857 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001858 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001859 if (RA.isPowerOf2()) {
1860 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001861 APInt Mask2 = LowBits & Mask;
1862 KnownZero |= ~LowBits & Mask;
1863 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1864 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1865 break;
1866 }
1867 }
1868
1869 // Since the result is less than or equal to either operand, any leading
1870 // zero bits in either operand must also exist in the result.
1871 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1872 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1873 Depth+1);
1874 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1875 Depth+1);
1876
1877 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1878 KnownZero2.countLeadingOnes());
1879 KnownOne.clear();
1880 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1881 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001882 }
1883 default:
1884 // Allow the target to implement this method for its nodes.
1885 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1886 case ISD::INTRINSIC_WO_CHAIN:
1887 case ISD::INTRINSIC_W_CHAIN:
1888 case ISD::INTRINSIC_VOID:
1889 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1890 }
1891 return;
1892 }
1893}
1894
1895/// ComputeNumSignBits - Return the number of times the sign bit of the
1896/// register is replicated into the other bits. We know that at least 1 bit
1897/// is always equal to the sign bit (itself), but other cases can give us
1898/// information. For example, immediately after an "SRA X, 2", we know that
1899/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001900unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001901 MVT VT = Op.getValueType();
1902 assert(VT.isInteger() && "Invalid VT!");
1903 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001904 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001905 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001906
1907 if (Depth == 6)
1908 return 1; // Limit search depth.
1909
1910 switch (Op.getOpcode()) {
1911 default: break;
1912 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001913 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001914 return VTBits-Tmp+1;
1915 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001916 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001917 return VTBits-Tmp;
1918
1919 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001920 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1921 // If negative, return # leading ones.
1922 if (Val.isNegative())
1923 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001924
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001925 // Return # leading zeros.
1926 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001927 }
1928
1929 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001930 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001931 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1932
1933 case ISD::SIGN_EXTEND_INREG:
1934 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001935 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001936 Tmp = VTBits-Tmp+1;
1937
1938 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1939 return std::max(Tmp, Tmp2);
1940
1941 case ISD::SRA:
1942 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1943 // SRA X, C -> adds C sign bits.
1944 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001945 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001946 if (Tmp > VTBits) Tmp = VTBits;
1947 }
1948 return Tmp;
1949 case ISD::SHL:
1950 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1951 // shl destroys sign bits.
1952 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001953 if (C->getZExtValue() >= VTBits || // Bad shift.
1954 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1955 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001956 }
1957 break;
1958 case ISD::AND:
1959 case ISD::OR:
1960 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001961 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001962 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001963 if (Tmp != 1) {
1964 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1965 FirstAnswer = std::min(Tmp, Tmp2);
1966 // We computed what we know about the sign bits as our first
1967 // answer. Now proceed to the generic code that uses
1968 // ComputeMaskedBits, and pick whichever answer is better.
1969 }
1970 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001971
1972 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001973 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001974 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001975 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001976 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00001977
1978 case ISD::SADDO:
1979 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001980 case ISD::SSUBO:
1981 case ISD::USUBO:
1982 case ISD::SMULO:
1983 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001984 if (Op.getResNo() != 1)
1985 break;
Duncan Sands03228082008-11-23 15:47:28 +00001986 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001987 case ISD::SETCC:
1988 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands03228082008-11-23 15:47:28 +00001989 if (TLI.getBooleanContents() ==
1990 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00001991 return VTBits;
1992 break;
1993 case ISD::ROTL:
1994 case ISD::ROTR:
1995 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001996 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001997
1998 // Handle rotate right by N like a rotate left by 32-N.
1999 if (Op.getOpcode() == ISD::ROTR)
2000 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2001
2002 // If we aren't rotating out all of the known-in sign bits, return the
2003 // number that are left. This handles rotl(sext(x), 1) for example.
2004 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2005 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2006 }
2007 break;
2008 case ISD::ADD:
2009 // Add can have at most one carry bit. Thus we know that the output
2010 // is, at worst, one more bit than the inputs.
2011 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2012 if (Tmp == 1) return 1; // Early out.
2013
2014 // Special case decrementing a value (ADD X, -1):
2015 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
2016 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002017 APInt KnownZero, KnownOne;
2018 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002019 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
2020
2021 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2022 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002023 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002024 return VTBits;
2025
2026 // If we are subtracting one from a positive number, there is no carry
2027 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002028 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002029 return Tmp;
2030 }
2031
2032 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2033 if (Tmp2 == 1) return 1;
2034 return std::min(Tmp, Tmp2)-1;
2035 break;
2036
2037 case ISD::SUB:
2038 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2039 if (Tmp2 == 1) return 1;
2040
2041 // Handle NEG.
2042 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002043 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002044 APInt KnownZero, KnownOne;
2045 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002046 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2047 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2048 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002049 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002050 return VTBits;
2051
2052 // If the input is known to be positive (the sign bit is known clear),
2053 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002054 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002055 return Tmp2;
2056
2057 // Otherwise, we treat this like a SUB.
2058 }
2059
2060 // Sub can have at most one carry bit. Thus we know that the output
2061 // is, at worst, one more bit than the inputs.
2062 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2063 if (Tmp == 1) return 1; // Early out.
2064 return std::min(Tmp, Tmp2)-1;
2065 break;
2066 case ISD::TRUNCATE:
2067 // FIXME: it's tricky to do anything useful for this, but it is an important
2068 // case for targets like X86.
2069 break;
2070 }
2071
2072 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2073 if (Op.getOpcode() == ISD::LOAD) {
2074 LoadSDNode *LD = cast<LoadSDNode>(Op);
2075 unsigned ExtType = LD->getExtensionType();
2076 switch (ExtType) {
2077 default: break;
2078 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002079 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002080 return VTBits-Tmp+1;
2081 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002082 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002083 return VTBits-Tmp;
2084 }
2085 }
2086
2087 // Allow the target to implement this method for its nodes.
2088 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2089 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2090 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2091 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2092 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002093 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002094 }
2095
2096 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2097 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002098 APInt KnownZero, KnownOne;
2099 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002100 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
2101
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002102 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002103 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002104 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002105 Mask = KnownOne;
2106 } else {
2107 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002108 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002109 }
2110
2111 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2112 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002113 Mask = ~Mask;
2114 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002115 // Return # leading zeros. We use 'min' here in case Val was zero before
2116 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002117 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002118}
2119
Chris Lattner51dabfb2006-10-14 00:41:01 +00002120
Dan Gohman475871a2008-07-27 21:46:04 +00002121bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002122 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2123 if (!GA) return false;
Dan Gohman6520e202008-10-18 02:06:02 +00002124 if (GA->getOffset() != 0) return false;
Evan Chenga844bde2008-02-02 04:07:54 +00002125 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2126 if (!GV) return false;
Devang Patel35fe7342009-01-13 22:54:57 +00002127 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patelbbdc8202009-01-13 23:54:55 +00002128 return MMI && MMI->hasDebugInfo();
Evan Chenga844bde2008-02-02 04:07:54 +00002129}
2130
2131
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002132/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2133/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002134SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002135 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00002136 SDValue PermMask = N->getOperand(2);
2137 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002138 if (Idx.getOpcode() == ISD::UNDEF)
2139 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002140 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002141 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002142 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002143 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002144
2145 if (V.getOpcode() == ISD::BIT_CONVERT) {
2146 V = V.getOperand(0);
Mon P Wange9f10152008-12-09 05:46:39 +00002147 MVT VVT = V.getValueType();
2148 if (!VVT.isVector() || VVT.getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002149 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002150 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002151 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002152 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002153 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002154 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002155 return V.getOperand(Index);
2156 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002157 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002158 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002159}
2160
2161
Chris Lattnerc3aae252005-01-07 07:46:32 +00002162/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002163///
Dan Gohman475871a2008-07-27 21:46:04 +00002164SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002165 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT);
2166}
2167
2168SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002169 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002170 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002171 void *IP = 0;
2172 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002173 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002174 SDNode *N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002175 new (N) SDNode(Opcode, DL, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002176 CSEMap.InsertNode(N, IP);
2177
2178 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002179#ifndef NDEBUG
2180 VerifyNode(N);
2181#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002182 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002183}
2184
Dan Gohman475871a2008-07-27 21:46:04 +00002185SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002186 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Operand);
2187}
2188
2189SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
2190 MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002191 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002192 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002193 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002194 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002195 switch (Opcode) {
2196 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002197 case ISD::SIGN_EXTEND:
2198 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002199 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002200 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002201 case ISD::TRUNCATE:
2202 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002203 case ISD::UINT_TO_FP:
2204 case ISD::SINT_TO_FP: {
2205 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002206 // No compile time operations on this type.
2207 if (VT==MVT::ppcf128)
2208 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002209 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2210 (void)apf.convertFromAPInt(Val,
2211 Opcode==ISD::SINT_TO_FP,
2212 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002213 return getConstantFP(apf, VT);
2214 }
Chris Lattner94683772005-12-23 05:30:37 +00002215 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002216 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002217 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002218 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002219 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002220 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002221 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002222 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002223 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002224 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002225 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002226 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002227 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002228 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002229 }
2230 }
2231
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002232 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002233 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002234 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002235 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002236 switch (Opcode) {
2237 case ISD::FNEG:
2238 V.changeSign();
2239 return getConstantFP(V, VT);
2240 case ISD::FABS:
2241 V.clearSign();
2242 return getConstantFP(V, VT);
2243 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002244 case ISD::FP_EXTEND: {
2245 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002246 // This can return overflow, underflow, or inexact; we don't care.
2247 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002248 (void)V.convert(*MVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002249 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002250 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002251 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002252 case ISD::FP_TO_SINT:
2253 case ISD::FP_TO_UINT: {
2254 integerPart x;
Dale Johannesen23a98552008-10-09 23:00:39 +00002255 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002256 assert(integerPartWidth >= 64);
2257 // FIXME need to be more flexible about rounding mode.
2258 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2259 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002260 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002261 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2262 break;
2263 return getConstant(x, VT);
2264 }
2265 case ISD::BIT_CONVERT:
2266 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002267 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002268 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002269 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002270 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002271 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002272 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002273 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002274
Gabor Greifba36cb52008-08-28 21:40:38 +00002275 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002276 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002277 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002278 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002279 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002280 return Operand; // Factor, merge or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002281 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002282 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002283 assert(VT.isFloatingPoint() &&
2284 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002285 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002286 if (Operand.getOpcode() == ISD::UNDEF)
2287 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002288 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002289 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002290 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002291 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002292 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002293 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002294 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002295 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greifba36cb52008-08-28 21:40:38 +00002296 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002297 break;
2298 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002299 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002300 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002301 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002302 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002303 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002304 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002305 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002306 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002307 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002308 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002309 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002310 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002311 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002312 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002313 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2314 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002315 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002316 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002317 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002318 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002319 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002320 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002321 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002322 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002323 if (OpOpcode == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002324 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002325 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2326 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002327 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002328 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2329 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2330 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2331 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002332 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002333 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002334 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002335 break;
Chris Lattner35481892005-12-23 00:16:34 +00002336 case ISD::BIT_CONVERT:
2337 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002338 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002339 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002340 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002341 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2342 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002343 if (OpOpcode == ISD::UNDEF)
2344 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002345 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002346 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002347 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2348 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002349 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002350 if (OpOpcode == ISD::UNDEF)
2351 return getNode(ISD::UNDEF, VT);
2352 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2353 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2354 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2355 Operand.getConstantOperandVal(1) == 0 &&
2356 Operand.getOperand(0).getValueType() == VT)
2357 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002358 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002359 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002360 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002361 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2362 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002363 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002364 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002365 break;
2366 case ISD::FABS:
2367 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002368 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002369 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002370 }
2371
Chris Lattner43247a12005-08-25 19:12:10 +00002372 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002373 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002374 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002375 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002376 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002377 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002378 void *IP = 0;
2379 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002380 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002381 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002382 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002383 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002384 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002385 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002386 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002387 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002388
Chris Lattnerc3aae252005-01-07 07:46:32 +00002389 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002390#ifndef NDEBUG
2391 VerifyNode(N);
2392#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002393 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002394}
2395
Bill Wendling688d1c42008-09-24 10:16:24 +00002396SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
2397 MVT VT,
2398 ConstantSDNode *Cst1,
2399 ConstantSDNode *Cst2) {
2400 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2401
2402 switch (Opcode) {
2403 case ISD::ADD: return getConstant(C1 + C2, VT);
2404 case ISD::SUB: return getConstant(C1 - C2, VT);
2405 case ISD::MUL: return getConstant(C1 * C2, VT);
2406 case ISD::UDIV:
2407 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2408 break;
2409 case ISD::UREM:
2410 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2411 break;
2412 case ISD::SDIV:
2413 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2414 break;
2415 case ISD::SREM:
2416 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2417 break;
2418 case ISD::AND: return getConstant(C1 & C2, VT);
2419 case ISD::OR: return getConstant(C1 | C2, VT);
2420 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2421 case ISD::SHL: return getConstant(C1 << C2, VT);
2422 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2423 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2424 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2425 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2426 default: break;
2427 }
2428
2429 return SDValue();
2430}
2431
Dan Gohman475871a2008-07-27 21:46:04 +00002432SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2433 SDValue N1, SDValue N2) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002434 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2);
2435}
2436
2437SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2438 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002439 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2440 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002441 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002442 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002443 case ISD::TokenFactor:
2444 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2445 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002446 // Fold trivial token factors.
2447 if (N1.getOpcode() == ISD::EntryToken) return N2;
2448 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002449 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002450 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002451 case ISD::CONCAT_VECTORS:
2452 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2453 // one big BUILD_VECTOR.
2454 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2455 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002456 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2457 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002458 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2459 }
2460 break;
Chris Lattner76365122005-01-16 02:23:22 +00002461 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002462 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002463 N1.getValueType() == VT && "Binary operator types must match!");
2464 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2465 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002466 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002467 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002468 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2469 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002470 break;
Chris Lattner76365122005-01-16 02:23:22 +00002471 case ISD::OR:
2472 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002473 case ISD::ADD:
2474 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002475 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002476 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002477 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2478 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002479 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002480 return N1;
2481 break;
Chris Lattner76365122005-01-16 02:23:22 +00002482 case ISD::UDIV:
2483 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002484 case ISD::MULHU:
2485 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002486 case ISD::MUL:
2487 case ISD::SDIV:
2488 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002489 assert(VT.isInteger() && "This operator does not apply to FP types!");
2490 // fall through
Chris Lattner01b3d732005-09-28 22:28:18 +00002491 case ISD::FADD:
2492 case ISD::FSUB:
2493 case ISD::FMUL:
2494 case ISD::FDIV:
2495 case ISD::FREM:
Dan Gohmana90c8e62009-01-23 19:10:37 +00002496 if (UnsafeFPMath) {
2497 if (Opcode == ISD::FADD) {
2498 // 0+x --> x
2499 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2500 if (CFP->getValueAPF().isZero())
2501 return N2;
2502 // x+0 --> x
2503 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2504 if (CFP->getValueAPF().isZero())
2505 return N1;
2506 } else if (Opcode == ISD::FSUB) {
2507 // x-0 --> x
2508 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2509 if (CFP->getValueAPF().isZero())
2510 return N1;
2511 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002512 }
Chris Lattner76365122005-01-16 02:23:22 +00002513 assert(N1.getValueType() == N2.getValueType() &&
2514 N1.getValueType() == VT && "Binary operator types must match!");
2515 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002516 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2517 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002518 N1.getValueType().isFloatingPoint() &&
2519 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002520 "Invalid FCOPYSIGN!");
2521 break;
Chris Lattner76365122005-01-16 02:23:22 +00002522 case ISD::SHL:
2523 case ISD::SRA:
2524 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002525 case ISD::ROTL:
2526 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002527 assert(VT == N1.getValueType() &&
2528 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002529 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002530 "Shifts only work on integers");
Mon P Wange9f10152008-12-09 05:46:39 +00002531 assert((N2.getValueType() == TLI.getShiftAmountTy() ||
2532 (N2.getValueType().isVector() && N2.getValueType().isInteger())) &&
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002533 "Wrong type for shift amount");
Chris Lattner349db172008-07-02 17:01:57 +00002534
2535 // Always fold shifts of i1 values so the code generator doesn't need to
2536 // handle them. Since we know the size of the shift has to be less than the
2537 // size of the value, the shift/rotate count is guaranteed to be zero.
2538 if (VT == MVT::i1)
2539 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002540 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002541 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002542 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002543 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002545 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002546 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002547 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002548 break;
2549 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002550 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002551 assert(VT.isFloatingPoint() &&
2552 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002553 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002554 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002555 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002556 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002557 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002558 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002559 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002560 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002561 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002562 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002563 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002564 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002565 break;
2566 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002567 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002568 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002569 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002570 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002571 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002572 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002573 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002574
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002575 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002576 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002577 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002578 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002579 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002580 return getConstant(Val, VT);
2581 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002582 break;
2583 }
2584 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002585 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2586 if (N1.getOpcode() == ISD::UNDEF)
2587 return getNode(ISD::UNDEF, VT);
2588
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002589 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2590 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002591 if (N2C &&
2592 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002593 N1.getNumOperands() > 0) {
2594 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002595 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002596 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002597 N1.getOperand(N2C->getZExtValue() / Factor),
2598 getConstant(N2C->getZExtValue() % Factor,
2599 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002600 }
2601
2602 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2603 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002604 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002605 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002606
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002607 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2608 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002609 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Dan Gohman197e88f2009-01-29 16:10:46 +00002610 // If the indices are the same, return the inserted element.
Dan Gohman6ab64222008-08-13 21:51:37 +00002611 if (N1.getOperand(2) == N2)
2612 return N1.getOperand(1);
Dan Gohman197e88f2009-01-29 16:10:46 +00002613 // If the indices are known different, extract the element from
2614 // the original vector.
2615 else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2616 isa<ConstantSDNode>(N2))
Dan Gohman6ab64222008-08-13 21:51:37 +00002617 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2618 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002619 break;
2620 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002621 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002622 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2623 (N1.getValueType().isInteger() == VT.isInteger()) &&
2624 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002625
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002626 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2627 // 64-bit integers into 32-bit parts. Instead of building the extract of
2628 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2629 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002630 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002631
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002632 // EXTRACT_ELEMENT of a constant int is also very common.
2633 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002634 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002635 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002636 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2637 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002638 }
2639 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002640 case ISD::EXTRACT_SUBVECTOR:
2641 if (N1.getValueType() == VT) // Trivial extraction.
2642 return N1;
2643 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002644 }
2645
2646 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002647 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002648 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2649 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002650 } else { // Cannonicalize constant to RHS if commutative
2651 if (isCommutativeBinOp(Opcode)) {
2652 std::swap(N1C, N2C);
2653 std::swap(N1, N2);
2654 }
2655 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002656 }
2657
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002658 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002659 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2660 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002661 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002662 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2663 // Cannonicalize constant to RHS if commutative
2664 std::swap(N1CFP, N2CFP);
2665 std::swap(N1, N2);
2666 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002667 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2668 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002669 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002670 case ISD::FADD:
2671 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002672 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002673 return getConstantFP(V1, VT);
2674 break;
2675 case ISD::FSUB:
2676 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2677 if (s!=APFloat::opInvalidOp)
2678 return getConstantFP(V1, VT);
2679 break;
2680 case ISD::FMUL:
2681 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2682 if (s!=APFloat::opInvalidOp)
2683 return getConstantFP(V1, VT);
2684 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002685 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002686 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2687 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2688 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002689 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002690 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002691 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2692 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2693 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002694 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002695 case ISD::FCOPYSIGN:
2696 V1.copySign(V2);
2697 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002698 default: break;
2699 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002700 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002701 }
Chris Lattner62b57722006-04-20 05:39:12 +00002702
2703 // Canonicalize an UNDEF to the RHS, even over a constant.
2704 if (N1.getOpcode() == ISD::UNDEF) {
2705 if (isCommutativeBinOp(Opcode)) {
2706 std::swap(N1, N2);
2707 } else {
2708 switch (Opcode) {
2709 case ISD::FP_ROUND_INREG:
2710 case ISD::SIGN_EXTEND_INREG:
2711 case ISD::SUB:
2712 case ISD::FSUB:
2713 case ISD::FDIV:
2714 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002715 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002716 return N1; // fold op(undef, arg2) -> undef
2717 case ISD::UDIV:
2718 case ISD::SDIV:
2719 case ISD::UREM:
2720 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002721 case ISD::SRL:
2722 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002723 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002724 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2725 // For vectors, we can't easily build an all zero vector, just return
2726 // the LHS.
2727 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002728 }
2729 }
2730 }
2731
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002732 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002733 if (N2.getOpcode() == ISD::UNDEF) {
2734 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002735 case ISD::XOR:
2736 if (N1.getOpcode() == ISD::UNDEF)
2737 // Handle undef ^ undef -> 0 special case. This is a common
2738 // idiom (misuse).
2739 return getConstant(0, VT);
2740 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002741 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002742 case ISD::ADDC:
2743 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002744 case ISD::SUB:
2745 case ISD::FADD:
2746 case ISD::FSUB:
2747 case ISD::FMUL:
2748 case ISD::FDIV:
2749 case ISD::FREM:
2750 case ISD::UDIV:
2751 case ISD::SDIV:
2752 case ISD::UREM:
2753 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002754 return N2; // fold op(arg1, undef) -> undef
2755 case ISD::MUL:
2756 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002757 case ISD::SRL:
2758 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002759 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002760 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2761 // For vectors, we can't easily build an all zero vector, just return
2762 // the LHS.
2763 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002764 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002765 if (!VT.isVector())
2766 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002767 // For vectors, we can't easily build an all one vector, just return
2768 // the LHS.
2769 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002770 case ISD::SRA:
2771 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002772 }
2773 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002774
Chris Lattner27e9b412005-05-11 18:57:39 +00002775 // Memoize this node if possible.
2776 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002777 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002778 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002779 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002780 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002781 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002782 void *IP = 0;
2783 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002784 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002785 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002786 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002787 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002788 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002789 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002790 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002791 }
2792
Chris Lattnerc3aae252005-01-07 07:46:32 +00002793 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002794#ifndef NDEBUG
2795 VerifyNode(N);
2796#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002797 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002798}
2799
Dan Gohman475871a2008-07-27 21:46:04 +00002800SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2801 SDValue N1, SDValue N2, SDValue N3) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002802 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3);
2803}
2804
2805SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2806 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002807 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002808 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2809 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002810 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002811 case ISD::CONCAT_VECTORS:
2812 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2813 // one big BUILD_VECTOR.
2814 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2815 N2.getOpcode() == ISD::BUILD_VECTOR &&
2816 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002817 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2818 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2819 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002820 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2821 }
2822 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002823 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002824 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002825 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002826 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002827 break;
2828 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002829 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002830 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002831 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002832 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002833 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002834 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002835 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002836
2837 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002838 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002839 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002840 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002841 if (N2C->getZExtValue()) // Unconditional branch
Chris Lattner5351e9b2005-01-07 22:49:57 +00002842 return getNode(ISD::BR, MVT::Other, N1, N3);
2843 else
2844 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002845 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002846 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002847 case ISD::VECTOR_SHUFFLE:
Mon P Wangaeb06d22008-11-10 04:46:22 +00002848 assert(N1.getValueType() == N2.getValueType() &&
2849 N1.getValueType().isVector() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002851 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002852 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002853 "Illegal VECTOR_SHUFFLE node!");
2854 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002855 case ISD::BIT_CONVERT:
2856 // Fold bit_convert nodes from a type to themselves.
2857 if (N1.getValueType() == VT)
2858 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002859 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002860 }
2861
Chris Lattner43247a12005-08-25 19:12:10 +00002862 // Memoize node if it doesn't produce a flag.
2863 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002864 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002865 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002866 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002867 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002868 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002869 void *IP = 0;
2870 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002871 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002872 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002873 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002874 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002875 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002876 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002877 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002878 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002879 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002880#ifndef NDEBUG
2881 VerifyNode(N);
2882#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002883 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002884}
2885
Dan Gohman475871a2008-07-27 21:46:04 +00002886SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2887 SDValue N1, SDValue N2, SDValue N3,
2888 SDValue N4) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002889 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3, N4);
2890}
2891
2892SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2893 SDValue N1, SDValue N2, SDValue N3,
2894 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00002895 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002896 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002897}
2898
Dan Gohman475871a2008-07-27 21:46:04 +00002899SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2900 SDValue N1, SDValue N2, SDValue N3,
2901 SDValue N4, SDValue N5) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002902 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3, N4, N5);
2903}
2904
2905SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2906 SDValue N1, SDValue N2, SDValue N3,
2907 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00002908 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002909 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002910}
2911
Dan Gohman707e0182008-04-12 04:36:06 +00002912/// getMemsetValue - Vectorized representation of the memset value
2913/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002914static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002915 unsigned NumBits = VT.isVector() ?
2916 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002917 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002918 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002919 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002920 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002921 Val = (Val << Shift) | Val;
2922 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002923 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002924 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002925 return DAG.getConstant(Val, VT);
2926 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002927 }
Evan Chengf0df0312008-05-15 08:39:06 +00002928
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002929 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Evan Chengf0df0312008-05-15 08:39:06 +00002930 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2931 unsigned Shift = 8;
2932 for (unsigned i = NumBits; i > 8; i >>= 1) {
2933 Value = DAG.getNode(ISD::OR, VT,
2934 DAG.getNode(ISD::SHL, VT, Value,
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002935 DAG.getConstant(Shift,
2936 TLI.getShiftAmountTy())),
2937 Value);
Evan Chengf0df0312008-05-15 08:39:06 +00002938 Shift <<= 1;
2939 }
2940
2941 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002942}
2943
Dan Gohman707e0182008-04-12 04:36:06 +00002944/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2945/// used when a memcpy is turned into a memset when the source is a constant
2946/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002947static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002948 const TargetLowering &TLI,
2949 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002950 // Handle vector with all elements zero.
2951 if (Str.empty()) {
2952 if (VT.isInteger())
2953 return DAG.getConstant(0, VT);
2954 unsigned NumElts = VT.getVectorNumElements();
2955 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2956 return DAG.getNode(ISD::BIT_CONVERT, VT,
2957 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2958 }
2959
Duncan Sands83ec4b62008-06-06 12:08:01 +00002960 assert(!VT.isVector() && "Can't handle vector type here!");
2961 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002962 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002963 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002964 if (TLI.isLittleEndian())
2965 Offset = Offset + MSB - 1;
2966 for (unsigned i = 0; i != MSB; ++i) {
2967 Val = (Val << 8) | (unsigned char)Str[Offset];
2968 Offset += TLI.isLittleEndian() ? -1 : 1;
2969 }
2970 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002971}
2972
Dan Gohman707e0182008-04-12 04:36:06 +00002973/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002974///
Dan Gohman475871a2008-07-27 21:46:04 +00002975static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002976 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002977 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002978 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2979}
2980
Evan Chengf0df0312008-05-15 08:39:06 +00002981/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2982///
Dan Gohman475871a2008-07-27 21:46:04 +00002983static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002984 unsigned SrcDelta = 0;
2985 GlobalAddressSDNode *G = NULL;
2986 if (Src.getOpcode() == ISD::GlobalAddress)
2987 G = cast<GlobalAddressSDNode>(Src);
2988 else if (Src.getOpcode() == ISD::ADD &&
2989 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2990 Src.getOperand(1).getOpcode() == ISD::Constant) {
2991 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002992 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00002993 }
2994 if (!G)
2995 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002996
Evan Chengf0df0312008-05-15 08:39:06 +00002997 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002998 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2999 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00003000
Evan Chengf0df0312008-05-15 08:39:06 +00003001 return false;
3002}
Dan Gohman707e0182008-04-12 04:36:06 +00003003
Evan Chengf0df0312008-05-15 08:39:06 +00003004/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3005/// to replace the memset / memcpy is below the threshold. It also returns the
3006/// types of the sequence of memory ops to perform memset / memcpy.
3007static
Duncan Sands83ec4b62008-06-06 12:08:01 +00003008bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00003009 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00003010 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003011 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00003012 SelectionDAG &DAG,
3013 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003014 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00003015 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00003016 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Chris Lattner7fe5e182008-10-28 07:10:51 +00003017 MVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00003018 if (VT != MVT::iAny) {
3019 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00003020 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00003021 // If source is a string constant, this will require an unaligned load.
3022 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
3023 if (Dst.getOpcode() != ISD::FrameIndex) {
3024 // Can't change destination alignment. It requires a unaligned store.
3025 if (AllowUnalign)
3026 VT = MVT::iAny;
3027 } else {
3028 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
3029 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3030 if (MFI->isFixedObjectIndex(FI)) {
3031 // Can't change destination alignment. It requires a unaligned store.
3032 if (AllowUnalign)
3033 VT = MVT::iAny;
3034 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00003035 // Give the stack frame object a larger alignment if needed.
3036 if (MFI->getObjectAlignment(FI) < NewAlign)
3037 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00003038 Align = NewAlign;
3039 }
3040 }
3041 }
3042 }
3043
3044 if (VT == MVT::iAny) {
3045 if (AllowUnalign) {
3046 VT = MVT::i64;
3047 } else {
3048 switch (Align & 7) {
3049 case 0: VT = MVT::i64; break;
3050 case 4: VT = MVT::i32; break;
3051 case 2: VT = MVT::i16; break;
3052 default: VT = MVT::i8; break;
3053 }
3054 }
3055
Duncan Sands83ec4b62008-06-06 12:08:01 +00003056 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003057 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003058 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
3059 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003060
Duncan Sands8e4eb092008-06-08 20:54:56 +00003061 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003062 VT = LVT;
3063 }
Dan Gohman707e0182008-04-12 04:36:06 +00003064
3065 unsigned NumMemOps = 0;
3066 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003067 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003068 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003069 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003070 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00003071 VT = MVT::i64;
3072 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003073 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
3074 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003075 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003076 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003077 VTSize >>= 1;
3078 }
Dan Gohman707e0182008-04-12 04:36:06 +00003079 }
Dan Gohman707e0182008-04-12 04:36:06 +00003080
3081 if (++NumMemOps > Limit)
3082 return false;
3083 MemOps.push_back(VT);
3084 Size -= VTSize;
3085 }
3086
3087 return true;
3088}
3089
Dan Gohman475871a2008-07-27 21:46:04 +00003090static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
3091 SDValue Chain, SDValue Dst,
3092 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00003093 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003094 const Value *DstSV, uint64_t DstSVOff,
3095 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00003096 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3097
Dan Gohman21323f32008-05-29 19:42:22 +00003098 // Expand memcpy to a series of load and store ops if the size operand falls
3099 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003100 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003101 uint64_t Limit = -1ULL;
Dan Gohman707e0182008-04-12 04:36:06 +00003102 if (!AlwaysInline)
3103 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00003104 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003105 std::string Str;
3106 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003107 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003108 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003109 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003110
Dan Gohman707e0182008-04-12 04:36:06 +00003111
Evan Cheng0ff39b32008-06-30 07:31:25 +00003112 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00003113 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003114 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003115 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003116 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003117 MVT VT = MemOps[i];
3118 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003119 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003120
Evan Cheng0ff39b32008-06-30 07:31:25 +00003121 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00003122 // It's unlikely a store of a vector immediate can be done in a single
3123 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003124 // We also handle store a vector with all zero's.
3125 // FIXME: Handle other cases where store of vector immediate is done in
3126 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00003127 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00003128 Store = DAG.getStore(Chain, Value,
3129 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00003130 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003131 } else {
3132 Value = DAG.getLoad(VT, Chain,
3133 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003134 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00003135 Store = DAG.getStore(Chain, Value,
3136 getMemBasePlusOffset(Dst, DstOff, DAG),
3137 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003138 }
3139 OutChains.push_back(Store);
3140 SrcOff += VTSize;
3141 DstOff += VTSize;
3142 }
3143
3144 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3145 &OutChains[0], OutChains.size());
3146}
3147
Dan Gohman475871a2008-07-27 21:46:04 +00003148static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
3149 SDValue Chain, SDValue Dst,
3150 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00003151 unsigned Align, bool AlwaysInline,
3152 const Value *DstSV, uint64_t DstSVOff,
3153 const Value *SrcSV, uint64_t SrcSVOff){
3154 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3155
3156 // Expand memmove to a series of load and store ops if the size operand falls
3157 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003158 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003159 uint64_t Limit = -1ULL;
Dan Gohman21323f32008-05-29 19:42:22 +00003160 if (!AlwaysInline)
3161 Limit = TLI.getMaxStoresPerMemmove();
3162 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003163 std::string Str;
3164 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00003165 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003166 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003167 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003168
Dan Gohman21323f32008-05-29 19:42:22 +00003169 uint64_t SrcOff = 0, DstOff = 0;
3170
Dan Gohman475871a2008-07-27 21:46:04 +00003171 SmallVector<SDValue, 8> LoadValues;
3172 SmallVector<SDValue, 8> LoadChains;
3173 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003174 unsigned NumMemOps = MemOps.size();
3175 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003176 MVT VT = MemOps[i];
3177 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003178 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003179
3180 Value = DAG.getLoad(VT, Chain,
3181 getMemBasePlusOffset(Src, SrcOff, DAG),
3182 SrcSV, SrcSVOff + SrcOff, false, Align);
3183 LoadValues.push_back(Value);
3184 LoadChains.push_back(Value.getValue(1));
3185 SrcOff += VTSize;
3186 }
3187 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3188 &LoadChains[0], LoadChains.size());
3189 OutChains.clear();
3190 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003191 MVT VT = MemOps[i];
3192 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003193 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003194
3195 Store = DAG.getStore(Chain, LoadValues[i],
3196 getMemBasePlusOffset(Dst, DstOff, DAG),
3197 DstSV, DstSVOff + DstOff, false, DstAlign);
3198 OutChains.push_back(Store);
3199 DstOff += VTSize;
3200 }
3201
3202 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3203 &OutChains[0], OutChains.size());
3204}
3205
Dan Gohman475871a2008-07-27 21:46:04 +00003206static SDValue getMemsetStores(SelectionDAG &DAG,
3207 SDValue Chain, SDValue Dst,
3208 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003209 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003210 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003211 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3212
3213 // Expand memset to a series of load/store ops if the size operand
3214 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003215 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003216 std::string Str;
3217 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003218 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003219 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003220 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003221
Dan Gohman475871a2008-07-27 21:46:04 +00003222 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003223 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003224
3225 unsigned NumMemOps = MemOps.size();
3226 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003227 MVT VT = MemOps[i];
3228 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003229 SDValue Value = getMemsetValue(Src, VT, DAG);
3230 SDValue Store = DAG.getStore(Chain, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003231 getMemBasePlusOffset(Dst, DstOff, DAG),
3232 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003233 OutChains.push_back(Store);
3234 DstOff += VTSize;
3235 }
3236
3237 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3238 &OutChains[0], OutChains.size());
3239}
3240
Dan Gohman475871a2008-07-27 21:46:04 +00003241SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3242 SDValue Src, SDValue Size,
3243 unsigned Align, bool AlwaysInline,
3244 const Value *DstSV, uint64_t DstSVOff,
3245 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003246
3247 // Check to see if we should lower the memcpy to loads and stores first.
3248 // For cases within the target-specified limits, this is the best choice.
3249 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3250 if (ConstantSize) {
3251 // Memcpy with size zero? Just return the original chain.
3252 if (ConstantSize->isNullValue())
3253 return Chain;
3254
Dan Gohman475871a2008-07-27 21:46:04 +00003255 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003256 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3257 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003258 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003259 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003260 return Result;
3261 }
3262
3263 // Then check to see if we should lower the memcpy with target-specific
3264 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003265 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003266 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3267 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003268 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003269 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003270 return Result;
3271
3272 // If we really need inline code and the target declined to provide it,
3273 // use a (potentially long) sequence of loads and stores.
3274 if (AlwaysInline) {
3275 assert(ConstantSize && "AlwaysInline requires a constant size!");
3276 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003277 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003278 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003279 }
3280
3281 // Emit a library call.
3282 TargetLowering::ArgListTy Args;
3283 TargetLowering::ArgListEntry Entry;
3284 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3285 Entry.Node = Dst; Args.push_back(Entry);
3286 Entry.Node = Src; Args.push_back(Entry);
3287 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003288 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003289 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003290 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003291 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003292 getExternalSymbol("memcpy", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003293 Args, *this, DebugLoc::getUnknownLoc());
Dan Gohman707e0182008-04-12 04:36:06 +00003294 return CallResult.second;
3295}
3296
Dan Gohman475871a2008-07-27 21:46:04 +00003297SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3298 SDValue Src, SDValue Size,
3299 unsigned Align,
3300 const Value *DstSV, uint64_t DstSVOff,
3301 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003302
Dan Gohman21323f32008-05-29 19:42:22 +00003303 // Check to see if we should lower the memmove to loads and stores first.
3304 // For cases within the target-specified limits, this is the best choice.
3305 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3306 if (ConstantSize) {
3307 // Memmove with size zero? Just return the original chain.
3308 if (ConstantSize->isNullValue())
3309 return Chain;
3310
Dan Gohman475871a2008-07-27 21:46:04 +00003311 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003312 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3313 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003314 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003315 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003316 return Result;
3317 }
Dan Gohman707e0182008-04-12 04:36:06 +00003318
3319 // Then check to see if we should lower the memmove with target-specific
3320 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003321 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003322 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003323 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003324 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003325 return Result;
3326
3327 // Emit a library call.
3328 TargetLowering::ArgListTy Args;
3329 TargetLowering::ArgListEntry Entry;
3330 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3331 Entry.Node = Dst; Args.push_back(Entry);
3332 Entry.Node = Src; Args.push_back(Entry);
3333 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003334 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003335 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003336 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003337 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003338 getExternalSymbol("memmove", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003339 Args, *this, DebugLoc::getUnknownLoc());
Dan Gohman707e0182008-04-12 04:36:06 +00003340 return CallResult.second;
3341}
3342
Dan Gohman475871a2008-07-27 21:46:04 +00003343SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3344 SDValue Src, SDValue Size,
3345 unsigned Align,
3346 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003347
3348 // Check to see if we should lower the memset to stores first.
3349 // For cases within the target-specified limits, this is the best choice.
3350 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3351 if (ConstantSize) {
3352 // Memset with size zero? Just return the original chain.
3353 if (ConstantSize->isNullValue())
3354 return Chain;
3355
Dan Gohman475871a2008-07-27 21:46:04 +00003356 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003357 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3358 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003359 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003360 return Result;
3361 }
3362
3363 // Then check to see if we should lower the memset with target-specific
3364 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003365 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003366 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Bill Wendling6158d842008-10-01 00:59:58 +00003367 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003368 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003369 return Result;
3370
3371 // Emit a library call.
3372 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3373 TargetLowering::ArgListTy Args;
3374 TargetLowering::ArgListEntry Entry;
3375 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3376 Args.push_back(Entry);
3377 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003378 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003379 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3380 else
3381 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3382 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3383 Args.push_back(Entry);
3384 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3385 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003386 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003387 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003388 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003389 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003390 getExternalSymbol("memset", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003391 Args, *this, DebugLoc::getUnknownLoc());
Dan Gohman707e0182008-04-12 04:36:06 +00003392 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003393}
3394
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003395SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3396 SDValue Chain,
Dan Gohman475871a2008-07-27 21:46:04 +00003397 SDValue Ptr, SDValue Cmp,
3398 SDValue Swp, const Value* PtrVal,
3399 unsigned Alignment) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003400 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003401 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003402
3403 MVT VT = Cmp.getValueType();
3404
3405 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003406 Alignment = getMVTAlignment(MemVT);
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003407
3408 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003409 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003410 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003411 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003412 void* IP = 0;
3413 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003414 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003415 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003416 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3417 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003418 CSEMap.InsertNode(N, IP);
3419 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003420 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003421}
3422
Dale Johannesene8c17332009-01-29 00:47:48 +00003423SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3424 SDValue Chain,
3425 SDValue Ptr, SDValue Cmp,
3426 SDValue Swp, const Value* PtrVal,
3427 unsigned Alignment) {
3428 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3429 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3430
3431 MVT VT = Cmp.getValueType();
3432
3433 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3434 Alignment = getMVTAlignment(MemVT);
3435
3436 SDVTList VTs = getVTList(VT, MVT::Other);
3437 FoldingSetNodeID ID;
3438 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3439 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3440 void* IP = 0;
3441 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3442 return SDValue(E, 0);
3443 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3444 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3445 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
3446 CSEMap.InsertNode(N, IP);
3447 AllNodes.push_back(N);
3448 return SDValue(N, 0);
3449}
3450
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003451SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3452 SDValue Chain,
Dan Gohman475871a2008-07-27 21:46:04 +00003453 SDValue Ptr, SDValue Val,
3454 const Value* PtrVal,
3455 unsigned Alignment) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003456 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3457 Opcode == ISD::ATOMIC_LOAD_SUB ||
3458 Opcode == ISD::ATOMIC_LOAD_AND ||
3459 Opcode == ISD::ATOMIC_LOAD_OR ||
3460 Opcode == ISD::ATOMIC_LOAD_XOR ||
3461 Opcode == ISD::ATOMIC_LOAD_NAND ||
3462 Opcode == ISD::ATOMIC_LOAD_MIN ||
3463 Opcode == ISD::ATOMIC_LOAD_MAX ||
3464 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3465 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3466 Opcode == ISD::ATOMIC_SWAP) &&
3467 "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003468
3469 MVT VT = Val.getValueType();
3470
3471 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003472 Alignment = getMVTAlignment(MemVT);
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003473
3474 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003475 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003476 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003477 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003478 void* IP = 0;
3479 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003480 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003481 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003482 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3483 Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003484 CSEMap.InsertNode(N, IP);
3485 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003486 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003487}
3488
Dale Johannesene8c17332009-01-29 00:47:48 +00003489SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3490 SDValue Chain,
3491 SDValue Ptr, SDValue Val,
3492 const Value* PtrVal,
3493 unsigned Alignment) {
3494 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3495 Opcode == ISD::ATOMIC_LOAD_SUB ||
3496 Opcode == ISD::ATOMIC_LOAD_AND ||
3497 Opcode == ISD::ATOMIC_LOAD_OR ||
3498 Opcode == ISD::ATOMIC_LOAD_XOR ||
3499 Opcode == ISD::ATOMIC_LOAD_NAND ||
3500 Opcode == ISD::ATOMIC_LOAD_MIN ||
3501 Opcode == ISD::ATOMIC_LOAD_MAX ||
3502 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3503 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3504 Opcode == ISD::ATOMIC_SWAP) &&
3505 "Invalid Atomic Op");
3506
3507 MVT VT = Val.getValueType();
3508
3509 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3510 Alignment = getMVTAlignment(MemVT);
3511
3512 SDVTList VTs = getVTList(VT, MVT::Other);
3513 FoldingSetNodeID ID;
3514 SDValue Ops[] = {Chain, Ptr, Val};
3515 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3516 void* IP = 0;
3517 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3518 return SDValue(E, 0);
3519 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3520 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3521 Chain, Ptr, Val, PtrVal, Alignment);
3522 CSEMap.InsertNode(N, IP);
3523 AllNodes.push_back(N);
3524 return SDValue(N, 0);
3525}
3526
Duncan Sands4bdcb612008-07-02 17:40:58 +00003527/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3528/// Allowed to return something different (and simpler) if Simplify is true.
Duncan Sandsaaffa052008-12-01 11:41:29 +00003529SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps) {
3530 if (NumOps == 1)
Duncan Sands4bdcb612008-07-02 17:40:58 +00003531 return Ops[0];
3532
3533 SmallVector<MVT, 4> VTs;
3534 VTs.reserve(NumOps);
3535 for (unsigned i = 0; i < NumOps; ++i)
3536 VTs.push_back(Ops[i].getValueType());
3537 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3538}
3539
Dan Gohman475871a2008-07-27 21:46:04 +00003540SDValue
Mon P Wangc4d10212008-10-17 18:22:58 +00003541SelectionDAG::getMemIntrinsicNode(unsigned Opcode,
3542 const MVT *VTs, unsigned NumVTs,
3543 const SDValue *Ops, unsigned NumOps,
3544 MVT MemVT, const Value *srcValue, int SVOff,
3545 unsigned Align, bool Vol,
3546 bool ReadMem, bool WriteMem) {
3547 return getMemIntrinsicNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps,
3548 MemVT, srcValue, SVOff, Align, Vol,
3549 ReadMem, WriteMem);
3550}
3551
3552SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003553SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
3554 const MVT *VTs, unsigned NumVTs,
3555 const SDValue *Ops, unsigned NumOps,
3556 MVT MemVT, const Value *srcValue, int SVOff,
3557 unsigned Align, bool Vol,
3558 bool ReadMem, bool WriteMem) {
3559 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3560 MemVT, srcValue, SVOff, Align, Vol,
3561 ReadMem, WriteMem);
3562}
3563
3564SDValue
Mon P Wangc4d10212008-10-17 18:22:58 +00003565SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDVTList VTList,
3566 const SDValue *Ops, unsigned NumOps,
3567 MVT MemVT, const Value *srcValue, int SVOff,
3568 unsigned Align, bool Vol,
3569 bool ReadMem, bool WriteMem) {
3570 // Memoize the node unless it returns a flag.
3571 MemIntrinsicSDNode *N;
3572 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3573 FoldingSetNodeID ID;
3574 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3575 void *IP = 0;
3576 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3577 return SDValue(E, 0);
3578
3579 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3580 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3581 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3582 CSEMap.InsertNode(N, IP);
3583 } else {
3584 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3585 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3586 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3587 }
3588 AllNodes.push_back(N);
3589 return SDValue(N, 0);
3590}
3591
3592SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003593SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3594 const SDValue *Ops, unsigned NumOps,
3595 MVT MemVT, const Value *srcValue, int SVOff,
3596 unsigned Align, bool Vol,
3597 bool ReadMem, bool WriteMem) {
3598 // Memoize the node unless it returns a flag.
3599 MemIntrinsicSDNode *N;
3600 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3601 FoldingSetNodeID ID;
3602 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3603 void *IP = 0;
3604 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3605 return SDValue(E, 0);
3606
3607 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3608 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3609 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3610 CSEMap.InsertNode(N, IP);
3611 } else {
3612 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3613 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3614 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3615 }
3616 AllNodes.push_back(N);
3617 return SDValue(N, 0);
3618}
3619
3620SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00003621SelectionDAG::getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003622 bool IsInreg, SDVTList VTs,
Dan Gohman095cc292008-09-13 01:54:27 +00003623 const SDValue *Operands, unsigned NumOperands) {
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003624 // Do not include isTailCall in the folding set profile.
3625 FoldingSetNodeID ID;
3626 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3627 ID.AddInteger(CallingConv);
3628 ID.AddInteger(IsVarArgs);
3629 void *IP = 0;
3630 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3631 // Instead of including isTailCall in the folding set, we just
3632 // set the flag of the existing node.
3633 if (!IsTailCall)
3634 cast<CallSDNode>(E)->setNotTailCall();
3635 return SDValue(E, 0);
3636 }
Dan Gohman095cc292008-09-13 01:54:27 +00003637 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
Dale Johannesen86098bd2008-09-26 19:31:26 +00003638 new (N) CallSDNode(CallingConv, IsVarArgs, IsTailCall, IsInreg,
Dan Gohman095cc292008-09-13 01:54:27 +00003639 VTs, Operands, NumOperands);
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003640 CSEMap.InsertNode(N, IP);
Dan Gohman095cc292008-09-13 01:54:27 +00003641 AllNodes.push_back(N);
3642 return SDValue(N, 0);
3643}
3644
3645SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003646SelectionDAG::getCall(unsigned CallingConv, DebugLoc dl, bool IsVarArgs,
3647 bool IsTailCall, bool IsInreg, SDVTList VTs,
3648 const SDValue *Operands, unsigned NumOperands) {
3649 // Do not include isTailCall in the folding set profile.
3650 FoldingSetNodeID ID;
3651 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3652 ID.AddInteger(CallingConv);
3653 ID.AddInteger(IsVarArgs);
3654 void *IP = 0;
3655 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3656 // Instead of including isTailCall in the folding set, we just
3657 // set the flag of the existing node.
3658 if (!IsTailCall)
3659 cast<CallSDNode>(E)->setNotTailCall();
3660 return SDValue(E, 0);
3661 }
3662 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
3663 new (N) CallSDNode(CallingConv, dl, IsVarArgs, IsTailCall, IsInreg,
3664 VTs, Operands, NumOperands);
3665 CSEMap.InsertNode(N, IP);
3666 AllNodes.push_back(N);
3667 return SDValue(N, 0);
3668}
3669
3670SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003671SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003672 MVT VT, SDValue Chain,
3673 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003674 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003675 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003676 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3677 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003678
Duncan Sands14ea39c2008-03-27 20:23:40 +00003679 if (VT == EVT) {
3680 ExtType = ISD::NON_EXTLOAD;
3681 } else if (ExtType == ISD::NON_EXTLOAD) {
3682 assert(VT == EVT && "Non-extending load from different memory type!");
3683 } else {
3684 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003685 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003686 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3687 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003688 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003689 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003690 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003691 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003692 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003693 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003694 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003695 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003696
3697 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003698 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003699 "Unindexed load with an offset!");
3700
3701 SDVTList VTs = Indexed ?
3702 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003703 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003704 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003705 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003706 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003707 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003708 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003709 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003710 void *IP = 0;
3711 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003712 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003713 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003714 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3715 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003716 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003717 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003718 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003719}
3720
Dale Johannesene8c17332009-01-29 00:47:48 +00003721SDValue
3722SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
3723 ISD::LoadExtType ExtType, MVT VT, SDValue Chain,
3724 SDValue Ptr, SDValue Offset,
3725 const Value *SV, int SVOffset, MVT EVT,
3726 bool isVolatile, unsigned Alignment) {
3727 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3728 Alignment = getMVTAlignment(VT);
3729
3730 if (VT == EVT) {
3731 ExtType = ISD::NON_EXTLOAD;
3732 } else if (ExtType == ISD::NON_EXTLOAD) {
3733 assert(VT == EVT && "Non-extending load from different memory type!");
3734 } else {
3735 // Extending load.
3736 if (VT.isVector())
3737 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3738 "Invalid vector extload!");
3739 else
3740 assert(EVT.bitsLT(VT) &&
3741 "Should only be an extending load, not truncating!");
3742 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
3743 "Cannot sign/zero extend a FP/Vector load!");
3744 assert(VT.isInteger() == EVT.isInteger() &&
3745 "Cannot convert from FP to Int or Int -> FP!");
3746 }
3747
3748 bool Indexed = AM != ISD::UNINDEXED;
3749 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3750 "Unindexed load with an offset!");
3751
3752 SDVTList VTs = Indexed ?
3753 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3754 SDValue Ops[] = { Chain, Ptr, Offset };
3755 FoldingSetNodeID ID;
3756 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
3757 ID.AddInteger(AM);
3758 ID.AddInteger(ExtType);
3759 ID.AddInteger(EVT.getRawBits());
3760 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
3761 void *IP = 0;
3762 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3763 return SDValue(E, 0);
3764 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
3765 new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, EVT, SV, SVOffset,
3766 Alignment, isVolatile);
3767 CSEMap.InsertNode(N, IP);
3768 AllNodes.push_back(N);
3769 return SDValue(N, 0);
3770}
3771
Dan Gohman475871a2008-07-27 21:46:04 +00003772SDValue SelectionDAG::getLoad(MVT VT,
3773 SDValue Chain, SDValue Ptr,
3774 const Value *SV, int SVOffset,
3775 bool isVolatile, unsigned Alignment) {
3776 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003777 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3778 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003779}
3780
Dale Johannesene8c17332009-01-29 00:47:48 +00003781SDValue SelectionDAG::getLoad(MVT VT, DebugLoc dl,
3782 SDValue Chain, SDValue Ptr,
3783 const Value *SV, int SVOffset,
3784 bool isVolatile, unsigned Alignment) {
3785 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3786 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3787 SV, SVOffset, VT, isVolatile, Alignment);
3788}
3789
Dan Gohman475871a2008-07-27 21:46:04 +00003790SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3791 SDValue Chain, SDValue Ptr,
3792 const Value *SV,
3793 int SVOffset, MVT EVT,
3794 bool isVolatile, unsigned Alignment) {
3795 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003796 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3797 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003798}
3799
Dale Johannesene8c17332009-01-29 00:47:48 +00003800SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, MVT VT,
3801 SDValue Chain, SDValue Ptr,
3802 const Value *SV,
3803 int SVOffset, MVT EVT,
3804 bool isVolatile, unsigned Alignment) {
3805 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3806 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
3807 SV, SVOffset, EVT, isVolatile, Alignment);
3808}
3809
Dan Gohman475871a2008-07-27 21:46:04 +00003810SDValue
3811SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3812 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003813 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003814 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3815 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003816 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3817 LD->getChain(), Base, Offset, LD->getSrcValue(),
3818 LD->getSrcValueOffset(), LD->getMemoryVT(),
3819 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003820}
3821
Dale Johannesene8c17332009-01-29 00:47:48 +00003822SDValue
3823SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3824 SDValue Offset, ISD::MemIndexedMode AM) {
3825 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3826 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3827 "Load is already a indexed load!");
3828 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3829 LD->getChain(), Base, Offset, LD->getSrcValue(),
3830 LD->getSrcValueOffset(), LD->getMemoryVT(),
3831 LD->isVolatile(), LD->getAlignment());
3832}
3833
Dan Gohman475871a2008-07-27 21:46:04 +00003834SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3835 SDValue Ptr, const Value *SV, int SVOffset,
3836 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003837 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003838
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003839 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3840 Alignment = getMVTAlignment(VT);
3841
Evan Chengad071e12006-10-05 22:57:11 +00003842 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003843 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3844 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003845 FoldingSetNodeID ID;
3846 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003847 ID.AddInteger(ISD::UNINDEXED);
3848 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003849 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003850 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003851 void *IP = 0;
3852 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003853 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003854 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003855 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3856 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003857 CSEMap.InsertNode(N, IP);
3858 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003859 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003860}
3861
Dale Johannesene8c17332009-01-29 00:47:48 +00003862SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3863 SDValue Ptr, const Value *SV, int SVOffset,
3864 bool isVolatile, unsigned Alignment) {
3865 MVT VT = Val.getValueType();
3866
3867 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3868 Alignment = getMVTAlignment(VT);
3869
3870 SDVTList VTs = getVTList(MVT::Other);
3871 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3872 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3873 FoldingSetNodeID ID;
3874 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3875 ID.AddInteger(ISD::UNINDEXED);
3876 ID.AddInteger(false);
3877 ID.AddInteger(VT.getRawBits());
3878 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
3879 void *IP = 0;
3880 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3881 return SDValue(E, 0);
3882 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3883 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false,
3884 VT, SV, SVOffset, Alignment, isVolatile);
3885 CSEMap.InsertNode(N, IP);
3886 AllNodes.push_back(N);
3887 return SDValue(N, 0);
3888}
3889
Dan Gohman475871a2008-07-27 21:46:04 +00003890SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3891 SDValue Ptr, const Value *SV,
3892 int SVOffset, MVT SVT,
3893 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003894 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003895
3896 if (VT == SVT)
3897 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003898
Duncan Sands8e4eb092008-06-08 20:54:56 +00003899 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003900 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003901 "Can't do FP-INT conversion!");
3902
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003903 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3904 Alignment = getMVTAlignment(VT);
3905
Evan Cheng8b2794a2006-10-13 21:14:26 +00003906 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003907 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3908 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003909 FoldingSetNodeID ID;
3910 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003911 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003912 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003913 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003914 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003915 void *IP = 0;
3916 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003917 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003918 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003919 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3920 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003921 CSEMap.InsertNode(N, IP);
3922 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003923 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003924}
3925
Dale Johannesene8c17332009-01-29 00:47:48 +00003926SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3927 SDValue Ptr, const Value *SV,
3928 int SVOffset, MVT SVT,
3929 bool isVolatile, unsigned Alignment) {
3930 MVT VT = Val.getValueType();
3931
3932 if (VT == SVT)
3933 return getStore(Chain, dl, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
3934
3935 assert(VT.bitsGT(SVT) && "Not a truncation?");
3936 assert(VT.isInteger() == SVT.isInteger() &&
3937 "Can't do FP-INT conversion!");
3938
3939 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3940 Alignment = getMVTAlignment(VT);
3941
3942 SDVTList VTs = getVTList(MVT::Other);
3943 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3944 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3945 FoldingSetNodeID ID;
3946 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3947 ID.AddInteger(ISD::UNINDEXED);
3948 ID.AddInteger(1);
3949 ID.AddInteger(SVT.getRawBits());
3950 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
3951 void *IP = 0;
3952 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3953 return SDValue(E, 0);
3954 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3955 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true,
3956 SVT, SV, SVOffset, Alignment, isVolatile);
3957 CSEMap.InsertNode(N, IP);
3958 AllNodes.push_back(N);
3959 return SDValue(N, 0);
3960}
3961
Dan Gohman475871a2008-07-27 21:46:04 +00003962SDValue
3963SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3964 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003965 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3966 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3967 "Store is already a indexed store!");
3968 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003969 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003970 FoldingSetNodeID ID;
3971 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3972 ID.AddInteger(AM);
3973 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003974 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003975 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003976 void *IP = 0;
3977 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003978 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003979 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003980 new (N) StoreSDNode(Ops, VTs, AM,
3981 ST->isTruncatingStore(), ST->getMemoryVT(),
3982 ST->getSrcValue(), ST->getSrcValueOffset(),
3983 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003984 CSEMap.InsertNode(N, IP);
3985 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003986 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003987}
3988
Dale Johannesene8c17332009-01-29 00:47:48 +00003989SDValue
3990SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
3991 SDValue Offset, ISD::MemIndexedMode AM) {
3992 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3993 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3994 "Store is already a indexed store!");
3995 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3996 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3997 FoldingSetNodeID ID;
3998 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3999 ID.AddInteger(AM);
4000 ID.AddInteger(ST->isTruncatingStore());
4001 ID.AddInteger(ST->getMemoryVT().getRawBits());
4002 ID.AddInteger(ST->getRawFlags());
4003 void *IP = 0;
4004 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4005 return SDValue(E, 0);
4006 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
4007 new (N) StoreSDNode(Ops, dl, VTs, AM,
4008 ST->isTruncatingStore(), ST->getMemoryVT(),
4009 ST->getSrcValue(), ST->getSrcValueOffset(),
4010 ST->getAlignment(), ST->isVolatile());
4011 CSEMap.InsertNode(N, IP);
4012 AllNodes.push_back(N);
4013 return SDValue(N, 0);
4014}
4015
Dan Gohman475871a2008-07-27 21:46:04 +00004016SDValue SelectionDAG::getVAArg(MVT VT,
4017 SDValue Chain, SDValue Ptr,
4018 SDValue SV) {
4019 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00004020 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00004021}
4022
Dan Gohman475871a2008-07-27 21:46:04 +00004023SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
4024 const SDUse *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004025 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Ops, NumOps);
4026}
4027
4028SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
4029 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004030 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004031 case 0: return getNode(Opcode, DL, VT);
4032 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4033 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4034 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004035 default: break;
4036 }
4037
Dan Gohman475871a2008-07-27 21:46:04 +00004038 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004039 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004040 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004041 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004042}
4043
Dan Gohman475871a2008-07-27 21:46:04 +00004044SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
4045 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004046 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Ops, NumOps);
4047}
4048
4049SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
4050 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004051 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004052 case 0: return getNode(Opcode, DL, VT);
4053 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4054 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4055 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004056 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004057 }
Chris Lattnerde202b32005-11-09 23:47:37 +00004058
Chris Lattneref847df2005-04-09 03:27:28 +00004059 switch (Opcode) {
4060 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004061 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004062 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004063 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4064 "LHS and RHS of condition must have same type!");
4065 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4066 "True and False arms of SelectCC must have same type!");
4067 assert(Ops[2].getValueType() == VT &&
4068 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004069 break;
4070 }
4071 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004072 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004073 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4074 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004075 break;
4076 }
Chris Lattneref847df2005-04-09 03:27:28 +00004077 }
4078
Chris Lattner385328c2005-05-14 07:42:29 +00004079 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004080 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004081 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004082
Chris Lattner43247a12005-08-25 19:12:10 +00004083 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00004084 FoldingSetNodeID ID;
4085 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004086 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004087
Chris Lattnera5682852006-08-07 23:03:03 +00004088 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004089 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004090
Dan Gohmanfed90b62008-07-28 21:51:04 +00004091 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004092 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004093 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004094 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004095 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004096 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004097 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004098
Chris Lattneref847df2005-04-09 03:27:28 +00004099 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004100#ifndef NDEBUG
4101 VerifyNode(N);
4102#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004103 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004104}
4105
Dan Gohman475871a2008-07-27 21:46:04 +00004106SDValue SelectionDAG::getNode(unsigned Opcode,
4107 const std::vector<MVT> &ResultTys,
4108 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004109 return getNode(Opcode, DebugLoc::getUnknownLoc(), ResultTys, Ops, NumOps);
4110}
4111
4112SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
4113 const std::vector<MVT> &ResultTys,
4114 const SDValue *Ops, unsigned NumOps) {
4115 return getNode(Opcode, DL, getNodeValueTypes(ResultTys), ResultTys.size(),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004116 Ops, NumOps);
4117}
4118
Dan Gohman475871a2008-07-27 21:46:04 +00004119SDValue SelectionDAG::getNode(unsigned Opcode,
4120 const MVT *VTs, unsigned NumVTs,
4121 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004122 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTs, NumVTs, Ops, NumOps);
4123}
4124
4125SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
4126 const MVT *VTs, unsigned NumVTs,
4127 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004128 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004129 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4130 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00004131}
4132
Dan Gohman475871a2008-07-27 21:46:04 +00004133SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4134 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004135 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, Ops, NumOps);
4136}
4137
4138SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4139 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004140 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004141 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004142
Chris Lattner5f056bf2005-07-10 01:55:33 +00004143 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004144 // FIXME: figure out how to safely handle things like
4145 // int foo(int x) { return 1 << (x & 255); }
4146 // int bar() { return foo(256); }
4147#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00004148 case ISD::SRA_PARTS:
4149 case ISD::SRL_PARTS:
4150 case ISD::SHL_PARTS:
4151 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00004152 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004153 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004154 else if (N3.getOpcode() == ISD::AND)
4155 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4156 // If the and is only masking out bits that cannot effect the shift,
4157 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004158 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004159 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004160 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004161 }
4162 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00004163#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00004164 }
Chris Lattner89c34632005-05-14 06:20:26 +00004165
Chris Lattner43247a12005-08-25 19:12:10 +00004166 // Memoize the node unless it returns a flag.
4167 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00004168 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00004169 FoldingSetNodeID ID;
4170 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004171 void *IP = 0;
4172 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004173 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004174 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004175 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004176 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004177 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004178 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004179 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004180 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004181 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004182 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004183 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004184 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004185 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004186 }
Chris Lattnera5682852006-08-07 23:03:03 +00004187 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004188 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004189 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004190 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004191 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004192 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004193 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004194 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004195 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004196 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004197 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004198 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004199 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004200 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004201 }
Chris Lattner43247a12005-08-25 19:12:10 +00004202 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004203 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004204#ifndef NDEBUG
4205 VerifyNode(N);
4206#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004207 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004208}
4209
Dan Gohman475871a2008-07-27 21:46:04 +00004210SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004211 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList);
4212}
4213
4214SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4215 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004216}
4217
Dan Gohman475871a2008-07-27 21:46:04 +00004218SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004219 SDValue N1) {
4220 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1);
4221}
4222
4223SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4224 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004225 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004226 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004227}
4228
Dan Gohman475871a2008-07-27 21:46:04 +00004229SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4230 SDValue N1, SDValue N2) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004231 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2);
4232}
4233
4234SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4235 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004236 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004237 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004238}
4239
Dan Gohman475871a2008-07-27 21:46:04 +00004240SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4241 SDValue N1, SDValue N2, SDValue N3) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004242 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3);
4243}
4244
4245SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4246 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004247 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004248 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004249}
4250
Dan Gohman475871a2008-07-27 21:46:04 +00004251SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4252 SDValue N1, SDValue N2, SDValue N3,
4253 SDValue N4) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004254 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3, N4);
4255}
4256
4257SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4258 SDValue N1, SDValue N2, SDValue N3,
4259 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004260 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004261 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004262}
4263
Dan Gohman475871a2008-07-27 21:46:04 +00004264SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4265 SDValue N1, SDValue N2, SDValue N3,
4266 SDValue N4, SDValue N5) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004267 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3, N4, N5);
4268}
4269
4270SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4271 SDValue N1, SDValue N2, SDValue N3,
4272 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004273 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004274 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004275}
4276
Duncan Sands83ec4b62008-06-06 12:08:01 +00004277SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004278 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004279}
4280
Duncan Sands83ec4b62008-06-06 12:08:01 +00004281SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004282 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4283 E = VTList.rend(); I != E; ++I)
4284 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4285 return *I;
4286
4287 MVT *Array = Allocator.Allocate<MVT>(2);
4288 Array[0] = VT1;
4289 Array[1] = VT2;
4290 SDVTList Result = makeVTList(Array, 2);
4291 VTList.push_back(Result);
4292 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004293}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004294
4295SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
4296 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4297 E = VTList.rend(); I != E; ++I)
4298 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4299 I->VTs[2] == VT3)
4300 return *I;
4301
4302 MVT *Array = Allocator.Allocate<MVT>(3);
4303 Array[0] = VT1;
4304 Array[1] = VT2;
4305 Array[2] = VT3;
4306 SDVTList Result = makeVTList(Array, 3);
4307 VTList.push_back(Result);
4308 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004309}
4310
Bill Wendling13d6d442008-12-01 23:28:22 +00004311SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
4312 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4313 E = VTList.rend(); I != E; ++I)
4314 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4315 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4316 return *I;
4317
4318 MVT *Array = Allocator.Allocate<MVT>(3);
4319 Array[0] = VT1;
4320 Array[1] = VT2;
4321 Array[2] = VT3;
4322 Array[3] = VT4;
4323 SDVTList Result = makeVTList(Array, 4);
4324 VTList.push_back(Result);
4325 return Result;
4326}
4327
Duncan Sands83ec4b62008-06-06 12:08:01 +00004328SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004329 switch (NumVTs) {
4330 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004331 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004332 case 2: return getVTList(VTs[0], VTs[1]);
4333 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
4334 default: break;
4335 }
4336
Dan Gohmane8be6c62008-07-17 19:10:17 +00004337 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4338 E = VTList.rend(); I != E; ++I) {
4339 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4340 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00004341
4342 bool NoMatch = false;
4343 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004344 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004345 NoMatch = true;
4346 break;
4347 }
4348 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004349 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004350 }
4351
Dan Gohmane8be6c62008-07-17 19:10:17 +00004352 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
4353 std::copy(VTs, VTs+NumVTs, Array);
4354 SDVTList Result = makeVTList(Array, NumVTs);
4355 VTList.push_back(Result);
4356 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004357}
4358
4359
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004360/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4361/// specified operands. If the resultant node already exists in the DAG,
4362/// this does not modify the specified node, instead it returns the node that
4363/// already exists. If the resultant node does not exist in the DAG, the
4364/// input node is returned. As a degenerate case, if you specify the same
4365/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00004366SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004367 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004368 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
4369
4370 // Check to see if there is no change.
4371 if (Op == N->getOperand(0)) return InN;
4372
4373 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004374 void *InsertPos = 0;
4375 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004376 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004377
Dan Gohman79acd2b2008-07-21 22:38:59 +00004378 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004379 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004380 if (!RemoveNodeFromCSEMaps(N))
4381 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004382
4383 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004384 N->OperandList[0].set(Op);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004385
4386 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004387 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004388 return InN;
4389}
4390
Dan Gohman475871a2008-07-27 21:46:04 +00004391SDValue SelectionDAG::
4392UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004393 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004394 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
4395
4396 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004397 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4398 return InN; // No operands changed, just return the input node.
4399
4400 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004401 void *InsertPos = 0;
4402 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004403 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004404
Dan Gohman79acd2b2008-07-21 22:38:59 +00004405 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004406 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004407 if (!RemoveNodeFromCSEMaps(N))
4408 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004409
4410 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004411 if (N->OperandList[0] != Op1)
4412 N->OperandList[0].set(Op1);
4413 if (N->OperandList[1] != Op2)
4414 N->OperandList[1].set(Op2);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004415
4416 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004417 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004418 return InN;
4419}
4420
Dan Gohman475871a2008-07-27 21:46:04 +00004421SDValue SelectionDAG::
4422UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4423 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004424 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004425}
4426
Dan Gohman475871a2008-07-27 21:46:04 +00004427SDValue SelectionDAG::
4428UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4429 SDValue Op3, SDValue Op4) {
4430 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004431 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004432}
4433
Dan Gohman475871a2008-07-27 21:46:04 +00004434SDValue SelectionDAG::
4435UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4436 SDValue Op3, SDValue Op4, SDValue Op5) {
4437 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004438 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004439}
4440
Dan Gohman475871a2008-07-27 21:46:04 +00004441SDValue SelectionDAG::
4442UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004443 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004444 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004445 "Update with wrong number of operands");
4446
4447 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004448 bool AnyChange = false;
4449 for (unsigned i = 0; i != NumOps; ++i) {
4450 if (Ops[i] != N->getOperand(i)) {
4451 AnyChange = true;
4452 break;
4453 }
4454 }
4455
4456 // No operands changed, just return the input node.
4457 if (!AnyChange) return InN;
4458
4459 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004460 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004461 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004462 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004463
Dan Gohman7ceda162008-05-02 00:05:03 +00004464 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004465 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004466 if (!RemoveNodeFromCSEMaps(N))
4467 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004468
4469 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004470 for (unsigned i = 0; i != NumOps; ++i)
4471 if (N->OperandList[i] != Ops[i])
4472 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004473
4474 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004475 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004476 return InN;
4477}
4478
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004479/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004480/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004481void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004482 // Unlike the code in MorphNodeTo that does this, we don't need to
4483 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004484 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4485 SDUse &Use = *I++;
4486 Use.set(SDValue());
4487 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004488}
Chris Lattner149c58c2005-08-16 18:17:10 +00004489
Dan Gohmane8be6c62008-07-17 19:10:17 +00004490/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4491/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004492///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004493SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004494 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004495 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004496 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004497}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004498
Dan Gohmane8be6c62008-07-17 19:10:17 +00004499SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004500 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004501 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004502 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004503 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004504}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004505
Dan Gohmane8be6c62008-07-17 19:10:17 +00004506SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004507 MVT VT, SDValue Op1,
4508 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004509 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004510 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004511 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004512}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004513
Dan Gohmane8be6c62008-07-17 19:10:17 +00004514SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004515 MVT VT, SDValue Op1,
4516 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004517 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004518 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004519 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004520}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004521
Dan Gohmane8be6c62008-07-17 19:10:17 +00004522SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004523 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004524 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004525 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004526 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004527}
4528
Dan Gohmane8be6c62008-07-17 19:10:17 +00004529SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004530 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004531 unsigned NumOps) {
4532 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004533 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004534}
4535
Dan Gohmane8be6c62008-07-17 19:10:17 +00004536SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004537 MVT VT1, MVT VT2) {
4538 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004539 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004540}
4541
Dan Gohmane8be6c62008-07-17 19:10:17 +00004542SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004543 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004544 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004545 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004546 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004547}
4548
Bill Wendling13d6d442008-12-01 23:28:22 +00004549SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
4550 MVT VT1, MVT VT2, MVT VT3, MVT VT4,
4551 const SDValue *Ops, unsigned NumOps) {
4552 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4553 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4554}
4555
Dan Gohmane8be6c62008-07-17 19:10:17 +00004556SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004557 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004558 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004559 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004560 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004561 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004562}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004563
Dan Gohmane8be6c62008-07-17 19:10:17 +00004564SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004565 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004566 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004567 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004568 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004569 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004570}
4571
Dan Gohmane8be6c62008-07-17 19:10:17 +00004572SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004573 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004574 SDValue Op1, SDValue Op2,
4575 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004576 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004577 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004578 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004579}
4580
Dan Gohmane8be6c62008-07-17 19:10:17 +00004581SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Bill Wendling13d6d442008-12-01 23:28:22 +00004582 MVT VT1, MVT VT2, MVT VT3,
4583 SDValue Op1, SDValue Op2,
4584 SDValue Op3) {
4585 SDVTList VTs = getVTList(VT1, VT2, VT3);
4586 SDValue Ops[] = { Op1, Op2, Op3 };
4587 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4588}
4589
4590SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004591 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004592 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004593 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4594}
4595
4596SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4597 MVT VT) {
4598 SDVTList VTs = getVTList(VT);
4599 return MorphNodeTo(N, Opc, VTs, 0, 0);
4600}
4601
4602SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004603 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004604 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004605 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004606 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4607}
4608
4609SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004610 MVT VT, SDValue Op1,
4611 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004612 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004613 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004614 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4615}
4616
4617SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004618 MVT VT, SDValue Op1,
4619 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004620 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004621 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004622 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4623}
4624
4625SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004626 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004627 unsigned NumOps) {
4628 SDVTList VTs = getVTList(VT);
4629 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4630}
4631
4632SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004633 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004634 unsigned NumOps) {
4635 SDVTList VTs = getVTList(VT1, VT2);
4636 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4637}
4638
4639SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4640 MVT VT1, MVT VT2) {
4641 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004642 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004643}
4644
4645SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4646 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004647 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004648 SDVTList VTs = getVTList(VT1, VT2, VT3);
4649 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4650}
4651
4652SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4653 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004654 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004655 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004656 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004657 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4658}
4659
4660SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4661 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004662 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004663 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004664 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004665 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4666}
4667
4668SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4669 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004670 SDValue Op1, SDValue Op2,
4671 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004672 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004673 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004674 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4675}
4676
4677/// MorphNodeTo - These *mutate* the specified node to have the specified
4678/// return type, opcode, and operands.
4679///
4680/// Note that MorphNodeTo returns the resultant node. If there is already a
4681/// node of the specified opcode and operands, it returns that node instead of
4682/// the current one.
4683///
4684/// Using MorphNodeTo is faster than creating a new node and swapping it in
4685/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004686/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004687/// the node's users.
4688///
4689SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004690 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004691 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004692 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004693 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004694 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4695 FoldingSetNodeID ID;
4696 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4697 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4698 return ON;
4699 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004700
Dan Gohman095cc292008-09-13 01:54:27 +00004701 if (!RemoveNodeFromCSEMaps(N))
4702 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004703
Dan Gohmane8be6c62008-07-17 19:10:17 +00004704 // Start the morphing.
4705 N->NodeType = Opc;
4706 N->ValueList = VTs.VTs;
4707 N->NumValues = VTs.NumVTs;
4708
4709 // Clear the operands list, updating used nodes to remove this from their
4710 // use list. Keep track of any operands that become dead as a result.
4711 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004712 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4713 SDUse &Use = *I++;
4714 SDNode *Used = Use.getNode();
4715 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004716 if (Used->use_empty())
4717 DeadNodeSet.insert(Used);
4718 }
4719
4720 // If NumOps is larger than the # of operands we currently have, reallocate
4721 // the operand list.
4722 if (NumOps > N->NumOperands) {
4723 if (N->OperandsNeedDelete)
4724 delete[] N->OperandList;
Bill Wendlinga1dc6022008-10-19 20:51:12 +00004725
Dan Gohmane8be6c62008-07-17 19:10:17 +00004726 if (N->isMachineOpcode()) {
4727 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004728 // remainder of the current SelectionDAG iteration, so we can allocate
4729 // the operands directly out of a pool with no recycling metadata.
4730 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004731 N->OperandsNeedDelete = false;
4732 } else {
4733 N->OperandList = new SDUse[NumOps];
4734 N->OperandsNeedDelete = true;
4735 }
4736 }
4737
4738 // Assign the new operands.
4739 N->NumOperands = NumOps;
4740 for (unsigned i = 0, e = NumOps; i != e; ++i) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004741 N->OperandList[i].setUser(N);
Dan Gohmane7852d02009-01-26 04:35:06 +00004742 N->OperandList[i].setInitial(Ops[i]);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004743 }
4744
4745 // Delete any nodes that are still dead after adding the uses for the
4746 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004747 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004748 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4749 E = DeadNodeSet.end(); I != E; ++I)
4750 if ((*I)->use_empty())
4751 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004752 RemoveDeadNodes(DeadNodes);
4753
Dan Gohmane8be6c62008-07-17 19:10:17 +00004754 if (IP)
4755 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004756 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004757}
4758
Chris Lattner0fb094f2005-11-19 01:44:53 +00004759
Evan Cheng6ae46c42006-02-09 07:15:23 +00004760/// getTargetNode - These are used for target selectors to create a new node
4761/// with specified return type(s), target opcode, and operands.
4762///
4763/// Note that getTargetNode returns the resultant node. If there is already a
4764/// node of the specified opcode and operands, it returns that node instead of
4765/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004766SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004767 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004768}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004769SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT) {
4770 return getNode(~Opcode, dl, VT).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004771}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004772
Dan Gohman475871a2008-07-27 21:46:04 +00004773SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004774 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004775}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004776SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4777 SDValue Op1) {
4778 return getNode(~Opcode, dl, VT, Op1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004779}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004780
Duncan Sands83ec4b62008-06-06 12:08:01 +00004781SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004782 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004783 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004784}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004785SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004786 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004787 return getNode(~Opcode, dl, VT, Op1, Op2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004788}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004789
Duncan Sands83ec4b62008-06-06 12:08:01 +00004790SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004791 SDValue Op1, SDValue Op2,
4792 SDValue Op3) {
4793 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004794}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004795SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4796 SDValue Op1, SDValue Op2,
4797 SDValue Op3) {
4798 return getNode(~Opcode, dl, VT, Op1, Op2, Op3).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004799}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004800
Duncan Sands83ec4b62008-06-06 12:08:01 +00004801SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004802 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004803 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004804}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004805SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004806 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004807 return getNode(~Opcode, dl, VT, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004808}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004809
4810SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4811 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4812 SDValue Op;
4813 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004814}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004815SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004816 MVT VT1, MVT VT2) {
4817 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4818 SDValue Op;
Bill Wendling56ab1a22009-01-29 09:01:55 +00004819 return getNode(~Opcode, dl, VTs, 2, &Op, 0).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004820}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004821
Duncan Sands83ec4b62008-06-06 12:08:01 +00004822SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004823 MVT VT2, SDValue Op1) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004824 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4825 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004826}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004827SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
Dale Johannesene8c17332009-01-29 00:47:48 +00004828 MVT VT2, SDValue Op1) {
4829 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004830 return getNode(~Opcode, dl, VTs, 2, &Op1, 1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004831}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004832
Duncan Sands83ec4b62008-06-06 12:08:01 +00004833SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004834 MVT VT2, SDValue Op1,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004835 SDValue Op2) {
4836 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4837 SDValue Ops[] = { Op1, Op2 };
4838 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Bill Wendling6e1bb382009-01-29 05:27:31 +00004839}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004840SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4841 MVT VT2, SDValue Op1,
4842 SDValue Op2) {
4843 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4844 SDValue Ops[] = { Op1, Op2 };
4845 return getNode(~Opcode, dl, VTs, 2, Ops, 2).getNode();
4846}
4847
4848SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Bill Wendling6e1bb382009-01-29 05:27:31 +00004849 MVT VT2, SDValue Op1,
4850 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004851 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004852 SDValue Ops[] = { Op1, Op2, Op3 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004853 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004854}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004855SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4856 MVT VT2, SDValue Op1,
4857 SDValue Op2, SDValue Op3) {
4858 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4859 SDValue Ops[] = { Op1, Op2, Op3 };
4860 return getNode(~Opcode, dl, VTs, 2, Ops, 3).getNode();
4861}
4862
4863SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004864 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004865 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4866 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004867}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004868SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004869 MVT VT1, MVT VT2,
4870 const SDValue *Ops, unsigned NumOps) {
4871 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004872 return getNode(~Opcode, dl, VTs, 2, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004873}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004874
4875SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004876 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004877 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4878 SDValue Ops[] = { Op1, Op2 };
4879 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004880}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004881SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004882 MVT VT1, MVT VT2, MVT VT3,
4883 SDValue Op1, SDValue Op2) {
4884 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4885 SDValue Ops[] = { Op1, Op2 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004886 return getNode(~Opcode, dl, VTs, 3, Ops, 2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004887}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004888
4889SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
4890 SDValue Op1, SDValue Op2,
4891 SDValue Op3) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004892 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4893 SDValue Ops[] = { Op1, Op2, Op3 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004894 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004895}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004896SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Bill Wendling6e1bb382009-01-29 05:27:31 +00004897 MVT VT1, MVT VT2, MVT VT3,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004898 SDValue Op1, SDValue Op2,
4899 SDValue Op3) {
4900 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4901 SDValue Ops[] = { Op1, Op2, Op3 };
4902 return getNode(~Opcode, dl, VTs, 3, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004903}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004904
4905SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
4906 const SDValue *Ops, unsigned NumOps) {
4907 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4908 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
4909}
4910SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004911 MVT VT1, MVT VT2, MVT VT3,
4912 const SDValue *Ops, unsigned NumOps) {
4913 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004914 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004915}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004916
4917SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4918 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004919 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004920 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004921 VTList.push_back(VT1);
4922 VTList.push_back(VT2);
4923 VTList.push_back(VT3);
4924 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004925 const MVT *VTs = getNodeValueTypes(VTList);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004926 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004927}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004928SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4929 MVT VT2, MVT VT3, MVT VT4,
4930 const SDValue *Ops, unsigned NumOps) {
4931 std::vector<MVT> VTList;
4932 VTList.push_back(VT1);
4933 VTList.push_back(VT2);
4934 VTList.push_back(VT3);
4935 VTList.push_back(VT4);
4936 const MVT *VTs = getNodeValueTypes(VTList);
4937 return getNode(~Opcode, dl, VTs, 4, Ops, NumOps).getNode();
4938}
4939
Evan Cheng39305cf2007-10-05 01:10:49 +00004940SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004941 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004942 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004943 const MVT *VTs = getNodeValueTypes(ResultTys);
4944 return getNode(~Opcode, VTs, ResultTys.size(),
4945 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004946}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004947SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004948 const std::vector<MVT> &ResultTys,
4949 const SDValue *Ops, unsigned NumOps) {
4950 const MVT *VTs = getNodeValueTypes(ResultTys);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004951 return getNode(~Opcode, dl, VTs, ResultTys.size(),
Dale Johannesene8c17332009-01-29 00:47:48 +00004952 Ops, NumOps).getNode();
4953}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004954
Evan Cheng08b11732008-03-22 01:55:50 +00004955/// getNodeIfExists - Get the specified node if it's already available, or
4956/// else return NULL.
4957SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004958 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004959 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4960 FoldingSetNodeID ID;
4961 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4962 void *IP = 0;
4963 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4964 return E;
4965 }
4966 return NULL;
4967}
4968
Evan Cheng99157a02006-08-07 22:13:29 +00004969/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004970/// This can cause recursive merging of nodes in the DAG.
4971///
Chris Lattner11d049c2008-02-03 03:35:22 +00004972/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004973///
Dan Gohman475871a2008-07-27 21:46:04 +00004974void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004975 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004976 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004977 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004978 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004979 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004980
Dan Gohman39946102009-01-25 16:29:12 +00004981 // Iterate over all the existing uses of From. New uses will be added
4982 // to the beginning of the use list, which we avoid visiting.
4983 // This specifically avoids visiting uses of From that arise while the
4984 // replacement is happening, because any such uses would be the result
4985 // of CSE: If an existing node looks like From after one of its operands
4986 // is replaced by To, we don't want to replace of all its users with To
4987 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00004988 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4989 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004990 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004991
Chris Lattner8b8749f2005-08-17 19:00:20 +00004992 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004993 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00004994
Dan Gohman39946102009-01-25 16:29:12 +00004995 // A user can appear in a use list multiple times, and when this
4996 // happens the uses are usually next to each other in the list.
4997 // To help reduce the number of CSE recomputations, process all
4998 // the uses of this user that we can find this way.
4999 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005000 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005001 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005002 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005003 } while (UI != UE && *UI == User);
5004
5005 // Now that we have modified User, add it back to the CSE maps. If it
5006 // already exists there, recursively merge the results together.
5007 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00005008 }
5009}
5010
5011/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5012/// This can cause recursive merging of nodes in the DAG.
5013///
5014/// This version assumes From/To have matching types and numbers of result
5015/// values.
5016///
Chris Lattner1e111c72005-09-07 05:37:01 +00005017void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005018 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005019 assert(From->getVTList().VTs == To->getVTList().VTs &&
5020 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005021 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00005022
5023 // Handle the trivial case.
5024 if (From == To)
5025 return;
5026
Dan Gohmandbe664a2009-01-19 21:44:21 +00005027 // Iterate over just the existing users of From. See the comments in
5028 // the ReplaceAllUsesWith above.
5029 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
5030 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005031 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005032
Chris Lattner8b52f212005-08-26 18:36:28 +00005033 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005034 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005035
Dan Gohman39946102009-01-25 16:29:12 +00005036 // A user can appear in a use list multiple times, and when this
5037 // happens the uses are usually next to each other in the list.
5038 // To help reduce the number of CSE recomputations, process all
5039 // the uses of this user that we can find this way.
5040 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005041 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005042 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005043 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005044 } while (UI != UE && *UI == User);
5045
5046 // Now that we have modified User, add it back to the CSE maps. If it
5047 // already exists there, recursively merge the results together.
5048 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005049 }
5050}
5051
Chris Lattner8b52f212005-08-26 18:36:28 +00005052/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5053/// This can cause recursive merging of nodes in the DAG.
5054///
5055/// This version can replace From with any result values. To must match the
5056/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00005057void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00005058 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005059 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005060 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00005061 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005062
Dan Gohmandbe664a2009-01-19 21:44:21 +00005063 // Iterate over just the existing users of From. See the comments in
5064 // the ReplaceAllUsesWith above.
5065 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
5066 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005067 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005068
Chris Lattner7b2880c2005-08-24 22:44:39 +00005069 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005070 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005071
Dan Gohman39946102009-01-25 16:29:12 +00005072 // A user can appear in a use list multiple times, and when this
5073 // happens the uses are usually next to each other in the list.
5074 // To help reduce the number of CSE recomputations, process all
5075 // the uses of this user that we can find this way.
5076 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005077 SDUse &Use = UI.getUse();
5078 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005079 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005080 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005081 } while (UI != UE && *UI == User);
5082
5083 // Now that we have modified User, add it back to the CSE maps. If it
5084 // already exists there, recursively merge the results together.
5085 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005086 }
5087}
5088
Chris Lattner012f2412006-02-17 21:58:01 +00005089/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005090/// uses of other values produced by From.getNode() alone. The Deleted
5091/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005092void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005093 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005094 // Handle the really simple, really trivial case efficiently.
5095 if (From == To) return;
5096
Chris Lattner012f2412006-02-17 21:58:01 +00005097 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005098 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005099 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005100 return;
5101 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005102
Dan Gohman39946102009-01-25 16:29:12 +00005103 // Iterate over just the existing users of From. See the comments in
5104 // the ReplaceAllUsesWith above.
5105 SDNode::use_iterator UI = From.getNode()->use_begin(),
5106 UE = From.getNode()->use_end();
5107 while (UI != UE) {
5108 SDNode *User = *UI;
5109 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005110
Dan Gohman39946102009-01-25 16:29:12 +00005111 // A user can appear in a use list multiple times, and when this
5112 // happens the uses are usually next to each other in the list.
5113 // To help reduce the number of CSE recomputations, process all
5114 // the uses of this user that we can find this way.
5115 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005116 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005117
5118 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005119 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005120 ++UI;
5121 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005122 }
Dan Gohman39946102009-01-25 16:29:12 +00005123
5124 // If this node hasn't been modified yet, it's still in the CSE maps,
5125 // so remove its old self from the CSE maps.
5126 if (!UserRemovedFromCSEMaps) {
5127 RemoveNodeFromCSEMaps(User);
5128 UserRemovedFromCSEMaps = true;
5129 }
5130
5131 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005132 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005133 } while (UI != UE && *UI == User);
5134
5135 // We are iterating over all uses of the From node, so if a use
5136 // doesn't use the specific value, no changes are made.
5137 if (!UserRemovedFromCSEMaps)
5138 continue;
5139
Chris Lattner01d029b2007-10-15 06:10:22 +00005140 // Now that we have modified User, add it back to the CSE maps. If it
5141 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00005142 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005143 }
5144}
5145
Dan Gohman39946102009-01-25 16:29:12 +00005146namespace {
5147 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5148 /// to record information about a use.
5149 struct UseMemo {
5150 SDNode *User;
5151 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005152 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005153 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005154
5155 /// operator< - Sort Memos by User.
5156 bool operator<(const UseMemo &L, const UseMemo &R) {
5157 return (intptr_t)L.User < (intptr_t)R.User;
5158 }
Dan Gohman39946102009-01-25 16:29:12 +00005159}
5160
Dan Gohmane8be6c62008-07-17 19:10:17 +00005161/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005162/// uses of other values produced by From.getNode() alone. The same value
5163/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005164/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005165void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5166 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005167 unsigned Num,
5168 DAGUpdateListener *UpdateListener){
5169 // Handle the simple, trivial case efficiently.
5170 if (Num == 1)
5171 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5172
Dan Gohman39946102009-01-25 16:29:12 +00005173 // Read up all the uses and make records of them. This helps
5174 // processing new uses that are introduced during the
5175 // replacement process.
5176 SmallVector<UseMemo, 4> Uses;
5177 for (unsigned i = 0; i != Num; ++i) {
5178 unsigned FromResNo = From[i].getResNo();
5179 SDNode *FromNode = From[i].getNode();
5180 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005181 E = FromNode->use_end(); UI != E; ++UI) {
5182 SDUse &Use = UI.getUse();
5183 if (Use.getResNo() == FromResNo) {
5184 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005185 Uses.push_back(Memo);
5186 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005187 }
Dan Gohman39946102009-01-25 16:29:12 +00005188 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005189
Dan Gohmane7852d02009-01-26 04:35:06 +00005190 // Sort the uses, so that all the uses from a given User are together.
5191 std::sort(Uses.begin(), Uses.end());
5192
Dan Gohman39946102009-01-25 16:29:12 +00005193 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5194 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005195 // We know that this user uses some value of From. If it is the right
5196 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005197 SDNode *User = Uses[UseIndex].User;
5198
5199 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005200 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005201
Dan Gohmane7852d02009-01-26 04:35:06 +00005202 // The Uses array is sorted, so all the uses for a given User
5203 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005204 // To help reduce the number of CSE recomputations, process all
5205 // the uses of this user that we can find this way.
5206 do {
5207 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005208 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005209 ++UseIndex;
5210
Dan Gohmane7852d02009-01-26 04:35:06 +00005211 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005212 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5213
Dan Gohmane8be6c62008-07-17 19:10:17 +00005214 // Now that we have modified User, add it back to the CSE maps. If it
5215 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00005216 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005217 }
5218}
5219
Evan Chenge6f35d82006-08-01 08:20:41 +00005220/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005221/// based on their topological order. It returns the maximum id and a vector
5222/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005223unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005224
Dan Gohmanf06c8352008-09-30 18:30:35 +00005225 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005226
Dan Gohmanf06c8352008-09-30 18:30:35 +00005227 // SortedPos tracks the progress of the algorithm. Nodes before it are
5228 // sorted, nodes after it are unsorted. When the algorithm completes
5229 // it is at the end of the list.
5230 allnodes_iterator SortedPos = allnodes_begin();
5231
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005232 // Visit all the nodes. Move nodes with no operands to the front of
5233 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005234 // operand count. Before we do this, the Node Id fields of the nodes
5235 // may contain arbitrary values. After, the Node Id fields for nodes
5236 // before SortedPos will contain the topological sort index, and the
5237 // Node Id fields for nodes At SortedPos and after will contain the
5238 // count of outstanding operands.
5239 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5240 SDNode *N = I++;
5241 unsigned Degree = N->getNumOperands();
5242 if (Degree == 0) {
5243 // A node with no uses, add it to the result array immediately.
5244 N->setNodeId(DAGSize++);
5245 allnodes_iterator Q = N;
5246 if (Q != SortedPos)
5247 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
5248 ++SortedPos;
5249 } else {
5250 // Temporarily use the Node Id as scratch space for the degree count.
5251 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005252 }
5253 }
5254
Dan Gohmanf06c8352008-09-30 18:30:35 +00005255 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5256 // such that by the time the end is reached all nodes will be sorted.
5257 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5258 SDNode *N = I;
5259 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5260 UI != UE; ++UI) {
5261 SDNode *P = *UI;
5262 unsigned Degree = P->getNodeId();
5263 --Degree;
5264 if (Degree == 0) {
5265 // All of P's operands are sorted, so P may sorted now.
5266 P->setNodeId(DAGSize++);
5267 if (P != SortedPos)
5268 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
5269 ++SortedPos;
5270 } else {
5271 // Update P's outstanding operand count.
5272 P->setNodeId(Degree);
5273 }
5274 }
5275 }
5276
5277 assert(SortedPos == AllNodes.end() &&
5278 "Topological sort incomplete!");
5279 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5280 "First node in topological sort is not the entry token!");
5281 assert(AllNodes.front().getNodeId() == 0 &&
5282 "First node in topological sort has non-zero id!");
5283 assert(AllNodes.front().getNumOperands() == 0 &&
5284 "First node in topological sort has operands!");
5285 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5286 "Last node in topologic sort has unexpected id!");
5287 assert(AllNodes.back().use_empty() &&
5288 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005289 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005290 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005291}
5292
5293
Evan Cheng091cba12006-07-27 06:39:06 +00005294
Jim Laskey58b968b2005-08-17 20:08:02 +00005295//===----------------------------------------------------------------------===//
5296// SDNode Class
5297//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005298
Chris Lattner48b85922007-02-04 02:41:42 +00005299HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005300 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005301}
5302
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005303GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Dan Gohman6520e202008-10-18 02:06:02 +00005304 MVT VT, int64_t o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005305 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00005306 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005307 // Thread Local
5308 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
5309 // Non Thread Local
5310 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
5311 getSDVTList(VT)), Offset(o) {
5312 TheGlobal = const_cast<GlobalValue*>(GA);
5313}
Chris Lattner48b85922007-02-04 02:41:42 +00005314
Dan Gohman1ea58a52008-07-09 22:08:04 +00005315MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00005316 const Value *srcValue, int SVO,
5317 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00005318 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005319 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00005320
5321 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5322 assert(getAlignment() == alignment && "Alignment representation error!");
5323 assert(isVolatile() == vol && "Volatile representation error!");
5324}
5325
Mon P Wangc4d10212008-10-17 18:22:58 +00005326MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops,
5327 unsigned NumOps, MVT memvt, const Value *srcValue,
5328 int SVO, unsigned alignment, bool vol)
5329 : SDNode(Opc, VTs, Ops, NumOps),
5330 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
5331 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
5332 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5333 assert(getAlignment() == alignment && "Alignment representation error!");
5334 assert(isVolatile() == vol && "Volatile representation error!");
5335}
5336
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005337MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, MVT memvt,
5338 const Value *srcValue, int SVO,
5339 unsigned alignment, bool vol)
5340 : SDNode(Opc, dl, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
5341 Flags(encodeMemSDNodeFlags(vol, alignment)) {
5342
5343 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5344 assert(getAlignment() == alignment && "Alignment representation error!");
5345 assert(isVolatile() == vol && "Volatile representation error!");
5346}
5347
5348MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
5349 const SDValue *Ops,
5350 unsigned NumOps, MVT memvt, const Value *srcValue,
5351 int SVO, unsigned alignment, bool vol)
5352 : SDNode(Opc, dl, VTs, Ops, NumOps),
5353 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
5354 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
5355 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5356 assert(getAlignment() == alignment && "Alignment representation error!");
5357 assert(isVolatile() == vol && "Volatile representation error!");
5358}
5359
Dan Gohman36b5c132008-04-07 19:35:22 +00005360/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00005361/// reference performed by this memory reference.
5362MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesenfea90882008-10-24 01:06:58 +00005363 int Flags = 0;
Dan Gohman1ea58a52008-07-09 22:08:04 +00005364 if (isa<LoadSDNode>(this))
5365 Flags = MachineMemOperand::MOLoad;
5366 else if (isa<StoreSDNode>(this))
5367 Flags = MachineMemOperand::MOStore;
Mon P Wangc4d10212008-10-17 18:22:58 +00005368 else if (isa<AtomicSDNode>(this)) {
Dan Gohman1ea58a52008-07-09 22:08:04 +00005369 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
5370 }
Mon P Wangc4d10212008-10-17 18:22:58 +00005371 else {
5372 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
5373 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
5374 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
5375 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
5376 }
Dan Gohman1ea58a52008-07-09 22:08:04 +00005377
5378 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00005379 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
5380
Dan Gohman1ea58a52008-07-09 22:08:04 +00005381 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00005382 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00005383 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00005384 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00005385 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
5386 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00005387 else
5388 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
5389 Size, getAlignment());
5390}
5391
Jim Laskey583bd472006-10-27 23:46:08 +00005392/// Profile - Gather unique data for the node.
5393///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005394void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005395 AddNodeIDNode(ID, this);
5396}
5397
Chris Lattnera3255112005-11-08 23:30:28 +00005398/// getValueTypeList - Return a pointer to the specified value type.
5399///
Duncan Sands83ec4b62008-06-06 12:08:01 +00005400const MVT *SDNode::getValueTypeList(MVT VT) {
5401 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005402 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00005403 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005404 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005405 static MVT VTs[MVT::LAST_VALUETYPE];
5406 VTs[VT.getSimpleVT()] = VT;
5407 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005408 }
Chris Lattnera3255112005-11-08 23:30:28 +00005409}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005410
Chris Lattner5c884562005-01-12 18:37:47 +00005411/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5412/// indicated value. This method ignores uses of other values defined by this
5413/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005414bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005415 assert(Value < getNumValues() && "Bad value!");
5416
Roman Levensteindc1adac2008-04-07 10:06:32 +00005417 // TODO: Only iterate over uses of a given value of the node
5418 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005419 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005420 if (NUses == 0)
5421 return false;
5422 --NUses;
5423 }
Chris Lattner5c884562005-01-12 18:37:47 +00005424 }
5425
5426 // Found exactly the right number of uses?
5427 return NUses == 0;
5428}
5429
5430
Evan Cheng33d55952007-08-02 05:29:38 +00005431/// hasAnyUseOfValue - Return true if there are any use of the indicated
5432/// value. This method ignores uses of other values defined by this operation.
5433bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5434 assert(Value < getNumValues() && "Bad value!");
5435
Dan Gohman1373c1c2008-07-09 22:39:01 +00005436 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005437 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005438 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005439
5440 return false;
5441}
5442
5443
Dan Gohman2a629952008-07-27 18:06:42 +00005444/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005445///
Dan Gohman2a629952008-07-27 18:06:42 +00005446bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005447 bool Seen = false;
5448 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005449 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005450 if (User == this)
5451 Seen = true;
5452 else
5453 return false;
5454 }
5455
5456 return Seen;
5457}
5458
Evan Chenge6e97e62006-11-03 07:31:32 +00005459/// isOperand - Return true if this node is an operand of N.
5460///
Dan Gohman475871a2008-07-27 21:46:04 +00005461bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005462 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5463 if (*this == N->getOperand(i))
5464 return true;
5465 return false;
5466}
5467
Evan Cheng917be682008-03-04 00:41:45 +00005468bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005469 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005470 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005471 return true;
5472 return false;
5473}
Evan Cheng4ee62112006-02-05 06:29:23 +00005474
Chris Lattner572dee72008-01-16 05:49:24 +00005475/// reachesChainWithoutSideEffects - Return true if this operand (which must
5476/// be a chain) reaches the specified operand without crossing any
5477/// side-effecting instructions. In practice, this looks through token
5478/// factors and non-volatile loads. In order to remain efficient, this only
5479/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00005480bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005481 unsigned Depth) const {
5482 if (*this == Dest) return true;
5483
5484 // Don't search too deeply, we just want to be able to see through
5485 // TokenFactor's etc.
5486 if (Depth == 0) return false;
5487
5488 // If this is a token factor, all inputs to the TF happen in parallel. If any
5489 // of the operands of the TF reach dest, then we can do the xform.
5490 if (getOpcode() == ISD::TokenFactor) {
5491 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5492 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5493 return true;
5494 return false;
5495 }
5496
5497 // Loads don't have side effects, look through them.
5498 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5499 if (!Ld->isVolatile())
5500 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5501 }
5502 return false;
5503}
5504
5505
Evan Chengc5fc57d2006-11-03 03:05:24 +00005506static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005507 SmallPtrSet<SDNode *, 32> &Visited) {
5508 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00005509 return;
5510
5511 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005512 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00005513 if (Op == P) {
5514 found = true;
5515 return;
5516 }
5517 findPredecessor(Op, P, found, Visited);
5518 }
5519}
5520
Evan Cheng917be682008-03-04 00:41:45 +00005521/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00005522/// is either an operand of N or it can be reached by recursively traversing
5523/// up the operands.
5524/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00005525bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005526 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005527 bool found = false;
5528 findPredecessor(N, this, found, Visited);
5529 return found;
5530}
5531
Evan Chengc5484282006-10-04 00:56:09 +00005532uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5533 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005534 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005535}
5536
Reid Spencer577cc322007-04-01 07:32:19 +00005537std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005538 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005539 default:
5540 if (getOpcode() < ISD::BUILTIN_OP_END)
5541 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00005542 if (isMachineOpcode()) {
5543 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005544 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00005545 if (getMachineOpcode() < TII->getNumOpcodes())
5546 return TII->get(getMachineOpcode()).getName();
5547 return "<<Unknown Machine Node>>";
5548 }
5549 if (G) {
Dan Gohmane9530ec2009-01-15 16:58:17 +00005550 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmane8be6c62008-07-17 19:10:17 +00005551 const char *Name = TLI.getTargetNodeName(getOpcode());
5552 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00005553 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005554 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005555 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005556
Dan Gohmane8be6c62008-07-17 19:10:17 +00005557#ifndef NDEBUG
5558 case ISD::DELETED_NODE:
5559 return "<<Deleted Node!>>";
5560#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00005561 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00005562 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohman0b1d4a72008-12-23 21:37:04 +00005563 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5564 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5565 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5566 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5567 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5568 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5569 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5570 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5571 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5572 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5573 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5574 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Andrew Lenharth95762122005-03-31 21:24:06 +00005575 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00005576 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005577 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00005578 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005579 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00005580 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00005581 case ISD::AssertSext: return "AssertSext";
5582 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005583
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005584 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005585 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005586 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00005587 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005588
5589 case ISD::Constant: return "Constant";
5590 case ISD::ConstantFP: return "ConstantFP";
5591 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005592 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005593 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005594 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00005595 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00005596 case ISD::RETURNADDR: return "RETURNADDR";
5597 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005598 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00005599 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00005600 case ISD::EHSELECTION: return "EHSELECTION";
5601 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00005602 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005603 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00005604 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005605 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00005606 return Intrinsic::getName((Intrinsic::ID)IID);
5607 }
5608 case ISD::INTRINSIC_VOID:
5609 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005610 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00005611 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00005612 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00005613
Chris Lattnerb2827b02006-03-19 00:52:58 +00005614 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005615 case ISD::TargetConstant: return "TargetConstant";
5616 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005617 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005618 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005619 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005620 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00005621 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005622 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005623
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005624 case ISD::CopyToReg: return "CopyToReg";
5625 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005626 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00005627 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005628 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00005629 case ISD::DBG_LABEL: return "dbg_label";
5630 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00005631 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00005632 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00005633 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00005634 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005635
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005636 // Unary operators
5637 case ISD::FABS: return "fabs";
5638 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00005639 case ISD::FSQRT: return "fsqrt";
5640 case ISD::FSIN: return "fsin";
5641 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00005642 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00005643 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00005644 case ISD::FTRUNC: return "ftrunc";
5645 case ISD::FFLOOR: return "ffloor";
5646 case ISD::FCEIL: return "fceil";
5647 case ISD::FRINT: return "frint";
5648 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005649
5650 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005651 case ISD::ADD: return "add";
5652 case ISD::SUB: return "sub";
5653 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00005654 case ISD::MULHU: return "mulhu";
5655 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005656 case ISD::SDIV: return "sdiv";
5657 case ISD::UDIV: return "udiv";
5658 case ISD::SREM: return "srem";
5659 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00005660 case ISD::SMUL_LOHI: return "smul_lohi";
5661 case ISD::UMUL_LOHI: return "umul_lohi";
5662 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00005663 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005664 case ISD::AND: return "and";
5665 case ISD::OR: return "or";
5666 case ISD::XOR: return "xor";
5667 case ISD::SHL: return "shl";
5668 case ISD::SRA: return "sra";
5669 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00005670 case ISD::ROTL: return "rotl";
5671 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00005672 case ISD::FADD: return "fadd";
5673 case ISD::FSUB: return "fsub";
5674 case ISD::FMUL: return "fmul";
5675 case ISD::FDIV: return "fdiv";
5676 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00005677 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00005678 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005679
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005680 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005681 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005682 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005683 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005684 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005685 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005686 case ISD::CONCAT_VECTORS: return "concat_vectors";
5687 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005688 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005689 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00005690 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005691 case ISD::ADDC: return "addc";
5692 case ISD::ADDE: return "adde";
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005693 case ISD::SADDO: return "saddo";
5694 case ISD::UADDO: return "uaddo";
Bill Wendling74c37652008-12-09 22:08:41 +00005695 case ISD::SSUBO: return "ssubo";
5696 case ISD::USUBO: return "usubo";
5697 case ISD::SMULO: return "smulo";
5698 case ISD::UMULO: return "umulo";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005699 case ISD::SUBC: return "subc";
5700 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005701 case ISD::SHL_PARTS: return "shl_parts";
5702 case ISD::SRA_PARTS: return "sra_parts";
5703 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00005704
5705 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5706 case ISD::INSERT_SUBREG: return "insert_subreg";
5707
Chris Lattner7f644642005-04-28 21:44:03 +00005708 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005709 case ISD::SIGN_EXTEND: return "sign_extend";
5710 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005711 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005712 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005713 case ISD::TRUNCATE: return "truncate";
5714 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005715 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005716 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005717 case ISD::FP_EXTEND: return "fp_extend";
5718
5719 case ISD::SINT_TO_FP: return "sint_to_fp";
5720 case ISD::UINT_TO_FP: return "uint_to_fp";
5721 case ISD::FP_TO_SINT: return "fp_to_sint";
5722 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005723 case ISD::BIT_CONVERT: return "bit_convert";
Mon P Wang77cdf302008-11-10 20:54:11 +00005724
5725 case ISD::CONVERT_RNDSAT: {
5726 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
5727 default: assert(0 && "Unknown cvt code!");
5728 case ISD::CVT_FF: return "cvt_ff";
5729 case ISD::CVT_FS: return "cvt_fs";
5730 case ISD::CVT_FU: return "cvt_fu";
5731 case ISD::CVT_SF: return "cvt_sf";
5732 case ISD::CVT_UF: return "cvt_uf";
5733 case ISD::CVT_SS: return "cvt_ss";
5734 case ISD::CVT_SU: return "cvt_su";
5735 case ISD::CVT_US: return "cvt_us";
5736 case ISD::CVT_UU: return "cvt_uu";
5737 }
5738 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005739
5740 // Control flow instructions
5741 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005742 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005743 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005744 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005745 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005746 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005747 case ISD::CALLSEQ_START: return "callseq_start";
5748 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005749
5750 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005751 case ISD::LOAD: return "load";
5752 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005753 case ISD::VAARG: return "vaarg";
5754 case ISD::VACOPY: return "vacopy";
5755 case ISD::VAEND: return "vaend";
5756 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005757 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005758 case ISD::EXTRACT_ELEMENT: return "extract_element";
5759 case ISD::BUILD_PAIR: return "build_pair";
5760 case ISD::STACKSAVE: return "stacksave";
5761 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005762 case ISD::TRAP: return "trap";
5763
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005764 // Bit manipulation
5765 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005766 case ISD::CTPOP: return "ctpop";
5767 case ISD::CTTZ: return "cttz";
5768 case ISD::CTLZ: return "ctlz";
5769
Chris Lattner36ce6912005-11-29 06:21:05 +00005770 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005771 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005772 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005773
Duncan Sands36397f52007-07-27 12:58:54 +00005774 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005775 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005776
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005777 case ISD::CONDCODE:
5778 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005779 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005780 case ISD::SETOEQ: return "setoeq";
5781 case ISD::SETOGT: return "setogt";
5782 case ISD::SETOGE: return "setoge";
5783 case ISD::SETOLT: return "setolt";
5784 case ISD::SETOLE: return "setole";
5785 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005786
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005787 case ISD::SETO: return "seto";
5788 case ISD::SETUO: return "setuo";
5789 case ISD::SETUEQ: return "setue";
5790 case ISD::SETUGT: return "setugt";
5791 case ISD::SETUGE: return "setuge";
5792 case ISD::SETULT: return "setult";
5793 case ISD::SETULE: return "setule";
5794 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005795
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005796 case ISD::SETEQ: return "seteq";
5797 case ISD::SETGT: return "setgt";
5798 case ISD::SETGE: return "setge";
5799 case ISD::SETLT: return "setlt";
5800 case ISD::SETLE: return "setle";
5801 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005802 }
5803 }
5804}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005805
Evan Cheng144d8f02006-11-09 17:55:04 +00005806const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005807 switch (AM) {
5808 default:
5809 return "";
5810 case ISD::PRE_INC:
5811 return "<pre-inc>";
5812 case ISD::PRE_DEC:
5813 return "<pre-dec>";
5814 case ISD::POST_INC:
5815 return "<post-inc>";
5816 case ISD::POST_DEC:
5817 return "<post-dec>";
5818 }
5819}
5820
Duncan Sands276dcbd2008-03-21 09:14:45 +00005821std::string ISD::ArgFlagsTy::getArgFlagsString() {
5822 std::string S = "< ";
5823
5824 if (isZExt())
5825 S += "zext ";
5826 if (isSExt())
5827 S += "sext ";
5828 if (isInReg())
5829 S += "inreg ";
5830 if (isSRet())
5831 S += "sret ";
5832 if (isByVal())
5833 S += "byval ";
5834 if (isNest())
5835 S += "nest ";
5836 if (getByValAlign())
5837 S += "byval-align:" + utostr(getByValAlign()) + " ";
5838 if (getOrigAlign())
5839 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5840 if (getByValSize())
5841 S += "byval-size:" + utostr(getByValSize()) + " ";
5842 return S + ">";
5843}
5844
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005845void SDNode::dump() const { dump(0); }
5846void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005847 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005848 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005849}
5850
5851void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5852 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005853
5854 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005855 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005856 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005857 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005858 else
Chris Lattner944fac72008-08-23 22:23:09 +00005859 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005860 }
Chris Lattner944fac72008-08-23 22:23:09 +00005861 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005862
Chris Lattner944fac72008-08-23 22:23:09 +00005863 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005864 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005865 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005866 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005867 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005868 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005869 }
5870
Evan Chengce254432007-12-11 02:08:35 +00005871 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005872 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005873 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005874 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005875 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005876 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005877 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005878 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005879 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005880 }
Chris Lattner944fac72008-08-23 22:23:09 +00005881 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005882 }
5883
Chris Lattnerc3aae252005-01-07 07:46:32 +00005884 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005885 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005886 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005887 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005888 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005889 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005890 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005891 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005892 OS << "<APFloat(";
Dale Johannesen23a98552008-10-09 23:00:39 +00005893 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005894 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005895 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005896 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005897 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohman668aff62008-10-18 18:22:42 +00005898 int64_t offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005899 OS << '<';
5900 WriteAsOperand(OS, GADN->getGlobal());
5901 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005902 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005903 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005904 else
Chris Lattner944fac72008-08-23 22:23:09 +00005905 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005906 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005907 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005908 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005909 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005910 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005911 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005912 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005913 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005914 else
Chris Lattner944fac72008-08-23 22:23:09 +00005915 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005916 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005917 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005918 else
Chris Lattner944fac72008-08-23 22:23:09 +00005919 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005920 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005921 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005922 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5923 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005924 OS << LBB->getName() << " ";
5925 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005926 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005927 if (G && R->getReg() &&
5928 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005929 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005930 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005931 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005932 }
Bill Wendling056292f2008-09-16 21:48:12 +00005933 } else if (const ExternalSymbolSDNode *ES =
5934 dyn_cast<ExternalSymbolSDNode>(this)) {
5935 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005936 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5937 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005938 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005939 else
Chris Lattner944fac72008-08-23 22:23:09 +00005940 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005941 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5942 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005943 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005944 else
Chris Lattner944fac72008-08-23 22:23:09 +00005945 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005946 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005947 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005948 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005949 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005950 }
5951 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005952 const Value *SrcValue = LD->getSrcValue();
5953 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005954 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005955 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005956 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005957 else
Chris Lattner944fac72008-08-23 22:23:09 +00005958 OS << "null";
5959 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005960
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005961 bool doExt = true;
5962 switch (LD->getExtensionType()) {
5963 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005964 case ISD::EXTLOAD: OS << " <anyext "; break;
5965 case ISD::SEXTLOAD: OS << " <sext "; break;
5966 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005967 }
5968 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005969 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005970
Evan Cheng144d8f02006-11-09 17:55:04 +00005971 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005972 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005973 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005974 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005975 OS << " <volatile>";
5976 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005977 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005978 const Value *SrcValue = ST->getSrcValue();
5979 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005980 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005981 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005982 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005983 else
Chris Lattner944fac72008-08-23 22:23:09 +00005984 OS << "null";
5985 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005986
5987 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005988 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005989
5990 const char *AM = getIndexedModeName(ST->getAddressingMode());
5991 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005992 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005993 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005994 OS << " <volatile>";
5995 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005996 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5997 const Value *SrcValue = AT->getSrcValue();
5998 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005999 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00006000 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00006001 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00006002 else
Chris Lattner944fac72008-08-23 22:23:09 +00006003 OS << "null";
6004 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00006005 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00006006 OS << " <volatile>";
6007 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00006008 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00006009}
6010
Chris Lattnerde202b32005-11-09 23:47:37 +00006011static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00006012 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00006013 if (N->getOperand(i).getNode()->hasOneUse())
6014 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00006015 else
Bill Wendling832171c2006-12-07 20:04:42 +00006016 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00006017 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00006018
Chris Lattnerea946cd2005-01-09 20:38:33 +00006019
Bill Wendling832171c2006-12-07 20:04:42 +00006020 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00006021 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00006022}
6023
Chris Lattnerc3aae252005-01-07 07:46:32 +00006024void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00006025 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00006026
Dan Gohman90a7b8f2008-07-15 18:18:54 +00006027 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
6028 I != E; ++I) {
6029 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00006030 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00006031 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00006032 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00006033
Gabor Greifba36cb52008-08-28 21:40:38 +00006034 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00006035
Bill Wendling832171c2006-12-07 20:04:42 +00006036 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00006037}
6038
Evan Chengd6594ae2006-09-12 21:00:35 +00006039const Type *ConstantPoolSDNode::getType() const {
6040 if (isMachineConstantPoolEntry())
6041 return Val.MachineCPVal->getType();
6042 return Val.ConstVal->getType();
6043}