blob: 6d1fdd23407d69c29273a4e1e21d486fa36d35bc [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 Gohman475871a2008-07-27 21:46:04 +0000345 ID.AddPointer(Ops->getVal());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000346 ID.AddInteger(Ops->getSDValue().getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000347 }
348}
349
Jim Laskey583bd472006-10-27 23:46:08 +0000350static void AddNodeIDNode(FoldingSetNodeID &ID,
351 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000352 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000353 AddNodeIDOpcode(ID, OpC);
354 AddNodeIDValueTypes(ID, VTList);
355 AddNodeIDOperands(ID, OpList, N);
356}
357
Duncan Sands0dc40452008-10-27 15:30:53 +0000358/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
359/// the NodeID data.
360static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000361 switch (N->getOpcode()) {
362 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000363 case ISD::ARG_FLAGS:
364 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
365 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 case ISD::TargetConstant:
367 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000368 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 break;
370 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000371 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000372 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000374 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000376 case ISD::GlobalAddress:
377 case ISD::TargetGlobalTLSAddress:
378 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000379 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 ID.AddPointer(GA->getGlobal());
381 ID.AddInteger(GA->getOffset());
382 break;
383 }
384 case ISD::BasicBlock:
385 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
386 break;
387 case ISD::Register:
388 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
389 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000390 case ISD::DBG_STOPPOINT: {
391 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
392 ID.AddInteger(DSP->getLine());
393 ID.AddInteger(DSP->getColumn());
394 ID.AddPointer(DSP->getCompileUnit());
395 break;
396 }
Dan Gohman69de1932008-02-06 22:27:42 +0000397 case ISD::SRCVALUE:
398 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
399 break;
400 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000401 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000402 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000403 break;
404 }
405 case ISD::FrameIndex:
406 case ISD::TargetFrameIndex:
407 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
408 break;
409 case ISD::JumpTable:
410 case ISD::TargetJumpTable:
411 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
412 break;
413 case ISD::ConstantPool:
414 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000415 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000416 ID.AddInteger(CP->getAlignment());
417 ID.AddInteger(CP->getOffset());
418 if (CP->isMachineConstantPoolEntry())
419 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
420 else
421 ID.AddPointer(CP->getConstVal());
422 break;
423 }
Dan Gohman5eb0cec2008-09-15 19:46:03 +0000424 case ISD::CALL: {
425 const CallSDNode *Call = cast<CallSDNode>(N);
426 ID.AddInteger(Call->getCallingConv());
427 ID.AddInteger(Call->isVarArg());
428 break;
429 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000430 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 ID.AddInteger(LD->getAddressingMode());
433 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000434 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000435 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 break;
437 }
438 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000439 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 ID.AddInteger(ST->getAddressingMode());
441 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000442 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000443 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000444 break;
445 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000446 case ISD::ATOMIC_CMP_SWAP:
447 case ISD::ATOMIC_SWAP:
448 case ISD::ATOMIC_LOAD_ADD:
449 case ISD::ATOMIC_LOAD_SUB:
450 case ISD::ATOMIC_LOAD_AND:
451 case ISD::ATOMIC_LOAD_OR:
452 case ISD::ATOMIC_LOAD_XOR:
453 case ISD::ATOMIC_LOAD_NAND:
454 case ISD::ATOMIC_LOAD_MIN:
455 case ISD::ATOMIC_LOAD_MAX:
456 case ISD::ATOMIC_LOAD_UMIN:
457 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000458 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
459 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000460 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000461 }
Mon P Wang28873102008-06-25 08:15:39 +0000462 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000463}
464
Duncan Sands0dc40452008-10-27 15:30:53 +0000465/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
466/// data.
467static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
468 AddNodeIDOpcode(ID, N->getOpcode());
469 // Add the return value info.
470 AddNodeIDValueTypes(ID, N->getVTList());
471 // Add the operand info.
472 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
473
474 // Handle SDNode leafs with special info.
475 AddNodeIDCustom(ID, N);
476}
477
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000478/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
479/// the CSE map that carries both alignment and volatility information.
480///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000481static inline unsigned
482encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000483 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
484}
485
Jim Laskey583bd472006-10-27 23:46:08 +0000486//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000487// SelectionDAG Class
488//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000489
Duncan Sands0dc40452008-10-27 15:30:53 +0000490/// doNotCSE - Return true if CSE should not be performed for this node.
491static bool doNotCSE(SDNode *N) {
492 if (N->getValueType(0) == MVT::Flag)
493 return true; // Never CSE anything that produces a flag.
494
495 switch (N->getOpcode()) {
496 default: break;
497 case ISD::HANDLENODE:
498 case ISD::DBG_LABEL:
499 case ISD::DBG_STOPPOINT:
500 case ISD::EH_LABEL:
501 case ISD::DECLARE:
502 return true; // Never CSE these nodes.
503 }
504
505 // Check that remaining values produced are not flags.
506 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
507 if (N->getValueType(i) == MVT::Flag)
508 return true; // Never CSE anything that produces a flag.
509
510 return false;
511}
512
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000513/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000514/// SelectionDAG.
515void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000516 // Create a dummy node (which is not added to allnodes), that adds a reference
517 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000518 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000519
Chris Lattner190a4182006-08-04 17:45:20 +0000520 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000521
Chris Lattner190a4182006-08-04 17:45:20 +0000522 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000523 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000524 if (I->use_empty())
525 DeadNodes.push_back(I);
526
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000527 RemoveDeadNodes(DeadNodes);
528
529 // If the root changed (e.g. it was a dead load, update the root).
530 setRoot(Dummy.getValue());
531}
532
533/// RemoveDeadNodes - This method deletes the unreachable nodes in the
534/// given list, and any nodes that become unreachable as a result.
535void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
536 DAGUpdateListener *UpdateListener) {
537
Chris Lattner190a4182006-08-04 17:45:20 +0000538 // Process the worklist, deleting the nodes and adding their uses to the
539 // worklist.
540 while (!DeadNodes.empty()) {
541 SDNode *N = DeadNodes.back();
542 DeadNodes.pop_back();
543
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000544 if (UpdateListener)
545 UpdateListener->NodeDeleted(N, 0);
546
Chris Lattner190a4182006-08-04 17:45:20 +0000547 // Take the node out of the appropriate CSE map.
548 RemoveNodeFromCSEMaps(N);
549
550 // Next, brutally remove the operand list. This is safe to do, as there are
551 // no cycles in the graph.
552 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000553 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000554 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000555
556 // Now that we removed this operand, see if there are no uses of it left.
557 if (Operand->use_empty())
558 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000559 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000560
Dan Gohmanc5336122009-01-19 22:39:36 +0000561 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000562 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000563}
564
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000565void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000566 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000567 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000568}
569
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000570void SelectionDAG::DeleteNode(SDNode *N) {
571 assert(N->use_empty() && "Cannot delete a node that is not dead!");
572
573 // First take this out of the appropriate CSE map.
574 RemoveNodeFromCSEMaps(N);
575
Chris Lattner1e111c72005-09-07 05:37:01 +0000576 // Finally, remove uses due to operands of this node, remove from the
577 // AllNodes list, and delete the node.
578 DeleteNodeNotInCSEMaps(N);
579}
580
581void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmanc5336122009-01-19 22:39:36 +0000582 assert(N != AllNodes.begin());
583
Dan Gohmanf06c8352008-09-30 18:30:35 +0000584 // Drop all of the operands and decrement used node's use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000585 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000586 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000587
Dan Gohmanc5336122009-01-19 22:39:36 +0000588 DeallocateNode(N);
589}
590
591void SelectionDAG::DeallocateNode(SDNode *N) {
592 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000593 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000594
Dan Gohmanc5336122009-01-19 22:39:36 +0000595 // Set the opcode to DELETED_NODE to help catch bugs when node
596 // memory is reallocated.
597 N->NodeType = ISD::DELETED_NODE;
598
Dan Gohman11467282008-08-26 01:44:34 +0000599 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000600}
601
Chris Lattner149c58c2005-08-16 18:17:10 +0000602/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
603/// correspond to it. This is useful when we're about to delete or repurpose
604/// the node. We don't want future request for structurally identical nodes
605/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000606bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000607 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000608 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000609 case ISD::EntryToken:
610 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000611 return false;
612 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000613 case ISD::CONDCODE:
614 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
615 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000616 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000617 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
618 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000619 case ISD::ExternalSymbol:
620 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000621 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000622 case ISD::TargetExternalSymbol:
623 Erased =
624 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000625 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000626 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000627 MVT VT = cast<VTSDNode>(N)->getVT();
628 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000629 Erased = ExtendedValueTypeNodes.erase(VT);
630 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000631 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
632 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000633 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000634 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000635 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000636 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000637 // Remove it from the CSE Map.
638 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000639 break;
640 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000641#ifndef NDEBUG
642 // Verify that the node was actually in one of the CSE maps, unless it has a
643 // flag result (which cannot be CSE'd) or is one of the special cases that are
644 // not subject to CSE.
645 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000646 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000647 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000648 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000649 assert(0 && "Node is not in map!");
650 }
651#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000652 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000653}
654
Chris Lattner8b8749f2005-08-17 19:00:20 +0000655/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
656/// has been taken out and modified in some way. If the specified node already
657/// exists in the CSE maps, do not modify the maps, but return the existing node
658/// instead. If it doesn't exist, add it and return null.
659///
660SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
661 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000662
Duncan Sands0dc40452008-10-27 15:30:53 +0000663 if (doNotCSE(N))
664 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000665
Chris Lattnera5682852006-08-07 23:03:03 +0000666 SDNode *New = CSEMap.GetOrInsertNode(N);
667 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000668 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000669}
670
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000671/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
672/// were replaced with those specified. If this node is never memoized,
673/// return null, otherwise return a pointer to the slot it would take. If a
674/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000675SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000676 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000677 if (doNotCSE(N))
678 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000679
Dan Gohman475871a2008-07-27 21:46:04 +0000680 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000681 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000682 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000683 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000684 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000685}
686
687/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
688/// were replaced with those specified. If this node is never memoized,
689/// return null, otherwise return a pointer to the slot it would take. If a
690/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000691SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000692 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000693 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000694 if (doNotCSE(N))
695 return 0;
696
Dan Gohman475871a2008-07-27 21:46:04 +0000697 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000698 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000699 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000700 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000701 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
702}
703
704
705/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
706/// were replaced with those specified. If this node is never memoized,
707/// return null, otherwise return a pointer to the slot it would take. If a
708/// node already exists with these operands, the slot will be non-null.
709SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000710 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000711 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000712 if (doNotCSE(N))
713 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000714
Jim Laskey583bd472006-10-27 23:46:08 +0000715 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000716 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000717 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000718 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000719}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000720
Duncan Sandsd038e042008-07-21 10:20:31 +0000721/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
722void SelectionDAG::VerifyNode(SDNode *N) {
723 switch (N->getOpcode()) {
724 default:
725 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000726 case ISD::BUILD_PAIR: {
727 MVT VT = N->getValueType(0);
728 assert(N->getNumValues() == 1 && "Too many results!");
729 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
730 "Wrong return type!");
731 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
732 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
733 "Mismatched operand types!");
734 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
735 "Wrong operand type!");
736 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
737 "Wrong return type size");
738 break;
739 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000740 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000741 assert(N->getNumValues() == 1 && "Too many results!");
742 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000743 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000744 "Wrong number of operands!");
Duncan Sands0954aef2008-10-22 09:00:33 +0000745 // FIXME: Change vector_shuffle to a variadic node with mask elements being
746 // operands of the node. Currently the mask is a BUILD_VECTOR passed as an
747 // operand, and it is not always possible to legalize it. Turning off the
748 // following checks at least makes it possible to legalize most of the time.
749// MVT EltVT = N->getValueType(0).getVectorElementType();
750// for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
751// assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000752// "Wrong operand type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000753 break;
754 }
755 }
756}
757
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000758/// getMVTAlignment - Compute the default alignment value for the
759/// given type.
760///
761unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
762 const Type *Ty = VT == MVT::iPTR ?
763 PointerType::get(Type::Int8Ty, 0) :
764 VT.getTypeForMVT();
765
766 return TLI.getTargetData()->getABITypeAlignment(Ty);
767}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000768
Dan Gohman7c3234c2008-08-27 23:52:12 +0000769SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
770 : TLI(tli), FLI(fli),
771 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000772 Root(getEntryNode()) {
773 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000774}
775
Devang Patel6e7a1612009-01-09 19:11:50 +0000776void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
777 DwarfWriter *dw) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000778 MF = &mf;
779 MMI = mmi;
Devang Patel6e7a1612009-01-09 19:11:50 +0000780 DW = dw;
Dan Gohman7c3234c2008-08-27 23:52:12 +0000781}
782
Chris Lattner78ec3112003-08-11 14:57:33 +0000783SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000784 allnodes_clear();
785}
786
787void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000788 assert(&*AllNodes.begin() == &EntryNode);
789 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000790 while (!AllNodes.empty())
791 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000792}
793
Dan Gohman7c3234c2008-08-27 23:52:12 +0000794void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000795 allnodes_clear();
796 OperandAllocator.Reset();
797 CSEMap.clear();
798
799 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000800 ExternalSymbols.clear();
801 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000802 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
803 static_cast<CondCodeSDNode*>(0));
804 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
805 static_cast<SDNode*>(0));
806
807 EntryNode.Uses = 0;
808 AllNodes.push_back(&EntryNode);
809 Root = getEntryNode();
810}
811
Dan Gohman475871a2008-07-27 21:46:04 +0000812SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000813 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000814 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000815 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000816 return getNode(ISD::AND, Op.getValueType(), Op,
817 getConstant(Imm, Op.getValueType()));
818}
819
Bob Wilson4c245462009-01-22 17:39:32 +0000820/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
821///
822SDValue SelectionDAG::getNOT(SDValue Val, MVT VT) {
823 SDValue NegOne;
824 if (VT.isVector()) {
825 MVT EltVT = VT.getVectorElementType();
826 SDValue NegOneElt = getConstant(EltVT.getIntegerVTBitMask(), EltVT);
827 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOneElt);
828 NegOne = getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0], NegOnes.size());
829 } else
830 NegOne = getConstant(VT.getIntegerVTBitMask(), VT);
831
832 return getNode(ISD::XOR, VT, Val, NegOne);
833}
834
Dan Gohman475871a2008-07-27 21:46:04 +0000835SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000836 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000837 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000838}
839
Dan Gohman475871a2008-07-27 21:46:04 +0000840SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000841 return getConstant(*ConstantInt::get(Val), VT, isT);
842}
843
844SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000845 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000846
Evan Cheng0ff39b32008-06-30 07:31:25 +0000847 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000848 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000849 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000850
851 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000852 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000853 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000854 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000855 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000856 SDNode *N = NULL;
857 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000858 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000859 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000860 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000861 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000862 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000863 CSEMap.InsertNode(N, IP);
864 AllNodes.push_back(N);
865 }
866
Dan Gohman475871a2008-07-27 21:46:04 +0000867 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000868 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000869 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000870 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000871 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
872 }
873 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000874}
875
Dan Gohman475871a2008-07-27 21:46:04 +0000876SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000877 return getConstant(Val, TLI.getPointerTy(), isTarget);
878}
879
880
Dan Gohman475871a2008-07-27 21:46:04 +0000881SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000882 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
883}
884
885SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000886 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000887
Duncan Sands83ec4b62008-06-06 12:08:01 +0000888 MVT EltVT =
889 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000890
Chris Lattnerd8658612005-02-17 20:17:32 +0000891 // Do the map lookup using the actual bit pattern for the floating point
892 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
893 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000894 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000895 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000896 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000897 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000898 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000899 SDNode *N = NULL;
900 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000901 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000902 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000903 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000904 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000905 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000906 CSEMap.InsertNode(N, IP);
907 AllNodes.push_back(N);
908 }
909
Dan Gohman475871a2008-07-27 21:46:04 +0000910 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000911 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000912 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000913 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000914 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
915 }
916 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000917}
918
Dan Gohman475871a2008-07-27 21:46:04 +0000919SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000920 MVT EltVT =
921 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000922 if (EltVT==MVT::f32)
923 return getConstantFP(APFloat((float)Val), VT, isTarget);
924 else
925 return getConstantFP(APFloat(Val), VT, isTarget);
926}
927
Dan Gohman475871a2008-07-27 21:46:04 +0000928SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Dan Gohman6520e202008-10-18 02:06:02 +0000929 MVT VT, int64_t Offset,
Dan Gohman475871a2008-07-27 21:46:04 +0000930 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000931 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000932
Dan Gohman6520e202008-10-18 02:06:02 +0000933 // Truncate (with sign-extension) the offset value to the pointer size.
Dan Gohman44013612008-10-21 03:38:42 +0000934 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +0000935 if (BitWidth < 64)
936 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
937
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000938 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
939 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000940 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000941 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000942 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000943 }
944
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000945 if (GVar && GVar->isThreadLocal())
946 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
947 else
948 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000949
Jim Laskey583bd472006-10-27 23:46:08 +0000950 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000951 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000952 ID.AddPointer(GV);
953 ID.AddInteger(Offset);
954 void *IP = 0;
955 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000956 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000957 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000958 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000959 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000960 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000961 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000962}
963
Dan Gohman475871a2008-07-27 21:46:04 +0000964SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000965 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000966 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000967 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000968 ID.AddInteger(FI);
969 void *IP = 0;
970 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000971 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000972 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000973 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000974 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000975 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000976 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000977}
978
Dan Gohman475871a2008-07-27 21:46:04 +0000979SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000980 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000981 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000982 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000983 ID.AddInteger(JTI);
984 void *IP = 0;
985 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000986 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000987 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000988 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000989 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000990 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000991 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +0000992}
993
Dan Gohman475871a2008-07-27 21:46:04 +0000994SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
995 unsigned Alignment, int Offset,
996 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +0000997 if (Alignment == 0)
998 Alignment =
999 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001000 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001001 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001002 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001003 ID.AddInteger(Alignment);
1004 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001005 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001006 void *IP = 0;
1007 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001008 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001009 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001010 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001011 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001012 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001013 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001014}
1015
Chris Lattnerc3aae252005-01-07 07:46:32 +00001016
Dan Gohman475871a2008-07-27 21:46:04 +00001017SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1018 unsigned Alignment, int Offset,
1019 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +00001020 if (Alignment == 0)
1021 Alignment =
1022 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001023 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001024 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001025 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001026 ID.AddInteger(Alignment);
1027 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001028 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001029 void *IP = 0;
1030 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001031 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001032 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001033 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001034 CSEMap.InsertNode(N, IP);
1035 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001036 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001037}
1038
1039
Dan Gohman475871a2008-07-27 21:46:04 +00001040SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001041 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001042 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001043 ID.AddPointer(MBB);
1044 void *IP = 0;
1045 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001046 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001047 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001048 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001049 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001050 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001051 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052}
1053
Dan Gohman475871a2008-07-27 21:46:04 +00001054SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001055 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001056 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001057 ID.AddInteger(Flags.getRawBits());
1058 void *IP = 0;
1059 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001060 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001061 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001062 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001063 CSEMap.InsertNode(N, IP);
1064 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001065 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001066}
1067
Dan Gohman475871a2008-07-27 21:46:04 +00001068SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001069 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1070 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001071
Duncan Sands83ec4b62008-06-06 12:08:01 +00001072 SDNode *&N = VT.isExtended() ?
1073 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001074
Dan Gohman475871a2008-07-27 21:46:04 +00001075 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001076 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001077 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001078 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001079 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001080}
1081
Bill Wendling056292f2008-09-16 21:48:12 +00001082SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1083 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001084 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001085 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1086 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001087 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001088 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001089}
1090
Bill Wendling056292f2008-09-16 21:48:12 +00001091SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
1092 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001093 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001094 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1095 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001097 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098}
1099
Dan Gohman475871a2008-07-27 21:46:04 +00001100SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001101 if ((unsigned)Cond >= CondCodeNodes.size())
1102 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001103
Chris Lattner079a27a2005-08-09 20:40:02 +00001104 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001105 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001106 new (N) CondCodeSDNode(Cond);
1107 CondCodeNodes[Cond] = N;
1108 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001109 }
Dan Gohman475871a2008-07-27 21:46:04 +00001110 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001111}
1112
Mon P Wang77cdf302008-11-10 20:54:11 +00001113SDValue SelectionDAG::getConvertRndSat(MVT VT, SDValue Val, SDValue DTy,
1114 SDValue STy, SDValue Rnd, SDValue Sat,
1115 ISD::CvtCode Code) {
Mon P Wang6db08c42008-12-11 03:30:13 +00001116 // If the src and dest types are the same, no conversion is necessary.
1117 if (DTy == STy)
1118 return Val;
1119
Mon P Wang77cdf302008-11-10 20:54:11 +00001120 FoldingSetNodeID ID;
1121 void* IP = 0;
1122 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1123 return SDValue(E, 0);
1124 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1125 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1126 new (N) CvtRndSatSDNode(VT, Ops, 5, Code);
1127 CSEMap.InsertNode(N, IP);
1128 AllNodes.push_back(N);
1129 return SDValue(N, 0);
1130}
1131
Dan Gohman475871a2008-07-27 21:46:04 +00001132SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001133 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001134 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001135 ID.AddInteger(RegNo);
1136 void *IP = 0;
1137 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001138 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001139 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001140 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001141 CSEMap.InsertNode(N, IP);
1142 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001143 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001144}
1145
Dan Gohman475871a2008-07-27 21:46:04 +00001146SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Devang Patel83489bb2009-01-13 00:35:13 +00001147 unsigned Line, unsigned Col,
1148 Value *CU) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001149 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001150 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001151 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001152 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001153}
1154
Dan Gohman475871a2008-07-27 21:46:04 +00001155SDValue SelectionDAG::getLabel(unsigned Opcode,
1156 SDValue Root,
1157 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001158 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001159 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001160 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1161 ID.AddInteger(LabelID);
1162 void *IP = 0;
1163 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001164 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001165 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001166 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001167 CSEMap.InsertNode(N, IP);
1168 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001169 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001170}
1171
Dan Gohman475871a2008-07-27 21:46:04 +00001172SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001173 assert((!V || isa<PointerType>(V->getType())) &&
1174 "SrcValue is not a pointer?");
1175
Jim Laskey583bd472006-10-27 23:46:08 +00001176 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001177 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001178 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001179
Chris Lattner61b09412006-08-11 21:01:22 +00001180 void *IP = 0;
1181 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001182 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001183
Dan Gohmanfed90b62008-07-28 21:51:04 +00001184 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001185 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001186 CSEMap.InsertNode(N, IP);
1187 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001188 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001189}
1190
Dan Gohman475871a2008-07-27 21:46:04 +00001191SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Cheng24ac4082008-11-24 07:09:49 +00001192#ifndef NDEBUG
Dan Gohman69de1932008-02-06 22:27:42 +00001193 const Value *v = MO.getValue();
1194 assert((!v || isa<PointerType>(v->getType())) &&
1195 "SrcValue is not a pointer?");
Evan Cheng24ac4082008-11-24 07:09:49 +00001196#endif
Dan Gohman69de1932008-02-06 22:27:42 +00001197
1198 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001199 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001200 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001201
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 Gohman69de1932008-02-06 22:27:42 +00001205
Dan Gohmanfed90b62008-07-28 21:51:04 +00001206 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001207 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001208 CSEMap.InsertNode(N, IP);
1209 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001210 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001211}
1212
Chris Lattner37ce9df2007-10-15 17:47:20 +00001213/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1214/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001215SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001216 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands9304f2c2008-12-04 18:08:40 +00001217 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001218 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001219 unsigned StackAlign =
1220 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1221
Chris Lattner37ce9df2007-10-15 17:47:20 +00001222 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1223 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1224}
1225
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001226/// CreateStackTemporary - Create a stack temporary suitable for holding
1227/// either of the specified value types.
1228SDValue SelectionDAG::CreateStackTemporary(MVT VT1, MVT VT2) {
1229 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1230 VT2.getStoreSizeInBits())/8;
1231 const Type *Ty1 = VT1.getTypeForMVT();
1232 const Type *Ty2 = VT2.getTypeForMVT();
1233 const TargetData *TD = TLI.getTargetData();
1234 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1235 TD->getPrefTypeAlignment(Ty2));
1236
1237 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1238 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1239 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1240}
1241
Dan Gohman475871a2008-07-27 21:46:04 +00001242SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1243 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001244 // These setcc operations always fold.
1245 switch (Cond) {
1246 default: break;
1247 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001248 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001250 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001251
1252 case ISD::SETOEQ:
1253 case ISD::SETOGT:
1254 case ISD::SETOGE:
1255 case ISD::SETOLT:
1256 case ISD::SETOLE:
1257 case ISD::SETONE:
1258 case ISD::SETO:
1259 case ISD::SETUO:
1260 case ISD::SETUEQ:
1261 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001262 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001263 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001264 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001265
Gabor Greifba36cb52008-08-28 21:40:38 +00001266 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001267 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001268 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001269 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001270
Chris Lattnerc3aae252005-01-07 07:46:32 +00001271 switch (Cond) {
1272 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001273 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1274 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001275 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1276 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1277 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1278 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1279 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1280 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1281 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1282 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001283 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001284 }
Chris Lattner67255a12005-04-07 18:14:58 +00001285 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1287 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001288 // No compile time operations on this type yet.
1289 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001290 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001291
1292 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001293 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001294 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001295 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1296 return getNode(ISD::UNDEF, VT);
1297 // fall through
1298 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1299 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1300 return getNode(ISD::UNDEF, VT);
1301 // fall through
1302 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001303 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001304 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1305 return getNode(ISD::UNDEF, VT);
1306 // fall through
1307 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1308 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1309 return getNode(ISD::UNDEF, VT);
1310 // fall through
1311 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1312 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1313 return getNode(ISD::UNDEF, VT);
1314 // fall through
1315 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001316 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001317 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1318 return getNode(ISD::UNDEF, VT);
1319 // fall through
1320 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001321 R==APFloat::cmpEqual, VT);
1322 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1323 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1324 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1325 R==APFloat::cmpEqual, VT);
1326 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1327 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1328 R==APFloat::cmpLessThan, VT);
1329 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1330 R==APFloat::cmpUnordered, VT);
1331 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1332 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001333 }
1334 } else {
1335 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001336 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001337 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001338 }
1339
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001340 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001341 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001342}
1343
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001344/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1345/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001346bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001347 unsigned BitWidth = Op.getValueSizeInBits();
1348 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1349}
1350
Dan Gohmanea859be2007-06-22 14:59:07 +00001351/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1352/// this predicate to simplify operations downstream. Mask is known to be zero
1353/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001354bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001355 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001356 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001357 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1358 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1359 return (KnownZero & Mask) == Mask;
1360}
1361
1362/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1363/// known to be either zero or one and return them in the KnownZero/KnownOne
1364/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1365/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001366void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001367 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001368 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001369 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001370 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001371 "Mask size mismatches value type size!");
1372
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001373 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001374 if (Depth == 6 || Mask == 0)
1375 return; // Limit search depth.
1376
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001377 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001378
1379 switch (Op.getOpcode()) {
1380 case ISD::Constant:
1381 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001382 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001383 KnownZero = ~KnownOne & Mask;
1384 return;
1385 case ISD::AND:
1386 // If either the LHS or the RHS are Zero, the result is zero.
1387 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001388 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1389 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001390 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1391 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1392
1393 // Output known-1 bits are only known if set in both the LHS & RHS.
1394 KnownOne &= KnownOne2;
1395 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1396 KnownZero |= KnownZero2;
1397 return;
1398 case ISD::OR:
1399 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001400 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1401 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001402 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1403 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1404
1405 // Output known-0 bits are only known if clear in both the LHS & RHS.
1406 KnownZero &= KnownZero2;
1407 // Output known-1 are known to be set if set in either the LHS | RHS.
1408 KnownOne |= KnownOne2;
1409 return;
1410 case ISD::XOR: {
1411 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1412 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1413 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1414 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1415
1416 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001417 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001418 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1419 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1420 KnownZero = KnownZeroOut;
1421 return;
1422 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001423 case ISD::MUL: {
1424 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1425 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1426 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1427 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1428 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1429
1430 // If low bits are zero in either operand, output low known-0 bits.
1431 // Also compute a conserative estimate for high known-0 bits.
1432 // More trickiness is possible, but this is sufficient for the
1433 // interesting case of alignment computation.
1434 KnownOne.clear();
1435 unsigned TrailZ = KnownZero.countTrailingOnes() +
1436 KnownZero2.countTrailingOnes();
1437 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001438 KnownZero2.countLeadingOnes(),
1439 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001440
1441 TrailZ = std::min(TrailZ, BitWidth);
1442 LeadZ = std::min(LeadZ, BitWidth);
1443 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1444 APInt::getHighBitsSet(BitWidth, LeadZ);
1445 KnownZero &= Mask;
1446 return;
1447 }
1448 case ISD::UDIV: {
1449 // For the purposes of computing leading zeros we can conservatively
1450 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001451 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001452 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1453 ComputeMaskedBits(Op.getOperand(0),
1454 AllOnes, KnownZero2, KnownOne2, Depth+1);
1455 unsigned LeadZ = KnownZero2.countLeadingOnes();
1456
1457 KnownOne2.clear();
1458 KnownZero2.clear();
1459 ComputeMaskedBits(Op.getOperand(1),
1460 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001461 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1462 if (RHSUnknownLeadingOnes != BitWidth)
1463 LeadZ = std::min(BitWidth,
1464 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001465
1466 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1467 return;
1468 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001469 case ISD::SELECT:
1470 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1471 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1472 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1473 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1474
1475 // Only known if known in both the LHS and RHS.
1476 KnownOne &= KnownOne2;
1477 KnownZero &= KnownZero2;
1478 return;
1479 case ISD::SELECT_CC:
1480 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1481 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1482 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1483 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1484
1485 // Only known if known in both the LHS and RHS.
1486 KnownOne &= KnownOne2;
1487 KnownZero &= KnownZero2;
1488 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001489 case ISD::SADDO:
1490 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001491 case ISD::SSUBO:
1492 case ISD::USUBO:
1493 case ISD::SMULO:
1494 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001495 if (Op.getResNo() != 1)
1496 return;
Duncan Sands03228082008-11-23 15:47:28 +00001497 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001498 case ISD::SETCC:
1499 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands03228082008-11-23 15:47:28 +00001500 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001501 BitWidth > 1)
1502 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001503 return;
1504 case ISD::SHL:
1505 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1506 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001507 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001508
1509 // If the shift count is an invalid immediate, don't do anything.
1510 if (ShAmt >= BitWidth)
1511 return;
1512
1513 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001514 KnownZero, KnownOne, Depth+1);
1515 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001516 KnownZero <<= ShAmt;
1517 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001518 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001519 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001520 }
1521 return;
1522 case ISD::SRL:
1523 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1524 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001525 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001526
Dan Gohmand4cf9922008-02-26 18:50:50 +00001527 // If the shift count is an invalid immediate, don't do anything.
1528 if (ShAmt >= BitWidth)
1529 return;
1530
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001531 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001532 KnownZero, KnownOne, Depth+1);
1533 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001534 KnownZero = KnownZero.lshr(ShAmt);
1535 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001536
Dan Gohman72d2fd52008-02-13 22:43:25 +00001537 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001538 KnownZero |= HighBits; // High bits known zero.
1539 }
1540 return;
1541 case ISD::SRA:
1542 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001543 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001544
Dan Gohmand4cf9922008-02-26 18:50:50 +00001545 // If the shift count is an invalid immediate, don't do anything.
1546 if (ShAmt >= BitWidth)
1547 return;
1548
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001549 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001550 // If any of the demanded bits are produced by the sign extension, we also
1551 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001552 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1553 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001554 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001555
1556 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1557 Depth+1);
1558 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001559 KnownZero = KnownZero.lshr(ShAmt);
1560 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001561
1562 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001563 APInt SignBit = APInt::getSignBit(BitWidth);
1564 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001565
Dan Gohmanca93a432008-02-20 16:30:17 +00001566 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001567 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001568 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001569 KnownOne |= HighBits; // New bits are known one.
1570 }
1571 }
1572 return;
1573 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001574 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1575 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001576
1577 // Sign extension. Compute the demanded bits in the result that are not
1578 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001579 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001580
Dan Gohman977a76f2008-02-13 22:28:48 +00001581 APInt InSignBit = APInt::getSignBit(EBits);
1582 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001583
1584 // If the sign extended bits are demanded, we know that the sign
1585 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001586 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001587 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001588 InputDemandedBits |= InSignBit;
1589
1590 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1591 KnownZero, KnownOne, Depth+1);
1592 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1593
1594 // If the sign bit of the input is known set or clear, then we know the
1595 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001596 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001597 KnownZero |= NewBits;
1598 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001599 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001600 KnownOne |= NewBits;
1601 KnownZero &= ~NewBits;
1602 } else { // Input sign bit unknown
1603 KnownZero &= ~NewBits;
1604 KnownOne &= ~NewBits;
1605 }
1606 return;
1607 }
1608 case ISD::CTTZ:
1609 case ISD::CTLZ:
1610 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001611 unsigned LowBits = Log2_32(BitWidth)+1;
1612 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001613 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001614 return;
1615 }
1616 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001617 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001618 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001619 MVT VT = LD->getMemoryVT();
1620 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001621 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001622 }
1623 return;
1624 }
1625 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001626 MVT InVT = Op.getOperand(0).getValueType();
1627 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001628 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1629 APInt InMask = Mask;
1630 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001631 KnownZero.trunc(InBits);
1632 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001633 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001634 KnownZero.zext(BitWidth);
1635 KnownOne.zext(BitWidth);
1636 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001637 return;
1638 }
1639 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001640 MVT InVT = Op.getOperand(0).getValueType();
1641 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001642 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001643 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1644 APInt InMask = Mask;
1645 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001646
1647 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001648 // bit is demanded. Temporarily set this bit in the mask for our callee.
1649 if (NewBits.getBoolValue())
1650 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001651
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001652 KnownZero.trunc(InBits);
1653 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001654 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1655
1656 // Note if the sign bit is known to be zero or one.
1657 bool SignBitKnownZero = KnownZero.isNegative();
1658 bool SignBitKnownOne = KnownOne.isNegative();
1659 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1660 "Sign bit can't be known to be both zero and one!");
1661
1662 // If the sign bit wasn't actually demanded by our caller, we don't
1663 // want it set in the KnownZero and KnownOne result values. Reset the
1664 // mask and reapply it to the result values.
1665 InMask = Mask;
1666 InMask.trunc(InBits);
1667 KnownZero &= InMask;
1668 KnownOne &= InMask;
1669
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001670 KnownZero.zext(BitWidth);
1671 KnownOne.zext(BitWidth);
1672
Dan Gohman977a76f2008-02-13 22:28:48 +00001673 // If the sign bit is known zero or one, the top bits match.
1674 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001675 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001676 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001677 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001678 return;
1679 }
1680 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001681 MVT InVT = Op.getOperand(0).getValueType();
1682 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001683 APInt InMask = Mask;
1684 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001685 KnownZero.trunc(InBits);
1686 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001687 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001688 KnownZero.zext(BitWidth);
1689 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001690 return;
1691 }
1692 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001693 MVT InVT = Op.getOperand(0).getValueType();
1694 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001695 APInt InMask = Mask;
1696 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001697 KnownZero.zext(InBits);
1698 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001699 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001700 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001701 KnownZero.trunc(BitWidth);
1702 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001703 break;
1704 }
1705 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001706 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1707 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001708 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1709 KnownOne, Depth+1);
1710 KnownZero |= (~InMask) & Mask;
1711 return;
1712 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001713 case ISD::FGETSIGN:
1714 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001715 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001716 return;
1717
Dan Gohman23e8b712008-04-28 17:02:21 +00001718 case ISD::SUB: {
1719 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1720 // We know that the top bits of C-X are clear if X contains less bits
1721 // than C (i.e. no wrap-around can happen). For example, 20-X is
1722 // positive if we can prove that X is >= 0 and < 16.
1723 if (CLHS->getAPIntValue().isNonNegative()) {
1724 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1725 // NLZ can't be BitWidth with no sign bit
1726 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1727 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1728 Depth+1);
1729
1730 // If all of the MaskV bits are known to be zero, then we know the
1731 // output top bits are zero, because we now know that the output is
1732 // from [0-C].
1733 if ((KnownZero2 & MaskV) == MaskV) {
1734 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1735 // Top bits known zero.
1736 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1737 }
1738 }
1739 }
1740 }
1741 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001742 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001743 // Output known-0 bits are known if clear or set in both the low clear bits
1744 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1745 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001746 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1747 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1748 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1749 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1750
1751 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1752 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1753 KnownZeroOut = std::min(KnownZeroOut,
1754 KnownZero2.countTrailingOnes());
1755
1756 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001757 return;
1758 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001759 case ISD::SREM:
1760 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001761 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001762 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001763 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001764 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1765 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001766
Dan Gohmana60832b2008-08-13 23:12:35 +00001767 // If the sign bit of the first operand is zero, the sign bit of
1768 // the result is zero. If the first operand has no one bits below
1769 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001770 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1771 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001772
Dan Gohman23e8b712008-04-28 17:02:21 +00001773 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001774
1775 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001776 }
1777 }
1778 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001779 case ISD::UREM: {
1780 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001781 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001782 if (RA.isPowerOf2()) {
1783 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001784 APInt Mask2 = LowBits & Mask;
1785 KnownZero |= ~LowBits & Mask;
1786 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1787 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1788 break;
1789 }
1790 }
1791
1792 // Since the result is less than or equal to either operand, any leading
1793 // zero bits in either operand must also exist in the result.
1794 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1795 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1796 Depth+1);
1797 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1798 Depth+1);
1799
1800 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1801 KnownZero2.countLeadingOnes());
1802 KnownOne.clear();
1803 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1804 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001805 }
1806 default:
1807 // Allow the target to implement this method for its nodes.
1808 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1809 case ISD::INTRINSIC_WO_CHAIN:
1810 case ISD::INTRINSIC_W_CHAIN:
1811 case ISD::INTRINSIC_VOID:
1812 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1813 }
1814 return;
1815 }
1816}
1817
1818/// ComputeNumSignBits - Return the number of times the sign bit of the
1819/// register is replicated into the other bits. We know that at least 1 bit
1820/// is always equal to the sign bit (itself), but other cases can give us
1821/// information. For example, immediately after an "SRA X, 2", we know that
1822/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001823unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001824 MVT VT = Op.getValueType();
1825 assert(VT.isInteger() && "Invalid VT!");
1826 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001827 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001828 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001829
1830 if (Depth == 6)
1831 return 1; // Limit search depth.
1832
1833 switch (Op.getOpcode()) {
1834 default: break;
1835 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001836 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001837 return VTBits-Tmp+1;
1838 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001839 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001840 return VTBits-Tmp;
1841
1842 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001843 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1844 // If negative, return # leading ones.
1845 if (Val.isNegative())
1846 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001847
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001848 // Return # leading zeros.
1849 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001850 }
1851
1852 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001853 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001854 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1855
1856 case ISD::SIGN_EXTEND_INREG:
1857 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001858 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001859 Tmp = VTBits-Tmp+1;
1860
1861 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1862 return std::max(Tmp, Tmp2);
1863
1864 case ISD::SRA:
1865 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1866 // SRA X, C -> adds C sign bits.
1867 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001868 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001869 if (Tmp > VTBits) Tmp = VTBits;
1870 }
1871 return Tmp;
1872 case ISD::SHL:
1873 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1874 // shl destroys sign bits.
1875 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001876 if (C->getZExtValue() >= VTBits || // Bad shift.
1877 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1878 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001879 }
1880 break;
1881 case ISD::AND:
1882 case ISD::OR:
1883 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001884 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001885 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001886 if (Tmp != 1) {
1887 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1888 FirstAnswer = std::min(Tmp, Tmp2);
1889 // We computed what we know about the sign bits as our first
1890 // answer. Now proceed to the generic code that uses
1891 // ComputeMaskedBits, and pick whichever answer is better.
1892 }
1893 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001894
1895 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001896 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001898 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001899 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00001900
1901 case ISD::SADDO:
1902 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001903 case ISD::SSUBO:
1904 case ISD::USUBO:
1905 case ISD::SMULO:
1906 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001907 if (Op.getResNo() != 1)
1908 break;
Duncan Sands03228082008-11-23 15:47:28 +00001909 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001910 case ISD::SETCC:
1911 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands03228082008-11-23 15:47:28 +00001912 if (TLI.getBooleanContents() ==
1913 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00001914 return VTBits;
1915 break;
1916 case ISD::ROTL:
1917 case ISD::ROTR:
1918 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001919 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001920
1921 // Handle rotate right by N like a rotate left by 32-N.
1922 if (Op.getOpcode() == ISD::ROTR)
1923 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1924
1925 // If we aren't rotating out all of the known-in sign bits, return the
1926 // number that are left. This handles rotl(sext(x), 1) for example.
1927 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1928 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1929 }
1930 break;
1931 case ISD::ADD:
1932 // Add can have at most one carry bit. Thus we know that the output
1933 // is, at worst, one more bit than the inputs.
1934 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1935 if (Tmp == 1) return 1; // Early out.
1936
1937 // Special case decrementing a value (ADD X, -1):
1938 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1939 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001940 APInt KnownZero, KnownOne;
1941 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001942 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1943
1944 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1945 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001946 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001947 return VTBits;
1948
1949 // If we are subtracting one from a positive number, there is no carry
1950 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001951 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001952 return Tmp;
1953 }
1954
1955 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1956 if (Tmp2 == 1) return 1;
1957 return std::min(Tmp, Tmp2)-1;
1958 break;
1959
1960 case ISD::SUB:
1961 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1962 if (Tmp2 == 1) return 1;
1963
1964 // Handle NEG.
1965 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001966 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001967 APInt KnownZero, KnownOne;
1968 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001969 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1970 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1971 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001972 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001973 return VTBits;
1974
1975 // If the input is known to be positive (the sign bit is known clear),
1976 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001977 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001978 return Tmp2;
1979
1980 // Otherwise, we treat this like a SUB.
1981 }
1982
1983 // Sub can have at most one carry bit. Thus we know that the output
1984 // is, at worst, one more bit than the inputs.
1985 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1986 if (Tmp == 1) return 1; // Early out.
1987 return std::min(Tmp, Tmp2)-1;
1988 break;
1989 case ISD::TRUNCATE:
1990 // FIXME: it's tricky to do anything useful for this, but it is an important
1991 // case for targets like X86.
1992 break;
1993 }
1994
1995 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1996 if (Op.getOpcode() == ISD::LOAD) {
1997 LoadSDNode *LD = cast<LoadSDNode>(Op);
1998 unsigned ExtType = LD->getExtensionType();
1999 switch (ExtType) {
2000 default: break;
2001 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002002 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002003 return VTBits-Tmp+1;
2004 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002005 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002006 return VTBits-Tmp;
2007 }
2008 }
2009
2010 // Allow the target to implement this method for its nodes.
2011 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2012 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2013 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2014 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2015 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002016 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002017 }
2018
2019 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2020 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002021 APInt KnownZero, KnownOne;
2022 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002023 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
2024
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002025 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002026 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002027 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002028 Mask = KnownOne;
2029 } else {
2030 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002031 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002032 }
2033
2034 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2035 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002036 Mask = ~Mask;
2037 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002038 // Return # leading zeros. We use 'min' here in case Val was zero before
2039 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002040 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002041}
2042
Chris Lattner51dabfb2006-10-14 00:41:01 +00002043
Dan Gohman475871a2008-07-27 21:46:04 +00002044bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002045 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2046 if (!GA) return false;
Dan Gohman6520e202008-10-18 02:06:02 +00002047 if (GA->getOffset() != 0) return false;
Evan Chenga844bde2008-02-02 04:07:54 +00002048 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2049 if (!GV) return false;
Devang Patel35fe7342009-01-13 22:54:57 +00002050 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patelbbdc8202009-01-13 23:54:55 +00002051 return MMI && MMI->hasDebugInfo();
Evan Chenga844bde2008-02-02 04:07:54 +00002052}
2053
2054
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002055/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2056/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002057SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002058 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00002059 SDValue PermMask = N->getOperand(2);
2060 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002061 if (Idx.getOpcode() == ISD::UNDEF)
2062 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002063 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002064 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002065 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002066 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002067
2068 if (V.getOpcode() == ISD::BIT_CONVERT) {
2069 V = V.getOperand(0);
Mon P Wange9f10152008-12-09 05:46:39 +00002070 MVT VVT = V.getValueType();
2071 if (!VVT.isVector() || VVT.getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002072 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002073 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002074 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002075 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002076 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002077 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002078 return V.getOperand(Index);
2079 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002080 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002081 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002082}
2083
2084
Chris Lattnerc3aae252005-01-07 07:46:32 +00002085/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002086///
Dan Gohman475871a2008-07-27 21:46:04 +00002087SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002088 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002089 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002090 void *IP = 0;
2091 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002092 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002093 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002094 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002095 CSEMap.InsertNode(N, IP);
2096
2097 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002098#ifndef NDEBUG
2099 VerifyNode(N);
2100#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002101 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002102}
2103
Dan Gohman475871a2008-07-27 21:46:04 +00002104SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002105 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002106 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002107 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002108 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002109 switch (Opcode) {
2110 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002111 case ISD::SIGN_EXTEND:
2112 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002113 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002114 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002115 case ISD::TRUNCATE:
2116 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002117 case ISD::UINT_TO_FP:
2118 case ISD::SINT_TO_FP: {
2119 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002120 // No compile time operations on this type.
2121 if (VT==MVT::ppcf128)
2122 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002123 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2124 (void)apf.convertFromAPInt(Val,
2125 Opcode==ISD::SINT_TO_FP,
2126 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002127 return getConstantFP(apf, VT);
2128 }
Chris Lattner94683772005-12-23 05:30:37 +00002129 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002130 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002131 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002132 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002133 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002134 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002135 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002136 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002137 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002138 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002139 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002140 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002141 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002142 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002143 }
2144 }
2145
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002146 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002147 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002148 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002149 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002150 switch (Opcode) {
2151 case ISD::FNEG:
2152 V.changeSign();
2153 return getConstantFP(V, VT);
2154 case ISD::FABS:
2155 V.clearSign();
2156 return getConstantFP(V, VT);
2157 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002158 case ISD::FP_EXTEND: {
2159 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002160 // This can return overflow, underflow, or inexact; we don't care.
2161 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002162 (void)V.convert(*MVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002163 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002164 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002165 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002166 case ISD::FP_TO_SINT:
2167 case ISD::FP_TO_UINT: {
2168 integerPart x;
Dale Johannesen23a98552008-10-09 23:00:39 +00002169 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002170 assert(integerPartWidth >= 64);
2171 // FIXME need to be more flexible about rounding mode.
2172 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2173 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002174 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002175 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2176 break;
2177 return getConstant(x, VT);
2178 }
2179 case ISD::BIT_CONVERT:
2180 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002181 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002182 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002183 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002184 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002185 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002186 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002187 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002188
Gabor Greifba36cb52008-08-28 21:40:38 +00002189 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002190 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002191 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002192 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002193 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002194 return Operand; // Factor, merge or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002195 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002196 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002197 assert(VT.isFloatingPoint() &&
2198 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002199 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002200 if (Operand.getOpcode() == ISD::UNDEF)
2201 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002202 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002203 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002204 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002205 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002206 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002207 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002208 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002209 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greifba36cb52008-08-28 21:40:38 +00002210 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002211 break;
2212 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002213 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002214 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002215 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002216 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002217 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002218 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002219 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002220 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002221 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002222 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002223 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002224 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002225 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002226 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002227 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2228 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002229 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002230 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002231 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002232 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002233 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002234 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002235 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002236 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002237 if (OpOpcode == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002238 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002239 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2240 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002241 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002242 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2243 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2244 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2245 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002246 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002247 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002248 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002249 break;
Chris Lattner35481892005-12-23 00:16:34 +00002250 case ISD::BIT_CONVERT:
2251 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002252 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002253 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002254 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002255 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2256 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002257 if (OpOpcode == ISD::UNDEF)
2258 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002259 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002260 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2262 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002263 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002264 if (OpOpcode == ISD::UNDEF)
2265 return getNode(ISD::UNDEF, VT);
2266 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2267 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2268 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2269 Operand.getConstantOperandVal(1) == 0 &&
2270 Operand.getOperand(0).getValueType() == VT)
2271 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002272 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002273 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002274 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002275 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2276 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002277 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002278 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002279 break;
2280 case ISD::FABS:
2281 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002282 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002283 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002284 }
2285
Chris Lattner43247a12005-08-25 19:12:10 +00002286 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002287 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002288 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002289 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002290 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002291 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002292 void *IP = 0;
2293 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002294 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002295 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002296 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002297 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002298 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002299 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002300 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002301 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002302
Chris Lattnerc3aae252005-01-07 07:46:32 +00002303 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002304#ifndef NDEBUG
2305 VerifyNode(N);
2306#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002307 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002308}
2309
Bill Wendling688d1c42008-09-24 10:16:24 +00002310SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
2311 MVT VT,
2312 ConstantSDNode *Cst1,
2313 ConstantSDNode *Cst2) {
2314 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2315
2316 switch (Opcode) {
2317 case ISD::ADD: return getConstant(C1 + C2, VT);
2318 case ISD::SUB: return getConstant(C1 - C2, VT);
2319 case ISD::MUL: return getConstant(C1 * C2, VT);
2320 case ISD::UDIV:
2321 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2322 break;
2323 case ISD::UREM:
2324 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2325 break;
2326 case ISD::SDIV:
2327 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2328 break;
2329 case ISD::SREM:
2330 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2331 break;
2332 case ISD::AND: return getConstant(C1 & C2, VT);
2333 case ISD::OR: return getConstant(C1 | C2, VT);
2334 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2335 case ISD::SHL: return getConstant(C1 << C2, VT);
2336 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2337 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2338 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2339 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2340 default: break;
2341 }
2342
2343 return SDValue();
2344}
2345
Dan Gohman475871a2008-07-27 21:46:04 +00002346SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2347 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002348 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2349 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002350 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002351 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002352 case ISD::TokenFactor:
2353 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2354 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002355 // Fold trivial token factors.
2356 if (N1.getOpcode() == ISD::EntryToken) return N2;
2357 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002358 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002359 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002360 case ISD::CONCAT_VECTORS:
2361 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2362 // one big BUILD_VECTOR.
2363 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2364 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002365 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2366 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002367 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2368 }
2369 break;
Chris Lattner76365122005-01-16 02:23:22 +00002370 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002372 N1.getValueType() == VT && "Binary operator types must match!");
2373 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2374 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002375 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002376 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002377 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2378 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002379 break;
Chris Lattner76365122005-01-16 02:23:22 +00002380 case ISD::OR:
2381 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002382 case ISD::ADD:
2383 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002384 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002385 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002386 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2387 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002388 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002389 return N1;
2390 break;
Chris Lattner76365122005-01-16 02:23:22 +00002391 case ISD::UDIV:
2392 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002393 case ISD::MULHU:
2394 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002395 case ISD::MUL:
2396 case ISD::SDIV:
2397 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002398 assert(VT.isInteger() && "This operator does not apply to FP types!");
2399 // fall through
Chris Lattner01b3d732005-09-28 22:28:18 +00002400 case ISD::FADD:
2401 case ISD::FSUB:
2402 case ISD::FMUL:
2403 case ISD::FDIV:
2404 case ISD::FREM:
Dan Gohmana90c8e62009-01-23 19:10:37 +00002405 if (UnsafeFPMath) {
2406 if (Opcode == ISD::FADD) {
2407 // 0+x --> x
2408 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2409 if (CFP->getValueAPF().isZero())
2410 return N2;
2411 // x+0 --> x
2412 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2413 if (CFP->getValueAPF().isZero())
2414 return N1;
2415 } else if (Opcode == ISD::FSUB) {
2416 // x-0 --> x
2417 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2418 if (CFP->getValueAPF().isZero())
2419 return N1;
2420 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002421 }
Chris Lattner76365122005-01-16 02:23:22 +00002422 assert(N1.getValueType() == N2.getValueType() &&
2423 N1.getValueType() == VT && "Binary operator types must match!");
2424 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002425 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2426 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002427 N1.getValueType().isFloatingPoint() &&
2428 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002429 "Invalid FCOPYSIGN!");
2430 break;
Chris Lattner76365122005-01-16 02:23:22 +00002431 case ISD::SHL:
2432 case ISD::SRA:
2433 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002434 case ISD::ROTL:
2435 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002436 assert(VT == N1.getValueType() &&
2437 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002438 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002439 "Shifts only work on integers");
Mon P Wange9f10152008-12-09 05:46:39 +00002440 assert((N2.getValueType() == TLI.getShiftAmountTy() ||
2441 (N2.getValueType().isVector() && N2.getValueType().isInteger())) &&
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002442 "Wrong type for shift amount");
Chris Lattner349db172008-07-02 17:01:57 +00002443
2444 // Always fold shifts of i1 values so the code generator doesn't need to
2445 // handle them. Since we know the size of the shift has to be less than the
2446 // size of the value, the shift/rotate count is guaranteed to be zero.
2447 if (VT == MVT::i1)
2448 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002449 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002450 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002451 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002452 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002453 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002454 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002455 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002456 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002457 break;
2458 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002459 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002460 assert(VT.isFloatingPoint() &&
2461 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002462 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002463 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002464 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002465 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002466 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002467 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002468 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002469 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002470 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002471 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002472 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002473 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002474 break;
2475 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002476 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002477 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002478 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002479 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002480 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002481 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002482 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002483
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002484 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002485 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002486 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002487 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002488 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002489 return getConstant(Val, VT);
2490 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002491 break;
2492 }
2493 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002494 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2495 if (N1.getOpcode() == ISD::UNDEF)
2496 return getNode(ISD::UNDEF, VT);
2497
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002498 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2499 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002500 if (N2C &&
2501 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002502 N1.getNumOperands() > 0) {
2503 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002504 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002505 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002506 N1.getOperand(N2C->getZExtValue() / Factor),
2507 getConstant(N2C->getZExtValue() % Factor,
2508 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002509 }
2510
2511 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2512 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002513 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002514 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002515
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002516 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2517 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002518 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2519 if (N1.getOperand(2) == N2)
2520 return N1.getOperand(1);
2521 else
2522 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2523 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002524 break;
2525 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002526 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002527 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2528 (N1.getValueType().isInteger() == VT.isInteger()) &&
2529 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002530
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002531 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2532 // 64-bit integers into 32-bit parts. Instead of building the extract of
2533 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2534 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002535 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002536
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002537 // EXTRACT_ELEMENT of a constant int is also very common.
2538 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002539 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002540 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002541 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2542 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002543 }
2544 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002545 case ISD::EXTRACT_SUBVECTOR:
2546 if (N1.getValueType() == VT) // Trivial extraction.
2547 return N1;
2548 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002549 }
2550
2551 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002552 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002553 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2554 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002555 } else { // Cannonicalize constant to RHS if commutative
2556 if (isCommutativeBinOp(Opcode)) {
2557 std::swap(N1C, N2C);
2558 std::swap(N1, N2);
2559 }
2560 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002561 }
2562
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002563 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002564 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2565 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002566 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002567 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2568 // Cannonicalize constant to RHS if commutative
2569 std::swap(N1CFP, N2CFP);
2570 std::swap(N1, N2);
2571 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002572 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2573 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002574 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002575 case ISD::FADD:
2576 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002577 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002578 return getConstantFP(V1, VT);
2579 break;
2580 case ISD::FSUB:
2581 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2582 if (s!=APFloat::opInvalidOp)
2583 return getConstantFP(V1, VT);
2584 break;
2585 case ISD::FMUL:
2586 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2587 if (s!=APFloat::opInvalidOp)
2588 return getConstantFP(V1, VT);
2589 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002590 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002591 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2592 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2593 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002594 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002595 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002596 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2597 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2598 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002599 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002600 case ISD::FCOPYSIGN:
2601 V1.copySign(V2);
2602 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002603 default: break;
2604 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002605 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002606 }
Chris Lattner62b57722006-04-20 05:39:12 +00002607
2608 // Canonicalize an UNDEF to the RHS, even over a constant.
2609 if (N1.getOpcode() == ISD::UNDEF) {
2610 if (isCommutativeBinOp(Opcode)) {
2611 std::swap(N1, N2);
2612 } else {
2613 switch (Opcode) {
2614 case ISD::FP_ROUND_INREG:
2615 case ISD::SIGN_EXTEND_INREG:
2616 case ISD::SUB:
2617 case ISD::FSUB:
2618 case ISD::FDIV:
2619 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002620 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002621 return N1; // fold op(undef, arg2) -> undef
2622 case ISD::UDIV:
2623 case ISD::SDIV:
2624 case ISD::UREM:
2625 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002626 case ISD::SRL:
2627 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002628 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002629 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2630 // For vectors, we can't easily build an all zero vector, just return
2631 // the LHS.
2632 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002633 }
2634 }
2635 }
2636
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002637 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002638 if (N2.getOpcode() == ISD::UNDEF) {
2639 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002640 case ISD::XOR:
2641 if (N1.getOpcode() == ISD::UNDEF)
2642 // Handle undef ^ undef -> 0 special case. This is a common
2643 // idiom (misuse).
2644 return getConstant(0, VT);
2645 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002646 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002647 case ISD::ADDC:
2648 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002649 case ISD::SUB:
2650 case ISD::FADD:
2651 case ISD::FSUB:
2652 case ISD::FMUL:
2653 case ISD::FDIV:
2654 case ISD::FREM:
2655 case ISD::UDIV:
2656 case ISD::SDIV:
2657 case ISD::UREM:
2658 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002659 return N2; // fold op(arg1, undef) -> undef
2660 case ISD::MUL:
2661 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002662 case ISD::SRL:
2663 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002664 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002665 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2666 // For vectors, we can't easily build an all zero vector, just return
2667 // the LHS.
2668 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002669 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002670 if (!VT.isVector())
2671 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002672 // For vectors, we can't easily build an all one vector, just return
2673 // the LHS.
2674 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002675 case ISD::SRA:
2676 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002677 }
2678 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002679
Chris Lattner27e9b412005-05-11 18:57:39 +00002680 // Memoize this node if possible.
2681 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002682 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002683 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002684 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002685 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002686 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002687 void *IP = 0;
2688 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002689 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002690 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002691 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002692 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002693 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002694 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002695 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002696 }
2697
Chris Lattnerc3aae252005-01-07 07:46:32 +00002698 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002699#ifndef NDEBUG
2700 VerifyNode(N);
2701#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002702 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002703}
2704
Dan Gohman475871a2008-07-27 21:46:04 +00002705SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2706 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002707 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002708 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2709 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002710 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002711 case ISD::CONCAT_VECTORS:
2712 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2713 // one big BUILD_VECTOR.
2714 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2715 N2.getOpcode() == ISD::BUILD_VECTOR &&
2716 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002717 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2718 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2719 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002720 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2721 }
2722 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002723 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002724 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002725 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002726 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002727 break;
2728 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002729 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002730 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002731 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002732 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002733 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002734 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002735 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002736
2737 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002738 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002739 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002740 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002741 if (N2C->getZExtValue()) // Unconditional branch
Chris Lattner5351e9b2005-01-07 22:49:57 +00002742 return getNode(ISD::BR, MVT::Other, N1, N3);
2743 else
2744 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002745 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002746 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002747 case ISD::VECTOR_SHUFFLE:
Mon P Wangaeb06d22008-11-10 04:46:22 +00002748 assert(N1.getValueType() == N2.getValueType() &&
2749 N1.getValueType().isVector() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002750 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002751 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002752 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002753 "Illegal VECTOR_SHUFFLE node!");
2754 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002755 case ISD::BIT_CONVERT:
2756 // Fold bit_convert nodes from a type to themselves.
2757 if (N1.getValueType() == VT)
2758 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002759 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002760 }
2761
Chris Lattner43247a12005-08-25 19:12:10 +00002762 // Memoize node if it doesn't produce a flag.
2763 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002764 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002765 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002766 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002767 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002768 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002769 void *IP = 0;
2770 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002771 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002772 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002773 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002774 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002775 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002776 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002777 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002778 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002779 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002780#ifndef NDEBUG
2781 VerifyNode(N);
2782#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002783 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002784}
2785
Dan Gohman475871a2008-07-27 21:46:04 +00002786SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2787 SDValue N1, SDValue N2, SDValue N3,
2788 SDValue N4) {
2789 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002790 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002791}
2792
Dan Gohman475871a2008-07-27 21:46:04 +00002793SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2794 SDValue N1, SDValue N2, SDValue N3,
2795 SDValue N4, SDValue N5) {
2796 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002797 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002798}
2799
Dan Gohman707e0182008-04-12 04:36:06 +00002800/// getMemsetValue - Vectorized representation of the memset value
2801/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002802static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002803 unsigned NumBits = VT.isVector() ?
2804 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002805 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002806 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002807 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002808 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002809 Val = (Val << Shift) | Val;
2810 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002811 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002812 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002813 return DAG.getConstant(Val, VT);
2814 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002815 }
Evan Chengf0df0312008-05-15 08:39:06 +00002816
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002817 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Evan Chengf0df0312008-05-15 08:39:06 +00002818 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2819 unsigned Shift = 8;
2820 for (unsigned i = NumBits; i > 8; i >>= 1) {
2821 Value = DAG.getNode(ISD::OR, VT,
2822 DAG.getNode(ISD::SHL, VT, Value,
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002823 DAG.getConstant(Shift,
2824 TLI.getShiftAmountTy())),
2825 Value);
Evan Chengf0df0312008-05-15 08:39:06 +00002826 Shift <<= 1;
2827 }
2828
2829 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002830}
2831
Dan Gohman707e0182008-04-12 04:36:06 +00002832/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2833/// used when a memcpy is turned into a memset when the source is a constant
2834/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002835static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002836 const TargetLowering &TLI,
2837 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002838 // Handle vector with all elements zero.
2839 if (Str.empty()) {
2840 if (VT.isInteger())
2841 return DAG.getConstant(0, VT);
2842 unsigned NumElts = VT.getVectorNumElements();
2843 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2844 return DAG.getNode(ISD::BIT_CONVERT, VT,
2845 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2846 }
2847
Duncan Sands83ec4b62008-06-06 12:08:01 +00002848 assert(!VT.isVector() && "Can't handle vector type here!");
2849 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002850 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002851 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002852 if (TLI.isLittleEndian())
2853 Offset = Offset + MSB - 1;
2854 for (unsigned i = 0; i != MSB; ++i) {
2855 Val = (Val << 8) | (unsigned char)Str[Offset];
2856 Offset += TLI.isLittleEndian() ? -1 : 1;
2857 }
2858 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002859}
2860
Dan Gohman707e0182008-04-12 04:36:06 +00002861/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002862///
Dan Gohman475871a2008-07-27 21:46:04 +00002863static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002864 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002865 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002866 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2867}
2868
Evan Chengf0df0312008-05-15 08:39:06 +00002869/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2870///
Dan Gohman475871a2008-07-27 21:46:04 +00002871static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002872 unsigned SrcDelta = 0;
2873 GlobalAddressSDNode *G = NULL;
2874 if (Src.getOpcode() == ISD::GlobalAddress)
2875 G = cast<GlobalAddressSDNode>(Src);
2876 else if (Src.getOpcode() == ISD::ADD &&
2877 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2878 Src.getOperand(1).getOpcode() == ISD::Constant) {
2879 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002880 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00002881 }
2882 if (!G)
2883 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002884
Evan Chengf0df0312008-05-15 08:39:06 +00002885 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002886 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2887 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002888
Evan Chengf0df0312008-05-15 08:39:06 +00002889 return false;
2890}
Dan Gohman707e0182008-04-12 04:36:06 +00002891
Evan Chengf0df0312008-05-15 08:39:06 +00002892/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2893/// to replace the memset / memcpy is below the threshold. It also returns the
2894/// types of the sequence of memory ops to perform memset / memcpy.
2895static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002896bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002897 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002898 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002899 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002900 SelectionDAG &DAG,
2901 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002902 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002903 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002904 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Chris Lattner7fe5e182008-10-28 07:10:51 +00002905 MVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002906 if (VT != MVT::iAny) {
2907 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002908 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002909 // If source is a string constant, this will require an unaligned load.
2910 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2911 if (Dst.getOpcode() != ISD::FrameIndex) {
2912 // Can't change destination alignment. It requires a unaligned store.
2913 if (AllowUnalign)
2914 VT = MVT::iAny;
2915 } else {
2916 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2917 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2918 if (MFI->isFixedObjectIndex(FI)) {
2919 // Can't change destination alignment. It requires a unaligned store.
2920 if (AllowUnalign)
2921 VT = MVT::iAny;
2922 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002923 // Give the stack frame object a larger alignment if needed.
2924 if (MFI->getObjectAlignment(FI) < NewAlign)
2925 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002926 Align = NewAlign;
2927 }
2928 }
2929 }
2930 }
2931
2932 if (VT == MVT::iAny) {
2933 if (AllowUnalign) {
2934 VT = MVT::i64;
2935 } else {
2936 switch (Align & 7) {
2937 case 0: VT = MVT::i64; break;
2938 case 4: VT = MVT::i32; break;
2939 case 2: VT = MVT::i16; break;
2940 default: VT = MVT::i8; break;
2941 }
2942 }
2943
Duncan Sands83ec4b62008-06-06 12:08:01 +00002944 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002945 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002946 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2947 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002948
Duncan Sands8e4eb092008-06-08 20:54:56 +00002949 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002950 VT = LVT;
2951 }
Dan Gohman707e0182008-04-12 04:36:06 +00002952
2953 unsigned NumMemOps = 0;
2954 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002955 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002956 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002957 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002958 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002959 VT = MVT::i64;
2960 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002961 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2962 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002963 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002964 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002965 VTSize >>= 1;
2966 }
Dan Gohman707e0182008-04-12 04:36:06 +00002967 }
Dan Gohman707e0182008-04-12 04:36:06 +00002968
2969 if (++NumMemOps > Limit)
2970 return false;
2971 MemOps.push_back(VT);
2972 Size -= VTSize;
2973 }
2974
2975 return true;
2976}
2977
Dan Gohman475871a2008-07-27 21:46:04 +00002978static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2979 SDValue Chain, SDValue Dst,
2980 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002981 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002982 const Value *DstSV, uint64_t DstSVOff,
2983 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002984 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2985
Dan Gohman21323f32008-05-29 19:42:22 +00002986 // Expand memcpy to a series of load and store ops if the size operand falls
2987 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002988 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00002989 uint64_t Limit = -1ULL;
Dan Gohman707e0182008-04-12 04:36:06 +00002990 if (!AlwaysInline)
2991 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002992 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002993 std::string Str;
2994 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002995 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002996 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002997 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002998
Dan Gohman707e0182008-04-12 04:36:06 +00002999
Evan Cheng0ff39b32008-06-30 07:31:25 +00003000 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00003001 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003002 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003003 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003004 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003005 MVT VT = MemOps[i];
3006 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003007 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003008
Evan Cheng0ff39b32008-06-30 07:31:25 +00003009 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00003010 // It's unlikely a store of a vector immediate can be done in a single
3011 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003012 // We also handle store a vector with all zero's.
3013 // FIXME: Handle other cases where store of vector immediate is done in
3014 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00003015 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00003016 Store = DAG.getStore(Chain, Value,
3017 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00003018 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003019 } else {
3020 Value = DAG.getLoad(VT, Chain,
3021 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003022 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00003023 Store = DAG.getStore(Chain, Value,
3024 getMemBasePlusOffset(Dst, DstOff, DAG),
3025 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003026 }
3027 OutChains.push_back(Store);
3028 SrcOff += VTSize;
3029 DstOff += VTSize;
3030 }
3031
3032 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3033 &OutChains[0], OutChains.size());
3034}
3035
Dan Gohman475871a2008-07-27 21:46:04 +00003036static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
3037 SDValue Chain, SDValue Dst,
3038 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00003039 unsigned Align, bool AlwaysInline,
3040 const Value *DstSV, uint64_t DstSVOff,
3041 const Value *SrcSV, uint64_t SrcSVOff){
3042 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3043
3044 // Expand memmove to a series of load and store ops if the size operand falls
3045 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003046 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003047 uint64_t Limit = -1ULL;
Dan Gohman21323f32008-05-29 19:42:22 +00003048 if (!AlwaysInline)
3049 Limit = TLI.getMaxStoresPerMemmove();
3050 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003051 std::string Str;
3052 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00003053 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003054 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003055 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003056
Dan Gohman21323f32008-05-29 19:42:22 +00003057 uint64_t SrcOff = 0, DstOff = 0;
3058
Dan Gohman475871a2008-07-27 21:46:04 +00003059 SmallVector<SDValue, 8> LoadValues;
3060 SmallVector<SDValue, 8> LoadChains;
3061 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003062 unsigned NumMemOps = MemOps.size();
3063 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003064 MVT VT = MemOps[i];
3065 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003066 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003067
3068 Value = DAG.getLoad(VT, Chain,
3069 getMemBasePlusOffset(Src, SrcOff, DAG),
3070 SrcSV, SrcSVOff + SrcOff, false, Align);
3071 LoadValues.push_back(Value);
3072 LoadChains.push_back(Value.getValue(1));
3073 SrcOff += VTSize;
3074 }
3075 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3076 &LoadChains[0], LoadChains.size());
3077 OutChains.clear();
3078 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003079 MVT VT = MemOps[i];
3080 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003081 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003082
3083 Store = DAG.getStore(Chain, LoadValues[i],
3084 getMemBasePlusOffset(Dst, DstOff, DAG),
3085 DstSV, DstSVOff + DstOff, false, DstAlign);
3086 OutChains.push_back(Store);
3087 DstOff += VTSize;
3088 }
3089
3090 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3091 &OutChains[0], OutChains.size());
3092}
3093
Dan Gohman475871a2008-07-27 21:46:04 +00003094static SDValue getMemsetStores(SelectionDAG &DAG,
3095 SDValue Chain, SDValue Dst,
3096 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003097 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003098 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003099 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3100
3101 // Expand memset to a series of load/store ops if the size operand
3102 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003103 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003104 std::string Str;
3105 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003106 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003107 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003108 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003109
Dan Gohman475871a2008-07-27 21:46:04 +00003110 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003111 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003112
3113 unsigned NumMemOps = MemOps.size();
3114 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003115 MVT VT = MemOps[i];
3116 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003117 SDValue Value = getMemsetValue(Src, VT, DAG);
3118 SDValue Store = DAG.getStore(Chain, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003119 getMemBasePlusOffset(Dst, DstOff, DAG),
3120 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003121 OutChains.push_back(Store);
3122 DstOff += VTSize;
3123 }
3124
3125 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3126 &OutChains[0], OutChains.size());
3127}
3128
Dan Gohman475871a2008-07-27 21:46:04 +00003129SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3130 SDValue Src, SDValue Size,
3131 unsigned Align, bool AlwaysInline,
3132 const Value *DstSV, uint64_t DstSVOff,
3133 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003134
3135 // Check to see if we should lower the memcpy to loads and stores first.
3136 // For cases within the target-specified limits, this is the best choice.
3137 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3138 if (ConstantSize) {
3139 // Memcpy with size zero? Just return the original chain.
3140 if (ConstantSize->isNullValue())
3141 return Chain;
3142
Dan Gohman475871a2008-07-27 21:46:04 +00003143 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003144 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3145 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003146 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003147 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003148 return Result;
3149 }
3150
3151 // Then check to see if we should lower the memcpy with target-specific
3152 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003153 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003154 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3155 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003156 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003157 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003158 return Result;
3159
3160 // If we really need inline code and the target declined to provide it,
3161 // use a (potentially long) sequence of loads and stores.
3162 if (AlwaysInline) {
3163 assert(ConstantSize && "AlwaysInline requires a constant size!");
3164 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003165 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003166 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003167 }
3168
3169 // Emit a library call.
3170 TargetLowering::ArgListTy Args;
3171 TargetLowering::ArgListEntry Entry;
3172 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3173 Entry.Node = Dst; Args.push_back(Entry);
3174 Entry.Node = Src; Args.push_back(Entry);
3175 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003176 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003177 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003178 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003179 getExternalSymbol("memcpy", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003180 Args, *this);
3181 return CallResult.second;
3182}
3183
Dan Gohman475871a2008-07-27 21:46:04 +00003184SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3185 SDValue Src, SDValue Size,
3186 unsigned Align,
3187 const Value *DstSV, uint64_t DstSVOff,
3188 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003189
Dan Gohman21323f32008-05-29 19:42:22 +00003190 // Check to see if we should lower the memmove to loads and stores first.
3191 // For cases within the target-specified limits, this is the best choice.
3192 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3193 if (ConstantSize) {
3194 // Memmove with size zero? Just return the original chain.
3195 if (ConstantSize->isNullValue())
3196 return Chain;
3197
Dan Gohman475871a2008-07-27 21:46:04 +00003198 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003199 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3200 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003201 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003202 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003203 return Result;
3204 }
Dan Gohman707e0182008-04-12 04:36:06 +00003205
3206 // Then check to see if we should lower the memmove with target-specific
3207 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003208 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003209 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003210 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003211 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003212 return Result;
3213
3214 // Emit a library call.
3215 TargetLowering::ArgListTy Args;
3216 TargetLowering::ArgListEntry Entry;
3217 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3218 Entry.Node = Dst; Args.push_back(Entry);
3219 Entry.Node = Src; Args.push_back(Entry);
3220 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003221 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003222 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003223 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003224 getExternalSymbol("memmove", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003225 Args, *this);
3226 return CallResult.second;
3227}
3228
Dan Gohman475871a2008-07-27 21:46:04 +00003229SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3230 SDValue Src, SDValue Size,
3231 unsigned Align,
3232 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003233
3234 // Check to see if we should lower the memset to stores first.
3235 // For cases within the target-specified limits, this is the best choice.
3236 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3237 if (ConstantSize) {
3238 // Memset with size zero? Just return the original chain.
3239 if (ConstantSize->isNullValue())
3240 return Chain;
3241
Dan Gohman475871a2008-07-27 21:46:04 +00003242 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003243 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3244 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003245 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003246 return Result;
3247 }
3248
3249 // Then check to see if we should lower the memset with target-specific
3250 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003251 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003252 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Bill Wendling6158d842008-10-01 00:59:58 +00003253 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003254 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003255 return Result;
3256
3257 // Emit a library call.
3258 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3259 TargetLowering::ArgListTy Args;
3260 TargetLowering::ArgListEntry Entry;
3261 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3262 Args.push_back(Entry);
3263 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003264 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003265 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3266 else
3267 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3268 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3269 Args.push_back(Entry);
3270 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3271 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003272 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003273 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003274 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003275 getExternalSymbol("memset", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003276 Args, *this);
3277 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003278}
3279
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003280SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3281 SDValue Chain,
Dan Gohman475871a2008-07-27 21:46:04 +00003282 SDValue Ptr, SDValue Cmp,
3283 SDValue Swp, const Value* PtrVal,
3284 unsigned Alignment) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003285 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003286 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003287
3288 MVT VT = Cmp.getValueType();
3289
3290 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003291 Alignment = getMVTAlignment(MemVT);
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003292
3293 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003294 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003295 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003296 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003297 void* IP = 0;
3298 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003299 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003300 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003301 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3302 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003303 CSEMap.InsertNode(N, IP);
3304 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003305 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003306}
3307
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003308SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3309 SDValue Chain,
Dan Gohman475871a2008-07-27 21:46:04 +00003310 SDValue Ptr, SDValue Val,
3311 const Value* PtrVal,
3312 unsigned Alignment) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003313 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3314 Opcode == ISD::ATOMIC_LOAD_SUB ||
3315 Opcode == ISD::ATOMIC_LOAD_AND ||
3316 Opcode == ISD::ATOMIC_LOAD_OR ||
3317 Opcode == ISD::ATOMIC_LOAD_XOR ||
3318 Opcode == ISD::ATOMIC_LOAD_NAND ||
3319 Opcode == ISD::ATOMIC_LOAD_MIN ||
3320 Opcode == ISD::ATOMIC_LOAD_MAX ||
3321 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3322 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3323 Opcode == ISD::ATOMIC_SWAP) &&
3324 "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003325
3326 MVT VT = Val.getValueType();
3327
3328 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003329 Alignment = getMVTAlignment(MemVT);
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003330
3331 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003332 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003333 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003334 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003335 void* IP = 0;
3336 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003337 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003338 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003339 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3340 Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003341 CSEMap.InsertNode(N, IP);
3342 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003343 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003344}
3345
Duncan Sands4bdcb612008-07-02 17:40:58 +00003346/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3347/// Allowed to return something different (and simpler) if Simplify is true.
Duncan Sandsaaffa052008-12-01 11:41:29 +00003348SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps) {
3349 if (NumOps == 1)
Duncan Sands4bdcb612008-07-02 17:40:58 +00003350 return Ops[0];
3351
3352 SmallVector<MVT, 4> VTs;
3353 VTs.reserve(NumOps);
3354 for (unsigned i = 0; i < NumOps; ++i)
3355 VTs.push_back(Ops[i].getValueType());
3356 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3357}
3358
Dan Gohman475871a2008-07-27 21:46:04 +00003359SDValue
Mon P Wangc4d10212008-10-17 18:22:58 +00003360SelectionDAG::getMemIntrinsicNode(unsigned Opcode,
3361 const MVT *VTs, unsigned NumVTs,
3362 const SDValue *Ops, unsigned NumOps,
3363 MVT MemVT, const Value *srcValue, int SVOff,
3364 unsigned Align, bool Vol,
3365 bool ReadMem, bool WriteMem) {
3366 return getMemIntrinsicNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps,
3367 MemVT, srcValue, SVOff, Align, Vol,
3368 ReadMem, WriteMem);
3369}
3370
3371SDValue
3372SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDVTList VTList,
3373 const SDValue *Ops, unsigned NumOps,
3374 MVT MemVT, const Value *srcValue, int SVOff,
3375 unsigned Align, bool Vol,
3376 bool ReadMem, bool WriteMem) {
3377 // Memoize the node unless it returns a flag.
3378 MemIntrinsicSDNode *N;
3379 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3380 FoldingSetNodeID ID;
3381 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3382 void *IP = 0;
3383 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3384 return SDValue(E, 0);
3385
3386 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3387 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3388 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3389 CSEMap.InsertNode(N, IP);
3390 } else {
3391 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3392 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3393 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3394 }
3395 AllNodes.push_back(N);
3396 return SDValue(N, 0);
3397}
3398
3399SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00003400SelectionDAG::getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003401 bool IsInreg, SDVTList VTs,
Dan Gohman095cc292008-09-13 01:54:27 +00003402 const SDValue *Operands, unsigned NumOperands) {
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003403 // Do not include isTailCall in the folding set profile.
3404 FoldingSetNodeID ID;
3405 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3406 ID.AddInteger(CallingConv);
3407 ID.AddInteger(IsVarArgs);
3408 void *IP = 0;
3409 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3410 // Instead of including isTailCall in the folding set, we just
3411 // set the flag of the existing node.
3412 if (!IsTailCall)
3413 cast<CallSDNode>(E)->setNotTailCall();
3414 return SDValue(E, 0);
3415 }
Dan Gohman095cc292008-09-13 01:54:27 +00003416 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
Dale Johannesen86098bd2008-09-26 19:31:26 +00003417 new (N) CallSDNode(CallingConv, IsVarArgs, IsTailCall, IsInreg,
Dan Gohman095cc292008-09-13 01:54:27 +00003418 VTs, Operands, NumOperands);
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003419 CSEMap.InsertNode(N, IP);
Dan Gohman095cc292008-09-13 01:54:27 +00003420 AllNodes.push_back(N);
3421 return SDValue(N, 0);
3422}
3423
3424SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003425SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003426 MVT VT, SDValue Chain,
3427 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003428 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003429 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003430 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3431 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003432
Duncan Sands14ea39c2008-03-27 20:23:40 +00003433 if (VT == EVT) {
3434 ExtType = ISD::NON_EXTLOAD;
3435 } else if (ExtType == ISD::NON_EXTLOAD) {
3436 assert(VT == EVT && "Non-extending load from different memory type!");
3437 } else {
3438 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003439 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003440 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3441 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003442 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003443 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003444 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003445 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003446 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003447 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003448 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003449 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003450
3451 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003452 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003453 "Unindexed load with an offset!");
3454
3455 SDVTList VTs = Indexed ?
3456 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003457 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003458 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003459 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003460 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003461 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003462 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003463 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003464 void *IP = 0;
3465 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003466 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003467 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003468 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3469 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003470 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003471 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003472 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003473}
3474
Dan Gohman475871a2008-07-27 21:46:04 +00003475SDValue SelectionDAG::getLoad(MVT VT,
3476 SDValue Chain, SDValue Ptr,
3477 const Value *SV, int SVOffset,
3478 bool isVolatile, unsigned Alignment) {
3479 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003480 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3481 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003482}
3483
Dan Gohman475871a2008-07-27 21:46:04 +00003484SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3485 SDValue Chain, SDValue Ptr,
3486 const Value *SV,
3487 int SVOffset, MVT EVT,
3488 bool isVolatile, unsigned Alignment) {
3489 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003490 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3491 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003492}
3493
Dan Gohman475871a2008-07-27 21:46:04 +00003494SDValue
3495SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3496 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003497 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003498 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3499 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003500 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3501 LD->getChain(), Base, Offset, LD->getSrcValue(),
3502 LD->getSrcValueOffset(), LD->getMemoryVT(),
3503 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003504}
3505
Dan Gohman475871a2008-07-27 21:46:04 +00003506SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3507 SDValue Ptr, const Value *SV, int SVOffset,
3508 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003509 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003510
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003511 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3512 Alignment = getMVTAlignment(VT);
3513
Evan Chengad071e12006-10-05 22:57:11 +00003514 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003515 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3516 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003517 FoldingSetNodeID ID;
3518 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003519 ID.AddInteger(ISD::UNINDEXED);
3520 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003521 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003522 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003523 void *IP = 0;
3524 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003525 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003526 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003527 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3528 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003529 CSEMap.InsertNode(N, IP);
3530 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003531 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003532}
3533
Dan Gohman475871a2008-07-27 21:46:04 +00003534SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3535 SDValue Ptr, const Value *SV,
3536 int SVOffset, MVT SVT,
3537 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003538 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003539
3540 if (VT == SVT)
3541 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003542
Duncan Sands8e4eb092008-06-08 20:54:56 +00003543 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003544 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003545 "Can't do FP-INT conversion!");
3546
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003547 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3548 Alignment = getMVTAlignment(VT);
3549
Evan Cheng8b2794a2006-10-13 21:14:26 +00003550 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003551 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3552 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003553 FoldingSetNodeID ID;
3554 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003555 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003556 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003557 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003558 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003559 void *IP = 0;
3560 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003561 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003562 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003563 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3564 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003565 CSEMap.InsertNode(N, IP);
3566 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003567 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003568}
3569
Dan Gohman475871a2008-07-27 21:46:04 +00003570SDValue
3571SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3572 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003573 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3574 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3575 "Store is already a indexed store!");
3576 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003577 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003578 FoldingSetNodeID ID;
3579 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3580 ID.AddInteger(AM);
3581 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003582 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003583 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003584 void *IP = 0;
3585 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003586 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003587 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003588 new (N) StoreSDNode(Ops, VTs, AM,
3589 ST->isTruncatingStore(), ST->getMemoryVT(),
3590 ST->getSrcValue(), ST->getSrcValueOffset(),
3591 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003592 CSEMap.InsertNode(N, IP);
3593 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003594 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003595}
3596
Dan Gohman475871a2008-07-27 21:46:04 +00003597SDValue SelectionDAG::getVAArg(MVT VT,
3598 SDValue Chain, SDValue Ptr,
3599 SDValue SV) {
3600 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003601 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003602}
3603
Dan Gohman475871a2008-07-27 21:46:04 +00003604SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3605 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003606 switch (NumOps) {
3607 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003608 case 1: return getNode(Opcode, VT, Ops[0]);
3609 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3610 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003611 default: break;
3612 }
3613
Dan Gohman475871a2008-07-27 21:46:04 +00003614 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003615 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003616 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3617 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003618}
3619
Dan Gohman475871a2008-07-27 21:46:04 +00003620SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3621 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003622 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003623 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003624 case 1: return getNode(Opcode, VT, Ops[0]);
3625 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3626 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003627 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003628 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003629
Chris Lattneref847df2005-04-09 03:27:28 +00003630 switch (Opcode) {
3631 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003632 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003633 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003634 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3635 "LHS and RHS of condition must have same type!");
3636 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3637 "True and False arms of SelectCC must have same type!");
3638 assert(Ops[2].getValueType() == VT &&
3639 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003640 break;
3641 }
3642 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003643 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003644 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3645 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003646 break;
3647 }
Chris Lattneref847df2005-04-09 03:27:28 +00003648 }
3649
Chris Lattner385328c2005-05-14 07:42:29 +00003650 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003651 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003652 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003653 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003654 FoldingSetNodeID ID;
3655 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003656 void *IP = 0;
3657 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003658 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003659 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003660 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003661 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003662 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003663 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003664 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003665 }
Chris Lattneref847df2005-04-09 03:27:28 +00003666 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003667#ifndef NDEBUG
3668 VerifyNode(N);
3669#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003670 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003671}
3672
Dan Gohman475871a2008-07-27 21:46:04 +00003673SDValue SelectionDAG::getNode(unsigned Opcode,
3674 const std::vector<MVT> &ResultTys,
3675 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003676 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3677 Ops, NumOps);
3678}
3679
Dan Gohman475871a2008-07-27 21:46:04 +00003680SDValue SelectionDAG::getNode(unsigned Opcode,
3681 const MVT *VTs, unsigned NumVTs,
3682 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003683 if (NumVTs == 1)
3684 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003685 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3686}
3687
Dan Gohman475871a2008-07-27 21:46:04 +00003688SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3689 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003690 if (VTList.NumVTs == 1)
3691 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003692
Chris Lattner5f056bf2005-07-10 01:55:33 +00003693 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003694 // FIXME: figure out how to safely handle things like
3695 // int foo(int x) { return 1 << (x & 255); }
3696 // int bar() { return foo(256); }
3697#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003698 case ISD::SRA_PARTS:
3699 case ISD::SRL_PARTS:
3700 case ISD::SHL_PARTS:
3701 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003702 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003703 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3704 else if (N3.getOpcode() == ISD::AND)
3705 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3706 // If the and is only masking out bits that cannot effect the shift,
3707 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003708 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003709 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3710 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3711 }
3712 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003713#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003714 }
Chris Lattner89c34632005-05-14 06:20:26 +00003715
Chris Lattner43247a12005-08-25 19:12:10 +00003716 // Memoize the node unless it returns a flag.
3717 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003718 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003719 FoldingSetNodeID ID;
3720 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003721 void *IP = 0;
3722 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003723 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003724 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003725 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003726 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3727 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003728 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003729 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3730 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003731 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003732 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3733 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003734 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003735 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3736 }
Chris Lattnera5682852006-08-07 23:03:03 +00003737 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003738 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003739 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003740 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003741 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3742 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003743 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003744 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3745 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003746 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003747 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3748 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003749 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003750 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3751 }
Chris Lattner43247a12005-08-25 19:12:10 +00003752 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003753 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003754#ifndef NDEBUG
3755 VerifyNode(N);
3756#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003757 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003758}
3759
Dan Gohman475871a2008-07-27 21:46:04 +00003760SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003761 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003762}
3763
Dan Gohman475871a2008-07-27 21:46:04 +00003764SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3765 SDValue N1) {
3766 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003767 return getNode(Opcode, VTList, Ops, 1);
3768}
3769
Dan Gohman475871a2008-07-27 21:46:04 +00003770SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3771 SDValue N1, SDValue N2) {
3772 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003773 return getNode(Opcode, VTList, Ops, 2);
3774}
3775
Dan Gohman475871a2008-07-27 21:46:04 +00003776SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3777 SDValue N1, SDValue N2, SDValue N3) {
3778 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003779 return getNode(Opcode, VTList, Ops, 3);
3780}
3781
Dan Gohman475871a2008-07-27 21:46:04 +00003782SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3783 SDValue N1, SDValue N2, SDValue N3,
3784 SDValue N4) {
3785 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003786 return getNode(Opcode, VTList, Ops, 4);
3787}
3788
Dan Gohman475871a2008-07-27 21:46:04 +00003789SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3790 SDValue N1, SDValue N2, SDValue N3,
3791 SDValue N4, SDValue N5) {
3792 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003793 return getNode(Opcode, VTList, Ops, 5);
3794}
3795
Duncan Sands83ec4b62008-06-06 12:08:01 +00003796SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003797 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003798}
3799
Duncan Sands83ec4b62008-06-06 12:08:01 +00003800SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003801 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3802 E = VTList.rend(); I != E; ++I)
3803 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3804 return *I;
3805
3806 MVT *Array = Allocator.Allocate<MVT>(2);
3807 Array[0] = VT1;
3808 Array[1] = VT2;
3809 SDVTList Result = makeVTList(Array, 2);
3810 VTList.push_back(Result);
3811 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003812}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003813
3814SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3815 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3816 E = VTList.rend(); I != E; ++I)
3817 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3818 I->VTs[2] == VT3)
3819 return *I;
3820
3821 MVT *Array = Allocator.Allocate<MVT>(3);
3822 Array[0] = VT1;
3823 Array[1] = VT2;
3824 Array[2] = VT3;
3825 SDVTList Result = makeVTList(Array, 3);
3826 VTList.push_back(Result);
3827 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003828}
3829
Bill Wendling13d6d442008-12-01 23:28:22 +00003830SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
3831 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3832 E = VTList.rend(); I != E; ++I)
3833 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3834 I->VTs[2] == VT3 && I->VTs[3] == VT4)
3835 return *I;
3836
3837 MVT *Array = Allocator.Allocate<MVT>(3);
3838 Array[0] = VT1;
3839 Array[1] = VT2;
3840 Array[2] = VT3;
3841 Array[3] = VT4;
3842 SDVTList Result = makeVTList(Array, 4);
3843 VTList.push_back(Result);
3844 return Result;
3845}
3846
Duncan Sands83ec4b62008-06-06 12:08:01 +00003847SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003848 switch (NumVTs) {
3849 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003850 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003851 case 2: return getVTList(VTs[0], VTs[1]);
3852 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3853 default: break;
3854 }
3855
Dan Gohmane8be6c62008-07-17 19:10:17 +00003856 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3857 E = VTList.rend(); I != E; ++I) {
3858 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3859 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003860
3861 bool NoMatch = false;
3862 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003863 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003864 NoMatch = true;
3865 break;
3866 }
3867 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003868 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003869 }
3870
Dan Gohmane8be6c62008-07-17 19:10:17 +00003871 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3872 std::copy(VTs, VTs+NumVTs, Array);
3873 SDVTList Result = makeVTList(Array, NumVTs);
3874 VTList.push_back(Result);
3875 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003876}
3877
3878
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003879/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3880/// specified operands. If the resultant node already exists in the DAG,
3881/// this does not modify the specified node, instead it returns the node that
3882/// already exists. If the resultant node does not exist in the DAG, the
3883/// input node is returned. As a degenerate case, if you specify the same
3884/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003885SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003886 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003887 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3888
3889 // Check to see if there is no change.
3890 if (Op == N->getOperand(0)) return InN;
3891
3892 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003893 void *InsertPos = 0;
3894 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003895 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003896
Dan Gohman79acd2b2008-07-21 22:38:59 +00003897 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003898 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003899 if (!RemoveNodeFromCSEMaps(N))
3900 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003901
3902 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003903 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003904 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003905 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003906 Op.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003907
3908 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003909 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003910 return InN;
3911}
3912
Dan Gohman475871a2008-07-27 21:46:04 +00003913SDValue SelectionDAG::
3914UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003915 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003916 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3917
3918 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003919 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3920 return InN; // No operands changed, just return the input node.
3921
3922 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003923 void *InsertPos = 0;
3924 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003925 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003926
Dan Gohman79acd2b2008-07-21 22:38:59 +00003927 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003928 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003929 if (!RemoveNodeFromCSEMaps(N))
3930 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003931
3932 // Now we update the operands.
3933 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003934 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003935 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003936 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003937 Op1.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003938 }
3939 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003940 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003941 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003942 N->OperandList[1].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003943 Op2.getNode()->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003944 }
3945
3946 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003947 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003948 return InN;
3949}
3950
Dan Gohman475871a2008-07-27 21:46:04 +00003951SDValue SelectionDAG::
3952UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3953 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003954 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003955}
3956
Dan Gohman475871a2008-07-27 21:46:04 +00003957SDValue SelectionDAG::
3958UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3959 SDValue Op3, SDValue Op4) {
3960 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003961 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003962}
3963
Dan Gohman475871a2008-07-27 21:46:04 +00003964SDValue SelectionDAG::
3965UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3966 SDValue Op3, SDValue Op4, SDValue Op5) {
3967 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003968 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003969}
3970
Dan Gohman475871a2008-07-27 21:46:04 +00003971SDValue SelectionDAG::
3972UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003973 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003974 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003975 "Update with wrong number of operands");
3976
3977 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003978 bool AnyChange = false;
3979 for (unsigned i = 0; i != NumOps; ++i) {
3980 if (Ops[i] != N->getOperand(i)) {
3981 AnyChange = true;
3982 break;
3983 }
3984 }
3985
3986 // No operands changed, just return the input node.
3987 if (!AnyChange) return InN;
3988
3989 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003990 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003991 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003992 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003993
Dan Gohman7ceda162008-05-02 00:05:03 +00003994 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003995 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003996 if (!RemoveNodeFromCSEMaps(N))
3997 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003998
3999 // Now we update the operands.
4000 for (unsigned i = 0; i != NumOps; ++i) {
4001 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004002 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004003 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004004 N->OperandList[i].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00004005 Ops[i].getNode()->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004006 }
4007 }
4008
4009 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004010 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004011 return InN;
4012}
4013
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004014/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004015/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004016void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004017 // Unlike the code in MorphNodeTo that does this, we don't need to
4018 // watch for dead nodes here.
4019 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
4020 I->getVal()->removeUser(std::distance(op_begin(), I), this);
4021
4022 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004023}
Chris Lattner149c58c2005-08-16 18:17:10 +00004024
Dan Gohmane8be6c62008-07-17 19:10:17 +00004025/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4026/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004027///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004028SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004029 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004030 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004031 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004032}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004033
Dan Gohmane8be6c62008-07-17 19:10:17 +00004034SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004035 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004036 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004037 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004038 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004039}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004040
Dan Gohmane8be6c62008-07-17 19:10:17 +00004041SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004042 MVT VT, SDValue Op1,
4043 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004044 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004045 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004046 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004047}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004048
Dan Gohmane8be6c62008-07-17 19:10:17 +00004049SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004050 MVT VT, SDValue Op1,
4051 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004052 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004053 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004054 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004055}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004056
Dan Gohmane8be6c62008-07-17 19:10:17 +00004057SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004058 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004059 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004060 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004061 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004062}
4063
Dan Gohmane8be6c62008-07-17 19:10:17 +00004064SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004065 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004066 unsigned NumOps) {
4067 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004068 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004069}
4070
Dan Gohmane8be6c62008-07-17 19:10:17 +00004071SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004072 MVT VT1, MVT VT2) {
4073 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004074 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004075}
4076
Dan Gohmane8be6c62008-07-17 19:10:17 +00004077SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004078 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004079 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004080 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004081 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004082}
4083
Bill Wendling13d6d442008-12-01 23:28:22 +00004084SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
4085 MVT VT1, MVT VT2, MVT VT3, MVT VT4,
4086 const SDValue *Ops, unsigned NumOps) {
4087 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4088 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4089}
4090
Dan Gohmane8be6c62008-07-17 19:10:17 +00004091SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004092 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004093 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004094 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004095 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004096 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004097}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004098
Dan Gohmane8be6c62008-07-17 19:10:17 +00004099SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004100 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004101 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004102 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004103 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004104 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004105}
4106
Dan Gohmane8be6c62008-07-17 19:10:17 +00004107SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004108 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004109 SDValue Op1, SDValue Op2,
4110 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004111 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004112 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004113 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004114}
4115
Dan Gohmane8be6c62008-07-17 19:10:17 +00004116SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Bill Wendling13d6d442008-12-01 23:28:22 +00004117 MVT VT1, MVT VT2, MVT VT3,
4118 SDValue Op1, SDValue Op2,
4119 SDValue Op3) {
4120 SDVTList VTs = getVTList(VT1, VT2, VT3);
4121 SDValue Ops[] = { Op1, Op2, Op3 };
4122 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4123}
4124
4125SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004126 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004127 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004128 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4129}
4130
4131SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4132 MVT VT) {
4133 SDVTList VTs = getVTList(VT);
4134 return MorphNodeTo(N, Opc, VTs, 0, 0);
4135}
4136
4137SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004138 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004139 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004140 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004141 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4142}
4143
4144SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004145 MVT VT, SDValue Op1,
4146 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004147 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004148 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004149 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4150}
4151
4152SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004153 MVT VT, SDValue Op1,
4154 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004155 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004156 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004157 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4158}
4159
4160SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004161 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004162 unsigned NumOps) {
4163 SDVTList VTs = getVTList(VT);
4164 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4165}
4166
4167SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004168 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004169 unsigned NumOps) {
4170 SDVTList VTs = getVTList(VT1, VT2);
4171 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4172}
4173
4174SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4175 MVT VT1, MVT VT2) {
4176 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004177 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004178}
4179
4180SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4181 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004182 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004183 SDVTList VTs = getVTList(VT1, VT2, VT3);
4184 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4185}
4186
4187SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4188 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004189 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004190 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004191 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004192 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4193}
4194
4195SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4196 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004197 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004198 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004199 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004200 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4201}
4202
4203SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4204 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004205 SDValue Op1, SDValue Op2,
4206 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004207 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004208 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004209 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4210}
4211
4212/// MorphNodeTo - These *mutate* the specified node to have the specified
4213/// return type, opcode, and operands.
4214///
4215/// Note that MorphNodeTo returns the resultant node. If there is already a
4216/// node of the specified opcode and operands, it returns that node instead of
4217/// the current one.
4218///
4219/// Using MorphNodeTo is faster than creating a new node and swapping it in
4220/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004221/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004222/// the node's users.
4223///
4224SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004225 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004226 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004227 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004228 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004229 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4230 FoldingSetNodeID ID;
4231 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4232 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4233 return ON;
4234 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004235
Dan Gohman095cc292008-09-13 01:54:27 +00004236 if (!RemoveNodeFromCSEMaps(N))
4237 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004238
Dan Gohmane8be6c62008-07-17 19:10:17 +00004239 // Start the morphing.
4240 N->NodeType = Opc;
4241 N->ValueList = VTs.VTs;
4242 N->NumValues = VTs.NumVTs;
4243
4244 // Clear the operands list, updating used nodes to remove this from their
4245 // use list. Keep track of any operands that become dead as a result.
4246 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4247 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4248 I != E; ++I) {
4249 SDNode *Used = I->getVal();
4250 Used->removeUser(std::distance(B, I), N);
4251 if (Used->use_empty())
4252 DeadNodeSet.insert(Used);
4253 }
4254
4255 // If NumOps is larger than the # of operands we currently have, reallocate
4256 // the operand list.
4257 if (NumOps > N->NumOperands) {
4258 if (N->OperandsNeedDelete)
4259 delete[] N->OperandList;
Bill Wendlinga1dc6022008-10-19 20:51:12 +00004260
Dan Gohmane8be6c62008-07-17 19:10:17 +00004261 if (N->isMachineOpcode()) {
4262 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004263 // remainder of the current SelectionDAG iteration, so we can allocate
4264 // the operands directly out of a pool with no recycling metadata.
4265 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004266 N->OperandsNeedDelete = false;
4267 } else {
4268 N->OperandList = new SDUse[NumOps];
4269 N->OperandsNeedDelete = true;
4270 }
4271 }
4272
4273 // Assign the new operands.
4274 N->NumOperands = NumOps;
4275 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4276 N->OperandList[i] = Ops[i];
4277 N->OperandList[i].setUser(N);
4278 SDNode *ToUse = N->OperandList[i].getVal();
4279 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004280 }
4281
4282 // Delete any nodes that are still dead after adding the uses for the
4283 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004284 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004285 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4286 E = DeadNodeSet.end(); I != E; ++I)
4287 if ((*I)->use_empty())
4288 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004289 RemoveDeadNodes(DeadNodes);
4290
Dan Gohmane8be6c62008-07-17 19:10:17 +00004291 if (IP)
4292 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004293 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004294}
4295
Chris Lattner0fb094f2005-11-19 01:44:53 +00004296
Evan Cheng6ae46c42006-02-09 07:15:23 +00004297/// getTargetNode - These are used for target selectors to create a new node
4298/// with specified return type(s), target opcode, and operands.
4299///
4300/// Note that getTargetNode returns the resultant node. If there is already a
4301/// node of the specified opcode and operands, it returns that node instead of
4302/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004303SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004304 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004305}
Dan Gohman475871a2008-07-27 21:46:04 +00004306SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004307 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004308}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004309SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004310 SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004311 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004312}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004313SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004314 SDValue Op1, SDValue Op2,
4315 SDValue Op3) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004316 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004317}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004318SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004319 const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004320 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004321}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004322SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4323 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004324 SDValue Op;
Gabor Greifba36cb52008-08-28 21:40:38 +00004325 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004326}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004327SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004328 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004329 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004330 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004331}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004332SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004333 MVT VT2, SDValue Op1,
4334 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004335 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004336 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004337 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004338}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004339SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004340 MVT VT2, SDValue Op1,
4341 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004342 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004343 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004344 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004345}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004346SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004347 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004348 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004349 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004350}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004351SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004352 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004353 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004354 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004355 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004356}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004357SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004358 SDValue Op1, SDValue Op2,
4359 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004360 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004361 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004362 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004363}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004364SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004365 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004366 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Gabor Greifba36cb52008-08-28 21:40:38 +00004367 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004368}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004369SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4370 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004371 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004372 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004373 VTList.push_back(VT1);
4374 VTList.push_back(VT2);
4375 VTList.push_back(VT3);
4376 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004377 const MVT *VTs = getNodeValueTypes(VTList);
Gabor Greifba36cb52008-08-28 21:40:38 +00004378 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004379}
Evan Cheng39305cf2007-10-05 01:10:49 +00004380SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004381 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004382 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004383 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004384 return getNode(~Opcode, VTs, ResultTys.size(),
Gabor Greifba36cb52008-08-28 21:40:38 +00004385 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004386}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004387
Evan Cheng08b11732008-03-22 01:55:50 +00004388/// getNodeIfExists - Get the specified node if it's already available, or
4389/// else return NULL.
4390SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004391 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004392 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4393 FoldingSetNodeID ID;
4394 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4395 void *IP = 0;
4396 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4397 return E;
4398 }
4399 return NULL;
4400}
4401
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004402
Evan Cheng99157a02006-08-07 22:13:29 +00004403/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004404/// This can cause recursive merging of nodes in the DAG.
4405///
Chris Lattner11d049c2008-02-03 03:35:22 +00004406/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004407///
Dan Gohman475871a2008-07-27 21:46:04 +00004408void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004409 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004410 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004411 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004412 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004413 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004414
Dan Gohmandbe664a2009-01-19 21:44:21 +00004415 // Iterate over all the existing uses of From. This specifically avoids
Chris Lattnere70909b2009-01-22 07:21:55 +00004416 // visiting any new uses of From that arise while the replacement is
Dan Gohmandbe664a2009-01-19 21:44:21 +00004417 // happening, because any such uses would be the result of CSE: If an
4418 // existing node looks like From after one of its operands is replaced
4419 // by To, we don't want to replace of all its users with To too.
4420 // See PR3018 for more info.
4421 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4422 while (UI != UE) {
Dan Gohman89684502008-07-27 20:43:25 +00004423 SDNode *U = *UI;
Dan Gohmandbe664a2009-01-19 21:44:21 +00004424 do ++UI; while (UI != UE && *UI == U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004425
Chris Lattner8b8749f2005-08-17 19:00:20 +00004426 // This node is about to morph, remove its old self from the CSE maps.
4427 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004428 int operandNum = 0;
4429 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4430 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004431 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004432 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004433 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004434 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004435 To.getNode()->addUser(operandNum, U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004436 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004437
4438 // Now that we have modified U, add it back to the CSE maps. If it already
4439 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004440 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004441 ReplaceAllUsesWith(U, Existing, UpdateListener);
4442 // U is now dead. Inform the listener if it exists and delete it.
4443 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004444 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004445 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004446 } else {
4447 // If the node doesn't already exist, we updated it. Inform a listener if
4448 // it exists.
4449 if (UpdateListener)
4450 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004451 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004452 }
4453}
4454
4455/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4456/// This can cause recursive merging of nodes in the DAG.
4457///
4458/// This version assumes From/To have matching types and numbers of result
4459/// values.
4460///
Chris Lattner1e111c72005-09-07 05:37:01 +00004461void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004462 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004463 assert(From->getVTList().VTs == To->getVTList().VTs &&
4464 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004465 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004466
4467 // Handle the trivial case.
4468 if (From == To)
4469 return;
4470
Dan Gohmandbe664a2009-01-19 21:44:21 +00004471 // Iterate over just the existing users of From. See the comments in
4472 // the ReplaceAllUsesWith above.
4473 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4474 while (UI != UE) {
Dan Gohman89684502008-07-27 20:43:25 +00004475 SDNode *U = *UI;
Dan Gohmandbe664a2009-01-19 21:44:21 +00004476 do ++UI; while (UI != UE && *UI == U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004477
Chris Lattner8b52f212005-08-26 18:36:28 +00004478 // This node is about to morph, remove its old self from the CSE maps.
4479 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004480 int operandNum = 0;
4481 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4482 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004483 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004484 From->removeUser(operandNum, U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004485 I->getSDValue().setNode(To);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004486 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004487 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004488
Chris Lattner8b8749f2005-08-17 19:00:20 +00004489 // Now that we have modified U, add it back to the CSE maps. If it already
4490 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004491 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004492 ReplaceAllUsesWith(U, Existing, UpdateListener);
4493 // U is now dead. Inform the listener if it exists and delete it.
4494 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004495 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004496 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004497 } else {
4498 // If the node doesn't already exist, we updated it. Inform a listener if
4499 // it exists.
4500 if (UpdateListener)
4501 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004502 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004503 }
4504}
4505
Chris Lattner8b52f212005-08-26 18:36:28 +00004506/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4507/// This can cause recursive merging of nodes in the DAG.
4508///
4509/// This version can replace From with any result values. To must match the
4510/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004511void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004512 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004513 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004514 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004515 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004516
Dan Gohmandbe664a2009-01-19 21:44:21 +00004517 // Iterate over just the existing users of From. See the comments in
4518 // the ReplaceAllUsesWith above.
4519 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4520 while (UI != UE) {
Dan Gohman89684502008-07-27 20:43:25 +00004521 SDNode *U = *UI;
Dan Gohmandbe664a2009-01-19 21:44:21 +00004522 do ++UI; while (UI != UE && *UI == U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004523
Chris Lattner7b2880c2005-08-24 22:44:39 +00004524 // This node is about to morph, remove its old self from the CSE maps.
4525 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004526 int operandNum = 0;
4527 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4528 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004529 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004530 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004531 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004532 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004533 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004534 ToOp.getNode()->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004535 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004536
Chris Lattner7b2880c2005-08-24 22:44:39 +00004537 // Now that we have modified U, add it back to the CSE maps. If it already
4538 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004539 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004540 ReplaceAllUsesWith(U, Existing, UpdateListener);
4541 // U is now dead. Inform the listener if it exists and delete it.
4542 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004543 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004544 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004545 } else {
4546 // If the node doesn't already exist, we updated it. Inform a listener if
4547 // it exists.
4548 if (UpdateListener)
4549 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004550 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004551 }
4552}
4553
Chris Lattner012f2412006-02-17 21:58:01 +00004554/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004555/// uses of other values produced by From.getVal() alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004556/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004557void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004558 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004559 // Handle the really simple, really trivial case efficiently.
4560 if (From == To) return;
4561
Chris Lattner012f2412006-02-17 21:58:01 +00004562 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004563 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004564 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004565 return;
4566 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004567
Gabor Greifba36cb52008-08-28 21:40:38 +00004568 // Get all of the users of From.getNode(). We want these in a nice,
Chris Lattnercf5640b2007-02-04 00:14:31 +00004569 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Gabor Greifba36cb52008-08-28 21:40:38 +00004570 SmallSetVector<SDNode*, 16> Users(From.getNode()->use_begin(), From.getNode()->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004571
4572 while (!Users.empty()) {
4573 // We know that this user uses some value of From. If it is the right
4574 // value, update it.
4575 SDNode *User = Users.back();
4576 Users.pop_back();
4577
Chris Lattner01d029b2007-10-15 06:10:22 +00004578 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004579 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004580 for (; Op != E; ++Op)
4581 if (*Op == From) break;
4582
4583 // If there are no matches, the user must use some other result of From.
4584 if (Op == E) continue;
4585
4586 // Okay, we know this user needs to be updated. Remove its old self
4587 // from the CSE maps.
4588 RemoveNodeFromCSEMaps(User);
4589
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004590 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004591 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004592 if (*Op == From) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004593 From.getNode()->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004594 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004595 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004596 To.getNode()->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004597 }
4598 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004599
4600 // Now that we have modified User, add it back to the CSE maps. If it
4601 // already exists there, recursively merge the results together.
4602 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004603 if (!Existing) {
4604 if (UpdateListener) UpdateListener->NodeUpdated(User);
4605 continue; // Continue on to next user.
4606 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004607
4608 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004609 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004610 // recursive merging of other unrelated nodes down the line.
4611 ReplaceAllUsesWith(User, Existing, UpdateListener);
4612
4613 // User is now dead. Notify a listener if present.
4614 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4615 DeleteNodeNotInCSEMaps(User);
4616 }
4617}
4618
4619/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004620/// uses of other values produced by From.getVal() alone. The same value may
Dan Gohmane8be6c62008-07-17 19:10:17 +00004621/// appear in both the From and To list. The Deleted vector is
4622/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004623void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4624 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004625 unsigned Num,
4626 DAGUpdateListener *UpdateListener){
4627 // Handle the simple, trivial case efficiently.
4628 if (Num == 1)
4629 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4630
4631 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4632 for (unsigned i = 0; i != Num; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00004633 for (SDNode::use_iterator UI = From[i].getNode()->use_begin(),
4634 E = From[i].getNode()->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004635 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004636
4637 while (!Users.empty()) {
4638 // We know that this user uses some value of From. If it is the right
4639 // value, update it.
4640 SDNode *User = Users.back().first;
4641 unsigned i = Users.back().second;
4642 Users.pop_back();
4643
4644 // Scan for an operand that matches From.
4645 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4646 for (; Op != E; ++Op)
4647 if (*Op == From[i]) break;
4648
4649 // If there are no matches, the user must use some other result of From.
4650 if (Op == E) continue;
4651
4652 // Okay, we know this user needs to be updated. Remove its old self
4653 // from the CSE maps.
4654 RemoveNodeFromCSEMaps(User);
4655
4656 // Update all operands that match "From" in case there are multiple uses.
4657 for (; Op != E; ++Op) {
4658 if (*Op == From[i]) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004659 From[i].getNode()->removeUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004660 *Op = To[i];
4661 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004662 To[i].getNode()->addUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004663 }
4664 }
4665
4666 // Now that we have modified User, add it back to the CSE maps. If it
4667 // already exists there, recursively merge the results together.
4668 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4669 if (!Existing) {
4670 if (UpdateListener) UpdateListener->NodeUpdated(User);
4671 continue; // Continue on to next user.
4672 }
4673
4674 // If there was already an existing matching node, use ReplaceAllUsesWith
4675 // to replace the dead one with the existing one. This can cause
4676 // recursive merging of other unrelated nodes down the line.
4677 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004678
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004679 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004680 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004681 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004682 }
4683}
4684
Evan Chenge6f35d82006-08-01 08:20:41 +00004685/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004686/// based on their topological order. It returns the maximum id and a vector
4687/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00004688unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00004689
Dan Gohmanf06c8352008-09-30 18:30:35 +00004690 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004691
Dan Gohmanf06c8352008-09-30 18:30:35 +00004692 // SortedPos tracks the progress of the algorithm. Nodes before it are
4693 // sorted, nodes after it are unsorted. When the algorithm completes
4694 // it is at the end of the list.
4695 allnodes_iterator SortedPos = allnodes_begin();
4696
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004697 // Visit all the nodes. Move nodes with no operands to the front of
4698 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00004699 // operand count. Before we do this, the Node Id fields of the nodes
4700 // may contain arbitrary values. After, the Node Id fields for nodes
4701 // before SortedPos will contain the topological sort index, and the
4702 // Node Id fields for nodes At SortedPos and after will contain the
4703 // count of outstanding operands.
4704 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
4705 SDNode *N = I++;
4706 unsigned Degree = N->getNumOperands();
4707 if (Degree == 0) {
4708 // A node with no uses, add it to the result array immediately.
4709 N->setNodeId(DAGSize++);
4710 allnodes_iterator Q = N;
4711 if (Q != SortedPos)
4712 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
4713 ++SortedPos;
4714 } else {
4715 // Temporarily use the Node Id as scratch space for the degree count.
4716 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004717 }
4718 }
4719
Dan Gohmanf06c8352008-09-30 18:30:35 +00004720 // Visit all the nodes. As we iterate, moves nodes into sorted order,
4721 // such that by the time the end is reached all nodes will be sorted.
4722 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
4723 SDNode *N = I;
4724 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4725 UI != UE; ++UI) {
4726 SDNode *P = *UI;
4727 unsigned Degree = P->getNodeId();
4728 --Degree;
4729 if (Degree == 0) {
4730 // All of P's operands are sorted, so P may sorted now.
4731 P->setNodeId(DAGSize++);
4732 if (P != SortedPos)
4733 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
4734 ++SortedPos;
4735 } else {
4736 // Update P's outstanding operand count.
4737 P->setNodeId(Degree);
4738 }
4739 }
4740 }
4741
4742 assert(SortedPos == AllNodes.end() &&
4743 "Topological sort incomplete!");
4744 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
4745 "First node in topological sort is not the entry token!");
4746 assert(AllNodes.front().getNodeId() == 0 &&
4747 "First node in topological sort has non-zero id!");
4748 assert(AllNodes.front().getNumOperands() == 0 &&
4749 "First node in topological sort has operands!");
4750 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
4751 "Last node in topologic sort has unexpected id!");
4752 assert(AllNodes.back().use_empty() &&
4753 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004754 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00004755 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00004756}
4757
4758
Evan Cheng091cba12006-07-27 06:39:06 +00004759
Jim Laskey58b968b2005-08-17 20:08:02 +00004760//===----------------------------------------------------------------------===//
4761// SDNode Class
4762//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004763
Chris Lattner48b85922007-02-04 02:41:42 +00004764HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004765 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004766}
4767
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004768GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Dan Gohman6520e202008-10-18 02:06:02 +00004769 MVT VT, int64_t o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004770 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004771 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004772 // Thread Local
4773 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4774 // Non Thread Local
4775 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4776 getSDVTList(VT)), Offset(o) {
4777 TheGlobal = const_cast<GlobalValue*>(GA);
4778}
Chris Lattner48b85922007-02-04 02:41:42 +00004779
Dan Gohman1ea58a52008-07-09 22:08:04 +00004780MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004781 const Value *srcValue, int SVO,
4782 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004783 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004784 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004785
4786 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4787 assert(getAlignment() == alignment && "Alignment representation error!");
4788 assert(isVolatile() == vol && "Volatile representation error!");
4789}
4790
Mon P Wangc4d10212008-10-17 18:22:58 +00004791MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops,
4792 unsigned NumOps, MVT memvt, const Value *srcValue,
4793 int SVO, unsigned alignment, bool vol)
4794 : SDNode(Opc, VTs, Ops, NumOps),
4795 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
4796 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
4797 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4798 assert(getAlignment() == alignment && "Alignment representation error!");
4799 assert(isVolatile() == vol && "Volatile representation error!");
4800}
4801
Dan Gohman36b5c132008-04-07 19:35:22 +00004802/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004803/// reference performed by this memory reference.
4804MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesenfea90882008-10-24 01:06:58 +00004805 int Flags = 0;
Dan Gohman1ea58a52008-07-09 22:08:04 +00004806 if (isa<LoadSDNode>(this))
4807 Flags = MachineMemOperand::MOLoad;
4808 else if (isa<StoreSDNode>(this))
4809 Flags = MachineMemOperand::MOStore;
Mon P Wangc4d10212008-10-17 18:22:58 +00004810 else if (isa<AtomicSDNode>(this)) {
Dan Gohman1ea58a52008-07-09 22:08:04 +00004811 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4812 }
Mon P Wangc4d10212008-10-17 18:22:58 +00004813 else {
4814 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
4815 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
4816 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
4817 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
4818 }
Dan Gohman1ea58a52008-07-09 22:08:04 +00004819
4820 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004821 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4822
Dan Gohman1ea58a52008-07-09 22:08:04 +00004823 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004824 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00004825 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00004826 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004827 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4828 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004829 else
4830 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4831 Size, getAlignment());
4832}
4833
Jim Laskey583bd472006-10-27 23:46:08 +00004834/// Profile - Gather unique data for the node.
4835///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004836void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004837 AddNodeIDNode(ID, this);
4838}
4839
Chris Lattnera3255112005-11-08 23:30:28 +00004840/// getValueTypeList - Return a pointer to the specified value type.
4841///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004842const MVT *SDNode::getValueTypeList(MVT VT) {
4843 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004844 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004845 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004846 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004847 static MVT VTs[MVT::LAST_VALUETYPE];
4848 VTs[VT.getSimpleVT()] = VT;
4849 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004850 }
Chris Lattnera3255112005-11-08 23:30:28 +00004851}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004852
Chris Lattner5c884562005-01-12 18:37:47 +00004853/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4854/// indicated value. This method ignores uses of other values defined by this
4855/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004856bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004857 assert(Value < getNumValues() && "Bad value!");
4858
Roman Levensteindc1adac2008-04-07 10:06:32 +00004859 // TODO: Only iterate over uses of a given value of the node
4860 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004861 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004862 if (NUses == 0)
4863 return false;
4864 --NUses;
4865 }
Chris Lattner5c884562005-01-12 18:37:47 +00004866 }
4867
4868 // Found exactly the right number of uses?
4869 return NUses == 0;
4870}
4871
4872
Evan Cheng33d55952007-08-02 05:29:38 +00004873/// hasAnyUseOfValue - Return true if there are any use of the indicated
4874/// value. This method ignores uses of other values defined by this operation.
4875bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4876 assert(Value < getNumValues() && "Bad value!");
4877
Dan Gohman1373c1c2008-07-09 22:39:01 +00004878 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004879 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004880 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004881
4882 return false;
4883}
4884
4885
Dan Gohman2a629952008-07-27 18:06:42 +00004886/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004887///
Dan Gohman2a629952008-07-27 18:06:42 +00004888bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004889 bool Seen = false;
4890 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004891 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004892 if (User == this)
4893 Seen = true;
4894 else
4895 return false;
4896 }
4897
4898 return Seen;
4899}
4900
Evan Chenge6e97e62006-11-03 07:31:32 +00004901/// isOperand - Return true if this node is an operand of N.
4902///
Dan Gohman475871a2008-07-27 21:46:04 +00004903bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004904 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4905 if (*this == N->getOperand(i))
4906 return true;
4907 return false;
4908}
4909
Evan Cheng917be682008-03-04 00:41:45 +00004910bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004911 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004912 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004913 return true;
4914 return false;
4915}
Evan Cheng4ee62112006-02-05 06:29:23 +00004916
Chris Lattner572dee72008-01-16 05:49:24 +00004917/// reachesChainWithoutSideEffects - Return true if this operand (which must
4918/// be a chain) reaches the specified operand without crossing any
4919/// side-effecting instructions. In practice, this looks through token
4920/// factors and non-volatile loads. In order to remain efficient, this only
4921/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004922bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004923 unsigned Depth) const {
4924 if (*this == Dest) return true;
4925
4926 // Don't search too deeply, we just want to be able to see through
4927 // TokenFactor's etc.
4928 if (Depth == 0) return false;
4929
4930 // If this is a token factor, all inputs to the TF happen in parallel. If any
4931 // of the operands of the TF reach dest, then we can do the xform.
4932 if (getOpcode() == ISD::TokenFactor) {
4933 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4934 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4935 return true;
4936 return false;
4937 }
4938
4939 // Loads don't have side effects, look through them.
4940 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4941 if (!Ld->isVolatile())
4942 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4943 }
4944 return false;
4945}
4946
4947
Evan Chengc5fc57d2006-11-03 03:05:24 +00004948static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004949 SmallPtrSet<SDNode *, 32> &Visited) {
4950 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004951 return;
4952
4953 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004954 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00004955 if (Op == P) {
4956 found = true;
4957 return;
4958 }
4959 findPredecessor(Op, P, found, Visited);
4960 }
4961}
4962
Evan Cheng917be682008-03-04 00:41:45 +00004963/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004964/// is either an operand of N or it can be reached by recursively traversing
4965/// up the operands.
4966/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004967bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004968 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004969 bool found = false;
4970 findPredecessor(N, this, found, Visited);
4971 return found;
4972}
4973
Evan Chengc5484282006-10-04 00:56:09 +00004974uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4975 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004976 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00004977}
4978
Reid Spencer577cc322007-04-01 07:32:19 +00004979std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004980 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004981 default:
4982 if (getOpcode() < ISD::BUILTIN_OP_END)
4983 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004984 if (isMachineOpcode()) {
4985 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004986 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004987 if (getMachineOpcode() < TII->getNumOpcodes())
4988 return TII->get(getMachineOpcode()).getName();
4989 return "<<Unknown Machine Node>>";
4990 }
4991 if (G) {
Dan Gohmane9530ec2009-01-15 16:58:17 +00004992 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmane8be6c62008-07-17 19:10:17 +00004993 const char *Name = TLI.getTargetNodeName(getOpcode());
4994 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004995 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004996 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004997 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004998
Dan Gohmane8be6c62008-07-17 19:10:17 +00004999#ifndef NDEBUG
5000 case ISD::DELETED_NODE:
5001 return "<<Deleted Node!>>";
5002#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00005003 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00005004 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohman0b1d4a72008-12-23 21:37:04 +00005005 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5006 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5007 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5008 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5009 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5010 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5011 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5012 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5013 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5014 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5015 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5016 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Andrew Lenharth95762122005-03-31 21:24:06 +00005017 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00005018 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005019 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00005020 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005021 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00005022 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00005023 case ISD::AssertSext: return "AssertSext";
5024 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005025
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005026 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005027 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005028 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00005029 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005030
5031 case ISD::Constant: return "Constant";
5032 case ISD::ConstantFP: return "ConstantFP";
5033 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005034 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005035 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005036 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00005037 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00005038 case ISD::RETURNADDR: return "RETURNADDR";
5039 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005040 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00005041 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00005042 case ISD::EHSELECTION: return "EHSELECTION";
5043 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00005044 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005045 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00005046 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005047 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00005048 return Intrinsic::getName((Intrinsic::ID)IID);
5049 }
5050 case ISD::INTRINSIC_VOID:
5051 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005052 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00005053 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00005054 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00005055
Chris Lattnerb2827b02006-03-19 00:52:58 +00005056 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005057 case ISD::TargetConstant: return "TargetConstant";
5058 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005059 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005060 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005061 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005062 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00005063 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005064 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005065
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005066 case ISD::CopyToReg: return "CopyToReg";
5067 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005068 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00005069 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005070 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00005071 case ISD::DBG_LABEL: return "dbg_label";
5072 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00005073 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00005074 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00005075 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00005076 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005077
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005078 // Unary operators
5079 case ISD::FABS: return "fabs";
5080 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00005081 case ISD::FSQRT: return "fsqrt";
5082 case ISD::FSIN: return "fsin";
5083 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00005084 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00005085 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00005086 case ISD::FTRUNC: return "ftrunc";
5087 case ISD::FFLOOR: return "ffloor";
5088 case ISD::FCEIL: return "fceil";
5089 case ISD::FRINT: return "frint";
5090 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005091
5092 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005093 case ISD::ADD: return "add";
5094 case ISD::SUB: return "sub";
5095 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00005096 case ISD::MULHU: return "mulhu";
5097 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005098 case ISD::SDIV: return "sdiv";
5099 case ISD::UDIV: return "udiv";
5100 case ISD::SREM: return "srem";
5101 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00005102 case ISD::SMUL_LOHI: return "smul_lohi";
5103 case ISD::UMUL_LOHI: return "umul_lohi";
5104 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00005105 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005106 case ISD::AND: return "and";
5107 case ISD::OR: return "or";
5108 case ISD::XOR: return "xor";
5109 case ISD::SHL: return "shl";
5110 case ISD::SRA: return "sra";
5111 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00005112 case ISD::ROTL: return "rotl";
5113 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00005114 case ISD::FADD: return "fadd";
5115 case ISD::FSUB: return "fsub";
5116 case ISD::FMUL: return "fmul";
5117 case ISD::FDIV: return "fdiv";
5118 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00005119 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00005120 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005121
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005122 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005123 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005124 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005125 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005126 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005127 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005128 case ISD::CONCAT_VECTORS: return "concat_vectors";
5129 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005130 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005131 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00005132 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005133 case ISD::ADDC: return "addc";
5134 case ISD::ADDE: return "adde";
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005135 case ISD::SADDO: return "saddo";
5136 case ISD::UADDO: return "uaddo";
Bill Wendling74c37652008-12-09 22:08:41 +00005137 case ISD::SSUBO: return "ssubo";
5138 case ISD::USUBO: return "usubo";
5139 case ISD::SMULO: return "smulo";
5140 case ISD::UMULO: return "umulo";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005141 case ISD::SUBC: return "subc";
5142 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005143 case ISD::SHL_PARTS: return "shl_parts";
5144 case ISD::SRA_PARTS: return "sra_parts";
5145 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00005146
5147 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5148 case ISD::INSERT_SUBREG: return "insert_subreg";
5149
Chris Lattner7f644642005-04-28 21:44:03 +00005150 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005151 case ISD::SIGN_EXTEND: return "sign_extend";
5152 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005153 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005154 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005155 case ISD::TRUNCATE: return "truncate";
5156 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005157 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005158 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005159 case ISD::FP_EXTEND: return "fp_extend";
5160
5161 case ISD::SINT_TO_FP: return "sint_to_fp";
5162 case ISD::UINT_TO_FP: return "uint_to_fp";
5163 case ISD::FP_TO_SINT: return "fp_to_sint";
5164 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005165 case ISD::BIT_CONVERT: return "bit_convert";
Mon P Wang77cdf302008-11-10 20:54:11 +00005166
5167 case ISD::CONVERT_RNDSAT: {
5168 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
5169 default: assert(0 && "Unknown cvt code!");
5170 case ISD::CVT_FF: return "cvt_ff";
5171 case ISD::CVT_FS: return "cvt_fs";
5172 case ISD::CVT_FU: return "cvt_fu";
5173 case ISD::CVT_SF: return "cvt_sf";
5174 case ISD::CVT_UF: return "cvt_uf";
5175 case ISD::CVT_SS: return "cvt_ss";
5176 case ISD::CVT_SU: return "cvt_su";
5177 case ISD::CVT_US: return "cvt_us";
5178 case ISD::CVT_UU: return "cvt_uu";
5179 }
5180 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005181
5182 // Control flow instructions
5183 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005184 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005185 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005186 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005187 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005188 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005189 case ISD::CALLSEQ_START: return "callseq_start";
5190 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005191
5192 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005193 case ISD::LOAD: return "load";
5194 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005195 case ISD::VAARG: return "vaarg";
5196 case ISD::VACOPY: return "vacopy";
5197 case ISD::VAEND: return "vaend";
5198 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005199 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005200 case ISD::EXTRACT_ELEMENT: return "extract_element";
5201 case ISD::BUILD_PAIR: return "build_pair";
5202 case ISD::STACKSAVE: return "stacksave";
5203 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005204 case ISD::TRAP: return "trap";
5205
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005206 // Bit manipulation
5207 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005208 case ISD::CTPOP: return "ctpop";
5209 case ISD::CTTZ: return "cttz";
5210 case ISD::CTLZ: return "ctlz";
5211
Chris Lattner36ce6912005-11-29 06:21:05 +00005212 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005213 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005214 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005215
Duncan Sands36397f52007-07-27 12:58:54 +00005216 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005217 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005218
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005219 case ISD::CONDCODE:
5220 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005221 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005222 case ISD::SETOEQ: return "setoeq";
5223 case ISD::SETOGT: return "setogt";
5224 case ISD::SETOGE: return "setoge";
5225 case ISD::SETOLT: return "setolt";
5226 case ISD::SETOLE: return "setole";
5227 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005228
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005229 case ISD::SETO: return "seto";
5230 case ISD::SETUO: return "setuo";
5231 case ISD::SETUEQ: return "setue";
5232 case ISD::SETUGT: return "setugt";
5233 case ISD::SETUGE: return "setuge";
5234 case ISD::SETULT: return "setult";
5235 case ISD::SETULE: return "setule";
5236 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005237
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005238 case ISD::SETEQ: return "seteq";
5239 case ISD::SETGT: return "setgt";
5240 case ISD::SETGE: return "setge";
5241 case ISD::SETLT: return "setlt";
5242 case ISD::SETLE: return "setle";
5243 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005244 }
5245 }
5246}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005247
Evan Cheng144d8f02006-11-09 17:55:04 +00005248const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005249 switch (AM) {
5250 default:
5251 return "";
5252 case ISD::PRE_INC:
5253 return "<pre-inc>";
5254 case ISD::PRE_DEC:
5255 return "<pre-dec>";
5256 case ISD::POST_INC:
5257 return "<post-inc>";
5258 case ISD::POST_DEC:
5259 return "<post-dec>";
5260 }
5261}
5262
Duncan Sands276dcbd2008-03-21 09:14:45 +00005263std::string ISD::ArgFlagsTy::getArgFlagsString() {
5264 std::string S = "< ";
5265
5266 if (isZExt())
5267 S += "zext ";
5268 if (isSExt())
5269 S += "sext ";
5270 if (isInReg())
5271 S += "inreg ";
5272 if (isSRet())
5273 S += "sret ";
5274 if (isByVal())
5275 S += "byval ";
5276 if (isNest())
5277 S += "nest ";
5278 if (getByValAlign())
5279 S += "byval-align:" + utostr(getByValAlign()) + " ";
5280 if (getOrigAlign())
5281 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5282 if (getByValSize())
5283 S += "byval-size:" + utostr(getByValSize()) + " ";
5284 return S + ">";
5285}
5286
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005287void SDNode::dump() const { dump(0); }
5288void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005289 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005290 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005291}
5292
5293void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5294 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005295
5296 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005297 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005298 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005299 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005300 else
Chris Lattner944fac72008-08-23 22:23:09 +00005301 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005302 }
Chris Lattner944fac72008-08-23 22:23:09 +00005303 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005304
Chris Lattner944fac72008-08-23 22:23:09 +00005305 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005306 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005307 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005308 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005309 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005310 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005311 }
5312
Evan Chengce254432007-12-11 02:08:35 +00005313 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005314 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005315 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005316 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005317 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005318 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005319 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005320 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005321 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005322 }
Chris Lattner944fac72008-08-23 22:23:09 +00005323 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005324 }
5325
Chris Lattnerc3aae252005-01-07 07:46:32 +00005326 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005327 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005328 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005329 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005330 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005331 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005332 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005333 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005334 OS << "<APFloat(";
Dale Johannesen23a98552008-10-09 23:00:39 +00005335 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005336 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005337 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005338 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005339 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohman668aff62008-10-18 18:22:42 +00005340 int64_t offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005341 OS << '<';
5342 WriteAsOperand(OS, GADN->getGlobal());
5343 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005344 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005345 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005346 else
Chris Lattner944fac72008-08-23 22:23:09 +00005347 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005348 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005349 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005350 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005351 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005352 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005353 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005354 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005355 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005356 else
Chris Lattner944fac72008-08-23 22:23:09 +00005357 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005358 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005359 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005360 else
Chris Lattner944fac72008-08-23 22:23:09 +00005361 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005362 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005363 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005364 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5365 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005366 OS << LBB->getName() << " ";
5367 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005368 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005369 if (G && R->getReg() &&
5370 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005371 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005372 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005373 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005374 }
Bill Wendling056292f2008-09-16 21:48:12 +00005375 } else if (const ExternalSymbolSDNode *ES =
5376 dyn_cast<ExternalSymbolSDNode>(this)) {
5377 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005378 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5379 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005380 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005381 else
Chris Lattner944fac72008-08-23 22:23:09 +00005382 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005383 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5384 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005385 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005386 else
Chris Lattner944fac72008-08-23 22:23:09 +00005387 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005388 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005389 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005390 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005391 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005392 }
5393 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005394 const Value *SrcValue = LD->getSrcValue();
5395 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005396 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005397 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005398 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005399 else
Chris Lattner944fac72008-08-23 22:23:09 +00005400 OS << "null";
5401 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005402
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005403 bool doExt = true;
5404 switch (LD->getExtensionType()) {
5405 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005406 case ISD::EXTLOAD: OS << " <anyext "; break;
5407 case ISD::SEXTLOAD: OS << " <sext "; break;
5408 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005409 }
5410 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005411 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005412
Evan Cheng144d8f02006-11-09 17:55:04 +00005413 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005414 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005415 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005416 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005417 OS << " <volatile>";
5418 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005419 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005420 const Value *SrcValue = ST->getSrcValue();
5421 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005422 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005423 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005424 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005425 else
Chris Lattner944fac72008-08-23 22:23:09 +00005426 OS << "null";
5427 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005428
5429 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005430 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005431
5432 const char *AM = getIndexedModeName(ST->getAddressingMode());
5433 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005434 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005435 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005436 OS << " <volatile>";
5437 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005438 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5439 const Value *SrcValue = AT->getSrcValue();
5440 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005441 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005442 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005443 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005444 else
Chris Lattner944fac72008-08-23 22:23:09 +00005445 OS << "null";
5446 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005447 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005448 OS << " <volatile>";
5449 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005450 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005451}
5452
Chris Lattnerde202b32005-11-09 23:47:37 +00005453static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005454 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005455 if (N->getOperand(i).getNode()->hasOneUse())
5456 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005457 else
Bill Wendling832171c2006-12-07 20:04:42 +00005458 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005459 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005460
Chris Lattnerea946cd2005-01-09 20:38:33 +00005461
Bill Wendling832171c2006-12-07 20:04:42 +00005462 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005463 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005464}
5465
Chris Lattnerc3aae252005-01-07 07:46:32 +00005466void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005467 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005468
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005469 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5470 I != E; ++I) {
5471 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005472 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005473 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005474 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005475
Gabor Greifba36cb52008-08-28 21:40:38 +00005476 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005477
Bill Wendling832171c2006-12-07 20:04:42 +00005478 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005479}
5480
Evan Chengd6594ae2006-09-12 21:00:35 +00005481const Type *ConstantPoolSDNode::getType() const {
5482 if (isMachineConstantPoolEntry())
5483 return Val.MachineCPVal->getType();
5484 return Val.ConstVal->getType();
5485}