blob: 1d8e3afec27ef3becb70ba5c0386a9b0358067b8 [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);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000432 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000433 ID.AddInteger(LD->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 break;
435 }
436 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000437 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000438 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000439 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 break;
441 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000442 case ISD::ATOMIC_CMP_SWAP:
443 case ISD::ATOMIC_SWAP:
444 case ISD::ATOMIC_LOAD_ADD:
445 case ISD::ATOMIC_LOAD_SUB:
446 case ISD::ATOMIC_LOAD_AND:
447 case ISD::ATOMIC_LOAD_OR:
448 case ISD::ATOMIC_LOAD_XOR:
449 case ISD::ATOMIC_LOAD_NAND:
450 case ISD::ATOMIC_LOAD_MIN:
451 case ISD::ATOMIC_LOAD_MAX:
452 case ISD::ATOMIC_LOAD_UMIN:
453 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000454 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000455 ID.AddInteger(AT->getMemoryVT().getRawBits());
456 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang28873102008-06-25 08:15:39 +0000457 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000458 }
Mon P Wang28873102008-06-25 08:15:39 +0000459 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000460}
461
Duncan Sands0dc40452008-10-27 15:30:53 +0000462/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
463/// data.
464static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
465 AddNodeIDOpcode(ID, N->getOpcode());
466 // Add the return value info.
467 AddNodeIDValueTypes(ID, N->getVTList());
468 // Add the operand info.
469 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
470
471 // Handle SDNode leafs with special info.
472 AddNodeIDCustom(ID, N);
473}
474
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000475/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
Dan Gohmana7ce7412009-02-03 00:08:45 +0000476/// the CSE map that carries alignment, volatility, indexing mode, and
477/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000478///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000479static inline unsigned
Dan Gohmana7ce7412009-02-03 00:08:45 +0000480encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM,
481 bool isVolatile, unsigned Alignment) {
482 assert((ConvType & 3) == ConvType &&
483 "ConvType may not require more than 2 bits!");
484 assert((AM & 7) == AM &&
485 "AM may not require more than 3 bits!");
486 return ConvType |
487 (AM << 2) |
488 (isVolatile << 5) |
489 ((Log2_32(Alignment) + 1) << 6);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000490}
491
Jim Laskey583bd472006-10-27 23:46:08 +0000492//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000493// SelectionDAG Class
494//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000495
Duncan Sands0dc40452008-10-27 15:30:53 +0000496/// doNotCSE - Return true if CSE should not be performed for this node.
497static bool doNotCSE(SDNode *N) {
498 if (N->getValueType(0) == MVT::Flag)
499 return true; // Never CSE anything that produces a flag.
500
501 switch (N->getOpcode()) {
502 default: break;
503 case ISD::HANDLENODE:
504 case ISD::DBG_LABEL:
505 case ISD::DBG_STOPPOINT:
506 case ISD::EH_LABEL:
507 case ISD::DECLARE:
508 return true; // Never CSE these nodes.
509 }
510
511 // Check that remaining values produced are not flags.
512 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
513 if (N->getValueType(i) == MVT::Flag)
514 return true; // Never CSE anything that produces a flag.
515
516 return false;
517}
518
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000519/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000520/// SelectionDAG.
521void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000522 // Create a dummy node (which is not added to allnodes), that adds a reference
523 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000524 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000525
Chris Lattner190a4182006-08-04 17:45:20 +0000526 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000527
Chris Lattner190a4182006-08-04 17:45:20 +0000528 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000529 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000530 if (I->use_empty())
531 DeadNodes.push_back(I);
532
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000533 RemoveDeadNodes(DeadNodes);
534
535 // If the root changed (e.g. it was a dead load, update the root).
536 setRoot(Dummy.getValue());
537}
538
539/// RemoveDeadNodes - This method deletes the unreachable nodes in the
540/// given list, and any nodes that become unreachable as a result.
541void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
542 DAGUpdateListener *UpdateListener) {
543
Chris Lattner190a4182006-08-04 17:45:20 +0000544 // Process the worklist, deleting the nodes and adding their uses to the
545 // worklist.
546 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000547 SDNode *N = DeadNodes.pop_back_val();
Chris Lattner190a4182006-08-04 17:45:20 +0000548
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000549 if (UpdateListener)
550 UpdateListener->NodeDeleted(N, 0);
551
Chris Lattner190a4182006-08-04 17:45:20 +0000552 // Take the node out of the appropriate CSE map.
553 RemoveNodeFromCSEMaps(N);
554
555 // Next, brutally remove the operand list. This is safe to do, as there are
556 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000557 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
558 SDUse &Use = *I++;
559 SDNode *Operand = Use.getNode();
560 Use.set(SDValue());
561
Chris Lattner190a4182006-08-04 17:45:20 +0000562 // Now that we removed this operand, see if there are no uses of it left.
563 if (Operand->use_empty())
564 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000565 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000566
Dan Gohmanc5336122009-01-19 22:39:36 +0000567 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000568 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000569}
570
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000571void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000572 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000573 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000574}
575
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000576void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000577 // First take this out of the appropriate CSE map.
578 RemoveNodeFromCSEMaps(N);
579
Chris Lattner1e111c72005-09-07 05:37:01 +0000580 // Finally, remove uses due to operands of this node, remove from the
581 // AllNodes list, and delete the node.
582 DeleteNodeNotInCSEMaps(N);
583}
584
585void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000586 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
587 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000588
Dan Gohmanf06c8352008-09-30 18:30:35 +0000589 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000590 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000591
Dan Gohmanc5336122009-01-19 22:39:36 +0000592 DeallocateNode(N);
593}
594
595void SelectionDAG::DeallocateNode(SDNode *N) {
596 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000597 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000598
Dan Gohmanc5336122009-01-19 22:39:36 +0000599 // Set the opcode to DELETED_NODE to help catch bugs when node
600 // memory is reallocated.
601 N->NodeType = ISD::DELETED_NODE;
602
Dan Gohman11467282008-08-26 01:44:34 +0000603 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000604}
605
Chris Lattner149c58c2005-08-16 18:17:10 +0000606/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
607/// correspond to it. This is useful when we're about to delete or repurpose
608/// the node. We don't want future request for structurally identical nodes
609/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000610bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000611 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000612 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000613 case ISD::EntryToken:
614 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000615 return false;
616 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000617 case ISD::CONDCODE:
618 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
619 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000620 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000621 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
622 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000623 case ISD::ExternalSymbol:
624 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000625 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000626 case ISD::TargetExternalSymbol:
627 Erased =
628 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000629 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000630 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000631 MVT VT = cast<VTSDNode>(N)->getVT();
632 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000633 Erased = ExtendedValueTypeNodes.erase(VT);
634 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000635 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
636 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000637 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000638 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000639 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000640 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000641 // Remove it from the CSE Map.
642 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000643 break;
644 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000645#ifndef NDEBUG
646 // Verify that the node was actually in one of the CSE maps, unless it has a
647 // flag result (which cannot be CSE'd) or is one of the special cases that are
648 // not subject to CSE.
649 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000650 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000651 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000652 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000653 assert(0 && "Node is not in map!");
654 }
655#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000656 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000657}
658
Dan Gohman39946102009-01-25 16:29:12 +0000659/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
660/// maps and modified in place. Add it back to the CSE maps, unless an identical
661/// node already exists, in which case transfer all its users to the existing
662/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000663///
Dan Gohman39946102009-01-25 16:29:12 +0000664void
665SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
666 DAGUpdateListener *UpdateListener) {
667 // For node types that aren't CSE'd, just act as if no identical node
668 // already exists.
669 if (!doNotCSE(N)) {
670 SDNode *Existing = CSEMap.GetOrInsertNode(N);
671 if (Existing != N) {
672 // If there was already an existing matching node, use ReplaceAllUsesWith
673 // to replace the dead one with the existing one. This can cause
674 // recursive merging of other unrelated nodes down the line.
675 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Cheng71e86852008-07-08 20:06:39 +0000676
Dan Gohman39946102009-01-25 16:29:12 +0000677 // N is now dead. Inform the listener if it exists and delete it.
678 if (UpdateListener)
679 UpdateListener->NodeDeleted(N, Existing);
680 DeleteNodeNotInCSEMaps(N);
681 return;
682 }
683 }
Evan Cheng71e86852008-07-08 20:06:39 +0000684
Dan Gohman39946102009-01-25 16:29:12 +0000685 // If the node doesn't already exist, we updated it. Inform a listener if
686 // it exists.
687 if (UpdateListener)
688 UpdateListener->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000689}
690
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000691/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
692/// were replaced with those specified. If this node is never memoized,
693/// return null, otherwise return a pointer to the slot it would take. If a
694/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000695SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000696 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000697 if (doNotCSE(N))
698 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000699
Dan Gohman475871a2008-07-27 21:46:04 +0000700 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000701 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000702 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000703 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000704 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000705}
706
707/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
708/// were replaced with those specified. If this node is never memoized,
709/// return null, otherwise return a pointer to the slot it would take. If a
710/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000711SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000712 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000713 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000714 if (doNotCSE(N))
715 return 0;
716
Dan Gohman475871a2008-07-27 21:46:04 +0000717 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000718 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000719 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000720 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000721 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
722}
723
724
725/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
726/// were replaced with those specified. If this node is never memoized,
727/// return null, otherwise return a pointer to the slot it would take. If a
728/// node already exists with these operands, the slot will be non-null.
729SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000730 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000731 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000732 if (doNotCSE(N))
733 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000734
Jim Laskey583bd472006-10-27 23:46:08 +0000735 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000736 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000737 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000738 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000739}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000740
Duncan Sandsd038e042008-07-21 10:20:31 +0000741/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
742void SelectionDAG::VerifyNode(SDNode *N) {
743 switch (N->getOpcode()) {
744 default:
745 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000746 case ISD::BUILD_PAIR: {
747 MVT VT = N->getValueType(0);
748 assert(N->getNumValues() == 1 && "Too many results!");
749 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
750 "Wrong return type!");
751 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
752 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
753 "Mismatched operand types!");
754 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
755 "Wrong operand type!");
756 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
757 "Wrong return type size");
758 break;
759 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000760 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000761 assert(N->getNumValues() == 1 && "Too many results!");
762 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000763 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000764 "Wrong number of operands!");
Duncan Sands0954aef2008-10-22 09:00:33 +0000765 // FIXME: Change vector_shuffle to a variadic node with mask elements being
766 // operands of the node. Currently the mask is a BUILD_VECTOR passed as an
767 // operand, and it is not always possible to legalize it. Turning off the
768 // following checks at least makes it possible to legalize most of the time.
769// MVT EltVT = N->getValueType(0).getVectorElementType();
770// for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohmane7852d02009-01-26 04:35:06 +0000771// assert(I->getValueType() == EltVT &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000772// "Wrong operand type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000773 break;
774 }
775 }
776}
777
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000778/// getMVTAlignment - Compute the default alignment value for the
779/// given type.
780///
781unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
782 const Type *Ty = VT == MVT::iPTR ?
783 PointerType::get(Type::Int8Ty, 0) :
784 VT.getTypeForMVT();
785
786 return TLI.getTargetData()->getABITypeAlignment(Ty);
787}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000788
Dan Gohman7c3234c2008-08-27 23:52:12 +0000789SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
790 : TLI(tli), FLI(fli),
791 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000792 Root(getEntryNode()) {
793 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000794}
795
Devang Patel6e7a1612009-01-09 19:11:50 +0000796void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
797 DwarfWriter *dw) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000798 MF = &mf;
799 MMI = mmi;
Devang Patel6e7a1612009-01-09 19:11:50 +0000800 DW = dw;
Dan Gohman7c3234c2008-08-27 23:52:12 +0000801}
802
Chris Lattner78ec3112003-08-11 14:57:33 +0000803SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000804 allnodes_clear();
805}
806
807void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000808 assert(&*AllNodes.begin() == &EntryNode);
809 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000810 while (!AllNodes.empty())
811 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000812}
813
Dan Gohman7c3234c2008-08-27 23:52:12 +0000814void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000815 allnodes_clear();
816 OperandAllocator.Reset();
817 CSEMap.clear();
818
819 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000820 ExternalSymbols.clear();
821 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000822 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
823 static_cast<CondCodeSDNode*>(0));
824 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
825 static_cast<SDNode*>(0));
826
Dan Gohmane7852d02009-01-26 04:35:06 +0000827 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000828 AllNodes.push_back(&EntryNode);
829 Root = getEntryNode();
830}
831
Dan Gohman475871a2008-07-27 21:46:04 +0000832SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000833 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000834 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000835 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000836 return getNode(ISD::AND, Op.getValueType(), Op,
837 getConstant(Imm, Op.getValueType()));
838}
839
Bill Wendling6ce610f2009-01-30 22:23:15 +0000840SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, MVT VT) {
841 if (Op.getValueType() == VT) return Op;
842 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
843 VT.getSizeInBits());
844 return getNode(ISD::AND, DL, Op.getValueType(), Op,
845 getConstant(Imm, Op.getValueType()));
846}
847
Bob Wilson4c245462009-01-22 17:39:32 +0000848/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
849///
Bill Wendling41b9d272009-01-30 22:11:22 +0000850SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, MVT VT) {
851 SDValue NegOne;
852 if (VT.isVector()) {
853 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +0000854 SDValue NegOneElt =
855 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), EltVT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000856 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOneElt);
857 NegOne = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(), VT,
858 &NegOnes[0], NegOnes.size());
859 } else {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +0000860 NegOne = getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000861 }
Bill Wendling41b9d272009-01-30 22:11:22 +0000862 return getNode(ISD::XOR, DL, VT, Val, NegOne);
863}
864
Dan Gohman475871a2008-07-27 21:46:04 +0000865SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000866 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmance9bc122009-01-27 20:39:34 +0000867 assert((EltVT.getSizeInBits() >= 64 ||
868 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
869 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000870 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000871}
872
Dan Gohman475871a2008-07-27 21:46:04 +0000873SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000874 return getConstant(*ConstantInt::get(Val), VT, isT);
875}
876
877SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000878 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000879
Evan Cheng0ff39b32008-06-30 07:31:25 +0000880 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000881 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000882 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000883
884 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000885 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000886 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000887 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000888 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000889 SDNode *N = NULL;
890 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000891 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000892 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000893 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000894 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000895 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000896 CSEMap.InsertNode(N, IP);
897 AllNodes.push_back(N);
898 }
899
Dan Gohman475871a2008-07-27 21:46:04 +0000900 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000901 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000902 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000904 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
905 }
906 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000907}
908
Dan Gohman475871a2008-07-27 21:46:04 +0000909SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000910 return getConstant(Val, TLI.getPointerTy(), isTarget);
911}
912
913
Dan Gohman475871a2008-07-27 21:46:04 +0000914SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000915 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
916}
917
918SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000919 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000920
Duncan Sands83ec4b62008-06-06 12:08:01 +0000921 MVT EltVT =
922 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923
Chris Lattnerd8658612005-02-17 20:17:32 +0000924 // Do the map lookup using the actual bit pattern for the floating point
925 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
926 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000927 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000928 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000929 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000930 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000931 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000932 SDNode *N = NULL;
933 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000934 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000935 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000936 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000937 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000938 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000939 CSEMap.InsertNode(N, IP);
940 AllNodes.push_back(N);
941 }
942
Dan Gohman475871a2008-07-27 21:46:04 +0000943 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000944 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000945 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000946 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000947 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
948 }
949 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950}
951
Dan Gohman475871a2008-07-27 21:46:04 +0000952SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000953 MVT EltVT =
954 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000955 if (EltVT==MVT::f32)
956 return getConstantFP(APFloat((float)Val), VT, isTarget);
957 else
958 return getConstantFP(APFloat(Val), VT, isTarget);
959}
960
Dan Gohman475871a2008-07-27 21:46:04 +0000961SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Dan Gohman6520e202008-10-18 02:06:02 +0000962 MVT VT, int64_t Offset,
Dan Gohman475871a2008-07-27 21:46:04 +0000963 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000964 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000965
Dan Gohman6520e202008-10-18 02:06:02 +0000966 // Truncate (with sign-extension) the offset value to the pointer size.
Dan Gohman44013612008-10-21 03:38:42 +0000967 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +0000968 if (BitWidth < 64)
969 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
970
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000971 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
972 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000973 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000974 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000975 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000976 }
977
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000978 if (GVar && GVar->isThreadLocal())
979 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
980 else
981 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000982
Jim Laskey583bd472006-10-27 23:46:08 +0000983 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000984 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000985 ID.AddPointer(GV);
986 ID.AddInteger(Offset);
987 void *IP = 0;
988 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +0000989 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000990 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000991 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000992 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000993 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000994 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000995}
996
Dan Gohman475871a2008-07-27 21:46:04 +0000997SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000998 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000999 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001000 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001001 ID.AddInteger(FI);
1002 void *IP = 0;
1003 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001004 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001005 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001006 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001007 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001008 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001009 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001010}
1011
Dan Gohman475871a2008-07-27 21:46:04 +00001012SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001013 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001014 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001015 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001016 ID.AddInteger(JTI);
1017 void *IP = 0;
1018 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001019 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001020 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001021 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001022 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001023 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001024 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001025}
1026
Dan Gohman475871a2008-07-27 21:46:04 +00001027SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1028 unsigned Alignment, int Offset,
1029 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +00001030 if (Alignment == 0)
1031 Alignment =
1032 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001033 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001034 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001035 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001036 ID.AddInteger(Alignment);
1037 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001038 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001039 void *IP = 0;
1040 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001041 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001042 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001043 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001044 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001045 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001046 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001047}
1048
Chris Lattnerc3aae252005-01-07 07:46:32 +00001049
Dan Gohman475871a2008-07-27 21:46:04 +00001050SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1051 unsigned Alignment, int Offset,
1052 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +00001053 if (Alignment == 0)
1054 Alignment =
1055 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001056 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001057 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001058 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001059 ID.AddInteger(Alignment);
1060 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001061 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001062 void *IP = 0;
1063 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001064 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001065 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001066 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001067 CSEMap.InsertNode(N, IP);
1068 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001069 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001070}
1071
1072
Dan Gohman475871a2008-07-27 21:46:04 +00001073SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001074 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001075 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001076 ID.AddPointer(MBB);
1077 void *IP = 0;
1078 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001079 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001080 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001081 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001082 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001083 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001084 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001085}
1086
Dale Johannesene8c17332009-01-29 00:47:48 +00001087SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl) {
1088 FoldingSetNodeID ID;
1089 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
1090 ID.AddPointer(MBB);
1091 void *IP = 0;
1092 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1093 return SDValue(E, 0);
1094 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
1095 new (N) BasicBlockSDNode(MBB, dl);
1096 CSEMap.InsertNode(N, IP);
1097 AllNodes.push_back(N);
1098 return SDValue(N, 0);
1099}
1100
Dan Gohman475871a2008-07-27 21:46:04 +00001101SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001102 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001103 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001104 ID.AddInteger(Flags.getRawBits());
1105 void *IP = 0;
1106 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001107 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001108 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001109 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001110 CSEMap.InsertNode(N, IP);
1111 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001112 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001113}
1114
Dan Gohman475871a2008-07-27 21:46:04 +00001115SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001116 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1117 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001118
Duncan Sands83ec4b62008-06-06 12:08:01 +00001119 SDNode *&N = VT.isExtended() ?
1120 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001121
Dan Gohman475871a2008-07-27 21:46:04 +00001122 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001123 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001124 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001125 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001126 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001127}
1128
Bill Wendling056292f2008-09-16 21:48:12 +00001129SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1130 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001131 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001132 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1133 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001134 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001135 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001136}
1137
Dale Johannesene8c17332009-01-29 00:47:48 +00001138SDValue SelectionDAG::getExternalSymbol(const char *Sym, DebugLoc dl, MVT VT) {
1139 SDNode *&N = ExternalSymbols[Sym];
1140 if (N) return SDValue(N, 0);
1141 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1142 new (N) ExternalSymbolSDNode(false, dl, Sym, VT);
1143 AllNodes.push_back(N);
1144 return SDValue(N, 0);
1145}
1146
Bill Wendling056292f2008-09-16 21:48:12 +00001147SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
1148 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001149 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001150 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1151 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001152 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001153 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001154}
1155
Dale Johannesene8c17332009-01-29 00:47:48 +00001156SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, DebugLoc dl,
1157 MVT VT) {
1158 SDNode *&N = TargetExternalSymbols[Sym];
1159 if (N) return SDValue(N, 0);
1160 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1161 new (N) ExternalSymbolSDNode(true, dl, Sym, VT);
1162 AllNodes.push_back(N);
1163 return SDValue(N, 0);
1164}
1165
Dan Gohman475871a2008-07-27 21:46:04 +00001166SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001167 if ((unsigned)Cond >= CondCodeNodes.size())
1168 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001169
Chris Lattner079a27a2005-08-09 20:40:02 +00001170 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001171 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001172 new (N) CondCodeSDNode(Cond);
1173 CondCodeNodes[Cond] = N;
1174 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001175 }
Dan Gohman475871a2008-07-27 21:46:04 +00001176 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001177}
1178
Mon P Wang77cdf302008-11-10 20:54:11 +00001179SDValue SelectionDAG::getConvertRndSat(MVT VT, SDValue Val, SDValue DTy,
1180 SDValue STy, SDValue Rnd, SDValue Sat,
1181 ISD::CvtCode Code) {
Mon P Wang6db08c42008-12-11 03:30:13 +00001182 // If the src and dest types are the same, no conversion is necessary.
1183 if (DTy == STy)
1184 return Val;
1185
Mon P Wang77cdf302008-11-10 20:54:11 +00001186 FoldingSetNodeID ID;
1187 void* IP = 0;
1188 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1189 return SDValue(E, 0);
1190 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1191 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1192 new (N) CvtRndSatSDNode(VT, Ops, 5, Code);
1193 CSEMap.InsertNode(N, IP);
1194 AllNodes.push_back(N);
1195 return SDValue(N, 0);
1196}
1197
Dan Gohman475871a2008-07-27 21:46:04 +00001198SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001199 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001200 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001201 ID.AddInteger(RegNo);
1202 void *IP = 0;
1203 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001204 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001205 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001206 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001207 CSEMap.InsertNode(N, IP);
1208 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001209 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001210}
1211
Dan Gohman475871a2008-07-27 21:46:04 +00001212SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Devang Patel83489bb2009-01-13 00:35:13 +00001213 unsigned Line, unsigned Col,
1214 Value *CU) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001215 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001216 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001217 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001218 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001219}
1220
Dan Gohman475871a2008-07-27 21:46:04 +00001221SDValue SelectionDAG::getLabel(unsigned Opcode,
1222 SDValue Root,
1223 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001224 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001225 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001226 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1227 ID.AddInteger(LabelID);
1228 void *IP = 0;
1229 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001230 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001231 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001232 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001233 CSEMap.InsertNode(N, IP);
1234 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001235 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001236}
1237
Dale Johannesene8c17332009-01-29 00:47:48 +00001238SDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl,
1239 SDValue Root,
1240 unsigned LabelID) {
1241 FoldingSetNodeID ID;
1242 SDValue Ops[] = { Root };
1243 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1244 ID.AddInteger(LabelID);
1245 void *IP = 0;
1246 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1247 return SDValue(E, 0);
1248 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
1249 new (N) LabelSDNode(Opcode, dl, Root, LabelID);
1250 CSEMap.InsertNode(N, IP);
1251 AllNodes.push_back(N);
1252 return SDValue(N, 0);
1253}
1254
Dan Gohman475871a2008-07-27 21:46:04 +00001255SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001256 assert((!V || isa<PointerType>(V->getType())) &&
1257 "SrcValue is not a pointer?");
1258
Jim Laskey583bd472006-10-27 23:46:08 +00001259 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001260 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001261 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001262
Chris Lattner61b09412006-08-11 21:01:22 +00001263 void *IP = 0;
1264 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001265 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001266
Dan Gohmanfed90b62008-07-28 21:51:04 +00001267 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001268 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001269 CSEMap.InsertNode(N, IP);
1270 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001271 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001272}
1273
Dan Gohman475871a2008-07-27 21:46:04 +00001274SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Cheng24ac4082008-11-24 07:09:49 +00001275#ifndef NDEBUG
Dan Gohman69de1932008-02-06 22:27:42 +00001276 const Value *v = MO.getValue();
1277 assert((!v || isa<PointerType>(v->getType())) &&
1278 "SrcValue is not a pointer?");
Evan Cheng24ac4082008-11-24 07:09:49 +00001279#endif
Dan Gohman69de1932008-02-06 22:27:42 +00001280
1281 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001282 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001283 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001284
1285 void *IP = 0;
1286 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001287 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001288
Dan Gohmanfed90b62008-07-28 21:51:04 +00001289 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001290 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001291 CSEMap.InsertNode(N, IP);
1292 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001293 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001294}
1295
Duncan Sands92abc622009-01-31 15:50:11 +00001296/// getShiftAmountOperand - Return the specified value casted to
1297/// the target's desired shift amount type.
1298SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
1299 MVT OpTy = Op.getValueType();
1300 MVT ShTy = TLI.getShiftAmountTy();
1301 if (OpTy == ShTy || OpTy.isVector()) return Op;
1302
1303 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1304 return getNode(Opcode, ShTy, Op);
1305}
1306
Chris Lattner37ce9df2007-10-15 17:47:20 +00001307/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1308/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001309SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001310 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands9304f2c2008-12-04 18:08:40 +00001311 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001312 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001313 unsigned StackAlign =
1314 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1315
Chris Lattner37ce9df2007-10-15 17:47:20 +00001316 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1317 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1318}
1319
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001320/// CreateStackTemporary - Create a stack temporary suitable for holding
1321/// either of the specified value types.
1322SDValue SelectionDAG::CreateStackTemporary(MVT VT1, MVT VT2) {
1323 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1324 VT2.getStoreSizeInBits())/8;
1325 const Type *Ty1 = VT1.getTypeForMVT();
1326 const Type *Ty2 = VT2.getTypeForMVT();
1327 const TargetData *TD = TLI.getTargetData();
1328 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1329 TD->getPrefTypeAlignment(Ty2));
1330
1331 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1332 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1333 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1334}
1335
Dan Gohman475871a2008-07-27 21:46:04 +00001336SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1337 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001338 // These setcc operations always fold.
1339 switch (Cond) {
1340 default: break;
1341 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001342 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001343 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001344 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001345
1346 case ISD::SETOEQ:
1347 case ISD::SETOGT:
1348 case ISD::SETOGE:
1349 case ISD::SETOLT:
1350 case ISD::SETOLE:
1351 case ISD::SETONE:
1352 case ISD::SETO:
1353 case ISD::SETUO:
1354 case ISD::SETUEQ:
1355 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001356 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001357 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001358 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001359
Gabor Greifba36cb52008-08-28 21:40:38 +00001360 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001361 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001362 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001363 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001364
Chris Lattnerc3aae252005-01-07 07:46:32 +00001365 switch (Cond) {
1366 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001367 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1368 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001369 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1370 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1371 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1372 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1373 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1374 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1375 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1376 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001377 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001378 }
Chris Lattner67255a12005-04-07 18:14:58 +00001379 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001380 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1381 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001382 // No compile time operations on this type yet.
1383 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001384 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001385
1386 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001387 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001388 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001389 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1390 return getNode(ISD::UNDEF, VT);
1391 // fall through
1392 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1393 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1394 return getNode(ISD::UNDEF, VT);
1395 // fall through
1396 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001397 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001398 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1399 return getNode(ISD::UNDEF, VT);
1400 // fall through
1401 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1402 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1403 return getNode(ISD::UNDEF, VT);
1404 // fall through
1405 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1406 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1407 return getNode(ISD::UNDEF, VT);
1408 // fall through
1409 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001410 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001411 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1412 return getNode(ISD::UNDEF, VT);
1413 // fall through
1414 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001415 R==APFloat::cmpEqual, VT);
1416 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1417 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1418 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1419 R==APFloat::cmpEqual, VT);
1420 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1421 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1422 R==APFloat::cmpLessThan, VT);
1423 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1424 R==APFloat::cmpUnordered, VT);
1425 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1426 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001427 }
1428 } else {
1429 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001430 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001431 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001432 }
1433
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001434 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001435 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001436}
1437
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001438/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1439/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001440bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001441 unsigned BitWidth = Op.getValueSizeInBits();
1442 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1443}
1444
Dan Gohmanea859be2007-06-22 14:59:07 +00001445/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1446/// this predicate to simplify operations downstream. Mask is known to be zero
1447/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001448bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001449 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001450 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001451 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1452 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1453 return (KnownZero & Mask) == Mask;
1454}
1455
1456/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1457/// known to be either zero or one and return them in the KnownZero/KnownOne
1458/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1459/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001460void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001461 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001462 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001463 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001464 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001465 "Mask size mismatches value type size!");
1466
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001467 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001468 if (Depth == 6 || Mask == 0)
1469 return; // Limit search depth.
1470
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001471 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001472
1473 switch (Op.getOpcode()) {
1474 case ISD::Constant:
1475 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001476 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001477 KnownZero = ~KnownOne & Mask;
1478 return;
1479 case ISD::AND:
1480 // If either the LHS or the RHS are Zero, the result is zero.
1481 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001482 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1483 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001484 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1485 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1486
1487 // Output known-1 bits are only known if set in both the LHS & RHS.
1488 KnownOne &= KnownOne2;
1489 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1490 KnownZero |= KnownZero2;
1491 return;
1492 case ISD::OR:
1493 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001494 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1495 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001496 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1497 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1498
1499 // Output known-0 bits are only known if clear in both the LHS & RHS.
1500 KnownZero &= KnownZero2;
1501 // Output known-1 are known to be set if set in either the LHS | RHS.
1502 KnownOne |= KnownOne2;
1503 return;
1504 case ISD::XOR: {
1505 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1506 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1507 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1508 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1509
1510 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001511 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001512 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1513 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1514 KnownZero = KnownZeroOut;
1515 return;
1516 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001517 case ISD::MUL: {
1518 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1519 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1520 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1521 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1522 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1523
1524 // If low bits are zero in either operand, output low known-0 bits.
1525 // Also compute a conserative estimate for high known-0 bits.
1526 // More trickiness is possible, but this is sufficient for the
1527 // interesting case of alignment computation.
1528 KnownOne.clear();
1529 unsigned TrailZ = KnownZero.countTrailingOnes() +
1530 KnownZero2.countTrailingOnes();
1531 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001532 KnownZero2.countLeadingOnes(),
1533 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001534
1535 TrailZ = std::min(TrailZ, BitWidth);
1536 LeadZ = std::min(LeadZ, BitWidth);
1537 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1538 APInt::getHighBitsSet(BitWidth, LeadZ);
1539 KnownZero &= Mask;
1540 return;
1541 }
1542 case ISD::UDIV: {
1543 // For the purposes of computing leading zeros we can conservatively
1544 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001545 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001546 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1547 ComputeMaskedBits(Op.getOperand(0),
1548 AllOnes, KnownZero2, KnownOne2, Depth+1);
1549 unsigned LeadZ = KnownZero2.countLeadingOnes();
1550
1551 KnownOne2.clear();
1552 KnownZero2.clear();
1553 ComputeMaskedBits(Op.getOperand(1),
1554 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001555 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1556 if (RHSUnknownLeadingOnes != BitWidth)
1557 LeadZ = std::min(BitWidth,
1558 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001559
1560 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1561 return;
1562 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001563 case ISD::SELECT:
1564 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1565 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1566 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1567 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1568
1569 // Only known if known in both the LHS and RHS.
1570 KnownOne &= KnownOne2;
1571 KnownZero &= KnownZero2;
1572 return;
1573 case ISD::SELECT_CC:
1574 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1575 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1576 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1577 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1578
1579 // Only known if known in both the LHS and RHS.
1580 KnownOne &= KnownOne2;
1581 KnownZero &= KnownZero2;
1582 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001583 case ISD::SADDO:
1584 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001585 case ISD::SSUBO:
1586 case ISD::USUBO:
1587 case ISD::SMULO:
1588 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001589 if (Op.getResNo() != 1)
1590 return;
Duncan Sands03228082008-11-23 15:47:28 +00001591 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001592 case ISD::SETCC:
1593 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands03228082008-11-23 15:47:28 +00001594 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001595 BitWidth > 1)
1596 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001597 return;
1598 case ISD::SHL:
1599 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1600 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001601 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001602
1603 // If the shift count is an invalid immediate, don't do anything.
1604 if (ShAmt >= BitWidth)
1605 return;
1606
1607 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001608 KnownZero, KnownOne, Depth+1);
1609 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001610 KnownZero <<= ShAmt;
1611 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001612 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001613 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001614 }
1615 return;
1616 case ISD::SRL:
1617 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1618 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001619 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001620
Dan Gohmand4cf9922008-02-26 18:50:50 +00001621 // If the shift count is an invalid immediate, don't do anything.
1622 if (ShAmt >= BitWidth)
1623 return;
1624
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001625 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001626 KnownZero, KnownOne, Depth+1);
1627 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001628 KnownZero = KnownZero.lshr(ShAmt);
1629 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001630
Dan Gohman72d2fd52008-02-13 22:43:25 +00001631 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001632 KnownZero |= HighBits; // High bits known zero.
1633 }
1634 return;
1635 case ISD::SRA:
1636 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001637 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001638
Dan Gohmand4cf9922008-02-26 18:50:50 +00001639 // If the shift count is an invalid immediate, don't do anything.
1640 if (ShAmt >= BitWidth)
1641 return;
1642
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001643 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001644 // If any of the demanded bits are produced by the sign extension, we also
1645 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001646 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1647 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001648 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001649
1650 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1651 Depth+1);
1652 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001653 KnownZero = KnownZero.lshr(ShAmt);
1654 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001655
1656 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001657 APInt SignBit = APInt::getSignBit(BitWidth);
1658 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001659
Dan Gohmanca93a432008-02-20 16:30:17 +00001660 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001661 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001662 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001663 KnownOne |= HighBits; // New bits are known one.
1664 }
1665 }
1666 return;
1667 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001668 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1669 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001670
1671 // Sign extension. Compute the demanded bits in the result that are not
1672 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001673 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001674
Dan Gohman977a76f2008-02-13 22:28:48 +00001675 APInt InSignBit = APInt::getSignBit(EBits);
1676 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001677
1678 // If the sign extended bits are demanded, we know that the sign
1679 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001680 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001681 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001682 InputDemandedBits |= InSignBit;
1683
1684 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1685 KnownZero, KnownOne, Depth+1);
1686 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1687
1688 // If the sign bit of the input is known set or clear, then we know the
1689 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001690 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001691 KnownZero |= NewBits;
1692 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001693 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001694 KnownOne |= NewBits;
1695 KnownZero &= ~NewBits;
1696 } else { // Input sign bit unknown
1697 KnownZero &= ~NewBits;
1698 KnownOne &= ~NewBits;
1699 }
1700 return;
1701 }
1702 case ISD::CTTZ:
1703 case ISD::CTLZ:
1704 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001705 unsigned LowBits = Log2_32(BitWidth)+1;
1706 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001707 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001708 return;
1709 }
1710 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001711 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001712 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001713 MVT VT = LD->getMemoryVT();
1714 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001715 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001716 }
1717 return;
1718 }
1719 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001720 MVT InVT = Op.getOperand(0).getValueType();
1721 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001722 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1723 APInt InMask = Mask;
1724 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001725 KnownZero.trunc(InBits);
1726 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001727 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001728 KnownZero.zext(BitWidth);
1729 KnownOne.zext(BitWidth);
1730 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001731 return;
1732 }
1733 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001734 MVT InVT = Op.getOperand(0).getValueType();
1735 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001736 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001737 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1738 APInt InMask = Mask;
1739 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001740
1741 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001742 // bit is demanded. Temporarily set this bit in the mask for our callee.
1743 if (NewBits.getBoolValue())
1744 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001745
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001746 KnownZero.trunc(InBits);
1747 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001748 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1749
1750 // Note if the sign bit is known to be zero or one.
1751 bool SignBitKnownZero = KnownZero.isNegative();
1752 bool SignBitKnownOne = KnownOne.isNegative();
1753 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1754 "Sign bit can't be known to be both zero and one!");
1755
1756 // If the sign bit wasn't actually demanded by our caller, we don't
1757 // want it set in the KnownZero and KnownOne result values. Reset the
1758 // mask and reapply it to the result values.
1759 InMask = Mask;
1760 InMask.trunc(InBits);
1761 KnownZero &= InMask;
1762 KnownOne &= InMask;
1763
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001764 KnownZero.zext(BitWidth);
1765 KnownOne.zext(BitWidth);
1766
Dan Gohman977a76f2008-02-13 22:28:48 +00001767 // If the sign bit is known zero or one, the top bits match.
1768 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001769 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001770 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001771 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001772 return;
1773 }
1774 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001775 MVT InVT = Op.getOperand(0).getValueType();
1776 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001777 APInt InMask = Mask;
1778 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001779 KnownZero.trunc(InBits);
1780 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001781 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001782 KnownZero.zext(BitWidth);
1783 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001784 return;
1785 }
1786 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001787 MVT InVT = Op.getOperand(0).getValueType();
1788 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001789 APInt InMask = Mask;
1790 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001791 KnownZero.zext(InBits);
1792 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001793 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001794 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001795 KnownZero.trunc(BitWidth);
1796 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001797 break;
1798 }
1799 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001800 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1801 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001802 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1803 KnownOne, Depth+1);
1804 KnownZero |= (~InMask) & Mask;
1805 return;
1806 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001807 case ISD::FGETSIGN:
1808 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001809 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001810 return;
1811
Dan Gohman23e8b712008-04-28 17:02:21 +00001812 case ISD::SUB: {
1813 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1814 // We know that the top bits of C-X are clear if X contains less bits
1815 // than C (i.e. no wrap-around can happen). For example, 20-X is
1816 // positive if we can prove that X is >= 0 and < 16.
1817 if (CLHS->getAPIntValue().isNonNegative()) {
1818 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1819 // NLZ can't be BitWidth with no sign bit
1820 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1821 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1822 Depth+1);
1823
1824 // If all of the MaskV bits are known to be zero, then we know the
1825 // output top bits are zero, because we now know that the output is
1826 // from [0-C].
1827 if ((KnownZero2 & MaskV) == MaskV) {
1828 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1829 // Top bits known zero.
1830 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1831 }
1832 }
1833 }
1834 }
1835 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001836 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001837 // Output known-0 bits are known if clear or set in both the low clear bits
1838 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1839 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001840 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1841 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1842 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1843 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1844
1845 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1846 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1847 KnownZeroOut = std::min(KnownZeroOut,
1848 KnownZero2.countTrailingOnes());
1849
1850 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001851 return;
1852 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001853 case ISD::SREM:
1854 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001855 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001856 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001857 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001858 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1859 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001860
Dan Gohmana60832b2008-08-13 23:12:35 +00001861 // If the sign bit of the first operand is zero, the sign bit of
1862 // the result is zero. If the first operand has no one bits below
1863 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001864 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1865 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001866
Dan Gohman23e8b712008-04-28 17:02:21 +00001867 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001868
1869 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001870 }
1871 }
1872 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001873 case ISD::UREM: {
1874 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001875 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001876 if (RA.isPowerOf2()) {
1877 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001878 APInt Mask2 = LowBits & Mask;
1879 KnownZero |= ~LowBits & Mask;
1880 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1881 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1882 break;
1883 }
1884 }
1885
1886 // Since the result is less than or equal to either operand, any leading
1887 // zero bits in either operand must also exist in the result.
1888 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1889 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1890 Depth+1);
1891 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1892 Depth+1);
1893
1894 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1895 KnownZero2.countLeadingOnes());
1896 KnownOne.clear();
1897 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1898 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001899 }
1900 default:
1901 // Allow the target to implement this method for its nodes.
1902 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1903 case ISD::INTRINSIC_WO_CHAIN:
1904 case ISD::INTRINSIC_W_CHAIN:
1905 case ISD::INTRINSIC_VOID:
1906 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1907 }
1908 return;
1909 }
1910}
1911
1912/// ComputeNumSignBits - Return the number of times the sign bit of the
1913/// register is replicated into the other bits. We know that at least 1 bit
1914/// is always equal to the sign bit (itself), but other cases can give us
1915/// information. For example, immediately after an "SRA X, 2", we know that
1916/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001917unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001918 MVT VT = Op.getValueType();
1919 assert(VT.isInteger() && "Invalid VT!");
1920 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001921 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001922 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001923
1924 if (Depth == 6)
1925 return 1; // Limit search depth.
1926
1927 switch (Op.getOpcode()) {
1928 default: break;
1929 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001930 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001931 return VTBits-Tmp+1;
1932 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001933 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001934 return VTBits-Tmp;
1935
1936 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001937 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1938 // If negative, return # leading ones.
1939 if (Val.isNegative())
1940 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001941
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001942 // Return # leading zeros.
1943 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001944 }
1945
1946 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001947 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001948 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1949
1950 case ISD::SIGN_EXTEND_INREG:
1951 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001952 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001953 Tmp = VTBits-Tmp+1;
1954
1955 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1956 return std::max(Tmp, Tmp2);
1957
1958 case ISD::SRA:
1959 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1960 // SRA X, C -> adds C sign bits.
1961 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001962 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001963 if (Tmp > VTBits) Tmp = VTBits;
1964 }
1965 return Tmp;
1966 case ISD::SHL:
1967 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1968 // shl destroys sign bits.
1969 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001970 if (C->getZExtValue() >= VTBits || // Bad shift.
1971 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1972 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001973 }
1974 break;
1975 case ISD::AND:
1976 case ISD::OR:
1977 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001978 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001979 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001980 if (Tmp != 1) {
1981 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1982 FirstAnswer = std::min(Tmp, Tmp2);
1983 // We computed what we know about the sign bits as our first
1984 // answer. Now proceed to the generic code that uses
1985 // ComputeMaskedBits, and pick whichever answer is better.
1986 }
1987 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001988
1989 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001990 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001991 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001992 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001993 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00001994
1995 case ISD::SADDO:
1996 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001997 case ISD::SSUBO:
1998 case ISD::USUBO:
1999 case ISD::SMULO:
2000 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002001 if (Op.getResNo() != 1)
2002 break;
Duncan Sands03228082008-11-23 15:47:28 +00002003 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002004 case ISD::SETCC:
2005 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands03228082008-11-23 15:47:28 +00002006 if (TLI.getBooleanContents() ==
2007 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 return VTBits;
2009 break;
2010 case ISD::ROTL:
2011 case ISD::ROTR:
2012 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002013 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002014
2015 // Handle rotate right by N like a rotate left by 32-N.
2016 if (Op.getOpcode() == ISD::ROTR)
2017 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2018
2019 // If we aren't rotating out all of the known-in sign bits, return the
2020 // number that are left. This handles rotl(sext(x), 1) for example.
2021 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2022 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2023 }
2024 break;
2025 case ISD::ADD:
2026 // Add can have at most one carry bit. Thus we know that the output
2027 // is, at worst, one more bit than the inputs.
2028 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2029 if (Tmp == 1) return 1; // Early out.
2030
2031 // Special case decrementing a value (ADD X, -1):
2032 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
2033 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002034 APInt KnownZero, KnownOne;
2035 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002036 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
2037
2038 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2039 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002040 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002041 return VTBits;
2042
2043 // If we are subtracting one from a positive number, there is no carry
2044 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002045 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002046 return Tmp;
2047 }
2048
2049 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2050 if (Tmp2 == 1) return 1;
2051 return std::min(Tmp, Tmp2)-1;
2052 break;
2053
2054 case ISD::SUB:
2055 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2056 if (Tmp2 == 1) return 1;
2057
2058 // Handle NEG.
2059 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002060 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002061 APInt KnownZero, KnownOne;
2062 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002063 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2064 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2065 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002066 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002067 return VTBits;
2068
2069 // If the input is known to be positive (the sign bit is known clear),
2070 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002071 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002072 return Tmp2;
2073
2074 // Otherwise, we treat this like a SUB.
2075 }
2076
2077 // Sub can have at most one carry bit. Thus we know that the output
2078 // is, at worst, one more bit than the inputs.
2079 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2080 if (Tmp == 1) return 1; // Early out.
2081 return std::min(Tmp, Tmp2)-1;
2082 break;
2083 case ISD::TRUNCATE:
2084 // FIXME: it's tricky to do anything useful for this, but it is an important
2085 // case for targets like X86.
2086 break;
2087 }
2088
2089 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2090 if (Op.getOpcode() == ISD::LOAD) {
2091 LoadSDNode *LD = cast<LoadSDNode>(Op);
2092 unsigned ExtType = LD->getExtensionType();
2093 switch (ExtType) {
2094 default: break;
2095 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002096 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002097 return VTBits-Tmp+1;
2098 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002099 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002100 return VTBits-Tmp;
2101 }
2102 }
2103
2104 // Allow the target to implement this method for its nodes.
2105 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2106 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2107 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2108 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2109 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002110 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002111 }
2112
2113 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2114 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002115 APInt KnownZero, KnownOne;
2116 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002117 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
2118
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002119 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002120 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002121 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002122 Mask = KnownOne;
2123 } else {
2124 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002125 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002126 }
2127
2128 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2129 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002130 Mask = ~Mask;
2131 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002132 // Return # leading zeros. We use 'min' here in case Val was zero before
2133 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002134 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002135}
2136
Chris Lattner51dabfb2006-10-14 00:41:01 +00002137
Dan Gohman475871a2008-07-27 21:46:04 +00002138bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002139 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2140 if (!GA) return false;
Dan Gohman6520e202008-10-18 02:06:02 +00002141 if (GA->getOffset() != 0) return false;
Evan Chenga844bde2008-02-02 04:07:54 +00002142 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2143 if (!GV) return false;
Devang Patel35fe7342009-01-13 22:54:57 +00002144 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patelbbdc8202009-01-13 23:54:55 +00002145 return MMI && MMI->hasDebugInfo();
Evan Chenga844bde2008-02-02 04:07:54 +00002146}
2147
2148
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002149/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2150/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002151SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002152 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00002153 SDValue PermMask = N->getOperand(2);
2154 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002155 if (Idx.getOpcode() == ISD::UNDEF)
2156 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002157 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002158 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002159 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002160 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002161
2162 if (V.getOpcode() == ISD::BIT_CONVERT) {
2163 V = V.getOperand(0);
Mon P Wange9f10152008-12-09 05:46:39 +00002164 MVT VVT = V.getValueType();
2165 if (!VVT.isVector() || VVT.getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002166 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002167 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002168 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002169 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002170 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002171 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002172 return V.getOperand(Index);
2173 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002174 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002175 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002176}
2177
2178
Chris Lattnerc3aae252005-01-07 07:46:32 +00002179/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002180///
Dan Gohman475871a2008-07-27 21:46:04 +00002181SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002182 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT);
2183}
2184
2185SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002186 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002187 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002188 void *IP = 0;
2189 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002190 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002191 SDNode *N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002192 new (N) SDNode(Opcode, DL, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002193 CSEMap.InsertNode(N, IP);
2194
2195 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002196#ifndef NDEBUG
2197 VerifyNode(N);
2198#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002199 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002200}
2201
Dan Gohman475871a2008-07-27 21:46:04 +00002202SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002203 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Operand);
2204}
2205
2206SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
2207 MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002208 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002209 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002210 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002211 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002212 switch (Opcode) {
2213 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002214 case ISD::SIGN_EXTEND:
2215 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002216 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002217 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002218 case ISD::TRUNCATE:
2219 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002220 case ISD::UINT_TO_FP:
2221 case ISD::SINT_TO_FP: {
2222 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002223 // No compile time operations on this type.
2224 if (VT==MVT::ppcf128)
2225 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002226 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2227 (void)apf.convertFromAPInt(Val,
2228 Opcode==ISD::SINT_TO_FP,
2229 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002230 return getConstantFP(apf, VT);
2231 }
Chris Lattner94683772005-12-23 05:30:37 +00002232 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002233 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002234 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002235 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002236 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002237 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002238 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002239 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002240 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002241 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002242 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002243 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002244 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002245 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002246 }
2247 }
2248
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002249 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002250 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002251 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002252 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002253 switch (Opcode) {
2254 case ISD::FNEG:
2255 V.changeSign();
2256 return getConstantFP(V, VT);
2257 case ISD::FABS:
2258 V.clearSign();
2259 return getConstantFP(V, VT);
2260 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002261 case ISD::FP_EXTEND: {
2262 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002263 // This can return overflow, underflow, or inexact; we don't care.
2264 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002265 (void)V.convert(*MVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002266 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002267 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002268 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002269 case ISD::FP_TO_SINT:
2270 case ISD::FP_TO_UINT: {
2271 integerPart x;
Dale Johannesen23a98552008-10-09 23:00:39 +00002272 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002273 assert(integerPartWidth >= 64);
2274 // FIXME need to be more flexible about rounding mode.
2275 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2276 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002277 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002278 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2279 break;
2280 return getConstant(x, VT);
2281 }
2282 case ISD::BIT_CONVERT:
2283 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002284 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002285 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002286 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002287 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002288 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002289 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002290 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002291
Gabor Greifba36cb52008-08-28 21:40:38 +00002292 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002293 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002294 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002295 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002296 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002297 return Operand; // Factor, merge or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002298 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002299 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002300 assert(VT.isFloatingPoint() &&
2301 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002302 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002303 if (Operand.getOpcode() == ISD::UNDEF)
2304 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002305 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002306 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002307 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002308 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002309 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002310 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002311 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002312 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greifba36cb52008-08-28 21:40:38 +00002313 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002314 break;
2315 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002316 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002317 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002318 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002319 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002320 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002321 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002322 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002323 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002324 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002325 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002326 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002327 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002328 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002329 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002330 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2331 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002332 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002333 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002334 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002335 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002336 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002337 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002338 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002339 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002340 if (OpOpcode == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002341 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002342 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2343 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002344 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002345 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2346 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2347 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2348 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002349 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002350 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002351 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002352 break;
Chris Lattner35481892005-12-23 00:16:34 +00002353 case ISD::BIT_CONVERT:
2354 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002355 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002356 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002357 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002358 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2359 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002360 if (OpOpcode == ISD::UNDEF)
2361 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002362 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002363 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002364 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2365 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002366 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002367 if (OpOpcode == ISD::UNDEF)
2368 return getNode(ISD::UNDEF, VT);
2369 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2370 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2371 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2372 Operand.getConstantOperandVal(1) == 0 &&
2373 Operand.getOperand(0).getValueType() == VT)
2374 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002375 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002376 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002377 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
2378 if (UnsafeFPMath && OpOpcode == ISD::FSUB)
Gabor Greifba36cb52008-08-28 21:40:38 +00002379 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2380 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002381 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002382 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002383 break;
2384 case ISD::FABS:
2385 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002386 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002387 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002388 }
2389
Chris Lattner43247a12005-08-25 19:12:10 +00002390 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002391 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002392 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002393 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002394 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002395 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002396 void *IP = 0;
2397 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002398 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002399 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002400 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002401 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002402 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002403 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002404 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002405 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002406
Chris Lattnerc3aae252005-01-07 07:46:32 +00002407 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002408#ifndef NDEBUG
2409 VerifyNode(N);
2410#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002411 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002412}
2413
Bill Wendling688d1c42008-09-24 10:16:24 +00002414SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
2415 MVT VT,
2416 ConstantSDNode *Cst1,
2417 ConstantSDNode *Cst2) {
2418 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2419
2420 switch (Opcode) {
2421 case ISD::ADD: return getConstant(C1 + C2, VT);
2422 case ISD::SUB: return getConstant(C1 - C2, VT);
2423 case ISD::MUL: return getConstant(C1 * C2, VT);
2424 case ISD::UDIV:
2425 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2426 break;
2427 case ISD::UREM:
2428 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2429 break;
2430 case ISD::SDIV:
2431 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2432 break;
2433 case ISD::SREM:
2434 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2435 break;
2436 case ISD::AND: return getConstant(C1 & C2, VT);
2437 case ISD::OR: return getConstant(C1 | C2, VT);
2438 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2439 case ISD::SHL: return getConstant(C1 << C2, VT);
2440 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2441 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2442 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2443 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2444 default: break;
2445 }
2446
2447 return SDValue();
2448}
2449
Dan Gohman475871a2008-07-27 21:46:04 +00002450SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2451 SDValue N1, SDValue N2) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002452 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2);
2453}
2454
2455SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2456 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002457 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2458 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002459 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002460 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002461 case ISD::TokenFactor:
2462 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2463 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002464 // Fold trivial token factors.
2465 if (N1.getOpcode() == ISD::EntryToken) return N2;
2466 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002467 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002468 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002469 case ISD::CONCAT_VECTORS:
2470 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2471 // one big BUILD_VECTOR.
2472 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2473 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002474 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2475 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002476 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2477 }
2478 break;
Chris Lattner76365122005-01-16 02:23:22 +00002479 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002480 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002481 N1.getValueType() == VT && "Binary operator types must match!");
2482 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2483 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002484 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002485 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002486 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2487 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002488 break;
Chris Lattner76365122005-01-16 02:23:22 +00002489 case ISD::OR:
2490 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002491 case ISD::ADD:
2492 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002493 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002494 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002495 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2496 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002497 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002498 return N1;
2499 break;
Chris Lattner76365122005-01-16 02:23:22 +00002500 case ISD::UDIV:
2501 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002502 case ISD::MULHU:
2503 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002504 case ISD::MUL:
2505 case ISD::SDIV:
2506 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002507 assert(VT.isInteger() && "This operator does not apply to FP types!");
2508 // fall through
Chris Lattner01b3d732005-09-28 22:28:18 +00002509 case ISD::FADD:
2510 case ISD::FSUB:
2511 case ISD::FMUL:
2512 case ISD::FDIV:
2513 case ISD::FREM:
Dan Gohmana90c8e62009-01-23 19:10:37 +00002514 if (UnsafeFPMath) {
2515 if (Opcode == ISD::FADD) {
2516 // 0+x --> x
2517 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2518 if (CFP->getValueAPF().isZero())
2519 return N2;
2520 // x+0 --> x
2521 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2522 if (CFP->getValueAPF().isZero())
2523 return N1;
2524 } else if (Opcode == ISD::FSUB) {
2525 // x-0 --> x
2526 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2527 if (CFP->getValueAPF().isZero())
2528 return N1;
2529 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002530 }
Chris Lattner76365122005-01-16 02:23:22 +00002531 assert(N1.getValueType() == N2.getValueType() &&
2532 N1.getValueType() == VT && "Binary operator types must match!");
2533 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002534 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2535 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002536 N1.getValueType().isFloatingPoint() &&
2537 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002538 "Invalid FCOPYSIGN!");
2539 break;
Chris Lattner76365122005-01-16 02:23:22 +00002540 case ISD::SHL:
2541 case ISD::SRA:
2542 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002543 case ISD::ROTL:
2544 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002545 assert(VT == N1.getValueType() &&
2546 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002547 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002548 "Shifts only work on integers");
2549
2550 // Always fold shifts of i1 values so the code generator doesn't need to
2551 // handle them. Since we know the size of the shift has to be less than the
2552 // size of the value, the shift/rotate count is guaranteed to be zero.
2553 if (VT == MVT::i1)
2554 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002555 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002556 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002557 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002558 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002559 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002560 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002561 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002562 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002563 break;
2564 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002565 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002566 assert(VT.isFloatingPoint() &&
2567 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002568 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002569 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002570 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002571 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002572 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002573 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002574 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002575 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002576 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002577 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002578 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002579 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002580 break;
2581 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002582 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002583 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002584 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002585 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002586 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002587 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002588 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002589
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002590 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002591 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002592 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002593 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002594 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002595 return getConstant(Val, VT);
2596 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002597 break;
2598 }
2599 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002600 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2601 if (N1.getOpcode() == ISD::UNDEF)
2602 return getNode(ISD::UNDEF, VT);
2603
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002604 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2605 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002606 if (N2C &&
2607 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002608 N1.getNumOperands() > 0) {
2609 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002610 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002611 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002612 N1.getOperand(N2C->getZExtValue() / Factor),
2613 getConstant(N2C->getZExtValue() % Factor,
2614 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002615 }
2616
2617 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2618 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002619 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002620 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002621
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002622 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2623 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002624 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Dan Gohman197e88f2009-01-29 16:10:46 +00002625 // If the indices are the same, return the inserted element.
Dan Gohman6ab64222008-08-13 21:51:37 +00002626 if (N1.getOperand(2) == N2)
2627 return N1.getOperand(1);
Dan Gohman197e88f2009-01-29 16:10:46 +00002628 // If the indices are known different, extract the element from
2629 // the original vector.
2630 else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2631 isa<ConstantSDNode>(N2))
Dan Gohman6ab64222008-08-13 21:51:37 +00002632 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2633 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002634 break;
2635 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002636 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002637 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2638 (N1.getValueType().isInteger() == VT.isInteger()) &&
2639 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002640
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002641 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2642 // 64-bit integers into 32-bit parts. Instead of building the extract of
2643 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2644 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002645 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002646
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002647 // EXTRACT_ELEMENT of a constant int is also very common.
2648 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002650 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002651 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2652 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002653 }
2654 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002655 case ISD::EXTRACT_SUBVECTOR:
2656 if (N1.getValueType() == VT) // Trivial extraction.
2657 return N1;
2658 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002659 }
2660
2661 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002662 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002663 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2664 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002665 } else { // Cannonicalize constant to RHS if commutative
2666 if (isCommutativeBinOp(Opcode)) {
2667 std::swap(N1C, N2C);
2668 std::swap(N1, N2);
2669 }
2670 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002671 }
2672
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002673 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002674 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2675 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002676 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002677 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2678 // Cannonicalize constant to RHS if commutative
2679 std::swap(N1CFP, N2CFP);
2680 std::swap(N1, N2);
2681 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002682 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2683 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002684 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002685 case ISD::FADD:
2686 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002687 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002688 return getConstantFP(V1, VT);
2689 break;
2690 case ISD::FSUB:
2691 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2692 if (s!=APFloat::opInvalidOp)
2693 return getConstantFP(V1, VT);
2694 break;
2695 case ISD::FMUL:
2696 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2697 if (s!=APFloat::opInvalidOp)
2698 return getConstantFP(V1, VT);
2699 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002700 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002701 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2702 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2703 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002704 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002705 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002706 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2707 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2708 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002709 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002710 case ISD::FCOPYSIGN:
2711 V1.copySign(V2);
2712 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002713 default: break;
2714 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002715 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002716 }
Chris Lattner62b57722006-04-20 05:39:12 +00002717
2718 // Canonicalize an UNDEF to the RHS, even over a constant.
2719 if (N1.getOpcode() == ISD::UNDEF) {
2720 if (isCommutativeBinOp(Opcode)) {
2721 std::swap(N1, N2);
2722 } else {
2723 switch (Opcode) {
2724 case ISD::FP_ROUND_INREG:
2725 case ISD::SIGN_EXTEND_INREG:
2726 case ISD::SUB:
2727 case ISD::FSUB:
2728 case ISD::FDIV:
2729 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002730 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002731 return N1; // fold op(undef, arg2) -> undef
2732 case ISD::UDIV:
2733 case ISD::SDIV:
2734 case ISD::UREM:
2735 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002736 case ISD::SRL:
2737 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002738 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002739 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2740 // For vectors, we can't easily build an all zero vector, just return
2741 // the LHS.
2742 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002743 }
2744 }
2745 }
2746
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002747 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002748 if (N2.getOpcode() == ISD::UNDEF) {
2749 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002750 case ISD::XOR:
2751 if (N1.getOpcode() == ISD::UNDEF)
2752 // Handle undef ^ undef -> 0 special case. This is a common
2753 // idiom (misuse).
2754 return getConstant(0, VT);
2755 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002756 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002757 case ISD::ADDC:
2758 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002759 case ISD::SUB:
2760 case ISD::FADD:
2761 case ISD::FSUB:
2762 case ISD::FMUL:
2763 case ISD::FDIV:
2764 case ISD::FREM:
2765 case ISD::UDIV:
2766 case ISD::SDIV:
2767 case ISD::UREM:
2768 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002769 return N2; // fold op(arg1, undef) -> undef
2770 case ISD::MUL:
2771 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002772 case ISD::SRL:
2773 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002774 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002775 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2776 // For vectors, we can't easily build an all zero vector, just return
2777 // the LHS.
2778 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002779 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002780 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002781 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002782 // For vectors, we can't easily build an all one vector, just return
2783 // the LHS.
2784 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002785 case ISD::SRA:
2786 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002787 }
2788 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002789
Chris Lattner27e9b412005-05-11 18:57:39 +00002790 // Memoize this node if possible.
2791 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002792 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002793 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002794 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002795 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002796 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002797 void *IP = 0;
2798 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002799 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002800 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002801 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002802 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002803 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002804 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002805 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002806 }
2807
Chris Lattnerc3aae252005-01-07 07:46:32 +00002808 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002809#ifndef NDEBUG
2810 VerifyNode(N);
2811#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002812 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002813}
2814
Dan Gohman475871a2008-07-27 21:46:04 +00002815SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2816 SDValue N1, SDValue N2, SDValue N3) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002817 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3);
2818}
2819
2820SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2821 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002822 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002823 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2824 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002825 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002826 case ISD::CONCAT_VECTORS:
2827 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2828 // one big BUILD_VECTOR.
2829 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2830 N2.getOpcode() == ISD::BUILD_VECTOR &&
2831 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002832 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2833 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2834 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002835 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2836 }
2837 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002838 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002839 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002840 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002841 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002842 break;
2843 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002844 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002845 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002846 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002847 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002848 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002849 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002850 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002851
2852 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002853 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002854 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002855 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002856 if (N2C->getZExtValue()) // Unconditional branch
Chris Lattner5351e9b2005-01-07 22:49:57 +00002857 return getNode(ISD::BR, MVT::Other, N1, N3);
2858 else
2859 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002860 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002861 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002862 case ISD::VECTOR_SHUFFLE:
Mon P Wangaeb06d22008-11-10 04:46:22 +00002863 assert(N1.getValueType() == N2.getValueType() &&
2864 N1.getValueType().isVector() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002865 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002866 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002867 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002868 "Illegal VECTOR_SHUFFLE node!");
2869 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002870 case ISD::BIT_CONVERT:
2871 // Fold bit_convert nodes from a type to themselves.
2872 if (N1.getValueType() == VT)
2873 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002874 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002875 }
2876
Chris Lattner43247a12005-08-25 19:12:10 +00002877 // Memoize node if it doesn't produce a flag.
2878 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002879 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002880 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002881 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002882 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002883 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002884 void *IP = 0;
2885 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002886 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002887 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002888 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002889 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002890 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002891 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002892 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002893 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002894 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002895#ifndef NDEBUG
2896 VerifyNode(N);
2897#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002898 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002899}
2900
Dan Gohman475871a2008-07-27 21:46:04 +00002901SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2902 SDValue N1, SDValue N2, SDValue N3,
2903 SDValue N4) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002904 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3, N4);
2905}
2906
2907SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2908 SDValue N1, SDValue N2, SDValue N3,
2909 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00002910 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002911 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002912}
2913
Dan Gohman475871a2008-07-27 21:46:04 +00002914SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2915 SDValue N1, SDValue N2, SDValue N3,
2916 SDValue N4, SDValue N5) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002917 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3, N4, N5);
2918}
2919
2920SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2921 SDValue N1, SDValue N2, SDValue N3,
2922 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00002923 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002924 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002925}
2926
Dan Gohman707e0182008-04-12 04:36:06 +00002927/// getMemsetValue - Vectorized representation of the memset value
2928/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002929static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002930 unsigned NumBits = VT.isVector() ?
2931 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002933 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002934 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002935 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002936 Val = (Val << Shift) | Val;
2937 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002938 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002939 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002940 return DAG.getConstant(Val, VT);
2941 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002942 }
Evan Chengf0df0312008-05-15 08:39:06 +00002943
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002944 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Evan Chengf0df0312008-05-15 08:39:06 +00002945 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2946 unsigned Shift = 8;
2947 for (unsigned i = NumBits; i > 8; i >>= 1) {
2948 Value = DAG.getNode(ISD::OR, VT,
2949 DAG.getNode(ISD::SHL, VT, Value,
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002950 DAG.getConstant(Shift,
2951 TLI.getShiftAmountTy())),
2952 Value);
Evan Chengf0df0312008-05-15 08:39:06 +00002953 Shift <<= 1;
2954 }
2955
2956 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002957}
2958
Dan Gohman707e0182008-04-12 04:36:06 +00002959/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2960/// used when a memcpy is turned into a memset when the source is a constant
2961/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002962static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002963 const TargetLowering &TLI,
2964 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002965 // Handle vector with all elements zero.
2966 if (Str.empty()) {
2967 if (VT.isInteger())
2968 return DAG.getConstant(0, VT);
2969 unsigned NumElts = VT.getVectorNumElements();
2970 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2971 return DAG.getNode(ISD::BIT_CONVERT, VT,
2972 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2973 }
2974
Duncan Sands83ec4b62008-06-06 12:08:01 +00002975 assert(!VT.isVector() && "Can't handle vector type here!");
2976 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002977 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002978 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002979 if (TLI.isLittleEndian())
2980 Offset = Offset + MSB - 1;
2981 for (unsigned i = 0; i != MSB; ++i) {
2982 Val = (Val << 8) | (unsigned char)Str[Offset];
2983 Offset += TLI.isLittleEndian() ? -1 : 1;
2984 }
2985 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002986}
2987
Dan Gohman707e0182008-04-12 04:36:06 +00002988/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002989///
Dan Gohman475871a2008-07-27 21:46:04 +00002990static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002991 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002992 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002993 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2994}
2995
Evan Chengf0df0312008-05-15 08:39:06 +00002996/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2997///
Dan Gohman475871a2008-07-27 21:46:04 +00002998static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002999 unsigned SrcDelta = 0;
3000 GlobalAddressSDNode *G = NULL;
3001 if (Src.getOpcode() == ISD::GlobalAddress)
3002 G = cast<GlobalAddressSDNode>(Src);
3003 else if (Src.getOpcode() == ISD::ADD &&
3004 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3005 Src.getOperand(1).getOpcode() == ISD::Constant) {
3006 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003007 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003008 }
3009 if (!G)
3010 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003011
Evan Chengf0df0312008-05-15 08:39:06 +00003012 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00003013 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3014 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00003015
Evan Chengf0df0312008-05-15 08:39:06 +00003016 return false;
3017}
Dan Gohman707e0182008-04-12 04:36:06 +00003018
Evan Chengf0df0312008-05-15 08:39:06 +00003019/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3020/// to replace the memset / memcpy is below the threshold. It also returns the
3021/// types of the sequence of memory ops to perform memset / memcpy.
3022static
Duncan Sands83ec4b62008-06-06 12:08:01 +00003023bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00003024 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00003025 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003026 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00003027 SelectionDAG &DAG,
3028 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003029 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00003030 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00003031 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Chris Lattner7fe5e182008-10-28 07:10:51 +00003032 MVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00003033 if (VT != MVT::iAny) {
3034 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00003035 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00003036 // If source is a string constant, this will require an unaligned load.
3037 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
3038 if (Dst.getOpcode() != ISD::FrameIndex) {
3039 // Can't change destination alignment. It requires a unaligned store.
3040 if (AllowUnalign)
3041 VT = MVT::iAny;
3042 } else {
3043 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
3044 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3045 if (MFI->isFixedObjectIndex(FI)) {
3046 // Can't change destination alignment. It requires a unaligned store.
3047 if (AllowUnalign)
3048 VT = MVT::iAny;
3049 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00003050 // Give the stack frame object a larger alignment if needed.
3051 if (MFI->getObjectAlignment(FI) < NewAlign)
3052 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00003053 Align = NewAlign;
3054 }
3055 }
3056 }
3057 }
3058
3059 if (VT == MVT::iAny) {
3060 if (AllowUnalign) {
3061 VT = MVT::i64;
3062 } else {
3063 switch (Align & 7) {
3064 case 0: VT = MVT::i64; break;
3065 case 4: VT = MVT::i32; break;
3066 case 2: VT = MVT::i16; break;
3067 default: VT = MVT::i8; break;
3068 }
3069 }
3070
Duncan Sands83ec4b62008-06-06 12:08:01 +00003071 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003072 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003073 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
3074 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003075
Duncan Sands8e4eb092008-06-08 20:54:56 +00003076 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003077 VT = LVT;
3078 }
Dan Gohman707e0182008-04-12 04:36:06 +00003079
3080 unsigned NumMemOps = 0;
3081 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003082 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003083 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003084 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003085 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00003086 VT = MVT::i64;
3087 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003088 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
3089 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003090 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003091 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003092 VTSize >>= 1;
3093 }
Dan Gohman707e0182008-04-12 04:36:06 +00003094 }
Dan Gohman707e0182008-04-12 04:36:06 +00003095
3096 if (++NumMemOps > Limit)
3097 return false;
3098 MemOps.push_back(VT);
3099 Size -= VTSize;
3100 }
3101
3102 return true;
3103}
3104
Dan Gohman475871a2008-07-27 21:46:04 +00003105static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
3106 SDValue Chain, SDValue Dst,
3107 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00003108 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003109 const Value *DstSV, uint64_t DstSVOff,
3110 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00003111 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3112
Dan Gohman21323f32008-05-29 19:42:22 +00003113 // Expand memcpy to a series of load and store ops if the size operand falls
3114 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003115 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003116 uint64_t Limit = -1ULL;
Dan Gohman707e0182008-04-12 04:36:06 +00003117 if (!AlwaysInline)
3118 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00003119 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003120 std::string Str;
3121 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003122 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003123 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003124 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003125
Dan Gohman707e0182008-04-12 04:36:06 +00003126
Evan Cheng0ff39b32008-06-30 07:31:25 +00003127 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00003128 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003129 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003130 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003131 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003132 MVT VT = MemOps[i];
3133 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003134 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003135
Evan Cheng0ff39b32008-06-30 07:31:25 +00003136 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00003137 // It's unlikely a store of a vector immediate can be done in a single
3138 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003139 // We also handle store a vector with all zero's.
3140 // FIXME: Handle other cases where store of vector immediate is done in
3141 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00003142 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00003143 Store = DAG.getStore(Chain, Value,
3144 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00003145 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003146 } else {
3147 Value = DAG.getLoad(VT, Chain,
3148 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003149 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00003150 Store = DAG.getStore(Chain, Value,
3151 getMemBasePlusOffset(Dst, DstOff, DAG),
3152 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003153 }
3154 OutChains.push_back(Store);
3155 SrcOff += VTSize;
3156 DstOff += VTSize;
3157 }
3158
3159 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3160 &OutChains[0], OutChains.size());
3161}
3162
Dan Gohman475871a2008-07-27 21:46:04 +00003163static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
3164 SDValue Chain, SDValue Dst,
3165 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00003166 unsigned Align, bool AlwaysInline,
3167 const Value *DstSV, uint64_t DstSVOff,
3168 const Value *SrcSV, uint64_t SrcSVOff){
3169 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3170
3171 // Expand memmove to a series of load and store ops if the size operand falls
3172 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003173 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003174 uint64_t Limit = -1ULL;
Dan Gohman21323f32008-05-29 19:42:22 +00003175 if (!AlwaysInline)
3176 Limit = TLI.getMaxStoresPerMemmove();
3177 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003178 std::string Str;
3179 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00003180 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003181 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003182 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003183
Dan Gohman21323f32008-05-29 19:42:22 +00003184 uint64_t SrcOff = 0, DstOff = 0;
3185
Dan Gohman475871a2008-07-27 21:46:04 +00003186 SmallVector<SDValue, 8> LoadValues;
3187 SmallVector<SDValue, 8> LoadChains;
3188 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003189 unsigned NumMemOps = MemOps.size();
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 Value = DAG.getLoad(VT, Chain,
3196 getMemBasePlusOffset(Src, SrcOff, DAG),
3197 SrcSV, SrcSVOff + SrcOff, false, Align);
3198 LoadValues.push_back(Value);
3199 LoadChains.push_back(Value.getValue(1));
3200 SrcOff += VTSize;
3201 }
3202 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3203 &LoadChains[0], LoadChains.size());
3204 OutChains.clear();
3205 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003206 MVT VT = MemOps[i];
3207 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003208 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003209
3210 Store = DAG.getStore(Chain, LoadValues[i],
3211 getMemBasePlusOffset(Dst, DstOff, DAG),
3212 DstSV, DstSVOff + DstOff, false, DstAlign);
3213 OutChains.push_back(Store);
3214 DstOff += VTSize;
3215 }
3216
3217 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3218 &OutChains[0], OutChains.size());
3219}
3220
Dan Gohman475871a2008-07-27 21:46:04 +00003221static SDValue getMemsetStores(SelectionDAG &DAG,
3222 SDValue Chain, SDValue Dst,
3223 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003224 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003225 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003226 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3227
3228 // Expand memset to a series of load/store ops if the size operand
3229 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003230 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003231 std::string Str;
3232 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003233 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003234 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003235 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003236
Dan Gohman475871a2008-07-27 21:46:04 +00003237 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003238 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003239
3240 unsigned NumMemOps = MemOps.size();
3241 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003242 MVT VT = MemOps[i];
3243 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003244 SDValue Value = getMemsetValue(Src, VT, DAG);
3245 SDValue Store = DAG.getStore(Chain, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003246 getMemBasePlusOffset(Dst, DstOff, DAG),
3247 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003248 OutChains.push_back(Store);
3249 DstOff += VTSize;
3250 }
3251
3252 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3253 &OutChains[0], OutChains.size());
3254}
3255
Dan Gohman475871a2008-07-27 21:46:04 +00003256SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3257 SDValue Src, SDValue Size,
3258 unsigned Align, bool AlwaysInline,
3259 const Value *DstSV, uint64_t DstSVOff,
3260 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003261
3262 // Check to see if we should lower the memcpy to loads and stores first.
3263 // For cases within the target-specified limits, this is the best choice.
3264 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3265 if (ConstantSize) {
3266 // Memcpy with size zero? Just return the original chain.
3267 if (ConstantSize->isNullValue())
3268 return Chain;
3269
Dan Gohman475871a2008-07-27 21:46:04 +00003270 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003271 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3272 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003273 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003274 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003275 return Result;
3276 }
3277
3278 // Then check to see if we should lower the memcpy with target-specific
3279 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003280 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003281 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3282 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003283 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003284 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003285 return Result;
3286
3287 // If we really need inline code and the target declined to provide it,
3288 // use a (potentially long) sequence of loads and stores.
3289 if (AlwaysInline) {
3290 assert(ConstantSize && "AlwaysInline requires a constant size!");
3291 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003292 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003293 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003294 }
3295
3296 // Emit a library call.
3297 TargetLowering::ArgListTy Args;
3298 TargetLowering::ArgListEntry Entry;
3299 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3300 Entry.Node = Dst; Args.push_back(Entry);
3301 Entry.Node = Src; Args.push_back(Entry);
3302 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003303 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003304 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003305 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003306 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003307 getExternalSymbol("memcpy", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003308 Args, *this, DebugLoc::getUnknownLoc());
Dan Gohman707e0182008-04-12 04:36:06 +00003309 return CallResult.second;
3310}
3311
Dan Gohman475871a2008-07-27 21:46:04 +00003312SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3313 SDValue Src, SDValue Size,
3314 unsigned Align,
3315 const Value *DstSV, uint64_t DstSVOff,
3316 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003317
Dan Gohman21323f32008-05-29 19:42:22 +00003318 // Check to see if we should lower the memmove to loads and stores first.
3319 // For cases within the target-specified limits, this is the best choice.
3320 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3321 if (ConstantSize) {
3322 // Memmove with size zero? Just return the original chain.
3323 if (ConstantSize->isNullValue())
3324 return Chain;
3325
Dan Gohman475871a2008-07-27 21:46:04 +00003326 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003327 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3328 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003329 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003330 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003331 return Result;
3332 }
Dan Gohman707e0182008-04-12 04:36:06 +00003333
3334 // Then check to see if we should lower the memmove with target-specific
3335 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003336 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003337 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003338 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003339 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003340 return Result;
3341
3342 // Emit a library call.
3343 TargetLowering::ArgListTy Args;
3344 TargetLowering::ArgListEntry Entry;
3345 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3346 Entry.Node = Dst; Args.push_back(Entry);
3347 Entry.Node = Src; Args.push_back(Entry);
3348 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003349 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003350 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003351 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003352 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003353 getExternalSymbol("memmove", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003354 Args, *this, DebugLoc::getUnknownLoc());
Dan Gohman707e0182008-04-12 04:36:06 +00003355 return CallResult.second;
3356}
3357
Dan Gohman475871a2008-07-27 21:46:04 +00003358SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3359 SDValue Src, SDValue Size,
3360 unsigned Align,
3361 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003362
3363 // Check to see if we should lower the memset to stores first.
3364 // For cases within the target-specified limits, this is the best choice.
3365 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3366 if (ConstantSize) {
3367 // Memset with size zero? Just return the original chain.
3368 if (ConstantSize->isNullValue())
3369 return Chain;
3370
Dan Gohman475871a2008-07-27 21:46:04 +00003371 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003372 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3373 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003374 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003375 return Result;
3376 }
3377
3378 // Then check to see if we should lower the memset with target-specific
3379 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003380 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003381 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Bill Wendling6158d842008-10-01 00:59:58 +00003382 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003383 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003384 return Result;
3385
3386 // Emit a library call.
3387 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3388 TargetLowering::ArgListTy Args;
3389 TargetLowering::ArgListEntry Entry;
3390 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3391 Args.push_back(Entry);
3392 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003393 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003394 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3395 else
3396 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3397 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3398 Args.push_back(Entry);
3399 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3400 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003401 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003402 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003403 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003404 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003405 getExternalSymbol("memset", TLI.getPointerTy()),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003406 Args, *this, DebugLoc::getUnknownLoc());
Dan Gohman707e0182008-04-12 04:36:06 +00003407 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003408}
3409
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003410SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3411 SDValue Chain,
Dan Gohman475871a2008-07-27 21:46:04 +00003412 SDValue Ptr, SDValue Cmp,
3413 SDValue Swp, const Value* PtrVal,
3414 unsigned Alignment) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003415 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003416 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003417
3418 MVT VT = Cmp.getValueType();
3419
3420 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003421 Alignment = getMVTAlignment(MemVT);
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003422
3423 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003424 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003425 ID.AddInteger(MemVT.getRawBits());
Dan Gohman475871a2008-07-27 21:46:04 +00003426 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003427 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003428 void* IP = 0;
3429 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003430 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003431 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003432 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3433 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003434 CSEMap.InsertNode(N, IP);
3435 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003436 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003437}
3438
Dale Johannesene8c17332009-01-29 00:47:48 +00003439SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3440 SDValue Chain,
3441 SDValue Ptr, SDValue Cmp,
3442 SDValue Swp, const Value* PtrVal,
3443 unsigned Alignment) {
3444 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3445 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3446
3447 MVT VT = Cmp.getValueType();
3448
3449 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3450 Alignment = getMVTAlignment(MemVT);
3451
3452 SDVTList VTs = getVTList(VT, MVT::Other);
3453 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003454 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003455 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3456 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3457 void* IP = 0;
3458 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3459 return SDValue(E, 0);
3460 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3461 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3462 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
3463 CSEMap.InsertNode(N, IP);
3464 AllNodes.push_back(N);
3465 return SDValue(N, 0);
3466}
3467
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003468SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3469 SDValue Chain,
Dan Gohman475871a2008-07-27 21:46:04 +00003470 SDValue Ptr, SDValue Val,
3471 const Value* PtrVal,
3472 unsigned Alignment) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003473 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3474 Opcode == ISD::ATOMIC_LOAD_SUB ||
3475 Opcode == ISD::ATOMIC_LOAD_AND ||
3476 Opcode == ISD::ATOMIC_LOAD_OR ||
3477 Opcode == ISD::ATOMIC_LOAD_XOR ||
3478 Opcode == ISD::ATOMIC_LOAD_NAND ||
3479 Opcode == ISD::ATOMIC_LOAD_MIN ||
3480 Opcode == ISD::ATOMIC_LOAD_MAX ||
3481 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3482 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3483 Opcode == ISD::ATOMIC_SWAP) &&
3484 "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003485
3486 MVT VT = Val.getValueType();
3487
3488 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003489 Alignment = getMVTAlignment(MemVT);
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003490
3491 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003492 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003493 ID.AddInteger(MemVT.getRawBits());
Dan Gohman475871a2008-07-27 21:46:04 +00003494 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003495 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003496 void* IP = 0;
3497 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003498 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003499 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003500 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3501 Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003502 CSEMap.InsertNode(N, IP);
3503 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003504 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003505}
3506
Dale Johannesene8c17332009-01-29 00:47:48 +00003507SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3508 SDValue Chain,
3509 SDValue Ptr, SDValue Val,
3510 const Value* PtrVal,
3511 unsigned Alignment) {
3512 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3513 Opcode == ISD::ATOMIC_LOAD_SUB ||
3514 Opcode == ISD::ATOMIC_LOAD_AND ||
3515 Opcode == ISD::ATOMIC_LOAD_OR ||
3516 Opcode == ISD::ATOMIC_LOAD_XOR ||
3517 Opcode == ISD::ATOMIC_LOAD_NAND ||
3518 Opcode == ISD::ATOMIC_LOAD_MIN ||
3519 Opcode == ISD::ATOMIC_LOAD_MAX ||
3520 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3521 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3522 Opcode == ISD::ATOMIC_SWAP) &&
3523 "Invalid Atomic Op");
3524
3525 MVT VT = Val.getValueType();
3526
3527 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3528 Alignment = getMVTAlignment(MemVT);
3529
3530 SDVTList VTs = getVTList(VT, MVT::Other);
3531 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003532 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003533 SDValue Ops[] = {Chain, Ptr, Val};
3534 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3535 void* IP = 0;
3536 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3537 return SDValue(E, 0);
3538 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3539 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3540 Chain, Ptr, Val, PtrVal, Alignment);
3541 CSEMap.InsertNode(N, IP);
3542 AllNodes.push_back(N);
3543 return SDValue(N, 0);
3544}
3545
Duncan Sands4bdcb612008-07-02 17:40:58 +00003546/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3547/// Allowed to return something different (and simpler) if Simplify is true.
Duncan Sandsaaffa052008-12-01 11:41:29 +00003548SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps) {
3549 if (NumOps == 1)
Duncan Sands4bdcb612008-07-02 17:40:58 +00003550 return Ops[0];
3551
3552 SmallVector<MVT, 4> VTs;
3553 VTs.reserve(NumOps);
3554 for (unsigned i = 0; i < NumOps; ++i)
3555 VTs.push_back(Ops[i].getValueType());
3556 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3557}
3558
Dale Johannesen54c94522009-02-02 20:47:48 +00003559/// DebugLoc-aware version.
3560SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3561 DebugLoc dl) {
3562 if (NumOps == 1)
3563 return Ops[0];
3564
3565 SmallVector<MVT, 4> VTs;
3566 VTs.reserve(NumOps);
3567 for (unsigned i = 0; i < NumOps; ++i)
3568 VTs.push_back(Ops[i].getValueType());
3569 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
3570 Ops, NumOps);
3571}
3572
Dan Gohman475871a2008-07-27 21:46:04 +00003573SDValue
Mon P Wangc4d10212008-10-17 18:22:58 +00003574SelectionDAG::getMemIntrinsicNode(unsigned Opcode,
3575 const MVT *VTs, unsigned NumVTs,
3576 const SDValue *Ops, unsigned NumOps,
3577 MVT MemVT, const Value *srcValue, int SVOff,
3578 unsigned Align, bool Vol,
3579 bool ReadMem, bool WriteMem) {
3580 return getMemIntrinsicNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps,
3581 MemVT, srcValue, SVOff, Align, Vol,
3582 ReadMem, WriteMem);
3583}
3584
3585SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003586SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
3587 const MVT *VTs, unsigned NumVTs,
3588 const SDValue *Ops, unsigned NumOps,
3589 MVT MemVT, const Value *srcValue, int SVOff,
3590 unsigned Align, bool Vol,
3591 bool ReadMem, bool WriteMem) {
3592 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3593 MemVT, srcValue, SVOff, Align, Vol,
3594 ReadMem, WriteMem);
3595}
3596
3597SDValue
Mon P Wangc4d10212008-10-17 18:22:58 +00003598SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDVTList VTList,
3599 const SDValue *Ops, unsigned NumOps,
3600 MVT MemVT, const Value *srcValue, int SVOff,
3601 unsigned Align, bool Vol,
3602 bool ReadMem, bool WriteMem) {
3603 // Memoize the node unless it returns a flag.
3604 MemIntrinsicSDNode *N;
3605 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3606 FoldingSetNodeID ID;
3607 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3608 void *IP = 0;
3609 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3610 return SDValue(E, 0);
3611
3612 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3613 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3614 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3615 CSEMap.InsertNode(N, IP);
3616 } else {
3617 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3618 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3619 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3620 }
3621 AllNodes.push_back(N);
3622 return SDValue(N, 0);
3623}
3624
3625SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003626SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3627 const SDValue *Ops, unsigned NumOps,
3628 MVT MemVT, const Value *srcValue, int SVOff,
3629 unsigned Align, bool Vol,
3630 bool ReadMem, bool WriteMem) {
3631 // Memoize the node unless it returns a flag.
3632 MemIntrinsicSDNode *N;
3633 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3634 FoldingSetNodeID ID;
3635 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3636 void *IP = 0;
3637 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3638 return SDValue(E, 0);
3639
3640 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3641 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3642 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3643 CSEMap.InsertNode(N, IP);
3644 } else {
3645 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3646 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3647 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3648 }
3649 AllNodes.push_back(N);
3650 return SDValue(N, 0);
3651}
3652
3653SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00003654SelectionDAG::getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003655 bool IsInreg, SDVTList VTs,
Dan Gohman095cc292008-09-13 01:54:27 +00003656 const SDValue *Operands, unsigned NumOperands) {
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003657 // Do not include isTailCall in the folding set profile.
3658 FoldingSetNodeID ID;
3659 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3660 ID.AddInteger(CallingConv);
3661 ID.AddInteger(IsVarArgs);
3662 void *IP = 0;
3663 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3664 // Instead of including isTailCall in the folding set, we just
3665 // set the flag of the existing node.
3666 if (!IsTailCall)
3667 cast<CallSDNode>(E)->setNotTailCall();
3668 return SDValue(E, 0);
3669 }
Dan Gohman095cc292008-09-13 01:54:27 +00003670 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
Dale Johannesen86098bd2008-09-26 19:31:26 +00003671 new (N) CallSDNode(CallingConv, IsVarArgs, IsTailCall, IsInreg,
Dan Gohman095cc292008-09-13 01:54:27 +00003672 VTs, Operands, NumOperands);
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003673 CSEMap.InsertNode(N, IP);
Dan Gohman095cc292008-09-13 01:54:27 +00003674 AllNodes.push_back(N);
3675 return SDValue(N, 0);
3676}
3677
3678SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003679SelectionDAG::getCall(unsigned CallingConv, DebugLoc dl, bool IsVarArgs,
3680 bool IsTailCall, bool IsInreg, SDVTList VTs,
3681 const SDValue *Operands, unsigned NumOperands) {
3682 // Do not include isTailCall in the folding set profile.
3683 FoldingSetNodeID ID;
3684 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3685 ID.AddInteger(CallingConv);
3686 ID.AddInteger(IsVarArgs);
3687 void *IP = 0;
3688 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3689 // Instead of including isTailCall in the folding set, we just
3690 // set the flag of the existing node.
3691 if (!IsTailCall)
3692 cast<CallSDNode>(E)->setNotTailCall();
3693 return SDValue(E, 0);
3694 }
3695 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
3696 new (N) CallSDNode(CallingConv, dl, IsVarArgs, IsTailCall, IsInreg,
3697 VTs, Operands, NumOperands);
3698 CSEMap.InsertNode(N, IP);
3699 AllNodes.push_back(N);
3700 return SDValue(N, 0);
3701}
3702
3703SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003704SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003705 MVT VT, SDValue Chain,
3706 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003707 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003708 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003709 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3710 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003711
Duncan Sands14ea39c2008-03-27 20:23:40 +00003712 if (VT == EVT) {
3713 ExtType = ISD::NON_EXTLOAD;
3714 } else if (ExtType == ISD::NON_EXTLOAD) {
3715 assert(VT == EVT && "Non-extending load from different memory type!");
3716 } else {
3717 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003718 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003719 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3720 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003721 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003722 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003723 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003724 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003725 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003726 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003727 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003728 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003729
3730 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003731 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003732 "Unindexed load with an offset!");
3733
3734 SDVTList VTs = Indexed ?
3735 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003736 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003737 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003738 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003739 ID.AddInteger(EVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003740 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003741 void *IP = 0;
3742 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003743 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003744 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003745 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3746 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003747 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003748 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003749 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003750}
3751
Dale Johannesene8c17332009-01-29 00:47:48 +00003752SDValue
3753SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
3754 ISD::LoadExtType ExtType, MVT VT, SDValue Chain,
3755 SDValue Ptr, SDValue Offset,
3756 const Value *SV, int SVOffset, MVT EVT,
3757 bool isVolatile, unsigned Alignment) {
3758 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3759 Alignment = getMVTAlignment(VT);
3760
3761 if (VT == EVT) {
3762 ExtType = ISD::NON_EXTLOAD;
3763 } else if (ExtType == ISD::NON_EXTLOAD) {
3764 assert(VT == EVT && "Non-extending load from different memory type!");
3765 } else {
3766 // Extending load.
3767 if (VT.isVector())
3768 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3769 "Invalid vector extload!");
3770 else
3771 assert(EVT.bitsLT(VT) &&
3772 "Should only be an extending load, not truncating!");
3773 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
3774 "Cannot sign/zero extend a FP/Vector load!");
3775 assert(VT.isInteger() == EVT.isInteger() &&
3776 "Cannot convert from FP to Int or Int -> FP!");
3777 }
3778
3779 bool Indexed = AM != ISD::UNINDEXED;
3780 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3781 "Unindexed load with an offset!");
3782
3783 SDVTList VTs = Indexed ?
3784 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3785 SDValue Ops[] = { Chain, Ptr, Offset };
3786 FoldingSetNodeID ID;
3787 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dale Johannesene8c17332009-01-29 00:47:48 +00003788 ID.AddInteger(EVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003789 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003790 void *IP = 0;
3791 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3792 return SDValue(E, 0);
3793 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
3794 new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, EVT, SV, SVOffset,
3795 Alignment, isVolatile);
3796 CSEMap.InsertNode(N, IP);
3797 AllNodes.push_back(N);
3798 return SDValue(N, 0);
3799}
3800
Dan Gohman475871a2008-07-27 21:46:04 +00003801SDValue SelectionDAG::getLoad(MVT VT,
3802 SDValue Chain, SDValue Ptr,
3803 const Value *SV, int SVOffset,
3804 bool isVolatile, unsigned Alignment) {
3805 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003806 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3807 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003808}
3809
Dale Johannesene8c17332009-01-29 00:47:48 +00003810SDValue SelectionDAG::getLoad(MVT VT, DebugLoc dl,
3811 SDValue Chain, SDValue Ptr,
3812 const Value *SV, int SVOffset,
3813 bool isVolatile, unsigned Alignment) {
3814 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3815 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3816 SV, SVOffset, VT, isVolatile, Alignment);
3817}
3818
Dan Gohman475871a2008-07-27 21:46:04 +00003819SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3820 SDValue Chain, SDValue Ptr,
3821 const Value *SV,
3822 int SVOffset, MVT EVT,
3823 bool isVolatile, unsigned Alignment) {
3824 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003825 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3826 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003827}
3828
Dale Johannesene8c17332009-01-29 00:47:48 +00003829SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, MVT VT,
3830 SDValue Chain, SDValue Ptr,
3831 const Value *SV,
3832 int SVOffset, MVT EVT,
3833 bool isVolatile, unsigned Alignment) {
3834 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3835 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
3836 SV, SVOffset, EVT, isVolatile, Alignment);
3837}
3838
Dan Gohman475871a2008-07-27 21:46:04 +00003839SDValue
3840SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3841 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003842 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003843 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3844 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003845 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3846 LD->getChain(), Base, Offset, LD->getSrcValue(),
3847 LD->getSrcValueOffset(), LD->getMemoryVT(),
3848 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003849}
3850
Dale Johannesene8c17332009-01-29 00:47:48 +00003851SDValue
3852SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3853 SDValue Offset, ISD::MemIndexedMode AM) {
3854 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3855 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3856 "Load is already a indexed load!");
3857 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3858 LD->getChain(), Base, Offset, LD->getSrcValue(),
3859 LD->getSrcValueOffset(), LD->getMemoryVT(),
3860 LD->isVolatile(), LD->getAlignment());
3861}
3862
Dan Gohman475871a2008-07-27 21:46:04 +00003863SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3864 SDValue Ptr, const Value *SV, int SVOffset,
3865 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003866 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003867
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003868 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3869 Alignment = getMVTAlignment(VT);
3870
Evan Chengad071e12006-10-05 22:57:11 +00003871 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003872 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3873 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003874 FoldingSetNodeID ID;
3875 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003876 ID.AddInteger(VT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003877 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED,
3878 isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003879 void *IP = 0;
3880 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003881 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003882 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003883 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3884 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003885 CSEMap.InsertNode(N, IP);
3886 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003887 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003888}
3889
Dale Johannesene8c17332009-01-29 00:47:48 +00003890SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3891 SDValue Ptr, const Value *SV, int SVOffset,
3892 bool isVolatile, unsigned Alignment) {
3893 MVT VT = Val.getValueType();
3894
3895 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3896 Alignment = getMVTAlignment(VT);
3897
3898 SDVTList VTs = getVTList(MVT::Other);
3899 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3900 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3901 FoldingSetNodeID ID;
3902 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003903 ID.AddInteger(VT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003904 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED,
3905 isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003906 void *IP = 0;
3907 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3908 return SDValue(E, 0);
3909 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3910 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false,
3911 VT, SV, SVOffset, Alignment, isVolatile);
3912 CSEMap.InsertNode(N, IP);
3913 AllNodes.push_back(N);
3914 return SDValue(N, 0);
3915}
3916
Dan Gohman475871a2008-07-27 21:46:04 +00003917SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3918 SDValue Ptr, const Value *SV,
3919 int SVOffset, MVT SVT,
3920 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003921 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003922
3923 if (VT == SVT)
3924 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003925
Duncan Sands8e4eb092008-06-08 20:54:56 +00003926 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003927 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003928 "Can't do FP-INT conversion!");
3929
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003930 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3931 Alignment = getMVTAlignment(VT);
3932
Evan Cheng8b2794a2006-10-13 21:14:26 +00003933 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003934 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3935 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003936 FoldingSetNodeID ID;
3937 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003938 ID.AddInteger(SVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003939 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED,
3940 isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003941 void *IP = 0;
3942 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003943 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003944 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003945 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3946 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003947 CSEMap.InsertNode(N, IP);
3948 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003949 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003950}
3951
Dale Johannesene8c17332009-01-29 00:47:48 +00003952SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3953 SDValue Ptr, const Value *SV,
3954 int SVOffset, MVT SVT,
3955 bool isVolatile, unsigned Alignment) {
3956 MVT VT = Val.getValueType();
3957
3958 if (VT == SVT)
3959 return getStore(Chain, dl, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
3960
3961 assert(VT.bitsGT(SVT) && "Not a truncation?");
3962 assert(VT.isInteger() == SVT.isInteger() &&
3963 "Can't do FP-INT conversion!");
3964
3965 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3966 Alignment = getMVTAlignment(VT);
3967
3968 SDVTList VTs = getVTList(MVT::Other);
3969 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3970 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3971 FoldingSetNodeID ID;
3972 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003973 ID.AddInteger(SVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003974 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED,
3975 isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003976 void *IP = 0;
3977 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3978 return SDValue(E, 0);
3979 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3980 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true,
3981 SVT, SV, SVOffset, Alignment, isVolatile);
3982 CSEMap.InsertNode(N, IP);
3983 AllNodes.push_back(N);
3984 return SDValue(N, 0);
3985}
3986
Dan Gohman475871a2008-07-27 21:46:04 +00003987SDValue
3988SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3989 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003990 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3991 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3992 "Store is already a indexed store!");
3993 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003994 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003995 FoldingSetNodeID ID;
3996 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003997 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003998 ID.AddInteger(ST->getRawSubclassData());
Evan Cheng9109fb12006-11-05 09:30:09 +00003999 void *IP = 0;
4000 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004001 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00004002 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004003 new (N) StoreSDNode(Ops, VTs, AM,
4004 ST->isTruncatingStore(), ST->getMemoryVT(),
4005 ST->getSrcValue(), ST->getSrcValueOffset(),
4006 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00004007 CSEMap.InsertNode(N, IP);
4008 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004009 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00004010}
4011
Dale Johannesene8c17332009-01-29 00:47:48 +00004012SDValue
4013SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4014 SDValue Offset, ISD::MemIndexedMode AM) {
4015 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4016 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4017 "Store is already a indexed store!");
4018 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
4019 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4020 FoldingSetNodeID ID;
4021 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004022 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004023 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesene8c17332009-01-29 00:47:48 +00004024 void *IP = 0;
4025 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4026 return SDValue(E, 0);
4027 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
4028 new (N) StoreSDNode(Ops, dl, VTs, AM,
4029 ST->isTruncatingStore(), ST->getMemoryVT(),
4030 ST->getSrcValue(), ST->getSrcValueOffset(),
4031 ST->getAlignment(), ST->isVolatile());
4032 CSEMap.InsertNode(N, IP);
4033 AllNodes.push_back(N);
4034 return SDValue(N, 0);
4035}
4036
Dan Gohman475871a2008-07-27 21:46:04 +00004037SDValue SelectionDAG::getVAArg(MVT VT,
4038 SDValue Chain, SDValue Ptr,
4039 SDValue SV) {
4040 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00004041 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00004042}
4043
Dan Gohman475871a2008-07-27 21:46:04 +00004044SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
4045 const SDUse *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 SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +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]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004056 default: break;
4057 }
4058
Dan Gohman475871a2008-07-27 21:46:04 +00004059 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004060 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004061 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004062 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004063}
4064
Dan Gohman475871a2008-07-27 21:46:04 +00004065SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
4066 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004067 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Ops, NumOps);
4068}
4069
4070SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
4071 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004072 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004073 case 0: return getNode(Opcode, DL, VT);
4074 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4075 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4076 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004077 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004078 }
Chris Lattnerde202b32005-11-09 23:47:37 +00004079
Chris Lattneref847df2005-04-09 03:27:28 +00004080 switch (Opcode) {
4081 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004082 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004083 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004084 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4085 "LHS and RHS of condition must have same type!");
4086 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4087 "True and False arms of SelectCC must have same type!");
4088 assert(Ops[2].getValueType() == VT &&
4089 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004090 break;
4091 }
4092 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004093 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004094 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4095 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004096 break;
4097 }
Chris Lattneref847df2005-04-09 03:27:28 +00004098 }
4099
Chris Lattner385328c2005-05-14 07:42:29 +00004100 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004101 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004102 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004103
Chris Lattner43247a12005-08-25 19:12:10 +00004104 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00004105 FoldingSetNodeID ID;
4106 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004107 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004108
Chris Lattnera5682852006-08-07 23:03:03 +00004109 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004110 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004111
Dan Gohmanfed90b62008-07-28 21:51:04 +00004112 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004113 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004114 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004115 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004116 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004117 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004118 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004119
Chris Lattneref847df2005-04-09 03:27:28 +00004120 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004121#ifndef NDEBUG
4122 VerifyNode(N);
4123#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004124 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004125}
4126
Dan Gohman475871a2008-07-27 21:46:04 +00004127SDValue SelectionDAG::getNode(unsigned Opcode,
4128 const std::vector<MVT> &ResultTys,
4129 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004130 return getNode(Opcode, DebugLoc::getUnknownLoc(), ResultTys, Ops, NumOps);
4131}
4132
4133SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
4134 const std::vector<MVT> &ResultTys,
4135 const SDValue *Ops, unsigned NumOps) {
4136 return getNode(Opcode, DL, getNodeValueTypes(ResultTys), ResultTys.size(),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004137 Ops, NumOps);
4138}
4139
Dan Gohman475871a2008-07-27 21:46:04 +00004140SDValue SelectionDAG::getNode(unsigned Opcode,
4141 const MVT *VTs, unsigned NumVTs,
4142 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004143 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTs, NumVTs, Ops, NumOps);
4144}
4145
4146SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
4147 const MVT *VTs, unsigned NumVTs,
4148 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004149 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004150 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4151 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00004152}
4153
Dan Gohman475871a2008-07-27 21:46:04 +00004154SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4155 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004156 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, Ops, NumOps);
4157}
4158
4159SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4160 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004161 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004162 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004163
Chris Lattner5f056bf2005-07-10 01:55:33 +00004164 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004165 // FIXME: figure out how to safely handle things like
4166 // int foo(int x) { return 1 << (x & 255); }
4167 // int bar() { return foo(256); }
4168#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00004169 case ISD::SRA_PARTS:
4170 case ISD::SRL_PARTS:
4171 case ISD::SHL_PARTS:
4172 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00004173 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004174 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004175 else if (N3.getOpcode() == ISD::AND)
4176 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4177 // If the and is only masking out bits that cannot effect the shift,
4178 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004179 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004180 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004181 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004182 }
4183 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00004184#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00004185 }
Chris Lattner89c34632005-05-14 06:20:26 +00004186
Chris Lattner43247a12005-08-25 19:12:10 +00004187 // Memoize the node unless it returns a flag.
4188 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00004189 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00004190 FoldingSetNodeID ID;
4191 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004192 void *IP = 0;
4193 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004194 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004195 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004196 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004197 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004198 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004199 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004200 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004201 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004202 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004203 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004204 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004205 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004206 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004207 }
Chris Lattnera5682852006-08-07 23:03:03 +00004208 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004209 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004210 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004211 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004212 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004213 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004214 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004215 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004216 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004217 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004218 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004219 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004220 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004221 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004222 }
Chris Lattner43247a12005-08-25 19:12:10 +00004223 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004224 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004225#ifndef NDEBUG
4226 VerifyNode(N);
4227#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004228 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004229}
4230
Dan Gohman475871a2008-07-27 21:46:04 +00004231SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004232 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList);
4233}
4234
4235SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4236 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004237}
4238
Dan Gohman475871a2008-07-27 21:46:04 +00004239SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004240 SDValue N1) {
4241 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1);
4242}
4243
4244SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4245 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004246 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004247 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004248}
4249
Dan Gohman475871a2008-07-27 21:46:04 +00004250SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4251 SDValue N1, SDValue N2) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004252 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2);
4253}
4254
4255SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4256 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004257 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004258 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004259}
4260
Dan Gohman475871a2008-07-27 21:46:04 +00004261SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4262 SDValue N1, SDValue N2, SDValue N3) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004263 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3);
4264}
4265
4266SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4267 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004268 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004269 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004270}
4271
Dan Gohman475871a2008-07-27 21:46:04 +00004272SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4273 SDValue N1, SDValue N2, SDValue N3,
4274 SDValue N4) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004275 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3, N4);
4276}
4277
4278SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4279 SDValue N1, SDValue N2, SDValue N3,
4280 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004281 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004282 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004283}
4284
Dan Gohman475871a2008-07-27 21:46:04 +00004285SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
4286 SDValue N1, SDValue N2, SDValue N3,
4287 SDValue N4, SDValue N5) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004288 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2, N3, N4, N5);
4289}
4290
4291SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4292 SDValue N1, SDValue N2, SDValue N3,
4293 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004294 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004295 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004296}
4297
Duncan Sands83ec4b62008-06-06 12:08:01 +00004298SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004299 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004300}
4301
Duncan Sands83ec4b62008-06-06 12:08:01 +00004302SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004303 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4304 E = VTList.rend(); I != E; ++I)
4305 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4306 return *I;
4307
4308 MVT *Array = Allocator.Allocate<MVT>(2);
4309 Array[0] = VT1;
4310 Array[1] = VT2;
4311 SDVTList Result = makeVTList(Array, 2);
4312 VTList.push_back(Result);
4313 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004314}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004315
4316SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
4317 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4318 E = VTList.rend(); I != E; ++I)
4319 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4320 I->VTs[2] == VT3)
4321 return *I;
4322
4323 MVT *Array = Allocator.Allocate<MVT>(3);
4324 Array[0] = VT1;
4325 Array[1] = VT2;
4326 Array[2] = VT3;
4327 SDVTList Result = makeVTList(Array, 3);
4328 VTList.push_back(Result);
4329 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004330}
4331
Bill Wendling13d6d442008-12-01 23:28:22 +00004332SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
4333 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4334 E = VTList.rend(); I != E; ++I)
4335 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4336 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4337 return *I;
4338
4339 MVT *Array = Allocator.Allocate<MVT>(3);
4340 Array[0] = VT1;
4341 Array[1] = VT2;
4342 Array[2] = VT3;
4343 Array[3] = VT4;
4344 SDVTList Result = makeVTList(Array, 4);
4345 VTList.push_back(Result);
4346 return Result;
4347}
4348
Duncan Sands83ec4b62008-06-06 12:08:01 +00004349SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004350 switch (NumVTs) {
4351 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004352 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004353 case 2: return getVTList(VTs[0], VTs[1]);
4354 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
4355 default: break;
4356 }
4357
Dan Gohmane8be6c62008-07-17 19:10:17 +00004358 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4359 E = VTList.rend(); I != E; ++I) {
4360 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4361 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00004362
4363 bool NoMatch = false;
4364 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004365 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004366 NoMatch = true;
4367 break;
4368 }
4369 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004370 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004371 }
4372
Dan Gohmane8be6c62008-07-17 19:10:17 +00004373 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
4374 std::copy(VTs, VTs+NumVTs, Array);
4375 SDVTList Result = makeVTList(Array, NumVTs);
4376 VTList.push_back(Result);
4377 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004378}
4379
4380
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004381/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4382/// specified operands. If the resultant node already exists in the DAG,
4383/// this does not modify the specified node, instead it returns the node that
4384/// already exists. If the resultant node does not exist in the DAG, the
4385/// input node is returned. As a degenerate case, if you specify the same
4386/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00004387SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004388 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004389 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
4390
4391 // Check to see if there is no change.
4392 if (Op == N->getOperand(0)) return InN;
4393
4394 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004395 void *InsertPos = 0;
4396 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004397 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004398
Dan Gohman79acd2b2008-07-21 22:38:59 +00004399 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004400 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004401 if (!RemoveNodeFromCSEMaps(N))
4402 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004403
4404 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004405 N->OperandList[0].set(Op);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004406
4407 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004408 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004409 return InN;
4410}
4411
Dan Gohman475871a2008-07-27 21:46:04 +00004412SDValue SelectionDAG::
4413UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004414 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004415 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
4416
4417 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004418 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4419 return InN; // No operands changed, just return the input node.
4420
4421 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004422 void *InsertPos = 0;
4423 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004424 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004425
Dan Gohman79acd2b2008-07-21 22:38:59 +00004426 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004427 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004428 if (!RemoveNodeFromCSEMaps(N))
4429 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004430
4431 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004432 if (N->OperandList[0] != Op1)
4433 N->OperandList[0].set(Op1);
4434 if (N->OperandList[1] != Op2)
4435 N->OperandList[1].set(Op2);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004436
4437 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004438 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004439 return InN;
4440}
4441
Dan Gohman475871a2008-07-27 21:46:04 +00004442SDValue SelectionDAG::
4443UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4444 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004445 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004446}
4447
Dan Gohman475871a2008-07-27 21:46:04 +00004448SDValue SelectionDAG::
4449UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4450 SDValue Op3, SDValue Op4) {
4451 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004452 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004453}
4454
Dan Gohman475871a2008-07-27 21:46:04 +00004455SDValue SelectionDAG::
4456UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4457 SDValue Op3, SDValue Op4, SDValue Op5) {
4458 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004459 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004460}
4461
Dan Gohman475871a2008-07-27 21:46:04 +00004462SDValue SelectionDAG::
4463UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004464 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004465 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004466 "Update with wrong number of operands");
4467
4468 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004469 bool AnyChange = false;
4470 for (unsigned i = 0; i != NumOps; ++i) {
4471 if (Ops[i] != N->getOperand(i)) {
4472 AnyChange = true;
4473 break;
4474 }
4475 }
4476
4477 // No operands changed, just return the input node.
4478 if (!AnyChange) return InN;
4479
4480 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004481 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004482 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004483 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004484
Dan Gohman7ceda162008-05-02 00:05:03 +00004485 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004486 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004487 if (!RemoveNodeFromCSEMaps(N))
4488 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004489
4490 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004491 for (unsigned i = 0; i != NumOps; ++i)
4492 if (N->OperandList[i] != Ops[i])
4493 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004494
4495 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004496 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004497 return InN;
4498}
4499
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004500/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004501/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004502void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004503 // Unlike the code in MorphNodeTo that does this, we don't need to
4504 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004505 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4506 SDUse &Use = *I++;
4507 Use.set(SDValue());
4508 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004509}
Chris Lattner149c58c2005-08-16 18:17:10 +00004510
Dan Gohmane8be6c62008-07-17 19:10:17 +00004511/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4512/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004513///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004514SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004515 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004516 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004517 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004518}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004519
Dan Gohmane8be6c62008-07-17 19:10:17 +00004520SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004521 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004522 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004523 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004524 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004525}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004526
Dan Gohmane8be6c62008-07-17 19:10:17 +00004527SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004528 MVT VT, SDValue Op1,
4529 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004530 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004531 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004532 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004533}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004534
Dan Gohmane8be6c62008-07-17 19:10:17 +00004535SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004536 MVT VT, SDValue Op1,
4537 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004538 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004539 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004540 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004541}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004542
Dan Gohmane8be6c62008-07-17 19:10:17 +00004543SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004544 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004545 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004546 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004547 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004548}
4549
Dan Gohmane8be6c62008-07-17 19:10:17 +00004550SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004551 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004552 unsigned NumOps) {
4553 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004554 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004555}
4556
Dan Gohmane8be6c62008-07-17 19:10:17 +00004557SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004558 MVT VT1, MVT VT2) {
4559 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004560 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004561}
4562
Dan Gohmane8be6c62008-07-17 19:10:17 +00004563SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004564 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004565 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004566 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004567 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004568}
4569
Bill Wendling13d6d442008-12-01 23:28:22 +00004570SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
4571 MVT VT1, MVT VT2, MVT VT3, MVT VT4,
4572 const SDValue *Ops, unsigned NumOps) {
4573 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4574 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4575}
4576
Dan Gohmane8be6c62008-07-17 19:10:17 +00004577SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004578 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004579 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004580 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004581 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004582 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004583}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004584
Dan Gohmane8be6c62008-07-17 19:10:17 +00004585SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004586 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004587 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004588 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004589 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004590 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004591}
4592
Dan Gohmane8be6c62008-07-17 19:10:17 +00004593SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004594 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004595 SDValue Op1, SDValue Op2,
4596 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004597 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004598 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004599 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004600}
4601
Dan Gohmane8be6c62008-07-17 19:10:17 +00004602SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Bill Wendling13d6d442008-12-01 23:28:22 +00004603 MVT VT1, MVT VT2, MVT VT3,
4604 SDValue Op1, SDValue Op2,
4605 SDValue Op3) {
4606 SDVTList VTs = getVTList(VT1, VT2, VT3);
4607 SDValue Ops[] = { Op1, Op2, Op3 };
4608 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4609}
4610
4611SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004612 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004613 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004614 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4615}
4616
4617SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4618 MVT VT) {
4619 SDVTList VTs = getVTList(VT);
4620 return MorphNodeTo(N, Opc, VTs, 0, 0);
4621}
4622
4623SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004624 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004625 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004626 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004627 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4628}
4629
4630SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004631 MVT VT, SDValue Op1,
4632 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004633 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004634 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004635 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4636}
4637
4638SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004639 MVT VT, SDValue Op1,
4640 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004641 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004642 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004643 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4644}
4645
4646SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004647 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004648 unsigned NumOps) {
4649 SDVTList VTs = getVTList(VT);
4650 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4651}
4652
4653SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004654 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004655 unsigned NumOps) {
4656 SDVTList VTs = getVTList(VT1, VT2);
4657 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4658}
4659
4660SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4661 MVT VT1, MVT VT2) {
4662 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004663 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004664}
4665
4666SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4667 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004668 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004669 SDVTList VTs = getVTList(VT1, VT2, VT3);
4670 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4671}
4672
4673SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4674 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004675 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004676 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004677 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004678 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4679}
4680
4681SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4682 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004683 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004684 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004685 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004686 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4687}
4688
4689SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4690 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004691 SDValue Op1, SDValue Op2,
4692 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004693 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004694 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004695 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4696}
4697
4698/// MorphNodeTo - These *mutate* the specified node to have the specified
4699/// return type, opcode, and operands.
4700///
4701/// Note that MorphNodeTo returns the resultant node. If there is already a
4702/// node of the specified opcode and operands, it returns that node instead of
4703/// the current one.
4704///
4705/// Using MorphNodeTo is faster than creating a new node and swapping it in
4706/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004707/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004708/// the node's users.
4709///
4710SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004711 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004712 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004713 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004714 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004715 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4716 FoldingSetNodeID ID;
4717 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4718 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4719 return ON;
4720 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004721
Dan Gohman095cc292008-09-13 01:54:27 +00004722 if (!RemoveNodeFromCSEMaps(N))
4723 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004724
Dan Gohmane8be6c62008-07-17 19:10:17 +00004725 // Start the morphing.
4726 N->NodeType = Opc;
4727 N->ValueList = VTs.VTs;
4728 N->NumValues = VTs.NumVTs;
4729
4730 // Clear the operands list, updating used nodes to remove this from their
4731 // use list. Keep track of any operands that become dead as a result.
4732 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004733 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4734 SDUse &Use = *I++;
4735 SDNode *Used = Use.getNode();
4736 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004737 if (Used->use_empty())
4738 DeadNodeSet.insert(Used);
4739 }
4740
4741 // If NumOps is larger than the # of operands we currently have, reallocate
4742 // the operand list.
4743 if (NumOps > N->NumOperands) {
4744 if (N->OperandsNeedDelete)
4745 delete[] N->OperandList;
Bill Wendlinga1dc6022008-10-19 20:51:12 +00004746
Dan Gohmane8be6c62008-07-17 19:10:17 +00004747 if (N->isMachineOpcode()) {
4748 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004749 // remainder of the current SelectionDAG iteration, so we can allocate
4750 // the operands directly out of a pool with no recycling metadata.
4751 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004752 N->OperandsNeedDelete = false;
4753 } else {
4754 N->OperandList = new SDUse[NumOps];
4755 N->OperandsNeedDelete = true;
4756 }
4757 }
4758
4759 // Assign the new operands.
4760 N->NumOperands = NumOps;
4761 for (unsigned i = 0, e = NumOps; i != e; ++i) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004762 N->OperandList[i].setUser(N);
Dan Gohmane7852d02009-01-26 04:35:06 +00004763 N->OperandList[i].setInitial(Ops[i]);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004764 }
4765
4766 // Delete any nodes that are still dead after adding the uses for the
4767 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004768 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004769 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4770 E = DeadNodeSet.end(); I != E; ++I)
4771 if ((*I)->use_empty())
4772 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004773 RemoveDeadNodes(DeadNodes);
4774
Dan Gohmane8be6c62008-07-17 19:10:17 +00004775 if (IP)
4776 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004777 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004778}
4779
Chris Lattner0fb094f2005-11-19 01:44:53 +00004780
Evan Cheng6ae46c42006-02-09 07:15:23 +00004781/// getTargetNode - These are used for target selectors to create a new node
4782/// with specified return type(s), target opcode, and operands.
4783///
4784/// Note that getTargetNode returns the resultant node. If there is already a
4785/// node of the specified opcode and operands, it returns that node instead of
4786/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004787SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004788 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004789}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004790SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT) {
4791 return getNode(~Opcode, dl, VT).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004792}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004793
Dan Gohman475871a2008-07-27 21:46:04 +00004794SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004795 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004796}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004797SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4798 SDValue Op1) {
4799 return getNode(~Opcode, dl, VT, Op1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004800}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004801
Duncan Sands83ec4b62008-06-06 12:08:01 +00004802SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004803 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004804 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004805}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004806SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004807 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004808 return getNode(~Opcode, dl, VT, Op1, Op2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004809}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004810
Duncan Sands83ec4b62008-06-06 12:08:01 +00004811SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004812 SDValue Op1, SDValue Op2,
4813 SDValue Op3) {
4814 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004815}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004816SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4817 SDValue Op1, SDValue Op2,
4818 SDValue Op3) {
4819 return getNode(~Opcode, dl, VT, Op1, Op2, Op3).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 VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004823 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004824 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004825}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004826SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004827 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004828 return getNode(~Opcode, dl, VT, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004829}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004830
4831SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4832 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4833 SDValue Op;
4834 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004835}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004836SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004837 MVT VT1, MVT VT2) {
4838 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4839 SDValue Op;
Bill Wendling56ab1a22009-01-29 09:01:55 +00004840 return getNode(~Opcode, dl, VTs, 2, &Op, 0).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004841}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004842
Duncan Sands83ec4b62008-06-06 12:08:01 +00004843SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004844 MVT VT2, SDValue Op1) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004845 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4846 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004847}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004848SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
Dale Johannesene8c17332009-01-29 00:47:48 +00004849 MVT VT2, SDValue Op1) {
4850 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004851 return getNode(~Opcode, dl, VTs, 2, &Op1, 1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004852}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004853
Duncan Sands83ec4b62008-06-06 12:08:01 +00004854SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004855 MVT VT2, SDValue Op1,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004856 SDValue Op2) {
4857 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4858 SDValue Ops[] = { Op1, Op2 };
4859 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Bill Wendling6e1bb382009-01-29 05:27:31 +00004860}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004861SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4862 MVT VT2, SDValue Op1,
4863 SDValue Op2) {
4864 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4865 SDValue Ops[] = { Op1, Op2 };
4866 return getNode(~Opcode, dl, VTs, 2, Ops, 2).getNode();
4867}
4868
4869SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Bill Wendling6e1bb382009-01-29 05:27:31 +00004870 MVT VT2, SDValue Op1,
4871 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004872 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004873 SDValue Ops[] = { Op1, Op2, Op3 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004874 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004875}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004876SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4877 MVT VT2, SDValue Op1,
4878 SDValue Op2, SDValue Op3) {
4879 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4880 SDValue Ops[] = { Op1, Op2, Op3 };
4881 return getNode(~Opcode, dl, VTs, 2, Ops, 3).getNode();
4882}
4883
4884SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004885 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004886 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4887 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004888}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004889SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004890 MVT VT1, MVT VT2,
4891 const SDValue *Ops, unsigned NumOps) {
4892 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004893 return getNode(~Opcode, dl, VTs, 2, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004894}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004895
4896SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004897 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004898 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4899 SDValue Ops[] = { Op1, Op2 };
4900 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004901}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004902SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004903 MVT VT1, MVT VT2, MVT VT3,
4904 SDValue Op1, SDValue Op2) {
4905 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4906 SDValue Ops[] = { Op1, Op2 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004907 return getNode(~Opcode, dl, VTs, 3, Ops, 2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004908}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004909
4910SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
4911 SDValue Op1, SDValue Op2,
4912 SDValue Op3) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004913 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4914 SDValue Ops[] = { Op1, Op2, Op3 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004915 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004916}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004917SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Bill Wendling6e1bb382009-01-29 05:27:31 +00004918 MVT VT1, MVT VT2, MVT VT3,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004919 SDValue Op1, SDValue Op2,
4920 SDValue Op3) {
4921 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4922 SDValue Ops[] = { Op1, Op2, Op3 };
4923 return getNode(~Opcode, dl, VTs, 3, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004924}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004925
4926SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
4927 const SDValue *Ops, unsigned NumOps) {
4928 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4929 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
4930}
4931SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004932 MVT VT1, MVT VT2, MVT VT3,
4933 const SDValue *Ops, unsigned NumOps) {
4934 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004935 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004936}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004937
4938SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4939 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004940 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004941 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004942 VTList.push_back(VT1);
4943 VTList.push_back(VT2);
4944 VTList.push_back(VT3);
4945 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004946 const MVT *VTs = getNodeValueTypes(VTList);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004947 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004948}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004949SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4950 MVT VT2, MVT VT3, MVT VT4,
4951 const SDValue *Ops, unsigned NumOps) {
4952 std::vector<MVT> VTList;
4953 VTList.push_back(VT1);
4954 VTList.push_back(VT2);
4955 VTList.push_back(VT3);
4956 VTList.push_back(VT4);
4957 const MVT *VTs = getNodeValueTypes(VTList);
4958 return getNode(~Opcode, dl, VTs, 4, Ops, NumOps).getNode();
4959}
4960
Evan Cheng39305cf2007-10-05 01:10:49 +00004961SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004962 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004963 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004964 const MVT *VTs = getNodeValueTypes(ResultTys);
4965 return getNode(~Opcode, VTs, ResultTys.size(),
4966 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004967}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004968SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004969 const std::vector<MVT> &ResultTys,
4970 const SDValue *Ops, unsigned NumOps) {
4971 const MVT *VTs = getNodeValueTypes(ResultTys);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004972 return getNode(~Opcode, dl, VTs, ResultTys.size(),
Dale Johannesene8c17332009-01-29 00:47:48 +00004973 Ops, NumOps).getNode();
4974}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004975
Evan Cheng08b11732008-03-22 01:55:50 +00004976/// getNodeIfExists - Get the specified node if it's already available, or
4977/// else return NULL.
4978SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004979 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004980 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4981 FoldingSetNodeID ID;
4982 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4983 void *IP = 0;
4984 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4985 return E;
4986 }
4987 return NULL;
4988}
4989
Evan Cheng99157a02006-08-07 22:13:29 +00004990/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004991/// This can cause recursive merging of nodes in the DAG.
4992///
Chris Lattner11d049c2008-02-03 03:35:22 +00004993/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004994///
Dan Gohman475871a2008-07-27 21:46:04 +00004995void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004996 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004997 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004998 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004999 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005000 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005001
Dan Gohman39946102009-01-25 16:29:12 +00005002 // Iterate over all the existing uses of From. New uses will be added
5003 // to the beginning of the use list, which we avoid visiting.
5004 // This specifically avoids visiting uses of From that arise while the
5005 // replacement is happening, because any such uses would be the result
5006 // of CSE: If an existing node looks like From after one of its operands
5007 // is replaced by To, we don't want to replace of all its users with To
5008 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005009 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
5010 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005011 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005012
Chris Lattner8b8749f2005-08-17 19:00:20 +00005013 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005014 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005015
Dan Gohman39946102009-01-25 16:29:12 +00005016 // A user can appear in a use list multiple times, and when this
5017 // happens the uses are usually next to each other in the list.
5018 // To help reduce the number of CSE recomputations, process all
5019 // the uses of this user that we can find this way.
5020 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005021 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005022 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005023 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005024 } while (UI != UE && *UI == User);
5025
5026 // Now that we have modified User, add it back to the CSE maps. If it
5027 // already exists there, recursively merge the results together.
5028 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00005029 }
5030}
5031
5032/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5033/// This can cause recursive merging of nodes in the DAG.
5034///
5035/// This version assumes From/To have matching types and numbers of result
5036/// values.
5037///
Chris Lattner1e111c72005-09-07 05:37:01 +00005038void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005039 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005040 assert(From->getVTList().VTs == To->getVTList().VTs &&
5041 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005042 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00005043
5044 // Handle the trivial case.
5045 if (From == To)
5046 return;
5047
Dan Gohmandbe664a2009-01-19 21:44:21 +00005048 // Iterate over just the existing users of From. See the comments in
5049 // the ReplaceAllUsesWith above.
5050 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
5051 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005052 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005053
Chris Lattner8b52f212005-08-26 18:36:28 +00005054 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005055 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005056
Dan Gohman39946102009-01-25 16:29:12 +00005057 // A user can appear in a use list multiple times, and when this
5058 // happens the uses are usually next to each other in the list.
5059 // To help reduce the number of CSE recomputations, process all
5060 // the uses of this user that we can find this way.
5061 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005062 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005063 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005064 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005065 } while (UI != UE && *UI == User);
5066
5067 // Now that we have modified User, add it back to the CSE maps. If it
5068 // already exists there, recursively merge the results together.
5069 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005070 }
5071}
5072
Chris Lattner8b52f212005-08-26 18:36:28 +00005073/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5074/// This can cause recursive merging of nodes in the DAG.
5075///
5076/// This version can replace From with any result values. To must match the
5077/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00005078void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00005079 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005080 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005081 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00005082 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005083
Dan Gohmandbe664a2009-01-19 21:44:21 +00005084 // Iterate over just the existing users of From. See the comments in
5085 // the ReplaceAllUsesWith above.
5086 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
5087 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005088 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005089
Chris Lattner7b2880c2005-08-24 22:44:39 +00005090 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005091 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005092
Dan Gohman39946102009-01-25 16:29:12 +00005093 // A user can appear in a use list multiple times, and when this
5094 // happens the uses are usually next to each other in the list.
5095 // To help reduce the number of CSE recomputations, process all
5096 // the uses of this user that we can find this way.
5097 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005098 SDUse &Use = UI.getUse();
5099 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005100 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005101 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005102 } while (UI != UE && *UI == User);
5103
5104 // Now that we have modified User, add it back to the CSE maps. If it
5105 // already exists there, recursively merge the results together.
5106 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005107 }
5108}
5109
Chris Lattner012f2412006-02-17 21:58:01 +00005110/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005111/// uses of other values produced by From.getNode() alone. The Deleted
5112/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005113void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005114 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005115 // Handle the really simple, really trivial case efficiently.
5116 if (From == To) return;
5117
Chris Lattner012f2412006-02-17 21:58:01 +00005118 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005119 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005120 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005121 return;
5122 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005123
Dan Gohman39946102009-01-25 16:29:12 +00005124 // Iterate over just the existing users of From. See the comments in
5125 // the ReplaceAllUsesWith above.
5126 SDNode::use_iterator UI = From.getNode()->use_begin(),
5127 UE = From.getNode()->use_end();
5128 while (UI != UE) {
5129 SDNode *User = *UI;
5130 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005131
Dan Gohman39946102009-01-25 16:29:12 +00005132 // A user can appear in a use list multiple times, and when this
5133 // happens the uses are usually next to each other in the list.
5134 // To help reduce the number of CSE recomputations, process all
5135 // the uses of this user that we can find this way.
5136 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005137 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005138
5139 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005140 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005141 ++UI;
5142 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005143 }
Dan Gohman39946102009-01-25 16:29:12 +00005144
5145 // If this node hasn't been modified yet, it's still in the CSE maps,
5146 // so remove its old self from the CSE maps.
5147 if (!UserRemovedFromCSEMaps) {
5148 RemoveNodeFromCSEMaps(User);
5149 UserRemovedFromCSEMaps = true;
5150 }
5151
5152 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005153 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005154 } while (UI != UE && *UI == User);
5155
5156 // We are iterating over all uses of the From node, so if a use
5157 // doesn't use the specific value, no changes are made.
5158 if (!UserRemovedFromCSEMaps)
5159 continue;
5160
Chris Lattner01d029b2007-10-15 06:10:22 +00005161 // Now that we have modified User, add it back to the CSE maps. If it
5162 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00005163 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005164 }
5165}
5166
Dan Gohman39946102009-01-25 16:29:12 +00005167namespace {
5168 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5169 /// to record information about a use.
5170 struct UseMemo {
5171 SDNode *User;
5172 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005173 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005174 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005175
5176 /// operator< - Sort Memos by User.
5177 bool operator<(const UseMemo &L, const UseMemo &R) {
5178 return (intptr_t)L.User < (intptr_t)R.User;
5179 }
Dan Gohman39946102009-01-25 16:29:12 +00005180}
5181
Dan Gohmane8be6c62008-07-17 19:10:17 +00005182/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005183/// uses of other values produced by From.getNode() alone. The same value
5184/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005185/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005186void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5187 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005188 unsigned Num,
5189 DAGUpdateListener *UpdateListener){
5190 // Handle the simple, trivial case efficiently.
5191 if (Num == 1)
5192 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5193
Dan Gohman39946102009-01-25 16:29:12 +00005194 // Read up all the uses and make records of them. This helps
5195 // processing new uses that are introduced during the
5196 // replacement process.
5197 SmallVector<UseMemo, 4> Uses;
5198 for (unsigned i = 0; i != Num; ++i) {
5199 unsigned FromResNo = From[i].getResNo();
5200 SDNode *FromNode = From[i].getNode();
5201 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005202 E = FromNode->use_end(); UI != E; ++UI) {
5203 SDUse &Use = UI.getUse();
5204 if (Use.getResNo() == FromResNo) {
5205 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005206 Uses.push_back(Memo);
5207 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005208 }
Dan Gohman39946102009-01-25 16:29:12 +00005209 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005210
Dan Gohmane7852d02009-01-26 04:35:06 +00005211 // Sort the uses, so that all the uses from a given User are together.
5212 std::sort(Uses.begin(), Uses.end());
5213
Dan Gohman39946102009-01-25 16:29:12 +00005214 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5215 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005216 // We know that this user uses some value of From. If it is the right
5217 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005218 SDNode *User = Uses[UseIndex].User;
5219
5220 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005221 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005222
Dan Gohmane7852d02009-01-26 04:35:06 +00005223 // The Uses array is sorted, so all the uses for a given User
5224 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005225 // To help reduce the number of CSE recomputations, process all
5226 // the uses of this user that we can find this way.
5227 do {
5228 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005229 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005230 ++UseIndex;
5231
Dan Gohmane7852d02009-01-26 04:35:06 +00005232 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005233 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5234
Dan Gohmane8be6c62008-07-17 19:10:17 +00005235 // Now that we have modified User, add it back to the CSE maps. If it
5236 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00005237 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005238 }
5239}
5240
Evan Chenge6f35d82006-08-01 08:20:41 +00005241/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005242/// based on their topological order. It returns the maximum id and a vector
5243/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005244unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005245
Dan Gohmanf06c8352008-09-30 18:30:35 +00005246 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005247
Dan Gohmanf06c8352008-09-30 18:30:35 +00005248 // SortedPos tracks the progress of the algorithm. Nodes before it are
5249 // sorted, nodes after it are unsorted. When the algorithm completes
5250 // it is at the end of the list.
5251 allnodes_iterator SortedPos = allnodes_begin();
5252
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005253 // Visit all the nodes. Move nodes with no operands to the front of
5254 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005255 // operand count. Before we do this, the Node Id fields of the nodes
5256 // may contain arbitrary values. After, the Node Id fields for nodes
5257 // before SortedPos will contain the topological sort index, and the
5258 // Node Id fields for nodes At SortedPos and after will contain the
5259 // count of outstanding operands.
5260 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5261 SDNode *N = I++;
5262 unsigned Degree = N->getNumOperands();
5263 if (Degree == 0) {
5264 // A node with no uses, add it to the result array immediately.
5265 N->setNodeId(DAGSize++);
5266 allnodes_iterator Q = N;
5267 if (Q != SortedPos)
5268 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
5269 ++SortedPos;
5270 } else {
5271 // Temporarily use the Node Id as scratch space for the degree count.
5272 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005273 }
5274 }
5275
Dan Gohmanf06c8352008-09-30 18:30:35 +00005276 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5277 // such that by the time the end is reached all nodes will be sorted.
5278 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5279 SDNode *N = I;
5280 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5281 UI != UE; ++UI) {
5282 SDNode *P = *UI;
5283 unsigned Degree = P->getNodeId();
5284 --Degree;
5285 if (Degree == 0) {
5286 // All of P's operands are sorted, so P may sorted now.
5287 P->setNodeId(DAGSize++);
5288 if (P != SortedPos)
5289 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
5290 ++SortedPos;
5291 } else {
5292 // Update P's outstanding operand count.
5293 P->setNodeId(Degree);
5294 }
5295 }
5296 }
5297
5298 assert(SortedPos == AllNodes.end() &&
5299 "Topological sort incomplete!");
5300 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5301 "First node in topological sort is not the entry token!");
5302 assert(AllNodes.front().getNodeId() == 0 &&
5303 "First node in topological sort has non-zero id!");
5304 assert(AllNodes.front().getNumOperands() == 0 &&
5305 "First node in topological sort has operands!");
5306 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5307 "Last node in topologic sort has unexpected id!");
5308 assert(AllNodes.back().use_empty() &&
5309 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005310 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005311 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005312}
5313
5314
Evan Cheng091cba12006-07-27 06:39:06 +00005315
Jim Laskey58b968b2005-08-17 20:08:02 +00005316//===----------------------------------------------------------------------===//
5317// SDNode Class
5318//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005319
Chris Lattner48b85922007-02-04 02:41:42 +00005320HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005321 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005322}
5323
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005324GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Dan Gohman6520e202008-10-18 02:06:02 +00005325 MVT VT, int64_t o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005326 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00005327 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005328 // Thread Local
5329 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
5330 // Non Thread Local
5331 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
5332 getSDVTList(VT)), Offset(o) {
5333 TheGlobal = const_cast<GlobalValue*>(GA);
5334}
Chris Lattner48b85922007-02-04 02:41:42 +00005335
Dan Gohman1ea58a52008-07-09 22:08:04 +00005336MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00005337 const Value *srcValue, int SVO,
5338 unsigned alignment, bool vol)
Dan Gohmana7ce7412009-02-03 00:08:45 +00005339 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5340 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dan Gohman492f2762008-07-09 21:23:02 +00005341 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5342 assert(getAlignment() == alignment && "Alignment representation error!");
5343 assert(isVolatile() == vol && "Volatile representation error!");
5344}
5345
Mon P Wangc4d10212008-10-17 18:22:58 +00005346MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops,
5347 unsigned NumOps, MVT memvt, const Value *srcValue,
5348 int SVO, unsigned alignment, bool vol)
5349 : SDNode(Opc, VTs, Ops, NumOps),
Dan Gohmana7ce7412009-02-03 00:08:45 +00005350 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5351 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Mon P Wangc4d10212008-10-17 18:22:58 +00005352 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5353 assert(getAlignment() == alignment && "Alignment representation error!");
5354 assert(isVolatile() == vol && "Volatile representation error!");
5355}
5356
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005357MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, MVT memvt,
5358 const Value *srcValue, int SVO,
5359 unsigned alignment, bool vol)
Dan Gohmana7ce7412009-02-03 00:08:45 +00005360 : SDNode(Opc, dl, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5361 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005362 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5363 assert(getAlignment() == alignment && "Alignment representation error!");
5364 assert(isVolatile() == vol && "Volatile representation error!");
5365}
5366
5367MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
5368 const SDValue *Ops,
5369 unsigned NumOps, MVT memvt, const Value *srcValue,
5370 int SVO, unsigned alignment, bool vol)
5371 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmana7ce7412009-02-03 00:08:45 +00005372 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5373 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005374 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5375 assert(getAlignment() == alignment && "Alignment representation error!");
5376 assert(isVolatile() == vol && "Volatile representation error!");
5377}
5378
Dan Gohman36b5c132008-04-07 19:35:22 +00005379/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00005380/// reference performed by this memory reference.
5381MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesenfea90882008-10-24 01:06:58 +00005382 int Flags = 0;
Dan Gohman1ea58a52008-07-09 22:08:04 +00005383 if (isa<LoadSDNode>(this))
5384 Flags = MachineMemOperand::MOLoad;
5385 else if (isa<StoreSDNode>(this))
5386 Flags = MachineMemOperand::MOStore;
Mon P Wangc4d10212008-10-17 18:22:58 +00005387 else if (isa<AtomicSDNode>(this)) {
Dan Gohman1ea58a52008-07-09 22:08:04 +00005388 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
5389 }
Mon P Wangc4d10212008-10-17 18:22:58 +00005390 else {
5391 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
5392 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
5393 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
5394 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
5395 }
Dan Gohman1ea58a52008-07-09 22:08:04 +00005396
5397 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00005398 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
5399
Dan Gohman1ea58a52008-07-09 22:08:04 +00005400 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00005401 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00005402 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00005403 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00005404 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
5405 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00005406 else
5407 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
5408 Size, getAlignment());
5409}
5410
Jim Laskey583bd472006-10-27 23:46:08 +00005411/// Profile - Gather unique data for the node.
5412///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005413void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005414 AddNodeIDNode(ID, this);
5415}
5416
Chris Lattnera3255112005-11-08 23:30:28 +00005417/// getValueTypeList - Return a pointer to the specified value type.
5418///
Duncan Sands83ec4b62008-06-06 12:08:01 +00005419const MVT *SDNode::getValueTypeList(MVT VT) {
5420 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005421 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00005422 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005423 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005424 static MVT VTs[MVT::LAST_VALUETYPE];
5425 VTs[VT.getSimpleVT()] = VT;
5426 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005427 }
Chris Lattnera3255112005-11-08 23:30:28 +00005428}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005429
Chris Lattner5c884562005-01-12 18:37:47 +00005430/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5431/// indicated value. This method ignores uses of other values defined by this
5432/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005433bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005434 assert(Value < getNumValues() && "Bad value!");
5435
Roman Levensteindc1adac2008-04-07 10:06:32 +00005436 // TODO: Only iterate over uses of a given value of the node
5437 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005438 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005439 if (NUses == 0)
5440 return false;
5441 --NUses;
5442 }
Chris Lattner5c884562005-01-12 18:37:47 +00005443 }
5444
5445 // Found exactly the right number of uses?
5446 return NUses == 0;
5447}
5448
5449
Evan Cheng33d55952007-08-02 05:29:38 +00005450/// hasAnyUseOfValue - Return true if there are any use of the indicated
5451/// value. This method ignores uses of other values defined by this operation.
5452bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5453 assert(Value < getNumValues() && "Bad value!");
5454
Dan Gohman1373c1c2008-07-09 22:39:01 +00005455 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005456 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005457 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005458
5459 return false;
5460}
5461
5462
Dan Gohman2a629952008-07-27 18:06:42 +00005463/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005464///
Dan Gohman2a629952008-07-27 18:06:42 +00005465bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005466 bool Seen = false;
5467 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005468 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005469 if (User == this)
5470 Seen = true;
5471 else
5472 return false;
5473 }
5474
5475 return Seen;
5476}
5477
Evan Chenge6e97e62006-11-03 07:31:32 +00005478/// isOperand - Return true if this node is an operand of N.
5479///
Dan Gohman475871a2008-07-27 21:46:04 +00005480bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005481 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5482 if (*this == N->getOperand(i))
5483 return true;
5484 return false;
5485}
5486
Evan Cheng917be682008-03-04 00:41:45 +00005487bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005488 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005489 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005490 return true;
5491 return false;
5492}
Evan Cheng4ee62112006-02-05 06:29:23 +00005493
Chris Lattner572dee72008-01-16 05:49:24 +00005494/// reachesChainWithoutSideEffects - Return true if this operand (which must
5495/// be a chain) reaches the specified operand without crossing any
5496/// side-effecting instructions. In practice, this looks through token
5497/// factors and non-volatile loads. In order to remain efficient, this only
5498/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00005499bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005500 unsigned Depth) const {
5501 if (*this == Dest) return true;
5502
5503 // Don't search too deeply, we just want to be able to see through
5504 // TokenFactor's etc.
5505 if (Depth == 0) return false;
5506
5507 // If this is a token factor, all inputs to the TF happen in parallel. If any
5508 // of the operands of the TF reach dest, then we can do the xform.
5509 if (getOpcode() == ISD::TokenFactor) {
5510 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5511 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5512 return true;
5513 return false;
5514 }
5515
5516 // Loads don't have side effects, look through them.
5517 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5518 if (!Ld->isVolatile())
5519 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5520 }
5521 return false;
5522}
5523
5524
Evan Chengc5fc57d2006-11-03 03:05:24 +00005525static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005526 SmallPtrSet<SDNode *, 32> &Visited) {
5527 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00005528 return;
5529
5530 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005531 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00005532 if (Op == P) {
5533 found = true;
5534 return;
5535 }
5536 findPredecessor(Op, P, found, Visited);
5537 }
5538}
5539
Evan Cheng917be682008-03-04 00:41:45 +00005540/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00005541/// is either an operand of N or it can be reached by recursively traversing
5542/// up the operands.
5543/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00005544bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005545 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005546 bool found = false;
5547 findPredecessor(N, this, found, Visited);
5548 return found;
5549}
5550
Evan Chengc5484282006-10-04 00:56:09 +00005551uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5552 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005553 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005554}
5555
Reid Spencer577cc322007-04-01 07:32:19 +00005556std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005557 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005558 default:
5559 if (getOpcode() < ISD::BUILTIN_OP_END)
5560 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00005561 if (isMachineOpcode()) {
5562 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005563 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00005564 if (getMachineOpcode() < TII->getNumOpcodes())
5565 return TII->get(getMachineOpcode()).getName();
5566 return "<<Unknown Machine Node>>";
5567 }
5568 if (G) {
Dan Gohmane9530ec2009-01-15 16:58:17 +00005569 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmane8be6c62008-07-17 19:10:17 +00005570 const char *Name = TLI.getTargetNodeName(getOpcode());
5571 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00005572 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005573 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005574 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005575
Dan Gohmane8be6c62008-07-17 19:10:17 +00005576#ifndef NDEBUG
5577 case ISD::DELETED_NODE:
5578 return "<<Deleted Node!>>";
5579#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00005580 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00005581 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohman0b1d4a72008-12-23 21:37:04 +00005582 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5583 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5584 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5585 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5586 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5587 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5588 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5589 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5590 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5591 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5592 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5593 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Andrew Lenharth95762122005-03-31 21:24:06 +00005594 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00005595 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005596 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00005597 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005598 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00005599 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00005600 case ISD::AssertSext: return "AssertSext";
5601 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005602
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005603 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005604 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005605 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00005606 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005607
5608 case ISD::Constant: return "Constant";
5609 case ISD::ConstantFP: return "ConstantFP";
5610 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005611 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005612 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005613 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00005614 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00005615 case ISD::RETURNADDR: return "RETURNADDR";
5616 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005617 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00005618 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00005619 case ISD::EHSELECTION: return "EHSELECTION";
5620 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00005621 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005622 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00005623 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005624 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00005625 return Intrinsic::getName((Intrinsic::ID)IID);
5626 }
5627 case ISD::INTRINSIC_VOID:
5628 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005629 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00005630 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00005631 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00005632
Chris Lattnerb2827b02006-03-19 00:52:58 +00005633 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005634 case ISD::TargetConstant: return "TargetConstant";
5635 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005636 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005637 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005638 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005639 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00005640 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005641 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005642
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005643 case ISD::CopyToReg: return "CopyToReg";
5644 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005645 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00005646 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005647 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00005648 case ISD::DBG_LABEL: return "dbg_label";
5649 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00005650 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00005651 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00005652 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00005653 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005654
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005655 // Unary operators
5656 case ISD::FABS: return "fabs";
5657 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00005658 case ISD::FSQRT: return "fsqrt";
5659 case ISD::FSIN: return "fsin";
5660 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00005661 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00005662 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00005663 case ISD::FTRUNC: return "ftrunc";
5664 case ISD::FFLOOR: return "ffloor";
5665 case ISD::FCEIL: return "fceil";
5666 case ISD::FRINT: return "frint";
5667 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005668
5669 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005670 case ISD::ADD: return "add";
5671 case ISD::SUB: return "sub";
5672 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00005673 case ISD::MULHU: return "mulhu";
5674 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005675 case ISD::SDIV: return "sdiv";
5676 case ISD::UDIV: return "udiv";
5677 case ISD::SREM: return "srem";
5678 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00005679 case ISD::SMUL_LOHI: return "smul_lohi";
5680 case ISD::UMUL_LOHI: return "umul_lohi";
5681 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00005682 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005683 case ISD::AND: return "and";
5684 case ISD::OR: return "or";
5685 case ISD::XOR: return "xor";
5686 case ISD::SHL: return "shl";
5687 case ISD::SRA: return "sra";
5688 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00005689 case ISD::ROTL: return "rotl";
5690 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00005691 case ISD::FADD: return "fadd";
5692 case ISD::FSUB: return "fsub";
5693 case ISD::FMUL: return "fmul";
5694 case ISD::FDIV: return "fdiv";
5695 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00005696 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00005697 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005698
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005699 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005700 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005701 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005702 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005703 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005704 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005705 case ISD::CONCAT_VECTORS: return "concat_vectors";
5706 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005707 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005708 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00005709 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005710 case ISD::ADDC: return "addc";
5711 case ISD::ADDE: return "adde";
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005712 case ISD::SADDO: return "saddo";
5713 case ISD::UADDO: return "uaddo";
Bill Wendling74c37652008-12-09 22:08:41 +00005714 case ISD::SSUBO: return "ssubo";
5715 case ISD::USUBO: return "usubo";
5716 case ISD::SMULO: return "smulo";
5717 case ISD::UMULO: return "umulo";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005718 case ISD::SUBC: return "subc";
5719 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005720 case ISD::SHL_PARTS: return "shl_parts";
5721 case ISD::SRA_PARTS: return "sra_parts";
5722 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00005723
5724 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5725 case ISD::INSERT_SUBREG: return "insert_subreg";
5726
Chris Lattner7f644642005-04-28 21:44:03 +00005727 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005728 case ISD::SIGN_EXTEND: return "sign_extend";
5729 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005730 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005731 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005732 case ISD::TRUNCATE: return "truncate";
5733 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005734 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005735 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005736 case ISD::FP_EXTEND: return "fp_extend";
5737
5738 case ISD::SINT_TO_FP: return "sint_to_fp";
5739 case ISD::UINT_TO_FP: return "uint_to_fp";
5740 case ISD::FP_TO_SINT: return "fp_to_sint";
5741 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005742 case ISD::BIT_CONVERT: return "bit_convert";
Mon P Wang77cdf302008-11-10 20:54:11 +00005743
5744 case ISD::CONVERT_RNDSAT: {
5745 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
5746 default: assert(0 && "Unknown cvt code!");
5747 case ISD::CVT_FF: return "cvt_ff";
5748 case ISD::CVT_FS: return "cvt_fs";
5749 case ISD::CVT_FU: return "cvt_fu";
5750 case ISD::CVT_SF: return "cvt_sf";
5751 case ISD::CVT_UF: return "cvt_uf";
5752 case ISD::CVT_SS: return "cvt_ss";
5753 case ISD::CVT_SU: return "cvt_su";
5754 case ISD::CVT_US: return "cvt_us";
5755 case ISD::CVT_UU: return "cvt_uu";
5756 }
5757 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005758
5759 // Control flow instructions
5760 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005761 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005762 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005763 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005764 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005765 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005766 case ISD::CALLSEQ_START: return "callseq_start";
5767 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005768
5769 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005770 case ISD::LOAD: return "load";
5771 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005772 case ISD::VAARG: return "vaarg";
5773 case ISD::VACOPY: return "vacopy";
5774 case ISD::VAEND: return "vaend";
5775 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005776 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005777 case ISD::EXTRACT_ELEMENT: return "extract_element";
5778 case ISD::BUILD_PAIR: return "build_pair";
5779 case ISD::STACKSAVE: return "stacksave";
5780 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005781 case ISD::TRAP: return "trap";
5782
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005783 // Bit manipulation
5784 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005785 case ISD::CTPOP: return "ctpop";
5786 case ISD::CTTZ: return "cttz";
5787 case ISD::CTLZ: return "ctlz";
5788
Chris Lattner36ce6912005-11-29 06:21:05 +00005789 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005790 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005791 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005792
Duncan Sands36397f52007-07-27 12:58:54 +00005793 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005794 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005795
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005796 case ISD::CONDCODE:
5797 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005798 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005799 case ISD::SETOEQ: return "setoeq";
5800 case ISD::SETOGT: return "setogt";
5801 case ISD::SETOGE: return "setoge";
5802 case ISD::SETOLT: return "setolt";
5803 case ISD::SETOLE: return "setole";
5804 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005805
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005806 case ISD::SETO: return "seto";
5807 case ISD::SETUO: return "setuo";
5808 case ISD::SETUEQ: return "setue";
5809 case ISD::SETUGT: return "setugt";
5810 case ISD::SETUGE: return "setuge";
5811 case ISD::SETULT: return "setult";
5812 case ISD::SETULE: return "setule";
5813 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005814
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005815 case ISD::SETEQ: return "seteq";
5816 case ISD::SETGT: return "setgt";
5817 case ISD::SETGE: return "setge";
5818 case ISD::SETLT: return "setlt";
5819 case ISD::SETLE: return "setle";
5820 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005821 }
5822 }
5823}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005824
Evan Cheng144d8f02006-11-09 17:55:04 +00005825const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005826 switch (AM) {
5827 default:
5828 return "";
5829 case ISD::PRE_INC:
5830 return "<pre-inc>";
5831 case ISD::PRE_DEC:
5832 return "<pre-dec>";
5833 case ISD::POST_INC:
5834 return "<post-inc>";
5835 case ISD::POST_DEC:
5836 return "<post-dec>";
5837 }
5838}
5839
Duncan Sands276dcbd2008-03-21 09:14:45 +00005840std::string ISD::ArgFlagsTy::getArgFlagsString() {
5841 std::string S = "< ";
5842
5843 if (isZExt())
5844 S += "zext ";
5845 if (isSExt())
5846 S += "sext ";
5847 if (isInReg())
5848 S += "inreg ";
5849 if (isSRet())
5850 S += "sret ";
5851 if (isByVal())
5852 S += "byval ";
5853 if (isNest())
5854 S += "nest ";
5855 if (getByValAlign())
5856 S += "byval-align:" + utostr(getByValAlign()) + " ";
5857 if (getOrigAlign())
5858 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5859 if (getByValSize())
5860 S += "byval-size:" + utostr(getByValSize()) + " ";
5861 return S + ">";
5862}
5863
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005864void SDNode::dump() const { dump(0); }
5865void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005866 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005867 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005868}
5869
5870void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5871 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005872
5873 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005874 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005875 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005876 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005877 else
Chris Lattner944fac72008-08-23 22:23:09 +00005878 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005879 }
Chris Lattner944fac72008-08-23 22:23:09 +00005880 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005881
Chris Lattner944fac72008-08-23 22:23:09 +00005882 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005883 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005884 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005885 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005886 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005887 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005888 }
5889
Evan Chengce254432007-12-11 02:08:35 +00005890 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005891 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005892 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005893 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005894 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005895 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005896 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005897 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005898 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005899 }
Chris Lattner944fac72008-08-23 22:23:09 +00005900 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005901 }
5902
Chris Lattnerc3aae252005-01-07 07:46:32 +00005903 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005904 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005905 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005906 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005907 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005908 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005909 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005910 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005911 OS << "<APFloat(";
Dale Johannesen23a98552008-10-09 23:00:39 +00005912 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005913 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005914 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005915 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005916 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohman668aff62008-10-18 18:22:42 +00005917 int64_t offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005918 OS << '<';
5919 WriteAsOperand(OS, GADN->getGlobal());
5920 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005921 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005922 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005923 else
Chris Lattner944fac72008-08-23 22:23:09 +00005924 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005925 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005926 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005927 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005928 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005929 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005930 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005931 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005932 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005933 else
Chris Lattner944fac72008-08-23 22:23:09 +00005934 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005935 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005936 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005937 else
Chris Lattner944fac72008-08-23 22:23:09 +00005938 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005939 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005940 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005941 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5942 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005943 OS << LBB->getName() << " ";
5944 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005945 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005946 if (G && R->getReg() &&
5947 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005948 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005949 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005950 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005951 }
Bill Wendling056292f2008-09-16 21:48:12 +00005952 } else if (const ExternalSymbolSDNode *ES =
5953 dyn_cast<ExternalSymbolSDNode>(this)) {
5954 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005955 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5956 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005957 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005958 else
Chris Lattner944fac72008-08-23 22:23:09 +00005959 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005960 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5961 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005962 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005963 else
Chris Lattner944fac72008-08-23 22:23:09 +00005964 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005965 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005966 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005967 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005968 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005969 }
5970 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005971 const Value *SrcValue = LD->getSrcValue();
5972 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005973 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005974 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005975 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005976 else
Chris Lattner944fac72008-08-23 22:23:09 +00005977 OS << "null";
5978 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005979
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005980 bool doExt = true;
5981 switch (LD->getExtensionType()) {
5982 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005983 case ISD::EXTLOAD: OS << " <anyext "; break;
5984 case ISD::SEXTLOAD: OS << " <sext "; break;
5985 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005986 }
5987 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005988 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005989
Evan Cheng144d8f02006-11-09 17:55:04 +00005990 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005991 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005992 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005993 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005994 OS << " <volatile>";
5995 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005996 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005997 const Value *SrcValue = ST->getSrcValue();
5998 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005999 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00006000 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00006001 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00006002 else
Chris Lattner944fac72008-08-23 22:23:09 +00006003 OS << "null";
6004 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00006005
6006 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00006007 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00006008
6009 const char *AM = getIndexedModeName(ST->getAddressingMode());
6010 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00006011 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00006012 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00006013 OS << " <volatile>";
6014 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00006015 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
6016 const Value *SrcValue = AT->getSrcValue();
6017 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00006018 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00006019 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00006020 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00006021 else
Chris Lattner944fac72008-08-23 22:23:09 +00006022 OS << "null";
6023 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00006024 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00006025 OS << " <volatile>";
6026 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00006027 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00006028}
6029
Chris Lattnerde202b32005-11-09 23:47:37 +00006030static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00006031 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00006032 if (N->getOperand(i).getNode()->hasOneUse())
6033 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00006034 else
Bill Wendling832171c2006-12-07 20:04:42 +00006035 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00006036 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00006037
Chris Lattnerea946cd2005-01-09 20:38:33 +00006038
Bill Wendling832171c2006-12-07 20:04:42 +00006039 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00006040 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00006041}
6042
Chris Lattnerc3aae252005-01-07 07:46:32 +00006043void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00006044 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00006045
Dan Gohman90a7b8f2008-07-15 18:18:54 +00006046 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
6047 I != E; ++I) {
6048 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00006049 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00006050 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00006051 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00006052
Gabor Greifba36cb52008-08-28 21:40:38 +00006053 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00006054
Bill Wendling832171c2006-12-07 20:04:42 +00006055 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00006056}
6057
Evan Chengd6594ae2006-09-12 21:00:35 +00006058const Type *ConstantPoolSDNode::getType() const {
6059 if (isMachineConstantPoolEntry())
6060 return Val.MachineCPVal->getType();
6061 return Val.ConstVal->getType();
6062}