blob: 9b1e963f99dd4c56c40e22123cf7332bbd266219 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG class.
11//
12//===----------------------------------------------------------------------===//
Bill Wendlingcb2504f2009-12-21 19:34:59 +000013
Dan Gohmanf17a25c2007-07-18 16:29:46 +000014#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendlingcb2504f2009-12-21 19:34:59 +000015#include "SDNodeOrdering.h"
Evan Cheng1899ca32010-03-14 19:56:39 +000016#include "SDNodeDbgValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Constants.h"
Evan Cheng833501d2008-06-30 07:31:25 +000018#include "llvm/Analysis/ValueTracking.h"
Dan Gohman45f13d42009-08-11 16:02:12 +000019#include "llvm/Function.h"
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +000020#include "llvm/GlobalAlias.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/GlobalVariable.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Assembly/Writer.h"
Dan Gohmane8b391e2008-04-12 04:36:06 +000025#include "llvm/CallingConv.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CodeGen/MachineBasicBlock.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner53f5aee2007-10-15 17:47:20 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000029#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000030#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetData.h"
Evan Cheng684647d2009-12-09 01:04:59 +000033#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Target/TargetLowering.h"
Dan Gohman3d015562009-01-22 21:58:43 +000035#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesen75dbdd12009-10-15 18:50:03 +000037#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetMachine.h"
Bill Wendling5db7ffb2008-09-30 21:22:07 +000039#include "llvm/Support/CommandLine.h"
David Greened44636d2010-01-05 01:24:36 +000040#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000041#include "llvm/Support/ErrorHandling.h"
Owen Andersonef31ae32009-06-25 17:09:00 +000042#include "llvm/Support/ManagedStatic.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000043#include "llvm/Support/MathExtras.h"
44#include "llvm/Support/raw_ostream.h"
Owen Andersonef31ae32009-06-25 17:09:00 +000045#include "llvm/System/Mutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046#include "llvm/ADT/SetVector.h"
47#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsa9810f32007-10-16 09:56:48 +000048#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049#include "llvm/ADT/SmallVector.h"
50#include "llvm/ADT/StringExtras.h"
51#include <algorithm>
52#include <cmath>
53using namespace llvm;
54
55/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Andersonac9de032009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Owen Andersonac9de032009-08-10 22:56:29 +000062static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000063 switch (VT.getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +000064 default: llvm_unreachable("Unknown FP format");
Owen Anderson36e3a6e2009-08-11 20:47:22 +000065 case MVT::f32: return &APFloat::IEEEsingle;
66 case MVT::f64: return &APFloat::IEEEdouble;
67 case MVT::f80: return &APFloat::x87DoubleExtended;
68 case MVT::f128: return &APFloat::IEEEquad;
69 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
Chris Lattnerd037d482008-03-05 06:48:13 +000070 }
71}
72
Chris Lattner7bcb18f2008-02-03 06:49:24 +000073SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
74
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075//===----------------------------------------------------------------------===//
76// ConstantFPSDNode Class
77//===----------------------------------------------------------------------===//
78
79/// isExactlyValue - We don't rely on operator== working on double values, as
80/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
81/// As such, this method can be used to do an exact bit-for-bit comparison of
82/// two floating point values.
Dale Johannesenc53301c2007-08-26 01:18:27 +000083bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanc1f3a072008-09-12 18:08:03 +000084 return getValueAPF().bitwiseIsEqual(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085}
86
Owen Andersonac9de032009-08-10 22:56:29 +000087bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenbbe2b702007-08-30 00:23:21 +000088 const APFloat& Val) {
Duncan Sands92c43912008-06-06 12:08:01 +000089 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michel91099d62009-02-17 22:15:04 +000090
Dale Johannesen90c25d12008-04-20 18:23:46 +000091 // PPC long double cannot be converted to any other type.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000092 if (VT == MVT::ppcf128 ||
Dale Johannesen90c25d12008-04-20 18:23:46 +000093 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerd037d482008-03-05 06:48:13 +000094 return false;
Scott Michel91099d62009-02-17 22:15:04 +000095
Dale Johannesenbbe2b702007-08-30 00:23:21 +000096 // convert modifies in place, so make a copy.
97 APFloat Val2 = APFloat(Val);
Dale Johannesen6e547b42008-10-09 23:00:39 +000098 bool losesInfo;
Owen Andersonac9de032009-08-10 22:56:29 +000099 (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
Dale Johannesen6e547b42008-10-09 23:00:39 +0000100 &losesInfo);
101 return !losesInfo;
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000102}
103
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104//===----------------------------------------------------------------------===//
105// ISD Namespace
106//===----------------------------------------------------------------------===//
107
108/// isBuildVectorAllOnes - Return true if the specified node is a
109/// BUILD_VECTOR where all of the elements are ~0 or undef.
110bool ISD::isBuildVectorAllOnes(const SDNode *N) {
111 // Look through a bit convert.
112 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greif1c80d112008-08-28 21:40:38 +0000113 N = N->getOperand(0).getNode();
Scott Michel91099d62009-02-17 22:15:04 +0000114
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michel91099d62009-02-17 22:15:04 +0000116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 unsigned i = 0, e = N->getNumOperands();
Scott Michel91099d62009-02-17 22:15:04 +0000118
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 // Skip over all of the undef values.
120 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
121 ++i;
Scott Michel91099d62009-02-17 22:15:04 +0000122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 // Do not accept an all-undef vector.
124 if (i == e) return false;
Scott Michel91099d62009-02-17 22:15:04 +0000125
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 // Do not accept build_vectors that aren't all constants or which have non-~0
127 // elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000128 SDValue NotZero = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 if (isa<ConstantSDNode>(NotZero)) {
130 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
131 return false;
132 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman161652c2008-02-29 01:47:35 +0000133 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen6e547b42008-10-09 23:00:39 +0000134 bitcastToAPInt().isAllOnesValue())
Dan Gohman161652c2008-02-29 01:47:35 +0000135 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 } else
137 return false;
Scott Michel91099d62009-02-17 22:15:04 +0000138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 // Okay, we have at least one ~0 value, check to see if the rest match or are
140 // undefs.
141 for (++i; i != e; ++i)
142 if (N->getOperand(i) != NotZero &&
143 N->getOperand(i).getOpcode() != ISD::UNDEF)
144 return false;
145 return true;
146}
147
148
149/// isBuildVectorAllZeros - Return true if the specified node is a
150/// BUILD_VECTOR where all of the elements are 0 or undef.
151bool ISD::isBuildVectorAllZeros(const SDNode *N) {
152 // Look through a bit convert.
153 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greif1c80d112008-08-28 21:40:38 +0000154 N = N->getOperand(0).getNode();
Scott Michel91099d62009-02-17 22:15:04 +0000155
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michel91099d62009-02-17 22:15:04 +0000157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 unsigned i = 0, e = N->getNumOperands();
Scott Michel91099d62009-02-17 22:15:04 +0000159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 // Skip over all of the undef values.
161 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
162 ++i;
Scott Michel91099d62009-02-17 22:15:04 +0000163
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 // Do not accept an all-undef vector.
165 if (i == e) return false;
Scott Michel91099d62009-02-17 22:15:04 +0000166
Dan Gohman2ac69f62009-06-04 16:49:15 +0000167 // Do not accept build_vectors that aren't all constants or which have non-0
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 // elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000169 SDValue Zero = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 if (isa<ConstantSDNode>(Zero)) {
171 if (!cast<ConstantSDNode>(Zero)->isNullValue())
172 return false;
173 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000174 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 return false;
176 } else
177 return false;
Scott Michel91099d62009-02-17 22:15:04 +0000178
Dan Gohman2ac69f62009-06-04 16:49:15 +0000179 // Okay, we have at least one 0 value, check to see if the rest match or are
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 // undefs.
181 for (++i; i != e; ++i)
182 if (N->getOperand(i) != Zero &&
183 N->getOperand(i).getOpcode() != ISD::UNDEF)
184 return false;
185 return true;
186}
187
Evan Chengd1045a62008-02-18 23:04:32 +0000188/// isScalarToVector - Return true if the specified node is a
189/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
190/// element is not an undef.
191bool ISD::isScalarToVector(const SDNode *N) {
192 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
193 return true;
194
195 if (N->getOpcode() != ISD::BUILD_VECTOR)
196 return false;
197 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
198 return false;
199 unsigned NumElems = N->getNumOperands();
200 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000201 SDValue V = N->getOperand(i);
Evan Chengd1045a62008-02-18 23:04:32 +0000202 if (V.getOpcode() != ISD::UNDEF)
203 return false;
204 }
205 return true;
206}
207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
209/// when given the operation for (X op Y).
210ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
211 // To perform this operation, we just need to swap the L and G bits of the
212 // operation.
213 unsigned OldL = (Operation >> 2) & 1;
214 unsigned OldG = (Operation >> 1) & 1;
215 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
216 (OldL << 1) | // New G bit
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000217 (OldG << 2)); // New L bit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218}
219
220/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
221/// 'op' is a valid SetCC operation.
222ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
223 unsigned Operation = Op;
224 if (isInteger)
225 Operation ^= 7; // Flip L, G, E bits, but not U.
226 else
227 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000228
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 if (Operation > ISD::SETTRUE2)
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000230 Operation &= ~8; // Don't let N and U bits get set.
231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 return ISD::CondCode(Operation);
233}
234
235
236/// isSignedOp - For an integer comparison, return 1 if the comparison is a
237/// signed operation and 2 if the result is an unsigned comparison. Return zero
238/// if the operation does not depend on the sign of the input (setne and seteq).
239static int isSignedOp(ISD::CondCode Opcode) {
240 switch (Opcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000241 default: llvm_unreachable("Illegal integer setcc operation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 case ISD::SETEQ:
243 case ISD::SETNE: return 0;
244 case ISD::SETLT:
245 case ISD::SETLE:
246 case ISD::SETGT:
247 case ISD::SETGE: return 1;
248 case ISD::SETULT:
249 case ISD::SETULE:
250 case ISD::SETUGT:
251 case ISD::SETUGE: return 2;
252 }
253}
254
255/// getSetCCOrOperation - Return the result of a logical OR between different
256/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
257/// returns SETCC_INVALID if it is not possible to represent the resultant
258/// comparison.
259ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
260 bool isInteger) {
261 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
262 // Cannot fold a signed integer setcc with an unsigned integer setcc.
263 return ISD::SETCC_INVALID;
264
265 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
266
267 // If the N and U bits get set then the resultant comparison DOES suddenly
268 // care about orderedness, and is true when ordered.
269 if (Op > ISD::SETTRUE2)
270 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michel91099d62009-02-17 22:15:04 +0000271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 // Canonicalize illegal integer setcc's.
273 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
274 Op = ISD::SETNE;
Scott Michel91099d62009-02-17 22:15:04 +0000275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 return ISD::CondCode(Op);
277}
278
279/// getSetCCAndOperation - Return the result of a logical AND between different
280/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
281/// function returns zero if it is not possible to represent the resultant
282/// comparison.
283ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
284 bool isInteger) {
285 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
286 // Cannot fold a signed setcc with an unsigned setcc.
287 return ISD::SETCC_INVALID;
288
289 // Combine all of the condition bits.
290 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michel91099d62009-02-17 22:15:04 +0000291
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 // Canonicalize illegal integer setcc's.
293 if (isInteger) {
294 switch (Result) {
295 default: break;
296 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmanf1e8e552008-05-14 18:17:09 +0000297 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
299 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
300 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
301 }
302 }
Scott Michel91099d62009-02-17 22:15:04 +0000303
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 return Result;
305}
306
307const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman404e8542008-09-04 15:39:15 +0000308 return MF->getTarget();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309}
310
311//===----------------------------------------------------------------------===//
312// SDNode Profile Support
313//===----------------------------------------------------------------------===//
314
315/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
316///
317static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
318 ID.AddInteger(OpC);
319}
320
321/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
322/// solely with their pointer.
Dan Gohman089efff2008-05-13 00:00:25 +0000323static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michel91099d62009-02-17 22:15:04 +0000324 ID.AddPointer(VTList.VTs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325}
326
327/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
328///
329static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman8181bd12008-07-27 21:46:04 +0000330 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 for (; NumOps; --NumOps, ++Ops) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000332 ID.AddPointer(Ops->getNode());
Gabor Greif46bf5472008-08-26 22:36:50 +0000333 ID.AddInteger(Ops->getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 }
335}
336
Dan Gohman703ce5d2008-07-07 18:26:29 +0000337/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
338///
339static void AddNodeIDOperands(FoldingSetNodeID &ID,
340 const SDUse *Ops, unsigned NumOps) {
341 for (; NumOps; --NumOps, ++Ops) {
djgc2517d32009-01-26 04:35:06 +0000342 ID.AddPointer(Ops->getNode());
343 ID.AddInteger(Ops->getResNo());
Dan Gohman703ce5d2008-07-07 18:26:29 +0000344 }
345}
346
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michel91099d62009-02-17 22:15:04 +0000348 unsigned short OpC, SDVTList VTList,
Dan Gohman8181bd12008-07-27 21:46:04 +0000349 const SDValue *OpList, unsigned N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 AddNodeIDOpcode(ID, OpC);
351 AddNodeIDValueTypes(ID, VTList);
352 AddNodeIDOperands(ID, OpList, N);
353}
354
Duncan Sands1e450d42008-10-27 15:30:53 +0000355/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
356/// the NodeID data.
357static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 switch (N->getOpcode()) {
Chris Lattner7123ef72009-06-25 21:21:14 +0000359 case ISD::TargetExternalSymbol:
360 case ISD::ExternalSymbol:
Edwin Törökbd448e32009-07-14 16:55:14 +0000361 llvm_unreachable("Should only be used on nodes with operands");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 default: break; // Normal nodes don't need extra info.
363 case ISD::TargetConstant:
364 case ISD::Constant:
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000365 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 break;
367 case ISD::TargetConstantFP:
Dale Johannesendf8a8312007-08-31 04:03:46 +0000368 case ISD::ConstantFP: {
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000369 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 break;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000371 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 case ISD::TargetGlobalAddress:
373 case ISD::GlobalAddress:
374 case ISD::TargetGlobalTLSAddress:
375 case ISD::GlobalTLSAddress: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000376 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 ID.AddPointer(GA->getGlobal());
378 ID.AddInteger(GA->getOffset());
Chris Lattner7123ef72009-06-25 21:21:14 +0000379 ID.AddInteger(GA->getTargetFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 break;
381 }
382 case ISD::BasicBlock:
383 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
384 break;
385 case ISD::Register:
386 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
387 break;
Devang Patel1432b622009-11-21 02:46:55 +0000388
Dan Gohman12a9c082008-02-06 22:27:42 +0000389 case ISD::SRCVALUE:
390 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
391 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 case ISD::FrameIndex:
393 case ISD::TargetFrameIndex:
394 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
395 break;
396 case ISD::JumpTable:
397 case ISD::TargetJumpTable:
398 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattner583c7962009-06-25 21:35:31 +0000399 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 break;
401 case ISD::ConstantPool:
402 case ISD::TargetConstantPool: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000403 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 ID.AddInteger(CP->getAlignment());
405 ID.AddInteger(CP->getOffset());
406 if (CP->isMachineConstantPoolEntry())
407 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
408 else
409 ID.AddPointer(CP->getConstVal());
Chris Lattner583c7962009-06-25 21:35:31 +0000410 ID.AddInteger(CP->getTargetFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 break;
412 }
413 case ISD::LOAD: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000414 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3f5d2432008-06-06 12:49:32 +0000415 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +0000416 ID.AddInteger(LD->getRawSubclassData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 break;
418 }
419 case ISD::STORE: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000420 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3f5d2432008-06-06 12:49:32 +0000421 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +0000422 ID.AddInteger(ST->getRawSubclassData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 break;
424 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000425 case ISD::ATOMIC_CMP_SWAP:
426 case ISD::ATOMIC_SWAP:
427 case ISD::ATOMIC_LOAD_ADD:
428 case ISD::ATOMIC_LOAD_SUB:
429 case ISD::ATOMIC_LOAD_AND:
430 case ISD::ATOMIC_LOAD_OR:
431 case ISD::ATOMIC_LOAD_XOR:
432 case ISD::ATOMIC_LOAD_NAND:
433 case ISD::ATOMIC_LOAD_MIN:
434 case ISD::ATOMIC_LOAD_MAX:
435 case ISD::ATOMIC_LOAD_UMIN:
436 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000437 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman9d0f5022009-02-03 00:08:45 +0000438 ID.AddInteger(AT->getMemoryVT().getRawBits());
439 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000440 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 }
Nate Begeman543d2142009-04-27 18:41:29 +0000442 case ISD::VECTOR_SHUFFLE: {
443 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher440d9eb2009-08-22 00:40:45 +0000444 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +0000445 i != e; ++i)
446 ID.AddInteger(SVN->getMaskElt(i));
447 break;
448 }
Dan Gohman91057512009-10-30 01:27:03 +0000449 case ISD::TargetBlockAddress:
450 case ISD::BlockAddress: {
Dan Gohman885793b2009-11-20 23:18:13 +0000451 ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress());
452 ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags());
Dan Gohman91057512009-10-30 01:27:03 +0000453 break;
454 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000455 } // end switch (N->getOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456}
457
Duncan Sands1e450d42008-10-27 15:30:53 +0000458/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
459/// data.
460static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
461 AddNodeIDOpcode(ID, N->getOpcode());
462 // Add the return value info.
463 AddNodeIDValueTypes(ID, N->getVTList());
464 // Add the operand info.
465 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
466
467 // Handle SDNode leafs with special info.
468 AddNodeIDCustom(ID, N);
469}
470
Dan Gohman98beebe2008-08-20 15:58:01 +0000471/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene05a001f2010-02-17 20:21:42 +0000472/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman9d0f5022009-02-03 00:08:45 +0000473/// extension/truncation information.
Dan Gohman98beebe2008-08-20 15:58:01 +0000474///
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000475static inline unsigned
David Greene05a001f2010-02-17 20:21:42 +0000476encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
477 bool isNonTemporal) {
Dan Gohman9d0f5022009-02-03 00:08:45 +0000478 assert((ConvType & 3) == ConvType &&
479 "ConvType may not require more than 2 bits!");
480 assert((AM & 7) == AM &&
481 "AM may not require more than 3 bits!");
482 return ConvType |
483 (AM << 2) |
David Greene05a001f2010-02-17 20:21:42 +0000484 (isVolatile << 5) |
485 (isNonTemporal << 6);
Dan Gohman98beebe2008-08-20 15:58:01 +0000486}
487
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488//===----------------------------------------------------------------------===//
489// SelectionDAG Class
490//===----------------------------------------------------------------------===//
491
Duncan Sands1e450d42008-10-27 15:30:53 +0000492/// doNotCSE - Return true if CSE should not be performed for this node.
493static bool doNotCSE(SDNode *N) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000494 if (N->getValueType(0) == MVT::Flag)
Duncan Sands1e450d42008-10-27 15:30:53 +0000495 return true; // Never CSE anything that produces a flag.
496
497 switch (N->getOpcode()) {
498 default: break;
499 case ISD::HANDLENODE:
Duncan Sands1e450d42008-10-27 15:30:53 +0000500 case ISD::EH_LABEL:
Duncan Sands1e450d42008-10-27 15:30:53 +0000501 return true; // Never CSE these nodes.
502 }
503
504 // Check that remaining values produced are not flags.
505 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000506 if (N->getValueType(i) == MVT::Flag)
Duncan Sands1e450d42008-10-27 15:30:53 +0000507 return true; // Never CSE anything that produces a flag.
508
509 return false;
510}
511
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512/// RemoveDeadNodes - This method deletes all unreachable nodes in the
513/// SelectionDAG.
514void SelectionDAG::RemoveDeadNodes() {
515 // Create a dummy node (which is not added to allnodes), that adds a reference
516 // to the root node, preventing it from being deleted.
517 HandleSDNode Dummy(getRoot());
518
519 SmallVector<SDNode*, 128> DeadNodes;
Scott Michel91099d62009-02-17 22:15:04 +0000520
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 // Add all obviously-dead nodes to the DeadNodes worklist.
522 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
523 if (I->use_empty())
524 DeadNodes.push_back(I);
525
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000526 RemoveDeadNodes(DeadNodes);
Scott Michel91099d62009-02-17 22:15:04 +0000527
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000528 // If the root changed (e.g. it was a dead load, update the root).
529 setRoot(Dummy.getValue());
530}
531
532/// RemoveDeadNodes - This method deletes the unreachable nodes in the
533/// given list, and any nodes that become unreachable as a result.
534void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
535 DAGUpdateListener *UpdateListener) {
536
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 // Process the worklist, deleting the nodes and adding their uses to the
538 // worklist.
539 while (!DeadNodes.empty()) {
djgc2517d32009-01-26 04:35:06 +0000540 SDNode *N = DeadNodes.pop_back_val();
Scott Michel91099d62009-02-17 22:15:04 +0000541
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000542 if (UpdateListener)
543 UpdateListener->NodeDeleted(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +0000544
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 // Take the node out of the appropriate CSE map.
546 RemoveNodeFromCSEMaps(N);
547
548 // Next, brutally remove the operand list. This is safe to do, as there are
549 // no cycles in the graph.
djgc2517d32009-01-26 04:35:06 +0000550 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
551 SDUse &Use = *I++;
552 SDNode *Operand = Use.getNode();
553 Use.set(SDValue());
554
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 // Now that we removed this operand, see if there are no uses of it left.
556 if (Operand->use_empty())
557 DeadNodes.push_back(Operand);
558 }
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000559
Dan Gohman98a355c2009-01-19 22:39:36 +0000560 DeallocateNode(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562}
563
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000564void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmanbd68c792008-07-17 19:10:17 +0000565 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000566 RemoveDeadNodes(DeadNodes, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567}
568
569void SelectionDAG::DeleteNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 // First take this out of the appropriate CSE map.
571 RemoveNodeFromCSEMaps(N);
572
Scott Michel91099d62009-02-17 22:15:04 +0000573 // Finally, remove uses due to operands of this node, remove from the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 // AllNodes list, and delete the node.
575 DeleteNodeNotInCSEMaps(N);
576}
577
578void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
djgf03abfe2009-01-25 16:20:37 +0000579 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
580 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman98a355c2009-01-19 22:39:36 +0000581
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000582 // Drop all of the operands and decrement used node's use counts.
djgc2517d32009-01-26 04:35:06 +0000583 N->DropOperands();
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000584
Dan Gohman98a355c2009-01-19 22:39:36 +0000585 DeallocateNode(N);
586}
587
588void SelectionDAG::DeallocateNode(SDNode *N) {
589 if (N->OperandsNeedDelete)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 delete[] N->OperandList;
Scott Michel91099d62009-02-17 22:15:04 +0000591
Dan Gohman98a355c2009-01-19 22:39:36 +0000592 // Set the opcode to DELETED_NODE to help catch bugs when node
593 // memory is reallocated.
594 N->NodeType = ISD::DELETED_NODE;
595
Dan Gohman0b08b152008-08-26 01:44:34 +0000596 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000597
598 // Remove the ordering of this node.
Bill Wendling0df2cd52010-01-23 10:26:57 +0000599 Ordering->remove(N);
Dale Johannesenf4243fb2010-03-10 22:13:47 +0000600
Evan Chengfd976052010-03-25 01:38:16 +0000601 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
602 SmallVector<SDDbgValue*, 2> &DbgVals = DbgInfo->getSDDbgValues(N);
603 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
604 DbgVals[i]->setIsInvalidated();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605}
606
607/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
608/// correspond to it. This is useful when we're about to delete or repurpose
609/// the node. We don't want future request for structurally identical nodes
610/// to return N anymore.
Dan Gohman705e3f72008-09-13 01:54:27 +0000611bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 bool Erased = false;
613 switch (N->getOpcode()) {
Dan Gohman14a66442008-08-23 02:25:05 +0000614 case ISD::EntryToken:
Edwin Törökbd448e32009-07-14 16:55:14 +0000615 llvm_unreachable("EntryToken should not be in CSEMaps!");
Dan Gohman705e3f72008-09-13 01:54:27 +0000616 return false;
617 case ISD::HANDLENODE: return false; // noop.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 case ISD::CONDCODE:
619 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
620 "Cond code doesn't exist!");
621 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
622 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
623 break;
Bill Wendlingfef06052008-09-16 21:48:12 +0000624 case ISD::ExternalSymbol:
625 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 break;
Chris Lattner8430a292009-06-25 18:45:50 +0000627 case ISD::TargetExternalSymbol: {
628 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
629 Erased = TargetExternalSymbols.erase(
630 std::pair<std::string,unsigned char>(ESN->getSymbol(),
631 ESN->getTargetFlags()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 break;
Chris Lattner8430a292009-06-25 18:45:50 +0000633 }
Duncan Sandsd7307a92007-10-17 13:49:58 +0000634 case ISD::VALUETYPE: {
Owen Andersonac9de032009-08-10 22:56:29 +0000635 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands92c43912008-06-06 12:08:01 +0000636 if (VT.isExtended()) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000637 Erased = ExtendedValueTypeNodes.erase(VT);
638 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000639 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
640 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000641 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000643 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 default:
645 // Remove it from the CSE Map.
646 Erased = CSEMap.RemoveNode(N);
647 break;
648 }
649#ifndef NDEBUG
Scott Michel91099d62009-02-17 22:15:04 +0000650 // Verify that the node was actually in one of the CSE maps, unless it has a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 // flag result (which cannot be CSE'd) or is one of the special cases that are
652 // not subject to CSE.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000653 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands1e450d42008-10-27 15:30:53 +0000654 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 N->dump(this);
David Greened44636d2010-01-05 01:24:36 +0000656 dbgs() << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000657 llvm_unreachable("Node is not in map!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 }
659#endif
Dan Gohman705e3f72008-09-13 01:54:27 +0000660 return Erased;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661}
662
djg943376a2009-01-25 16:29:12 +0000663/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
664/// maps and modified in place. Add it back to the CSE maps, unless an identical
665/// node already exists, in which case transfer all its users to the existing
666/// node. This transfer can potentially trigger recursive merging.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667///
djg943376a2009-01-25 16:29:12 +0000668void
669SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
670 DAGUpdateListener *UpdateListener) {
671 // For node types that aren't CSE'd, just act as if no identical node
672 // already exists.
673 if (!doNotCSE(N)) {
674 SDNode *Existing = CSEMap.GetOrInsertNode(N);
675 if (Existing != N) {
676 // If there was already an existing matching node, use ReplaceAllUsesWith
677 // to replace the dead one with the existing one. This can cause
678 // recursive merging of other unrelated nodes down the line.
679 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Chengd6f57682008-07-08 20:06:39 +0000680
djg943376a2009-01-25 16:29:12 +0000681 // N is now dead. Inform the listener if it exists and delete it.
Scott Michel91099d62009-02-17 22:15:04 +0000682 if (UpdateListener)
djg943376a2009-01-25 16:29:12 +0000683 UpdateListener->NodeDeleted(N, Existing);
684 DeleteNodeNotInCSEMaps(N);
685 return;
686 }
687 }
Evan Chengd6f57682008-07-08 20:06:39 +0000688
djg943376a2009-01-25 16:29:12 +0000689 // If the node doesn't already exist, we updated it. Inform a listener if
690 // it exists.
Scott Michel91099d62009-02-17 22:15:04 +0000691 if (UpdateListener)
djg943376a2009-01-25 16:29:12 +0000692 UpdateListener->NodeUpdated(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693}
694
695/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michel91099d62009-02-17 22:15:04 +0000696/// were replaced with those specified. If this node is never memoized,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697/// return null, otherwise return a pointer to the slot it would take. If a
698/// node already exists with these operands, the slot will be non-null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000699SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000701 if (doNotCSE(N))
702 return 0;
Evan Chengd6f57682008-07-08 20:06:39 +0000703
Dan Gohman8181bd12008-07-27 21:46:04 +0000704 SDValue Ops[] = { Op };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 FoldingSetNodeID ID;
706 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands1e450d42008-10-27 15:30:53 +0000707 AddNodeIDCustom(ID, N);
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000708 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000709 return Node;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710}
711
712/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michel91099d62009-02-17 22:15:04 +0000713/// were replaced with those specified. If this node is never memoized,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714/// return null, otherwise return a pointer to the slot it would take. If a
715/// node already exists with these operands, the slot will be non-null.
Scott Michel91099d62009-02-17 22:15:04 +0000716SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000717 SDValue Op1, SDValue Op2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000719 if (doNotCSE(N))
720 return 0;
721
Dan Gohman8181bd12008-07-27 21:46:04 +0000722 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 FoldingSetNodeID ID;
724 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands1e450d42008-10-27 15:30:53 +0000725 AddNodeIDCustom(ID, N);
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000726 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000727 return Node;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728}
729
730
731/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michel91099d62009-02-17 22:15:04 +0000732/// were replaced with those specified. If this node is never memoized,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733/// return null, otherwise return a pointer to the slot it would take. If a
734/// node already exists with these operands, the slot will be non-null.
Scott Michel91099d62009-02-17 22:15:04 +0000735SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000736 const SDValue *Ops,unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000738 if (doNotCSE(N))
739 return 0;
Evan Chengd6f57682008-07-08 20:06:39 +0000740
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 FoldingSetNodeID ID;
742 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands1e450d42008-10-27 15:30:53 +0000743 AddNodeIDCustom(ID, N);
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000744 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000745 return Node;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746}
747
Duncan Sandsd3ace282008-07-21 10:20:31 +0000748/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
749void SelectionDAG::VerifyNode(SDNode *N) {
750 switch (N->getOpcode()) {
751 default:
752 break;
Duncan Sands70269a72008-10-29 14:22:20 +0000753 case ISD::BUILD_PAIR: {
Owen Andersonac9de032009-08-10 22:56:29 +0000754 EVT VT = N->getValueType(0);
Duncan Sands70269a72008-10-29 14:22:20 +0000755 assert(N->getNumValues() == 1 && "Too many results!");
756 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
757 "Wrong return type!");
758 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
759 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
760 "Mismatched operand types!");
761 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
762 "Wrong operand type!");
763 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
764 "Wrong return type size");
765 break;
766 }
Duncan Sandsd3ace282008-07-21 10:20:31 +0000767 case ISD::BUILD_VECTOR: {
Duncan Sands70269a72008-10-29 14:22:20 +0000768 assert(N->getNumValues() == 1 && "Too many results!");
769 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd3ace282008-07-21 10:20:31 +0000770 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands70269a72008-10-29 14:22:20 +0000771 "Wrong number of operands!");
Owen Andersonac9de032009-08-10 22:56:29 +0000772 EVT EltVT = N->getValueType(0).getVectorElementType();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +0000773 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
774 assert((I->getValueType() == EltVT ||
Nate Begeman543d2142009-04-27 18:41:29 +0000775 (EltVT.isInteger() && I->getValueType().isInteger() &&
776 EltVT.bitsLE(I->getValueType()))) &&
777 "Wrong operand type!");
Duncan Sandsd3ace282008-07-21 10:20:31 +0000778 break;
779 }
780 }
781}
782
Owen Andersonac9de032009-08-10 22:56:29 +0000783/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9e3a3422008-07-08 23:46:32 +0000784/// given type.
785///
Owen Andersonac9de032009-08-10 22:56:29 +0000786unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000787 const Type *Ty = VT == MVT::iPTR ?
Owen Anderson35b47072009-08-13 21:58:54 +0000788 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson77f4eb52009-08-12 00:36:31 +0000789 VT.getTypeForEVT(*getContext());
Dan Gohman9e3a3422008-07-08 23:46:32 +0000790
791 return TLI.getTargetData()->getABITypeAlignment(Ty);
792}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793
Dale Johannesenf89c5ca2009-02-07 02:15:05 +0000794// EntryNode could meaningfully have debug info if we can find it...
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000795SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
Chris Lattner3c00f892010-04-05 02:23:33 +0000796 : TLI(tli), FLI(fli),
Chris Lattner65657ae2010-04-02 20:17:23 +0000797 EntryNode(ISD::EntryToken, DebugLoc(), getVTList(MVT::Other)),
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000798 Root(getEntryNode()), Ordering(0) {
Dan Gohman14a66442008-08-23 02:25:05 +0000799 AllNodes.push_back(&EntryNode);
Bill Wendling0df2cd52010-01-23 10:26:57 +0000800 Ordering = new SDNodeOrdering();
Dale Johannesenf4243fb2010-03-10 22:13:47 +0000801 DbgInfo = new SDDbgInfo();
Dan Gohman03405562008-08-23 00:50:30 +0000802}
803
Chris Lattnerd3d234d2010-04-05 06:10:13 +0000804void SelectionDAG::init(MachineFunction &mf) {
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000805 MF = &mf;
Eric Christopher440d9eb2009-08-22 00:40:45 +0000806 Context = &mf.getFunction()->getContext();
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000807}
808
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809SelectionDAG::~SelectionDAG() {
Dan Gohman14a66442008-08-23 02:25:05 +0000810 allnodes_clear();
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +0000811 delete Ordering;
Evan Chengfd976052010-03-25 01:38:16 +0000812 DbgInfo->clear();
Dale Johannesenf4243fb2010-03-10 22:13:47 +0000813 delete DbgInfo;
Dan Gohman14a66442008-08-23 02:25:05 +0000814}
815
816void SelectionDAG::allnodes_clear() {
Dan Gohman0b08b152008-08-26 01:44:34 +0000817 assert(&*AllNodes.begin() == &EntryNode);
818 AllNodes.remove(AllNodes.begin());
Dan Gohman98a355c2009-01-19 22:39:36 +0000819 while (!AllNodes.empty())
820 DeallocateNode(AllNodes.begin());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821}
822
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000823void SelectionDAG::clear() {
Dan Gohman14a66442008-08-23 02:25:05 +0000824 allnodes_clear();
825 OperandAllocator.Reset();
826 CSEMap.clear();
827
828 ExtendedValueTypeNodes.clear();
Bill Wendlingfef06052008-09-16 21:48:12 +0000829 ExternalSymbols.clear();
830 TargetExternalSymbols.clear();
Dan Gohman14a66442008-08-23 02:25:05 +0000831 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
832 static_cast<CondCodeSDNode*>(0));
833 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
834 static_cast<SDNode*>(0));
835
djgc2517d32009-01-26 04:35:06 +0000836 EntryNode.UseList = 0;
Dan Gohman14a66442008-08-23 02:25:05 +0000837 AllNodes.push_back(&EntryNode);
838 Root = getEntryNode();
Evan Cheng7e80b272010-02-15 23:16:53 +0000839 delete Ordering;
Bill Wendling0df2cd52010-01-23 10:26:57 +0000840 Ordering = new SDNodeOrdering();
Evan Chenge3922422010-03-29 20:48:30 +0000841 DbgInfo->clear();
Dale Johannesenf4243fb2010-03-10 22:13:47 +0000842 delete DbgInfo;
843 DbgInfo = new SDDbgInfo();
Dan Gohman14a66442008-08-23 02:25:05 +0000844}
845
Duncan Sandsdd7bd932009-10-13 21:04:12 +0000846SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
847 return VT.bitsGT(Op.getValueType()) ?
848 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
849 getNode(ISD::TRUNCATE, DL, VT, Op);
850}
851
852SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
853 return VT.bitsGT(Op.getValueType()) ?
854 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
855 getNode(ISD::TRUNCATE, DL, VT, Op);
856}
857
Owen Andersonac9de032009-08-10 22:56:29 +0000858SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Dan Gohman9d501bd2009-12-11 21:31:27 +0000859 assert(!VT.isVector() &&
860 "getZeroExtendInReg should use the vector element type instead of "
861 "the vector type!");
Bill Wendling085b2072009-01-30 22:23:15 +0000862 if (Op.getValueType() == VT) return Op;
Dan Gohman9d501bd2009-12-11 21:31:27 +0000863 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
864 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling085b2072009-01-30 22:23:15 +0000865 VT.getSizeInBits());
866 return getNode(ISD::AND, DL, Op.getValueType(), Op,
867 getConstant(Imm, Op.getValueType()));
868}
869
Bob Wilson81a42cf2009-01-22 17:39:32 +0000870/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
871///
Owen Andersonac9de032009-08-10 22:56:29 +0000872SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
Chris Lattnerae64c542010-02-24 22:44:06 +0000873 EVT EltVT = VT.getScalarType();
Dan Gohman0b8972d2009-04-20 22:51:43 +0000874 SDValue NegOne =
875 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling4b8dd442009-01-30 22:11:22 +0000876 return getNode(ISD::XOR, DL, VT, Val, NegOne);
877}
878
Owen Andersonac9de032009-08-10 22:56:29 +0000879SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattnerae64c542010-02-24 22:44:06 +0000880 EVT EltVT = VT.getScalarType();
djg51bef2e2009-01-27 20:39:34 +0000881 assert((EltVT.getSizeInBits() >= 64 ||
882 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
883 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands92c43912008-06-06 12:08:01 +0000884 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohmandc458cf2008-02-08 22:59:30 +0000885}
886
Owen Andersonac9de032009-08-10 22:56:29 +0000887SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000888 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000889}
890
Owen Andersonac9de032009-08-10 22:56:29 +0000891SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000892 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000893
Chris Lattnerae64c542010-02-24 22:44:06 +0000894 EVT EltVT = VT.getScalarType();
Duncan Sands92c43912008-06-06 12:08:01 +0000895 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohmandc458cf2008-02-08 22:59:30 +0000896 "APInt size does not match type size!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897
898 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
899 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000900 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000901 ID.AddPointer(&Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000903 SDNode *N = NULL;
Bill Wendling339b1532009-12-18 23:32:53 +0000904 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000905 if (!VT.isVector())
Dan Gohman8181bd12008-07-27 21:46:04 +0000906 return SDValue(N, 0);
Bill Wendling339b1532009-12-18 23:32:53 +0000907
Dan Gohman5b9d6412007-12-12 22:21:26 +0000908 if (!N) {
Dan Gohman7616a1e2010-03-18 18:49:47 +0000909 N = new (NodeAllocator) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000910 CSEMap.InsertNode(N, IP);
911 AllNodes.push_back(N);
912 }
913
Dan Gohman8181bd12008-07-27 21:46:04 +0000914 SDValue Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000915 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000916 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000917 Ops.assign(VT.getVectorNumElements(), Result);
Chris Lattner65657ae2010-04-02 20:17:23 +0000918 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman5b9d6412007-12-12 22:21:26 +0000919 }
920 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921}
922
Dan Gohman8181bd12008-07-27 21:46:04 +0000923SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner5872a362008-01-17 07:00:52 +0000924 return getConstant(Val, TLI.getPointerTy(), isTarget);
925}
926
927
Owen Andersonac9de032009-08-10 22:56:29 +0000928SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Andersond363a0e2009-07-27 20:59:43 +0000929 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000930}
931
Owen Andersonac9de032009-08-10 22:56:29 +0000932SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands92c43912008-06-06 12:08:01 +0000933 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michel91099d62009-02-17 22:15:04 +0000934
Chris Lattnerae64c542010-02-24 22:44:06 +0000935 EVT EltVT = VT.getScalarType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936
937 // Do the map lookup using the actual bit pattern for the floating point
938 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
939 // we don't have issues with SNANs.
940 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
941 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000942 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000943 ID.AddPointer(&V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 void *IP = 0;
945 SDNode *N = NULL;
Bill Wendling339b1532009-12-18 23:32:53 +0000946 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000947 if (!VT.isVector())
Dan Gohman8181bd12008-07-27 21:46:04 +0000948 return SDValue(N, 0);
Bill Wendling339b1532009-12-18 23:32:53 +0000949
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950 if (!N) {
Dan Gohman7616a1e2010-03-18 18:49:47 +0000951 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 CSEMap.InsertNode(N, IP);
953 AllNodes.push_back(N);
954 }
955
Dan Gohman8181bd12008-07-27 21:46:04 +0000956 SDValue Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000957 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000958 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000959 Ops.assign(VT.getVectorNumElements(), Result);
Evan Cheng907a2d22009-02-25 22:49:59 +0000960 // FIXME DebugLoc info might be appropriate here
Chris Lattner65657ae2010-04-02 20:17:23 +0000961 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000962 }
963 return Result;
964}
965
Owen Andersonac9de032009-08-10 22:56:29 +0000966SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerae64c542010-02-24 22:44:06 +0000967 EVT EltVT = VT.getScalarType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000968 if (EltVT==MVT::f32)
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000969 return getConstantFP(APFloat((float)Val), VT, isTarget);
970 else
971 return getConstantFP(APFloat(Val), VT, isTarget);
972}
973
Dan Gohman8181bd12008-07-27 21:46:04 +0000974SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Owen Andersonac9de032009-08-10 22:56:29 +0000975 EVT VT, int64_t Offset,
Chris Lattner7123ef72009-06-25 21:21:14 +0000976 bool isTargetGA,
977 unsigned char TargetFlags) {
978 assert((TargetFlags == 0 || isTargetGA) &&
979 "Cannot set target flags on target-independent globals");
Eric Christopher440d9eb2009-08-22 00:40:45 +0000980
Dan Gohman36322c72008-10-18 02:06:02 +0000981 // Truncate (with sign-extension) the offset value to the pointer size.
Owen Andersonac9de032009-08-10 22:56:29 +0000982 EVT PTy = TLI.getPointerTy();
Owen Anderson27bfb672009-08-10 18:56:59 +0000983 unsigned BitWidth = PTy.getSizeInBits();
Dan Gohman36322c72008-10-18 02:06:02 +0000984 if (BitWidth < 64)
985 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
986
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000987 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
988 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000989 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000990 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000991 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000992 }
993
Chris Lattner7123ef72009-06-25 21:21:14 +0000994 unsigned Opc;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 if (GVar && GVar->isThreadLocal())
996 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
997 else
998 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000999
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001001 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 ID.AddPointer(GV);
1003 ID.AddInteger(Offset);
Chris Lattner7123ef72009-06-25 21:21:14 +00001004 ID.AddInteger(TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001006 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
djg08f48f52009-01-25 16:21:38 +00001007 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001008
Dan Gohman7616a1e2010-03-18 18:49:47 +00001009 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, GV, VT,
1010 Offset, TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 CSEMap.InsertNode(N, IP);
1012 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001013 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014}
1015
Owen Andersonac9de032009-08-10 22:56:29 +00001016SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1018 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001019 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 ID.AddInteger(FI);
1021 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001022 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001023 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001024
Dan Gohman7616a1e2010-03-18 18:49:47 +00001025 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 CSEMap.InsertNode(N, IP);
1027 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001028 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029}
1030
Owen Andersonac9de032009-08-10 22:56:29 +00001031SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattner583c7962009-06-25 21:35:31 +00001032 unsigned char TargetFlags) {
1033 assert((TargetFlags == 0 || isTarget) &&
1034 "Cannot set target flags on target-independent jump tables");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1036 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001037 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 ID.AddInteger(JTI);
Chris Lattner583c7962009-06-25 21:35:31 +00001039 ID.AddInteger(TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001041 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001042 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001043
Dan Gohman7616a1e2010-03-18 18:49:47 +00001044 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1045 TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 CSEMap.InsertNode(N, IP);
1047 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001048 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049}
1050
Owen Andersonac9de032009-08-10 22:56:29 +00001051SDValue SelectionDAG::getConstantPool(Constant *C, EVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00001052 unsigned Alignment, int Offset,
Eric Christopher440d9eb2009-08-22 00:40:45 +00001053 bool isTarget,
Chris Lattner583c7962009-06-25 21:35:31 +00001054 unsigned char TargetFlags) {
1055 assert((TargetFlags == 0 || isTarget) &&
1056 "Cannot set target flags on target-independent globals");
Dan Gohman04637d12008-09-16 22:05:41 +00001057 if (Alignment == 0)
Evan Cheng68c18682009-03-13 07:51:59 +00001058 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1060 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001061 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 ID.AddInteger(Alignment);
1063 ID.AddInteger(Offset);
1064 ID.AddPointer(C);
Chris Lattner583c7962009-06-25 21:35:31 +00001065 ID.AddInteger(TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001067 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001068 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001069
Dan Gohman7616a1e2010-03-18 18:49:47 +00001070 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1071 Alignment, TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 CSEMap.InsertNode(N, IP);
1073 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001074 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075}
1076
1077
Owen Andersonac9de032009-08-10 22:56:29 +00001078SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00001079 unsigned Alignment, int Offset,
Chris Lattner583c7962009-06-25 21:35:31 +00001080 bool isTarget,
1081 unsigned char TargetFlags) {
1082 assert((TargetFlags == 0 || isTarget) &&
1083 "Cannot set target flags on target-independent globals");
Dan Gohman04637d12008-09-16 22:05:41 +00001084 if (Alignment == 0)
Evan Cheng68c18682009-03-13 07:51:59 +00001085 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1087 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001088 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 ID.AddInteger(Alignment);
1090 ID.AddInteger(Offset);
1091 C->AddSelectionDAGCSEId(ID);
Chris Lattner583c7962009-06-25 21:35:31 +00001092 ID.AddInteger(TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001094 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001095 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001096
Dan Gohman7616a1e2010-03-18 18:49:47 +00001097 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1098 Alignment, TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 CSEMap.InsertNode(N, IP);
1100 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001101 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102}
1103
Dan Gohman8181bd12008-07-27 21:46:04 +00001104SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001105 FoldingSetNodeID ID;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001106 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001107 ID.AddPointer(MBB);
1108 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001109 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001110 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001111
Dan Gohman7616a1e2010-03-18 18:49:47 +00001112 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113 CSEMap.InsertNode(N, IP);
1114 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001115 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116}
1117
Owen Andersonac9de032009-08-10 22:56:29 +00001118SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001119 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1120 ValueTypeNodes.size())
1121 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122
Duncan Sands92c43912008-06-06 12:08:01 +00001123 SDNode *&N = VT.isExtended() ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001124 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd7307a92007-10-17 13:49:58 +00001125
Dan Gohman8181bd12008-07-27 21:46:04 +00001126 if (N) return SDValue(N, 0);
Dan Gohman7616a1e2010-03-18 18:49:47 +00001127 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd7307a92007-10-17 13:49:58 +00001128 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001129 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130}
1131
Owen Andersonac9de032009-08-10 22:56:29 +00001132SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendlingfef06052008-09-16 21:48:12 +00001133 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman8181bd12008-07-27 21:46:04 +00001134 if (N) return SDValue(N, 0);
Dan Gohman7616a1e2010-03-18 18:49:47 +00001135 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001137 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138}
1139
Owen Andersonac9de032009-08-10 22:56:29 +00001140SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner8430a292009-06-25 18:45:50 +00001141 unsigned char TargetFlags) {
1142 SDNode *&N =
1143 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1144 TargetFlags)];
Dan Gohman8181bd12008-07-27 21:46:04 +00001145 if (N) return SDValue(N, 0);
Dan Gohman7616a1e2010-03-18 18:49:47 +00001146 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001147 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001148 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001149}
1150
Dan Gohman8181bd12008-07-27 21:46:04 +00001151SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 if ((unsigned)Cond >= CondCodeNodes.size())
1153 CondCodeNodes.resize(Cond+1);
Duncan Sands92c43912008-06-06 12:08:01 +00001154
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155 if (CondCodeNodes[Cond] == 0) {
Dan Gohman7616a1e2010-03-18 18:49:47 +00001156 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohmaned825d12008-07-07 23:02:41 +00001157 CondCodeNodes[Cond] = N;
1158 AllNodes.push_back(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159 }
Bill Wendling339b1532009-12-18 23:32:53 +00001160
Dan Gohman8181bd12008-07-27 21:46:04 +00001161 return SDValue(CondCodeNodes[Cond], 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001162}
1163
Nate Begemane8f61cb2009-04-29 05:20:52 +00001164// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1165// the shuffle mask M that point at N1 to point at N2, and indices that point
1166// N2 to point at N1.
Nate Begeman543d2142009-04-27 18:41:29 +00001167static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1168 std::swap(N1, N2);
1169 int NElts = M.size();
1170 for (int i = 0; i != NElts; ++i) {
1171 if (M[i] >= NElts)
1172 M[i] -= NElts;
1173 else if (M[i] >= 0)
1174 M[i] += NElts;
1175 }
1176}
1177
Eric Christopher440d9eb2009-08-22 00:40:45 +00001178SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman543d2142009-04-27 18:41:29 +00001179 SDValue N2, const int *Mask) {
1180 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher440d9eb2009-08-22 00:40:45 +00001181 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman543d2142009-04-27 18:41:29 +00001182 "Vector Shuffle VTs must be a vectors");
1183 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1184 && "Vector Shuffle VTs must have same element type");
1185
1186 // Canonicalize shuffle undef, undef -> undef
1187 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman76a3e1c2009-07-09 00:46:33 +00001188 return getUNDEF(VT);
Nate Begeman543d2142009-04-27 18:41:29 +00001189
Eric Christopher440d9eb2009-08-22 00:40:45 +00001190 // Validate that all indices in Mask are within the range of the elements
Nate Begeman543d2142009-04-27 18:41:29 +00001191 // input to the shuffle.
Nate Begemane8f61cb2009-04-29 05:20:52 +00001192 unsigned NElts = VT.getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +00001193 SmallVector<int, 8> MaskVec;
Nate Begemane8f61cb2009-04-29 05:20:52 +00001194 for (unsigned i = 0; i != NElts; ++i) {
1195 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman543d2142009-04-27 18:41:29 +00001196 MaskVec.push_back(Mask[i]);
1197 }
Eric Christopher440d9eb2009-08-22 00:40:45 +00001198
Nate Begeman543d2142009-04-27 18:41:29 +00001199 // Canonicalize shuffle v, v -> v, undef
1200 if (N1 == N2) {
1201 N2 = getUNDEF(VT);
Nate Begemane8f61cb2009-04-29 05:20:52 +00001202 for (unsigned i = 0; i != NElts; ++i)
1203 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman543d2142009-04-27 18:41:29 +00001204 }
Eric Christopher440d9eb2009-08-22 00:40:45 +00001205
Nate Begeman543d2142009-04-27 18:41:29 +00001206 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1207 if (N1.getOpcode() == ISD::UNDEF)
1208 commuteShuffle(N1, N2, MaskVec);
Eric Christopher440d9eb2009-08-22 00:40:45 +00001209
Nate Begeman543d2142009-04-27 18:41:29 +00001210 // Canonicalize all index into lhs, -> shuffle lhs, undef
1211 // Canonicalize all index into rhs, -> shuffle rhs, undef
1212 bool AllLHS = true, AllRHS = true;
1213 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begemane8f61cb2009-04-29 05:20:52 +00001214 for (unsigned i = 0; i != NElts; ++i) {
1215 if (MaskVec[i] >= (int)NElts) {
Nate Begeman543d2142009-04-27 18:41:29 +00001216 if (N2Undef)
1217 MaskVec[i] = -1;
1218 else
1219 AllLHS = false;
1220 } else if (MaskVec[i] >= 0) {
1221 AllRHS = false;
1222 }
1223 }
1224 if (AllLHS && AllRHS)
1225 return getUNDEF(VT);
Nate Begemane8f61cb2009-04-29 05:20:52 +00001226 if (AllLHS && !N2Undef)
Nate Begeman543d2142009-04-27 18:41:29 +00001227 N2 = getUNDEF(VT);
1228 if (AllRHS) {
1229 N1 = getUNDEF(VT);
1230 commuteShuffle(N1, N2, MaskVec);
1231 }
Eric Christopher440d9eb2009-08-22 00:40:45 +00001232
Nate Begeman543d2142009-04-27 18:41:29 +00001233 // If Identity shuffle, or all shuffle in to undef, return that node.
1234 bool AllUndef = true;
1235 bool Identity = true;
Nate Begemane8f61cb2009-04-29 05:20:52 +00001236 for (unsigned i = 0; i != NElts; ++i) {
1237 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman543d2142009-04-27 18:41:29 +00001238 if (MaskVec[i] >= 0) AllUndef = false;
1239 }
Dan Gohman76a3e1c2009-07-09 00:46:33 +00001240 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman543d2142009-04-27 18:41:29 +00001241 return N1;
1242 if (AllUndef)
1243 return getUNDEF(VT);
1244
1245 FoldingSetNodeID ID;
1246 SDValue Ops[2] = { N1, N2 };
1247 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begemane8f61cb2009-04-29 05:20:52 +00001248 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00001249 ID.AddInteger(MaskVec[i]);
Eric Christopher440d9eb2009-08-22 00:40:45 +00001250
Nate Begeman543d2142009-04-27 18:41:29 +00001251 void* IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001252 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman543d2142009-04-27 18:41:29 +00001253 return SDValue(E, 0);
Eric Christopher440d9eb2009-08-22 00:40:45 +00001254
Nate Begeman543d2142009-04-27 18:41:29 +00001255 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1256 // SDNode doesn't have access to it. This memory will be "leaked" when
1257 // the node is deallocated, but recovered when the NodeAllocator is released.
1258 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1259 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher440d9eb2009-08-22 00:40:45 +00001260
Dan Gohman7616a1e2010-03-18 18:49:47 +00001261 ShuffleVectorSDNode *N =
1262 new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
Nate Begeman543d2142009-04-27 18:41:29 +00001263 CSEMap.InsertNode(N, IP);
1264 AllNodes.push_back(N);
1265 return SDValue(N, 0);
1266}
1267
Owen Andersonac9de032009-08-10 22:56:29 +00001268SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001269 SDValue Val, SDValue DTy,
1270 SDValue STy, SDValue Rnd, SDValue Sat,
1271 ISD::CvtCode Code) {
Mon P Wang8a443d72009-02-05 04:47:42 +00001272 // If the src and dest types are the same and the conversion is between
1273 // integer types of the same sign or two floats, no conversion is necessary.
1274 if (DTy == STy &&
1275 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001276 return Val;
1277
1278 FoldingSetNodeID ID;
Mon P Wange6f60f12009-11-07 04:46:25 +00001279 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1280 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001281 void* IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001282 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001283 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001284
Dan Gohman7616a1e2010-03-18 18:49:47 +00001285 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
1286 Code);
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001287 CSEMap.InsertNode(N, IP);
1288 AllNodes.push_back(N);
1289 return SDValue(N, 0);
1290}
1291
Owen Andersonac9de032009-08-10 22:56:29 +00001292SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001293 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001294 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 ID.AddInteger(RegNo);
1296 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001297 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001298 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001299
Dan Gohman7616a1e2010-03-18 18:49:47 +00001300 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 CSEMap.InsertNode(N, IP);
1302 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001303 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304}
1305
Chris Lattner8574ddd2010-03-14 02:33:54 +00001306SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001307 FoldingSetNodeID ID;
1308 SDValue Ops[] = { Root };
Chris Lattner8574ddd2010-03-14 02:33:54 +00001309 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1310 ID.AddPointer(Label);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001311 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001312 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001313 return SDValue(E, 0);
Chris Lattner8574ddd2010-03-14 02:33:54 +00001314
Dan Gohman7616a1e2010-03-18 18:49:47 +00001315 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001316 CSEMap.InsertNode(N, IP);
1317 AllNodes.push_back(N);
1318 return SDValue(N, 0);
1319}
1320
Chris Lattner8574ddd2010-03-14 02:33:54 +00001321
Dan Gohman885793b2009-11-20 23:18:13 +00001322SDValue SelectionDAG::getBlockAddress(BlockAddress *BA, EVT VT,
1323 bool isTarget,
1324 unsigned char TargetFlags) {
Dan Gohman91057512009-10-30 01:27:03 +00001325 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1326
1327 FoldingSetNodeID ID;
Dan Gohman885793b2009-11-20 23:18:13 +00001328 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman91057512009-10-30 01:27:03 +00001329 ID.AddPointer(BA);
Dan Gohman885793b2009-11-20 23:18:13 +00001330 ID.AddInteger(TargetFlags);
Dan Gohman91057512009-10-30 01:27:03 +00001331 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001332 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman91057512009-10-30 01:27:03 +00001333 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00001334
Dan Gohman7616a1e2010-03-18 18:49:47 +00001335 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, TargetFlags);
Dan Gohman91057512009-10-30 01:27:03 +00001336 CSEMap.InsertNode(N, IP);
1337 AllNodes.push_back(N);
1338 return SDValue(N, 0);
1339}
1340
Dan Gohman8181bd12008-07-27 21:46:04 +00001341SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands10343d92010-02-16 11:11:14 +00001342 assert((!V || V->getType()->isPointerTy()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343 "SrcValue is not a pointer?");
1344
1345 FoldingSetNodeID ID;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001346 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 ID.AddPointer(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00001350 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001351 return SDValue(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001352
Dan Gohman7616a1e2010-03-18 18:49:47 +00001353 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001354 CSEMap.InsertNode(N, IP);
1355 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001356 return SDValue(N, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001357}
1358
Duncan Sands7d9e3612009-01-31 15:50:11 +00001359/// getShiftAmountOperand - Return the specified value casted to
1360/// the target's desired shift amount type.
1361SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
Owen Andersonac9de032009-08-10 22:56:29 +00001362 EVT OpTy = Op.getValueType();
Owen Anderson2dd68a22009-08-11 21:59:30 +00001363 MVT ShTy = TLI.getShiftAmountTy();
Duncan Sands7d9e3612009-01-31 15:50:11 +00001364 if (OpTy == ShTy || OpTy.isVector()) return Op;
1365
1366 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesen24dd9a52009-02-07 00:55:49 +00001367 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands7d9e3612009-01-31 15:50:11 +00001368}
1369
Chris Lattner53f5aee2007-10-15 17:47:20 +00001370/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1371/// specified value type.
Owen Andersonac9de032009-08-10 22:56:29 +00001372SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner53f5aee2007-10-15 17:47:20 +00001373 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohmanad488032009-09-23 21:07:02 +00001374 unsigned ByteSize = VT.getStoreSize();
Owen Anderson77f4eb52009-08-12 00:36:31 +00001375 const Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang55854cc2008-07-05 20:40:31 +00001376 unsigned StackAlign =
1377 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
Scott Michel91099d62009-02-17 22:15:04 +00001378
David Greene6424ab92009-11-12 20:49:22 +00001379 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner53f5aee2007-10-15 17:47:20 +00001380 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1381}
1382
Duncan Sands871e55f2008-12-09 21:33:20 +00001383/// CreateStackTemporary - Create a stack temporary suitable for holding
1384/// either of the specified value types.
Owen Andersonac9de032009-08-10 22:56:29 +00001385SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands871e55f2008-12-09 21:33:20 +00001386 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1387 VT2.getStoreSizeInBits())/8;
Owen Anderson77f4eb52009-08-12 00:36:31 +00001388 const Type *Ty1 = VT1.getTypeForEVT(*getContext());
1389 const Type *Ty2 = VT2.getTypeForEVT(*getContext());
Duncan Sands871e55f2008-12-09 21:33:20 +00001390 const TargetData *TD = TLI.getTargetData();
1391 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1392 TD->getPrefTypeAlignment(Ty2));
1393
1394 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene6424ab92009-11-12 20:49:22 +00001395 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands871e55f2008-12-09 21:33:20 +00001396 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1397}
1398
Owen Andersonac9de032009-08-10 22:56:29 +00001399SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesen38496eb2009-02-03 00:47:48 +00001400 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 // These setcc operations always fold.
1402 switch (Cond) {
1403 default: break;
1404 case ISD::SETFALSE:
1405 case ISD::SETFALSE2: return getConstant(0, VT);
1406 case ISD::SETTRUE:
1407 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 case ISD::SETOEQ:
1410 case ISD::SETOGT:
1411 case ISD::SETOGE:
1412 case ISD::SETOLT:
1413 case ISD::SETOLE:
1414 case ISD::SETONE:
1415 case ISD::SETO:
1416 case ISD::SETUO:
1417 case ISD::SETUEQ:
1418 case ISD::SETUNE:
Duncan Sands92c43912008-06-06 12:08:01 +00001419 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 break;
1421 }
Scott Michel91099d62009-02-17 22:15:04 +00001422
Gabor Greif1c80d112008-08-28 21:40:38 +00001423 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00001424 const APInt &C2 = N2C->getAPIntValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00001425 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00001426 const APInt &C1 = N1C->getAPIntValue();
Scott Michel91099d62009-02-17 22:15:04 +00001427
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428 switch (Cond) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001429 default: llvm_unreachable("Unknown integer setcc!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1431 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00001432 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1433 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1434 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1435 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1436 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1437 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1438 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1439 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440 }
1441 }
1442 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001443 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1444 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001445 // No compile time operations on this type yet.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001446 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman8181bd12008-07-27 21:46:04 +00001447 return SDValue();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001448
1449 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001451 default: break;
Scott Michel91099d62009-02-17 22:15:04 +00001452 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001453 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001454 // fall through
1455 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001456 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001457 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001458 // fall through
1459 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001460 R==APFloat::cmpLessThan, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001461 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001462 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001463 // fall through
1464 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001465 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001466 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001467 // fall through
1468 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001469 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001470 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001471 // fall through
1472 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001473 R==APFloat::cmpEqual, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001474 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001475 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001476 // fall through
1477 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001478 R==APFloat::cmpEqual, VT);
1479 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1480 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1481 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1482 R==APFloat::cmpEqual, VT);
1483 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1484 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1485 R==APFloat::cmpLessThan, VT);
1486 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1487 R==APFloat::cmpUnordered, VT);
1488 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1489 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490 }
1491 } else {
1492 // Ensure that the constant occurs on the RHS.
Dale Johannesen38496eb2009-02-03 00:47:48 +00001493 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001495 }
1496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 // Could not fold it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001498 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499}
1500
Dan Gohman07961cd2008-02-25 21:11:39 +00001501/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1502/// use this predicate to simplify operations downstream.
Dan Gohman8181bd12008-07-27 21:46:04 +00001503bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerb9e009a2009-07-07 23:28:46 +00001504 // This predicate is not safe for vector operations.
1505 if (Op.getValueType().isVector())
1506 return false;
Eric Christopher440d9eb2009-08-22 00:40:45 +00001507
Dan Gohman9d501bd2009-12-11 21:31:27 +00001508 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman07961cd2008-02-25 21:11:39 +00001509 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1510}
1511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1513/// this predicate to simplify operations downstream. Mask is known to be zero
1514/// for bits that V cannot have.
Scott Michel91099d62009-02-17 22:15:04 +00001515bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001517 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michel91099d62009-02-17 22:15:04 +00001519 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 return (KnownZero & Mask) == Mask;
1521}
1522
1523/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1524/// known to be either zero or one and return them in the KnownZero/KnownOne
1525/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1526/// processing.
Scott Michel91099d62009-02-17 22:15:04 +00001527void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00001528 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 unsigned Depth) const {
Dan Gohman229fa052008-02-13 00:35:47 +00001530 unsigned BitWidth = Mask.getBitWidth();
Dan Gohman9d501bd2009-12-11 21:31:27 +00001531 assert(BitWidth == Op.getValueType().getScalarType().getSizeInBits() &&
Dan Gohman56eaab32008-02-13 23:13:32 +00001532 "Mask size mismatches value type size!");
1533
Dan Gohman229fa052008-02-13 00:35:47 +00001534 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535 if (Depth == 6 || Mask == 0)
1536 return; // Limit search depth.
Scott Michel91099d62009-02-17 22:15:04 +00001537
Dan Gohman229fa052008-02-13 00:35:47 +00001538 APInt KnownZero2, KnownOne2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001539
1540 switch (Op.getOpcode()) {
1541 case ISD::Constant:
1542 // We know all of the bits for a constant!
Dan Gohman229fa052008-02-13 00:35:47 +00001543 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001544 KnownZero = ~KnownOne & Mask;
1545 return;
1546 case ISD::AND:
1547 // If either the LHS or the RHS are Zero, the result is zero.
1548 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001549 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1550 KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001551 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1552 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553
1554 // Output known-1 bits are only known if set in both the LHS & RHS.
1555 KnownOne &= KnownOne2;
1556 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1557 KnownZero |= KnownZero2;
1558 return;
1559 case ISD::OR:
1560 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001561 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1562 KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001563 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1564 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1565
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 // Output known-0 bits are only known if clear in both the LHS & RHS.
1567 KnownZero &= KnownZero2;
1568 // Output known-1 are known to be set if set in either the LHS | RHS.
1569 KnownOne |= KnownOne2;
1570 return;
1571 case ISD::XOR: {
1572 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1573 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001574 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1575 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1576
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001577 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohman229fa052008-02-13 00:35:47 +00001578 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1580 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1581 KnownZero = KnownZeroOut;
1582 return;
1583 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001584 case ISD::MUL: {
1585 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1586 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1587 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1588 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1589 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1590
1591 // If low bits are zero in either operand, output low known-0 bits.
1592 // Also compute a conserative estimate for high known-0 bits.
1593 // More trickiness is possible, but this is sufficient for the
1594 // interesting case of alignment computation.
1595 KnownOne.clear();
1596 unsigned TrailZ = KnownZero.countTrailingOnes() +
1597 KnownZero2.countTrailingOnes();
1598 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman4c451852008-05-07 00:35:55 +00001599 KnownZero2.countLeadingOnes(),
1600 BitWidth) - BitWidth;
Dan Gohmanbec16052008-04-28 17:02:21 +00001601
1602 TrailZ = std::min(TrailZ, BitWidth);
1603 LeadZ = std::min(LeadZ, BitWidth);
1604 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1605 APInt::getHighBitsSet(BitWidth, LeadZ);
1606 KnownZero &= Mask;
1607 return;
1608 }
1609 case ISD::UDIV: {
1610 // For the purposes of computing leading zeros we can conservatively
1611 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001612 // be less than the denominator.
Dan Gohmanbec16052008-04-28 17:02:21 +00001613 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1614 ComputeMaskedBits(Op.getOperand(0),
1615 AllOnes, KnownZero2, KnownOne2, Depth+1);
1616 unsigned LeadZ = KnownZero2.countLeadingOnes();
1617
1618 KnownOne2.clear();
1619 KnownZero2.clear();
1620 ComputeMaskedBits(Op.getOperand(1),
1621 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001622 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1623 if (RHSUnknownLeadingOnes != BitWidth)
1624 LeadZ = std::min(BitWidth,
1625 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001626
1627 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1628 return;
1629 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001630 case ISD::SELECT:
1631 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1632 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001633 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1634 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1635
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636 // Only known if known in both the LHS and RHS.
1637 KnownOne &= KnownOne2;
1638 KnownZero &= KnownZero2;
1639 return;
1640 case ISD::SELECT_CC:
1641 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1642 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001643 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1644 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1645
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001646 // Only known if known in both the LHS and RHS.
1647 KnownOne &= KnownOne2;
1648 KnownZero &= KnownZero2;
1649 return;
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001650 case ISD::SADDO:
1651 case ISD::UADDO:
Bill Wendling7e04be62008-12-09 22:08:41 +00001652 case ISD::SSUBO:
1653 case ISD::USUBO:
1654 case ISD::SMULO:
1655 case ISD::UMULO:
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001656 if (Op.getResNo() != 1)
1657 return;
Duncan Sands8cf4a822008-11-23 15:47:28 +00001658 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001659 case ISD::SETCC:
1660 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands8cf4a822008-11-23 15:47:28 +00001661 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohman229fa052008-02-13 00:35:47 +00001662 BitWidth > 1)
1663 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001664 return;
1665 case ISD::SHL:
1666 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1667 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001668 unsigned ShAmt = SA->getZExtValue();
Dan Gohman4b8d0002008-02-26 18:50:50 +00001669
1670 // If the shift count is an invalid immediate, don't do anything.
1671 if (ShAmt >= BitWidth)
1672 return;
1673
1674 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001676 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman4b8d0002008-02-26 18:50:50 +00001677 KnownZero <<= ShAmt;
1678 KnownOne <<= ShAmt;
Dan Gohman229fa052008-02-13 00:35:47 +00001679 // low bits known zero.
Dan Gohman4b8d0002008-02-26 18:50:50 +00001680 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 }
1682 return;
1683 case ISD::SRL:
1684 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1685 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001686 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001687
Dan Gohman4b8d0002008-02-26 18:50:50 +00001688 // If the shift count is an invalid immediate, don't do anything.
1689 if (ShAmt >= BitWidth)
1690 return;
1691
Dan Gohman229fa052008-02-13 00:35:47 +00001692 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001694 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001695 KnownZero = KnownZero.lshr(ShAmt);
1696 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001697
Dan Gohman4d81a742008-02-13 22:43:25 +00001698 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699 KnownZero |= HighBits; // High bits known zero.
1700 }
1701 return;
1702 case ISD::SRA:
1703 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001704 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705
Dan Gohman4b8d0002008-02-26 18:50:50 +00001706 // If the shift count is an invalid immediate, don't do anything.
1707 if (ShAmt >= BitWidth)
1708 return;
1709
Dan Gohman229fa052008-02-13 00:35:47 +00001710 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001711 // If any of the demanded bits are produced by the sign extension, we also
1712 // demand the input sign bit.
Dan Gohman4d81a742008-02-13 22:43:25 +00001713 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1714 if (HighBits.getBoolValue())
Dan Gohman229fa052008-02-13 00:35:47 +00001715 InDemandedMask |= APInt::getSignBit(BitWidth);
Scott Michel91099d62009-02-17 22:15:04 +00001716
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1718 Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001719 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001720 KnownZero = KnownZero.lshr(ShAmt);
1721 KnownOne = KnownOne.lshr(ShAmt);
Scott Michel91099d62009-02-17 22:15:04 +00001722
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001723 // Handle the sign bits.
Dan Gohman229fa052008-02-13 00:35:47 +00001724 APInt SignBit = APInt::getSignBit(BitWidth);
1725 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michel91099d62009-02-17 22:15:04 +00001726
Dan Gohman91507292008-02-20 16:30:17 +00001727 if (KnownZero.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001728 KnownZero |= HighBits; // New bits are known zero.
Dan Gohman91507292008-02-20 16:30:17 +00001729 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730 KnownOne |= HighBits; // New bits are known one.
1731 }
1732 }
1733 return;
1734 case ISD::SIGN_EXTEND_INREG: {
Owen Andersonac9de032009-08-10 22:56:29 +00001735 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman7196cb12010-01-09 02:13:55 +00001736 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00001737
1738 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001739 // present in the input.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001740 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001741
Dan Gohmand0dfc772008-02-13 22:28:48 +00001742 APInt InSignBit = APInt::getSignBit(EBits);
1743 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Scott Michel91099d62009-02-17 22:15:04 +00001744
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001745 // If the sign extended bits are demanded, we know that the sign
1746 // bit is demanded.
Dan Gohman229fa052008-02-13 00:35:47 +00001747 InSignBit.zext(BitWidth);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001748 if (NewBits.getBoolValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001749 InputDemandedBits |= InSignBit;
Scott Michel91099d62009-02-17 22:15:04 +00001750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1752 KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001753 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1754
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 // If the sign bit of the input is known set or clear, then we know the
1756 // top bits of the result.
Dan Gohman91507292008-02-20 16:30:17 +00001757 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758 KnownZero |= NewBits;
1759 KnownOne &= ~NewBits;
Dan Gohman91507292008-02-20 16:30:17 +00001760 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 KnownOne |= NewBits;
1762 KnownZero &= ~NewBits;
1763 } else { // Input sign bit unknown
1764 KnownZero &= ~NewBits;
1765 KnownOne &= ~NewBits;
1766 }
1767 return;
1768 }
1769 case ISD::CTTZ:
1770 case ISD::CTLZ:
1771 case ISD::CTPOP: {
Dan Gohman229fa052008-02-13 00:35:47 +00001772 unsigned LowBits = Log2_32(BitWidth)+1;
1773 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman75e24fb2008-06-21 22:02:15 +00001774 KnownOne.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001775 return;
1776 }
1777 case ISD::LOAD: {
Gabor Greif1c80d112008-08-28 21:40:38 +00001778 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779 LoadSDNode *LD = cast<LoadSDNode>(Op);
Owen Andersonac9de032009-08-10 22:56:29 +00001780 EVT VT = LD->getMemoryVT();
Dan Gohman7196cb12010-01-09 02:13:55 +00001781 unsigned MemBits = VT.getScalarType().getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001782 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001783 }
1784 return;
1785 }
1786 case ISD::ZERO_EXTEND: {
Owen Andersonac9de032009-08-10 22:56:29 +00001787 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman9d501bd2009-12-11 21:31:27 +00001788 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001789 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1790 APInt InMask = Mask;
1791 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001792 KnownZero.trunc(InBits);
1793 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001794 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001795 KnownZero.zext(BitWidth);
1796 KnownOne.zext(BitWidth);
1797 KnownZero |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001798 return;
1799 }
1800 case ISD::SIGN_EXTEND: {
Owen Andersonac9de032009-08-10 22:56:29 +00001801 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman9d501bd2009-12-11 21:31:27 +00001802 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohman229fa052008-02-13 00:35:47 +00001803 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001804 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1805 APInt InMask = Mask;
1806 InMask.trunc(InBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001807
1808 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohmand0dfc772008-02-13 22:28:48 +00001809 // bit is demanded. Temporarily set this bit in the mask for our callee.
1810 if (NewBits.getBoolValue())
1811 InMask |= InSignBit;
Dan Gohman229fa052008-02-13 00:35:47 +00001812
Dan Gohman229fa052008-02-13 00:35:47 +00001813 KnownZero.trunc(InBits);
1814 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001815 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1816
1817 // Note if the sign bit is known to be zero or one.
1818 bool SignBitKnownZero = KnownZero.isNegative();
1819 bool SignBitKnownOne = KnownOne.isNegative();
1820 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1821 "Sign bit can't be known to be both zero and one!");
1822
1823 // If the sign bit wasn't actually demanded by our caller, we don't
1824 // want it set in the KnownZero and KnownOne result values. Reset the
1825 // mask and reapply it to the result values.
1826 InMask = Mask;
1827 InMask.trunc(InBits);
1828 KnownZero &= InMask;
1829 KnownOne &= InMask;
1830
Dan Gohman229fa052008-02-13 00:35:47 +00001831 KnownZero.zext(BitWidth);
1832 KnownOne.zext(BitWidth);
1833
Dan Gohmand0dfc772008-02-13 22:28:48 +00001834 // If the sign bit is known zero or one, the top bits match.
1835 if (SignBitKnownZero)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001836 KnownZero |= NewBits;
Dan Gohmand0dfc772008-02-13 22:28:48 +00001837 else if (SignBitKnownOne)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001838 KnownOne |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001839 return;
1840 }
1841 case ISD::ANY_EXTEND: {
Owen Andersonac9de032009-08-10 22:56:29 +00001842 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman9d501bd2009-12-11 21:31:27 +00001843 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001844 APInt InMask = Mask;
1845 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001846 KnownZero.trunc(InBits);
1847 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001848 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001849 KnownZero.zext(BitWidth);
1850 KnownOne.zext(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001851 return;
1852 }
1853 case ISD::TRUNCATE: {
Owen Andersonac9de032009-08-10 22:56:29 +00001854 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman9d501bd2009-12-11 21:31:27 +00001855 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001856 APInt InMask = Mask;
1857 InMask.zext(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001858 KnownZero.zext(InBits);
1859 KnownOne.zext(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001860 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001861 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001862 KnownZero.trunc(BitWidth);
1863 KnownOne.trunc(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001864 break;
1865 }
1866 case ISD::AssertZext: {
Owen Andersonac9de032009-08-10 22:56:29 +00001867 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands92c43912008-06-06 12:08:01 +00001868 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Scott Michel91099d62009-02-17 22:15:04 +00001869 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001870 KnownOne, Depth+1);
1871 KnownZero |= (~InMask) & Mask;
1872 return;
1873 }
Chris Lattner13f06832007-12-22 21:26:52 +00001874 case ISD::FGETSIGN:
1875 // All bits are zero except the low bit.
Dan Gohman229fa052008-02-13 00:35:47 +00001876 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattner13f06832007-12-22 21:26:52 +00001877 return;
Scott Michel91099d62009-02-17 22:15:04 +00001878
Dan Gohmanbec16052008-04-28 17:02:21 +00001879 case ISD::SUB: {
1880 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1881 // We know that the top bits of C-X are clear if X contains less bits
1882 // than C (i.e. no wrap-around can happen). For example, 20-X is
1883 // positive if we can prove that X is >= 0 and < 16.
1884 if (CLHS->getAPIntValue().isNonNegative()) {
1885 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1886 // NLZ can't be BitWidth with no sign bit
1887 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1888 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1889 Depth+1);
1890
1891 // If all of the MaskV bits are known to be zero, then we know the
1892 // output top bits are zero, because we now know that the output is
1893 // from [0-C].
1894 if ((KnownZero2 & MaskV) == MaskV) {
1895 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1896 // Top bits known zero.
1897 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1898 }
1899 }
1900 }
1901 }
1902 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001903 case ISD::ADD: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001904 // Output known-0 bits are known if clear or set in both the low clear bits
1905 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1906 // low 3 bits clear.
Dan Gohmanbec16052008-04-28 17:02:21 +00001907 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1908 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001909 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanbec16052008-04-28 17:02:21 +00001910 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1911
1912 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001913 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanbec16052008-04-28 17:02:21 +00001914 KnownZeroOut = std::min(KnownZeroOut,
1915 KnownZero2.countTrailingOnes());
1916
1917 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001918 return;
1919 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001920 case ISD::SREM:
1921 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands574e2b22010-01-29 09:45:26 +00001922 const APInt &RA = Rem->getAPIntValue().abs();
1923 if (RA.isPowerOf2()) {
1924 APInt LowBits = RA - 1;
Dan Gohmanbec16052008-04-28 17:02:21 +00001925 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1926 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927
Duncan Sands574e2b22010-01-29 09:45:26 +00001928 // The low bits of the first operand are unchanged by the srem.
1929 KnownZero = KnownZero2 & LowBits;
1930 KnownOne = KnownOne2 & LowBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931
Duncan Sands574e2b22010-01-29 09:45:26 +00001932 // If the first operand is non-negative or has all low bits zero, then
1933 // the upper bits are all zero.
1934 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1935 KnownZero |= ~LowBits;
1936
1937 // If the first operand is negative and not all low bits are zero, then
1938 // the upper bits are all one.
1939 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
1940 KnownOne |= ~LowBits;
1941
1942 KnownZero &= Mask;
1943 KnownOne &= Mask;
Dan Gohmanbec16052008-04-28 17:02:21 +00001944
1945 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001946 }
1947 }
1948 return;
Dan Gohmanbec16052008-04-28 17:02:21 +00001949 case ISD::UREM: {
1950 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman1b1a3c62008-07-03 00:52:03 +00001951 const APInt &RA = Rem->getAPIntValue();
Dan Gohman5a154a12008-05-06 00:51:48 +00001952 if (RA.isPowerOf2()) {
1953 APInt LowBits = (RA - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001954 APInt Mask2 = LowBits & Mask;
1955 KnownZero |= ~LowBits & Mask;
1956 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1957 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1958 break;
1959 }
1960 }
1961
1962 // Since the result is less than or equal to either operand, any leading
1963 // zero bits in either operand must also exist in the result.
1964 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1965 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1966 Depth+1);
1967 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1968 Depth+1);
1969
1970 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1971 KnownZero2.countLeadingOnes());
1972 KnownOne.clear();
1973 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1974 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 }
1976 default:
1977 // Allow the target to implement this method for its nodes.
1978 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1979 case ISD::INTRINSIC_WO_CHAIN:
1980 case ISD::INTRINSIC_W_CHAIN:
1981 case ISD::INTRINSIC_VOID:
Dan Gohmandd5cdf62009-08-04 00:24:42 +00001982 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
1983 Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001984 }
1985 return;
1986 }
1987}
1988
1989/// ComputeNumSignBits - Return the number of times the sign bit of the
1990/// register is replicated into the other bits. We know that at least 1 bit
1991/// is always equal to the sign bit (itself), but other cases can give us
1992/// information. For example, immediately after an "SRA X, 2", we know that
1993/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman8181bd12008-07-27 21:46:04 +00001994unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersonac9de032009-08-10 22:56:29 +00001995 EVT VT = Op.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00001996 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman9d501bd2009-12-11 21:31:27 +00001997 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001998 unsigned Tmp, Tmp2;
Dan Gohman4afc4252008-05-23 02:28:01 +00001999 unsigned FirstAnswer = 1;
Scott Michel91099d62009-02-17 22:15:04 +00002000
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001 if (Depth == 6)
2002 return 1; // Limit search depth.
2003
2004 switch (Op.getOpcode()) {
2005 default: break;
2006 case ISD::AssertSext:
Duncan Sands92c43912008-06-06 12:08:01 +00002007 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002008 return VTBits-Tmp+1;
2009 case ISD::AssertZext:
Duncan Sands92c43912008-06-06 12:08:01 +00002010 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 return VTBits-Tmp;
Scott Michel91099d62009-02-17 22:15:04 +00002012
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 case ISD::Constant: {
Dan Gohman463db8c2008-03-03 23:35:36 +00002014 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
2015 // If negative, return # leading ones.
2016 if (Val.isNegative())
2017 return Val.countLeadingOnes();
Scott Michel91099d62009-02-17 22:15:04 +00002018
Dan Gohman463db8c2008-03-03 23:35:36 +00002019 // Return # leading zeros.
2020 return Val.countLeadingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 }
Scott Michel91099d62009-02-17 22:15:04 +00002022
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002023 case ISD::SIGN_EXTEND:
Dan Gohman9d501bd2009-12-11 21:31:27 +00002024 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michel91099d62009-02-17 22:15:04 +00002026
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002027 case ISD::SIGN_EXTEND_INREG:
2028 // Max of the input and what this extends.
Dan Gohman7196cb12010-01-09 02:13:55 +00002029 Tmp =
2030 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002031 Tmp = VTBits-Tmp+1;
Scott Michel91099d62009-02-17 22:15:04 +00002032
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2034 return std::max(Tmp, Tmp2);
2035
2036 case ISD::SRA:
2037 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2038 // SRA X, C -> adds C sign bits.
2039 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002040 Tmp += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002041 if (Tmp > VTBits) Tmp = VTBits;
2042 }
2043 return Tmp;
2044 case ISD::SHL:
2045 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2046 // shl destroys sign bits.
2047 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002048 if (C->getZExtValue() >= VTBits || // Bad shift.
2049 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2050 return Tmp - C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 }
2052 break;
2053 case ISD::AND:
2054 case ISD::OR:
2055 case ISD::XOR: // NOT is handled here.
Dan Gohman4afc4252008-05-23 02:28:01 +00002056 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002057 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman4afc4252008-05-23 02:28:01 +00002058 if (Tmp != 1) {
2059 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2060 FirstAnswer = std::min(Tmp, Tmp2);
2061 // We computed what we know about the sign bits as our first
2062 // answer. Now proceed to the generic code that uses
2063 // ComputeMaskedBits, and pick whichever answer is better.
2064 }
2065 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002066
2067 case ISD::SELECT:
Dan Gohman6ffcf262008-05-20 20:59:51 +00002068 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 if (Tmp == 1) return 1; // Early out.
Dan Gohman6ffcf262008-05-20 20:59:51 +00002070 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002071 return std::min(Tmp, Tmp2);
Bill Wendlingf8bb4402008-11-22 07:24:01 +00002072
2073 case ISD::SADDO:
2074 case ISD::UADDO:
Bill Wendling7e04be62008-12-09 22:08:41 +00002075 case ISD::SSUBO:
2076 case ISD::USUBO:
2077 case ISD::SMULO:
2078 case ISD::UMULO:
Bill Wendlingf8bb4402008-11-22 07:24:01 +00002079 if (Op.getResNo() != 1)
2080 break;
Duncan Sands8cf4a822008-11-23 15:47:28 +00002081 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002082 case ISD::SETCC:
2083 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands8cf4a822008-11-23 15:47:28 +00002084 if (TLI.getBooleanContents() ==
2085 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 return VTBits;
2087 break;
2088 case ISD::ROTL:
2089 case ISD::ROTR:
2090 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002091 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michel91099d62009-02-17 22:15:04 +00002092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093 // Handle rotate right by N like a rotate left by 32-N.
2094 if (Op.getOpcode() == ISD::ROTR)
2095 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2096
2097 // If we aren't rotating out all of the known-in sign bits, return the
2098 // number that are left. This handles rotl(sext(x), 1) for example.
2099 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2100 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2101 }
2102 break;
2103 case ISD::ADD:
2104 // Add can have at most one carry bit. Thus we know that the output
2105 // is, at worst, one more bit than the inputs.
2106 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2107 if (Tmp == 1) return 1; // Early out.
Scott Michel91099d62009-02-17 22:15:04 +00002108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002109 // Special case decrementing a value (ADD X, -1):
Dan Gohman843649e2009-02-24 02:00:40 +00002110 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002111 if (CRHS->isAllOnesValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00002112 APInt KnownZero, KnownOne;
2113 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002114 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00002115
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002116 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2117 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00002118 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002119 return VTBits;
Scott Michel91099d62009-02-17 22:15:04 +00002120
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 // If we are subtracting one from a positive number, there is no carry
2122 // out of the result.
Dan Gohman63f4e462008-02-27 01:23:58 +00002123 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002124 return Tmp;
2125 }
Scott Michel91099d62009-02-17 22:15:04 +00002126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002127 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2128 if (Tmp2 == 1) return 1;
2129 return std::min(Tmp, Tmp2)-1;
2130 break;
Scott Michel91099d62009-02-17 22:15:04 +00002131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 case ISD::SUB:
2133 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2134 if (Tmp2 == 1) return 1;
Scott Michel91099d62009-02-17 22:15:04 +00002135
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002136 // Handle NEG.
2137 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00002138 if (CLHS->isNullValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00002139 APInt KnownZero, KnownOne;
2140 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002141 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2142 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2143 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00002144 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002145 return VTBits;
Scott Michel91099d62009-02-17 22:15:04 +00002146
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002147 // If the input is known to be positive (the sign bit is known clear),
2148 // the output of the NEG has the same number of sign bits as the input.
Dan Gohman63f4e462008-02-27 01:23:58 +00002149 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 return Tmp2;
Scott Michel91099d62009-02-17 22:15:04 +00002151
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002152 // Otherwise, we treat this like a SUB.
2153 }
Scott Michel91099d62009-02-17 22:15:04 +00002154
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002155 // Sub can have at most one carry bit. Thus we know that the output
2156 // is, at worst, one more bit than the inputs.
2157 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2158 if (Tmp == 1) return 1; // Early out.
2159 return std::min(Tmp, Tmp2)-1;
2160 break;
2161 case ISD::TRUNCATE:
2162 // FIXME: it's tricky to do anything useful for this, but it is an important
2163 // case for targets like X86.
2164 break;
2165 }
Scott Michel91099d62009-02-17 22:15:04 +00002166
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002167 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2168 if (Op.getOpcode() == ISD::LOAD) {
2169 LoadSDNode *LD = cast<LoadSDNode>(Op);
2170 unsigned ExtType = LD->getExtensionType();
2171 switch (ExtType) {
2172 default: break;
2173 case ISD::SEXTLOAD: // '17' bits known
Dan Gohman7196cb12010-01-09 02:13:55 +00002174 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175 return VTBits-Tmp+1;
2176 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohman7196cb12010-01-09 02:13:55 +00002177 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002178 return VTBits-Tmp;
2179 }
2180 }
2181
2182 // Allow the target to implement this method for its nodes.
2183 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michel91099d62009-02-17 22:15:04 +00002184 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2186 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2187 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman4afc4252008-05-23 02:28:01 +00002188 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189 }
Scott Michel91099d62009-02-17 22:15:04 +00002190
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2192 // use this information.
Dan Gohman63f4e462008-02-27 01:23:58 +00002193 APInt KnownZero, KnownOne;
2194 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002195 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michel91099d62009-02-17 22:15:04 +00002196
Dan Gohman63f4e462008-02-27 01:23:58 +00002197 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002198 Mask = KnownZero;
Dan Gohman63f4e462008-02-27 01:23:58 +00002199 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002200 Mask = KnownOne;
2201 } else {
2202 // Nothing known.
Dan Gohman4afc4252008-05-23 02:28:01 +00002203 return FirstAnswer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002204 }
Scott Michel91099d62009-02-17 22:15:04 +00002205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002206 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2207 // the number of identical bits in the top of the input value.
Dan Gohman63f4e462008-02-27 01:23:58 +00002208 Mask = ~Mask;
2209 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002210 // Return # leading zeros. We use 'min' here in case Val was zero before
2211 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman4afc4252008-05-23 02:28:01 +00002212 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002213}
2214
Dan Gohman41b3f4a2009-09-03 20:34:31 +00002215bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2216 // If we're told that NaNs won't happen, assume they won't.
2217 if (FiniteOnlyFPMath())
2218 return true;
2219
2220 // If the value is a constant, we can obviously see if it is a NaN or not.
2221 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2222 return !C->getValueAPF().isNaN();
2223
2224 // TODO: Recognize more cases here.
2225
2226 return false;
2227}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002228
Dan Gohmane8cc39f2010-02-24 06:52:40 +00002229bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2230 // If the value is a constant, we can obviously see if it is a zero or not.
2231 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2232 return !C->isZero();
2233
2234 // TODO: Recognize more cases here.
2235
2236 return false;
2237}
2238
2239bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2240 // Check the obvious case.
2241 if (A == B) return true;
2242
2243 // For for negative and positive zero.
2244 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2245 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2246 if (CA->isZero() && CB->isZero()) return true;
2247
2248 // Otherwise they may not be equal.
2249 return false;
2250}
2251
Dan Gohman8181bd12008-07-27 21:46:04 +00002252bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Cheng2e28d622008-02-02 04:07:54 +00002253 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2254 if (!GA) return false;
Dan Gohman36322c72008-10-18 02:06:02 +00002255 if (GA->getOffset() != 0) return false;
Evan Cheng2e28d622008-02-02 04:07:54 +00002256 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2257 if (!GV) return false;
Chris Lattner511b6692010-04-05 06:19:28 +00002258 return MF->getMMI().hasDebugInfo();
Evan Cheng2e28d622008-02-02 04:07:54 +00002259}
2260
2261
Evan Cheng411fc172008-05-13 08:35:03 +00002262/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2263/// element of the result of the vector shuffle.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002264SDValue SelectionDAG::getShuffleScalarElt(const ShuffleVectorSDNode *N,
2265 unsigned i) {
Owen Andersonac9de032009-08-10 22:56:29 +00002266 EVT VT = N->getValueType(0);
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002267 DebugLoc dl = N->getDebugLoc();
Nate Begemane8f61cb2009-04-29 05:20:52 +00002268 if (N->getMaskElt(i) < 0)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002269 return getUNDEF(VT.getVectorElementType());
Nate Begemane8f61cb2009-04-29 05:20:52 +00002270 unsigned Index = N->getMaskElt(i);
2271 unsigned NumElems = VT.getVectorNumElements();
Dan Gohman8181bd12008-07-27 21:46:04 +00002272 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Cheng57db53b2008-06-25 20:52:59 +00002273 Index %= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00002274
2275 if (V.getOpcode() == ISD::BIT_CONVERT) {
2276 V = V.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002277 EVT VVT = V.getValueType();
Nate Begeman543d2142009-04-27 18:41:29 +00002278 if (!VVT.isVector() || VVT.getVectorNumElements() != (unsigned)NumElems)
Dan Gohman8181bd12008-07-27 21:46:04 +00002279 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002280 }
Evan Chengdea99362008-05-29 08:22:04 +00002281 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00002282 return (Index == 0) ? V.getOperand(0)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002283 : getUNDEF(VT.getVectorElementType());
Evan Chengdea99362008-05-29 08:22:04 +00002284 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00002285 return V.getOperand(Index);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002286 if (const ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(V))
2287 return getShuffleScalarElt(SVN, Index);
Dan Gohman8181bd12008-07-27 21:46:04 +00002288 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002289}
2290
2291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292/// getNode - Gets or creates the specified node.
2293///
Owen Andersonac9de032009-08-10 22:56:29 +00002294SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002295 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00002296 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00002298 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002299 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00002300
Dan Gohman7616a1e2010-03-18 18:49:47 +00002301 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002302 CSEMap.InsertNode(N, IP);
Scott Michel91099d62009-02-17 22:15:04 +00002303
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002304 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002305#ifndef NDEBUG
2306 VerifyNode(N);
2307#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002308 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002309}
2310
Bill Wendlingdf4de112009-01-28 22:17:52 +00002311SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersonac9de032009-08-10 22:56:29 +00002312 EVT VT, SDValue Operand) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002313 // Constant fold unary operations with an integer constant operand.
Gabor Greif1c80d112008-08-28 21:40:38 +00002314 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00002315 const APInt &Val = C->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002316 switch (Opcode) {
2317 default: break;
Evan Chengeadaf442008-03-06 17:42:34 +00002318 case ISD::SIGN_EXTEND:
Chris Lattner63607d02010-03-15 16:15:56 +00002319 return getConstant(APInt(Val).sextOrTrunc(VT.getSizeInBits()), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002320 case ISD::ANY_EXTEND:
Dan Gohman161652c2008-02-29 01:47:35 +00002321 case ISD::ZERO_EXTEND:
Evan Chengeadaf442008-03-06 17:42:34 +00002322 case ISD::TRUNCATE:
Chris Lattnerf53c2c72010-03-15 16:05:15 +00002323 return getConstant(APInt(Val).zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00002324 case ISD::UINT_TO_FP:
2325 case ISD::SINT_TO_FP: {
2326 const uint64_t zero[] = {0, 0};
Chris Lattnerf53c2c72010-03-15 16:05:15 +00002327 // No compile time operations on ppcf128.
2328 if (VT == MVT::ppcf128) break;
2329 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
Scott Michel91099d62009-02-17 22:15:04 +00002330 (void)apf.convertFromAPInt(Val,
Dan Gohman161652c2008-02-29 01:47:35 +00002331 Opcode==ISD::SINT_TO_FP,
2332 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00002333 return getConstantFP(apf, VT);
2334 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 case ISD::BIT_CONVERT:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002336 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman161652c2008-02-29 01:47:35 +00002337 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002338 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman161652c2008-02-29 01:47:35 +00002339 return getConstantFP(Val.bitsToDouble(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002340 break;
2341 case ISD::BSWAP:
Dan Gohman161652c2008-02-29 01:47:35 +00002342 return getConstant(Val.byteSwap(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002343 case ISD::CTPOP:
Dan Gohman161652c2008-02-29 01:47:35 +00002344 return getConstant(Val.countPopulation(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345 case ISD::CTLZ:
Dan Gohman161652c2008-02-29 01:47:35 +00002346 return getConstant(Val.countLeadingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 case ISD::CTTZ:
Dan Gohman161652c2008-02-29 01:47:35 +00002348 return getConstant(Val.countTrailingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002349 }
2350 }
2351
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002352 // Constant fold unary operations with a floating point constant operand.
Gabor Greif1c80d112008-08-28 21:40:38 +00002353 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002354 APFloat V = C->getValueAPF(); // make copy
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002355 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00002356 switch (Opcode) {
2357 case ISD::FNEG:
2358 V.changeSign();
2359 return getConstantFP(V, VT);
2360 case ISD::FABS:
2361 V.clearSign();
2362 return getConstantFP(V, VT);
2363 case ISD::FP_ROUND:
Dale Johannesen6e547b42008-10-09 23:00:39 +00002364 case ISD::FP_EXTEND: {
2365 bool ignored;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002366 // This can return overflow, underflow, or inexact; we don't care.
2367 // FIXME need to be more flexible about rounding mode.
Owen Andersonac9de032009-08-10 22:56:29 +00002368 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen6e547b42008-10-09 23:00:39 +00002369 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002370 return getConstantFP(V, VT);
Dale Johannesen6e547b42008-10-09 23:00:39 +00002371 }
Dale Johannesenb89072e2007-10-16 23:38:29 +00002372 case ISD::FP_TO_SINT:
2373 case ISD::FP_TO_UINT: {
Dale Johannesen63d45032009-04-24 21:34:13 +00002374 integerPart x[2];
Dale Johannesen6e547b42008-10-09 23:00:39 +00002375 bool ignored;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002376 assert(integerPartWidth >= 64);
2377 // FIXME need to be more flexible about rounding mode.
Dale Johannesen63d45032009-04-24 21:34:13 +00002378 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesenb89072e2007-10-16 23:38:29 +00002379 Opcode==ISD::FP_TO_SINT,
Dale Johannesen6e547b42008-10-09 23:00:39 +00002380 APFloat::rmTowardZero, &ignored);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002381 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2382 break;
Dale Johannesen63d45032009-04-24 21:34:13 +00002383 APInt api(VT.getSizeInBits(), 2, x);
2384 return getConstant(api, VT);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002385 }
2386 case ISD::BIT_CONVERT:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002387 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen6e547b42008-10-09 23:00:39 +00002388 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002389 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen6e547b42008-10-09 23:00:39 +00002390 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002391 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002392 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002393 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002394 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002395
Gabor Greif1c80d112008-08-28 21:40:38 +00002396 unsigned OpOpcode = Operand.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002397 switch (Opcode) {
2398 case ISD::TokenFactor:
Duncan Sands42d7bb82008-12-01 11:41:29 +00002399 case ISD::MERGE_VALUES:
Dan Gohman29c3cef2008-08-14 20:04:46 +00002400 case ISD::CONCAT_VECTORS:
Duncan Sands42d7bb82008-12-01 11:41:29 +00002401 return Operand; // Factor, merge or concat of one node? No need.
Edwin Törökbd448e32009-07-14 16:55:14 +00002402 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002403 case ISD::FP_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002404 assert(VT.isFloatingPoint() &&
2405 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00002406 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00002407 assert((!VT.isVector() ||
2408 VT.getVectorNumElements() ==
2409 Operand.getValueType().getVectorNumElements()) &&
2410 "Vector element count mismatch!");
Chris Lattnere6465dd2008-03-11 06:21:08 +00002411 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002412 return getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002413 break;
Chris Lattnere6465dd2008-03-11 06:21:08 +00002414 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002415 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 "Invalid SIGN_EXTEND!");
2417 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00002418 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2419 "Invalid sext node, dst < src!");
2420 assert((!VT.isVector() ||
2421 VT.getVectorNumElements() ==
2422 Operand.getValueType().getVectorNumElements()) &&
2423 "Vector element count mismatch!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002425 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002426 break;
2427 case ISD::ZERO_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002428 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 "Invalid ZERO_EXTEND!");
2430 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00002431 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2432 "Invalid zext node, dst < src!");
2433 assert((!VT.isVector() ||
2434 VT.getVectorNumElements() ==
2435 Operand.getValueType().getVectorNumElements()) &&
2436 "Vector element count mismatch!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michel91099d62009-02-17 22:15:04 +00002438 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002439 Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002440 break;
2441 case ISD::ANY_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002442 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 "Invalid ANY_EXTEND!");
2444 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00002445 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2446 "Invalid anyext node, dst < src!");
2447 assert((!VT.isVector() ||
2448 VT.getVectorNumElements() ==
2449 Operand.getValueType().getVectorNumElements()) &&
2450 "Vector element count mismatch!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2452 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002453 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 break;
2455 case ISD::TRUNCATE:
Duncan Sands92c43912008-06-06 12:08:01 +00002456 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 "Invalid TRUNCATE!");
2458 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00002459 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2460 "Invalid truncate node, src < dst!");
2461 assert((!VT.isVector() ||
2462 VT.getVectorNumElements() ==
2463 Operand.getValueType().getVectorNumElements()) &&
2464 "Vector element count mismatch!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002466 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002467 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2468 OpOpcode == ISD::ANY_EXTEND) {
2469 // If the source is smaller than the dest, we still need an extend.
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00002470 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2471 .bitsLT(VT.getScalarType()))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002472 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00002473 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002474 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002475 else
Gabor Greif1c80d112008-08-28 21:40:38 +00002476 return Operand.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 }
2478 break;
2479 case ISD::BIT_CONVERT:
2480 // Basic sanity checking.
Duncan Sands92c43912008-06-06 12:08:01 +00002481 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 && "Cannot BIT_CONVERT between types of different sizes!");
2483 if (VT == Operand.getValueType()) return Operand; // noop conversion.
2484 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002485 return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 if (OpOpcode == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002487 return getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 break;
2489 case ISD::SCALAR_TO_VECTOR:
Duncan Sands92c43912008-06-06 12:08:01 +00002490 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sands6a3a01e2009-04-18 20:16:54 +00002491 (VT.getVectorElementType() == Operand.getValueType() ||
2492 (VT.getVectorElementType().isInteger() &&
2493 Operand.getValueType().isInteger() &&
2494 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattner9f0705c2008-03-08 23:43:36 +00002496 if (OpOpcode == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002497 return getUNDEF(VT);
Chris Lattner9f0705c2008-03-08 23:43:36 +00002498 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2499 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2500 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2501 Operand.getConstantOperandVal(1) == 0 &&
2502 Operand.getOperand(0).getValueType() == VT)
2503 return Operand.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 break;
2505 case ISD::FNEG:
Mon P Wang4340d5d2009-01-31 06:07:45 +00002506 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
2507 if (UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002508 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greif1c80d112008-08-28 21:40:38 +00002509 Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002510 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greif1c80d112008-08-28 21:40:38 +00002511 return Operand.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 break;
2513 case ISD::FABS:
2514 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002515 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516 break;
2517 }
2518
2519 SDNode *N;
2520 SDVTList VTs = getVTList(VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002521 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002522 FoldingSetNodeID ID;
Dan Gohman8181bd12008-07-27 21:46:04 +00002523 SDValue Ops[1] = { Operand };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002524 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
2525 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00002526 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002527 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00002528
Dan Gohman7616a1e2010-03-18 18:49:47 +00002529 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 CSEMap.InsertNode(N, IP);
2531 } else {
Dan Gohman7616a1e2010-03-18 18:49:47 +00002532 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533 }
Duncan Sandsd3ace282008-07-21 10:20:31 +00002534
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002535 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002536#ifndef NDEBUG
2537 VerifyNode(N);
2538#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002539 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002540}
2541
Bill Wendling16bc18c2008-09-24 10:16:24 +00002542SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersonac9de032009-08-10 22:56:29 +00002543 EVT VT,
Bill Wendling16bc18c2008-09-24 10:16:24 +00002544 ConstantSDNode *Cst1,
2545 ConstantSDNode *Cst2) {
2546 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2547
2548 switch (Opcode) {
2549 case ISD::ADD: return getConstant(C1 + C2, VT);
2550 case ISD::SUB: return getConstant(C1 - C2, VT);
2551 case ISD::MUL: return getConstant(C1 * C2, VT);
2552 case ISD::UDIV:
2553 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2554 break;
2555 case ISD::UREM:
2556 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2557 break;
2558 case ISD::SDIV:
2559 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2560 break;
2561 case ISD::SREM:
2562 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2563 break;
2564 case ISD::AND: return getConstant(C1 & C2, VT);
2565 case ISD::OR: return getConstant(C1 | C2, VT);
2566 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2567 case ISD::SHL: return getConstant(C1 << C2, VT);
2568 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2569 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2570 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2571 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2572 default: break;
2573 }
2574
2575 return SDValue();
2576}
2577
Owen Andersonac9de032009-08-10 22:56:29 +00002578SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00002579 SDValue N1, SDValue N2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002580 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2581 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002583 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002584 case ISD::TokenFactor:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002585 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2586 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002587 // Fold trivial token factors.
2588 if (N1.getOpcode() == ISD::EntryToken) return N2;
2589 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanc0e18d62008-10-01 15:11:19 +00002590 if (N1 == N2) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002591 break;
Dan Gohman29c3cef2008-08-14 20:04:46 +00002592 case ISD::CONCAT_VECTORS:
2593 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2594 // one big BUILD_VECTOR.
2595 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2596 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002597 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2598 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Cheng907a2d22009-02-25 22:49:59 +00002599 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman29c3cef2008-08-14 20:04:46 +00002600 }
2601 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602 case ISD::AND:
Duncan Sands92c43912008-06-06 12:08:01 +00002603 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002604 N1.getValueType() == VT && "Binary operator types must match!");
2605 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2606 // worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002607 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002608 return N2;
Chris Lattner8aa8a5e2008-01-26 01:05:42 +00002609 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2610 return N1;
Chris Lattnercc126e32008-01-22 19:09:33 +00002611 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612 case ISD::OR:
2613 case ISD::XOR:
Dan Gohman3f76aed2008-06-02 22:27:05 +00002614 case ISD::ADD:
2615 case ISD::SUB:
Duncan Sands92c43912008-06-06 12:08:01 +00002616 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002617 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman3f76aed2008-06-02 22:27:05 +00002618 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2619 // it's worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002620 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002621 return N1;
2622 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 case ISD::UDIV:
2624 case ISD::UREM:
2625 case ISD::MULHU:
2626 case ISD::MULHS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627 case ISD::MUL:
2628 case ISD::SDIV:
2629 case ISD::SREM:
Dan Gohman3d015562009-01-22 21:58:43 +00002630 assert(VT.isInteger() && "This operator does not apply to FP types!");
2631 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002632 case ISD::FADD:
2633 case ISD::FSUB:
2634 case ISD::FMUL:
2635 case ISD::FDIV:
2636 case ISD::FREM:
Dan Gohman46ef3eb2009-01-23 19:10:37 +00002637 if (UnsafeFPMath) {
2638 if (Opcode == ISD::FADD) {
2639 // 0+x --> x
2640 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2641 if (CFP->getValueAPF().isZero())
2642 return N2;
2643 // x+0 --> x
2644 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2645 if (CFP->getValueAPF().isZero())
2646 return N1;
2647 } else if (Opcode == ISD::FSUB) {
2648 // x-0 --> x
2649 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2650 if (CFP->getValueAPF().isZero())
2651 return N1;
2652 }
Dan Gohman3d015562009-01-22 21:58:43 +00002653 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654 assert(N1.getValueType() == N2.getValueType() &&
2655 N1.getValueType() == VT && "Binary operator types must match!");
2656 break;
2657 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2658 assert(N1.getValueType() == VT &&
Duncan Sands92c43912008-06-06 12:08:01 +00002659 N1.getValueType().isFloatingPoint() &&
2660 N2.getValueType().isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002661 "Invalid FCOPYSIGN!");
2662 break;
2663 case ISD::SHL:
2664 case ISD::SRA:
2665 case ISD::SRL:
2666 case ISD::ROTL:
2667 case ISD::ROTR:
2668 assert(VT == N1.getValueType() &&
2669 "Shift operators return type must be the same as their first arg");
Duncan Sands92c43912008-06-06 12:08:01 +00002670 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattnera113d3e2008-07-02 17:01:57 +00002671 "Shifts only work on integers");
2672
2673 // Always fold shifts of i1 values so the code generator doesn't need to
2674 // handle them. Since we know the size of the shift has to be less than the
2675 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002676 if (VT == MVT::i1)
Chris Lattnera113d3e2008-07-02 17:01:57 +00002677 return N1;
Evan Cheng095dac22010-01-06 19:38:29 +00002678 if (N2C && N2C->isNullValue())
2679 return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680 break;
2681 case ISD::FP_ROUND_INREG: {
Owen Andersonac9de032009-08-10 22:56:29 +00002682 EVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002683 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands92c43912008-06-06 12:08:01 +00002684 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002685 "Cannot FP_ROUND_INREG integer types");
Dan Gohman7196cb12010-01-09 02:13:55 +00002686 assert(EVT.isVector() == VT.isVector() &&
2687 "FP_ROUND_INREG type should be vector iff the operand "
2688 "type is vector!");
2689 assert((!EVT.isVector() ||
2690 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2691 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002692 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002693 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002694 break;
2695 }
Chris Lattner5872a362008-01-17 07:00:52 +00002696 case ISD::FP_ROUND:
Duncan Sands92c43912008-06-06 12:08:01 +00002697 assert(VT.isFloatingPoint() &&
2698 N1.getValueType().isFloatingPoint() &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00002699 VT.bitsLE(N1.getValueType()) &&
Chris Lattner5872a362008-01-17 07:00:52 +00002700 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002701 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00002702 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002703 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00002704 case ISD::AssertZext: {
Owen Andersonac9de032009-08-10 22:56:29 +00002705 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattnercc126e32008-01-22 19:09:33 +00002706 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002707 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002708 "Cannot *_EXTEND_INREG FP types");
Dan Gohman9d501bd2009-12-11 21:31:27 +00002709 assert(!EVT.isVector() &&
2710 "AssertSExt/AssertZExt type should be the vector element type "
2711 "rather than the vector type!");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002712 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands539510b2008-02-10 10:08:52 +00002713 if (VT == EVT) return N1; // noop assertion.
Chris Lattnercc126e32008-01-22 19:09:33 +00002714 break;
2715 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002716 case ISD::SIGN_EXTEND_INREG: {
Owen Andersonac9de032009-08-10 22:56:29 +00002717 EVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002719 assert(VT.isInteger() && EVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720 "Cannot *_EXTEND_INREG FP types");
Dan Gohman7196cb12010-01-09 02:13:55 +00002721 assert(EVT.isVector() == VT.isVector() &&
2722 "SIGN_EXTEND_INREG type should be vector iff the operand "
2723 "type is vector!");
2724 assert((!EVT.isVector() ||
2725 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2726 "Vector element counts must match in SIGN_EXTEND_INREG");
2727 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002728 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002729
Chris Lattnercc126e32008-01-22 19:09:33 +00002730 if (N1C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002731 APInt Val = N1C->getAPIntValue();
Dan Gohman7196cb12010-01-09 02:13:55 +00002732 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman161652c2008-02-29 01:47:35 +00002733 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng7faa1d72008-03-06 08:20:51 +00002734 Val = Val.ashr(Val.getBitWidth()-FromBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002735 return getConstant(Val, VT);
2736 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002737 break;
2738 }
2739 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattner9f0705c2008-03-08 23:43:36 +00002740 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2741 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002742 return getUNDEF(VT);
Scott Michel91099d62009-02-17 22:15:04 +00002743
Chris Lattnercc126e32008-01-22 19:09:33 +00002744 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2745 // expanding copies of large vectors from registers.
Dan Gohman101124c2008-08-13 21:51:37 +00002746 if (N2C &&
2747 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002748 N1.getNumOperands() > 0) {
2749 unsigned Factor =
Duncan Sands92c43912008-06-06 12:08:01 +00002750 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002751 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002752 N1.getOperand(N2C->getZExtValue() / Factor),
2753 getConstant(N2C->getZExtValue() % Factor,
2754 N2.getValueType()));
Chris Lattnercc126e32008-01-22 19:09:33 +00002755 }
2756
2757 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2758 // expanding large vector constants.
Bob Wilson2b570bb2009-04-13 22:05:19 +00002759 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2760 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersonac9de032009-08-10 22:56:29 +00002761 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanf1896f22009-07-09 22:01:03 +00002762 if (Elt.getValueType() != VEltTy) {
Bob Wilson2b570bb2009-04-13 22:05:19 +00002763 // If the vector element type is not legal, the BUILD_VECTOR operands
2764 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanf1896f22009-07-09 22:01:03 +00002765 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2766 }
2767 if (VT != VEltTy) {
2768 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2769 // result is implicitly extended.
2770 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilson2b570bb2009-04-13 22:05:19 +00002771 }
2772 return Elt;
2773 }
Scott Michel91099d62009-02-17 22:15:04 +00002774
Chris Lattnercc126e32008-01-22 19:09:33 +00002775 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2776 // operations are lowered to scalars.
Dan Gohman101124c2008-08-13 21:51:37 +00002777 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang98e97822010-02-01 22:15:09 +00002778 // If the indices are the same, return the inserted element else
2779 // if the indices are known different, extract the element from
Dan Gohmanb38eabf2009-01-29 16:10:46 +00002780 // the original vector.
Mon P Wang98e97822010-02-01 22:15:09 +00002781 if (N1.getOperand(2) == N2) {
2782 if (VT == N1.getOperand(1).getValueType())
2783 return N1.getOperand(1);
2784 else
2785 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
2786 } else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2787 isa<ConstantSDNode>(N2))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002788 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Dan Gohman101124c2008-08-13 21:51:37 +00002789 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002790 break;
2791 case ISD::EXTRACT_ELEMENT:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002792 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands1568af82008-06-25 20:24:48 +00002793 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2794 (N1.getValueType().isInteger() == VT.isInteger()) &&
2795 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sandsc4d85172008-03-12 20:30:08 +00002796
Chris Lattnercc126e32008-01-22 19:09:33 +00002797 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2798 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michel91099d62009-02-17 22:15:04 +00002799 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattnercc126e32008-01-22 19:09:33 +00002800 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002801 return N1.getOperand(N2C->getZExtValue());
Duncan Sandsc4d85172008-03-12 20:30:08 +00002802
Chris Lattnercc126e32008-01-22 19:09:33 +00002803 // EXTRACT_ELEMENT of a constant int is also very common.
2804 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002805 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002806 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman452b4ce2008-03-24 16:38:05 +00002807 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2808 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattnercc126e32008-01-22 19:09:33 +00002809 }
2810 break;
Duncan Sandsbd13a812008-02-20 17:38:09 +00002811 case ISD::EXTRACT_SUBVECTOR:
2812 if (N1.getValueType() == VT) // Trivial extraction.
2813 return N1;
2814 break;
Chris Lattnercc126e32008-01-22 19:09:33 +00002815 }
2816
2817 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818 if (N2C) {
Bill Wendling16bc18c2008-09-24 10:16:24 +00002819 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2820 if (SV.getNode()) return SV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 } else { // Cannonicalize constant to RHS if commutative
2822 if (isCommutativeBinOp(Opcode)) {
2823 std::swap(N1C, N2C);
2824 std::swap(N1, N2);
2825 }
2826 }
2827 }
2828
Chris Lattnercc126e32008-01-22 19:09:33 +00002829 // Constant fold FP operations.
Gabor Greif1c80d112008-08-28 21:40:38 +00002830 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2831 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002832 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002833 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2834 // Cannonicalize constant to RHS if commutative
2835 std::swap(N1CFP, N2CFP);
2836 std::swap(N1, N2);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002837 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002838 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2839 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840 switch (Opcode) {
Scott Michel91099d62009-02-17 22:15:04 +00002841 case ISD::FADD:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002842 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002843 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002844 return getConstantFP(V1, VT);
2845 break;
Scott Michel91099d62009-02-17 22:15:04 +00002846 case ISD::FSUB:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002847 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2848 if (s!=APFloat::opInvalidOp)
2849 return getConstantFP(V1, VT);
2850 break;
2851 case ISD::FMUL:
2852 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2853 if (s!=APFloat::opInvalidOp)
2854 return getConstantFP(V1, VT);
2855 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002857 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2858 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2859 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002860 break;
2861 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002862 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2863 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2864 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002866 case ISD::FCOPYSIGN:
2867 V1.copySign(V2);
2868 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869 default: break;
2870 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871 }
2872 }
Scott Michel91099d62009-02-17 22:15:04 +00002873
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002874 // Canonicalize an UNDEF to the RHS, even over a constant.
2875 if (N1.getOpcode() == ISD::UNDEF) {
2876 if (isCommutativeBinOp(Opcode)) {
2877 std::swap(N1, N2);
2878 } else {
2879 switch (Opcode) {
2880 case ISD::FP_ROUND_INREG:
2881 case ISD::SIGN_EXTEND_INREG:
2882 case ISD::SUB:
2883 case ISD::FSUB:
2884 case ISD::FDIV:
2885 case ISD::FREM:
2886 case ISD::SRA:
2887 return N1; // fold op(undef, arg2) -> undef
2888 case ISD::UDIV:
2889 case ISD::SDIV:
2890 case ISD::UREM:
2891 case ISD::SREM:
2892 case ISD::SRL:
2893 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002894 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2896 // For vectors, we can't easily build an all zero vector, just return
2897 // the LHS.
2898 return N2;
2899 }
2900 }
2901 }
Scott Michel91099d62009-02-17 22:15:04 +00002902
2903 // Fold a bunch of operators when the RHS is undef.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 if (N2.getOpcode() == ISD::UNDEF) {
2905 switch (Opcode) {
Evan Cheng5d00cb42008-03-25 20:08:07 +00002906 case ISD::XOR:
2907 if (N1.getOpcode() == ISD::UNDEF)
2908 // Handle undef ^ undef -> 0 special case. This is a common
2909 // idiom (misuse).
2910 return getConstant(0, VT);
2911 // fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002912 case ISD::ADD:
2913 case ISD::ADDC:
2914 case ISD::ADDE:
2915 case ISD::SUB:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002916 case ISD::UDIV:
2917 case ISD::SDIV:
2918 case ISD::UREM:
2919 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002920 return N2; // fold op(arg1, undef) -> undef
Dan Gohmana22a8402009-06-04 17:12:12 +00002921 case ISD::FADD:
2922 case ISD::FSUB:
2923 case ISD::FMUL:
2924 case ISD::FDIV:
2925 case ISD::FREM:
2926 if (UnsafeFPMath)
2927 return N2;
2928 break;
Scott Michel91099d62009-02-17 22:15:04 +00002929 case ISD::MUL:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002930 case ISD::AND:
2931 case ISD::SRL:
2932 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002933 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2935 // For vectors, we can't easily build an all zero vector, just return
2936 // the LHS.
2937 return N1;
2938 case ISD::OR:
Duncan Sands92c43912008-06-06 12:08:01 +00002939 if (!VT.isVector())
Duncan Sands505ba942009-02-01 18:06:53 +00002940 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941 // For vectors, we can't easily build an all one vector, just return
2942 // the LHS.
2943 return N1;
2944 case ISD::SRA:
2945 return N1;
2946 }
2947 }
2948
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002949 // Memoize this node if possible.
2950 SDNode *N;
2951 SDVTList VTs = getVTList(VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002952 if (VT != MVT::Flag) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002953 SDValue Ops[] = { N1, N2 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002954 FoldingSetNodeID ID;
2955 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2956 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00002957 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002958 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00002959
Dan Gohman7616a1e2010-03-18 18:49:47 +00002960 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 CSEMap.InsertNode(N, IP);
2962 } else {
Dan Gohman7616a1e2010-03-18 18:49:47 +00002963 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964 }
2965
2966 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002967#ifndef NDEBUG
2968 VerifyNode(N);
2969#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002970 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971}
2972
Owen Andersonac9de032009-08-10 22:56:29 +00002973SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00002974 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002975 // Perform various simplifications.
Gabor Greif1c80d112008-08-28 21:40:38 +00002976 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2977 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002978 switch (Opcode) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00002979 case ISD::CONCAT_VECTORS:
2980 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2981 // one big BUILD_VECTOR.
2982 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2983 N2.getOpcode() == ISD::BUILD_VECTOR &&
2984 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002985 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2986 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2987 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Cheng907a2d22009-02-25 22:49:59 +00002988 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman29c3cef2008-08-14 20:04:46 +00002989 }
2990 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991 case ISD::SETCC: {
2992 // Use FoldSetCC to simplify SETCC's.
Dale Johannesen38496eb2009-02-03 00:47:48 +00002993 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002994 if (Simp.getNode()) return Simp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 break;
2996 }
2997 case ISD::SELECT:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002998 if (N1C) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002999 if (N1C->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000 return N2; // select true, X, Y -> X
3001 else
3002 return N3; // select false, X, Y -> Y
Anton Korobeynikov53422f62008-02-20 11:10:28 +00003003 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003004
3005 if (N2 == N3) return N2; // select C, X, X -> X
3006 break;
3007 case ISD::BRCOND:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00003008 if (N2C) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003009 if (N2C->getZExtValue()) // Unconditional branch
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003010 return getNode(ISD::BR, DL, MVT::Other, N1, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003011 else
3012 return N1; // Never-taken branch
Anton Korobeynikov53422f62008-02-20 11:10:28 +00003013 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003014 break;
3015 case ISD::VECTOR_SHUFFLE:
Edwin Törökbd448e32009-07-14 16:55:14 +00003016 llvm_unreachable("should use getVectorShuffle constructor!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 break;
3018 case ISD::BIT_CONVERT:
3019 // Fold bit_convert nodes from a type to themselves.
3020 if (N1.getValueType() == VT)
3021 return N1;
3022 break;
3023 }
3024
3025 // Memoize node if it doesn't produce a flag.
3026 SDNode *N;
3027 SDVTList VTs = getVTList(VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003028 if (VT != MVT::Flag) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003029 SDValue Ops[] = { N1, N2, N3 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003030 FoldingSetNodeID ID;
3031 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3032 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00003033 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003034 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00003035
Dan Gohman7616a1e2010-03-18 18:49:47 +00003036 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003037 CSEMap.InsertNode(N, IP);
3038 } else {
Dan Gohman7616a1e2010-03-18 18:49:47 +00003039 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003040 }
Daniel Dunbarb3fc6a92009-12-16 20:10:05 +00003041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003043#ifndef NDEBUG
3044 VerifyNode(N);
3045#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00003046 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003047}
3048
Owen Andersonac9de032009-08-10 22:56:29 +00003049SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00003050 SDValue N1, SDValue N2, SDValue N3,
3051 SDValue N4) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003052 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00003053 return getNode(Opcode, DL, VT, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054}
3055
Owen Andersonac9de032009-08-10 22:56:29 +00003056SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00003057 SDValue N1, SDValue N2, SDValue N3,
3058 SDValue N4, SDValue N5) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003059 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00003060 return getNode(Opcode, DL, VT, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061}
3062
Dan Gohman9178de12009-08-05 01:29:28 +00003063/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3064/// the incoming stack arguments to be loaded from the stack.
3065SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3066 SmallVector<SDValue, 8> ArgChains;
3067
3068 // Include the original chain at the beginning of the list. When this is
3069 // used by target LowerCall hooks, this helps legalize find the
3070 // CALLSEQ_BEGIN node.
3071 ArgChains.push_back(Chain);
3072
3073 // Add a chain value for each stack argument.
3074 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3075 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3076 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3077 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3078 if (FI->getIndex() < 0)
3079 ArgChains.push_back(SDValue(L, 1));
3080
3081 // Build a tokenfactor for all the chains.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003082 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman9178de12009-08-05 01:29:28 +00003083 &ArgChains[0], ArgChains.size());
3084}
3085
Dan Gohmane8b391e2008-04-12 04:36:06 +00003086/// getMemsetValue - Vectorized representation of the memset value
3087/// operand.
Owen Andersonac9de032009-08-10 22:56:29 +00003088static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003089 DebugLoc dl) {
Evan Cheng52ff54e2010-04-02 19:36:14 +00003090 assert(Value.getOpcode() != ISD::UNDEF);
3091
Chris Lattnerae64c542010-02-24 22:44:06 +00003092 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003093 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003094 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003095 unsigned Shift = 8;
Evan Cheng8c590372008-05-15 08:39:06 +00003096 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003097 Val = (Val << Shift) | Val;
3098 Shift <<= 1;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003099 }
Duncan Sands92c43912008-06-06 12:08:01 +00003100 if (VT.isInteger())
Evan Cheng8c590372008-05-15 08:39:06 +00003101 return DAG.getConstant(Val, VT);
3102 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003103 }
Evan Cheng8c590372008-05-15 08:39:06 +00003104
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00003105 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003106 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Evan Cheng8c590372008-05-15 08:39:06 +00003107 unsigned Shift = 8;
3108 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003109 Value = DAG.getNode(ISD::OR, dl, VT,
3110 DAG.getNode(ISD::SHL, dl, VT, Value,
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00003111 DAG.getConstant(Shift,
3112 TLI.getShiftAmountTy())),
3113 Value);
Evan Cheng8c590372008-05-15 08:39:06 +00003114 Shift <<= 1;
3115 }
3116
3117 return Value;
Rafael Espindola80825902007-10-19 10:41:11 +00003118}
3119
Dan Gohmane8b391e2008-04-12 04:36:06 +00003120/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3121/// used when a memcpy is turned into a memset when the source is a constant
3122/// string ptr.
Owen Andersonac9de032009-08-10 22:56:29 +00003123static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner9c220f62009-09-20 17:32:21 +00003124 const TargetLowering &TLI,
3125 std::string &Str, unsigned Offset) {
Evan Cheng833501d2008-06-30 07:31:25 +00003126 // Handle vector with all elements zero.
3127 if (Str.empty()) {
3128 if (VT.isInteger())
3129 return DAG.getConstant(0, VT);
Evan Cheng0b592c02010-04-01 06:04:33 +00003130 else if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
3131 VT.getSimpleVT().SimpleTy == MVT::f64)
3132 return DAG.getConstantFP(0.0, VT);
3133 else if (VT.isVector()) {
3134 unsigned NumElts = VT.getVectorNumElements();
3135 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
3136 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3137 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3138 EltVT, NumElts)));
3139 } else
3140 llvm_unreachable("Expected type!");
Evan Cheng833501d2008-06-30 07:31:25 +00003141 }
3142
Duncan Sands92c43912008-06-06 12:08:01 +00003143 assert(!VT.isVector() && "Can't handle vector type here!");
3144 unsigned NumBits = VT.getSizeInBits();
Evan Cheng8c590372008-05-15 08:39:06 +00003145 unsigned MSB = NumBits / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003146 uint64_t Val = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003147 if (TLI.isLittleEndian())
3148 Offset = Offset + MSB - 1;
3149 for (unsigned i = 0; i != MSB; ++i) {
3150 Val = (Val << 8) | (unsigned char)Str[Offset];
3151 Offset += TLI.isLittleEndian() ? -1 : 1;
3152 }
3153 return DAG.getConstant(Val, VT);
Rafael Espindola80825902007-10-19 10:41:11 +00003154}
3155
Scott Michel91099d62009-02-17 22:15:04 +00003156/// getMemBasePlusOffset - Returns base and offset node for the
Evan Cheng8c590372008-05-15 08:39:06 +00003157///
Dan Gohman8181bd12008-07-27 21:46:04 +00003158static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003159 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003160 EVT VT = Base.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003161 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesenf2103ee2009-02-03 01:55:44 +00003162 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohmane8b391e2008-04-12 04:36:06 +00003163}
3164
Evan Cheng8c590372008-05-15 08:39:06 +00003165/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3166///
Dan Gohman8181bd12008-07-27 21:46:04 +00003167static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Cheng8c590372008-05-15 08:39:06 +00003168 unsigned SrcDelta = 0;
3169 GlobalAddressSDNode *G = NULL;
3170 if (Src.getOpcode() == ISD::GlobalAddress)
3171 G = cast<GlobalAddressSDNode>(Src);
3172 else if (Src.getOpcode() == ISD::ADD &&
3173 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3174 Src.getOperand(1).getOpcode() == ISD::Constant) {
3175 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003176 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Cheng8c590372008-05-15 08:39:06 +00003177 }
3178 if (!G)
3179 return false;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003180
Evan Cheng8c590372008-05-15 08:39:06 +00003181 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003182 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3183 return true;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003184
Evan Cheng8c590372008-05-15 08:39:06 +00003185 return false;
3186}
Dan Gohmane8b391e2008-04-12 04:36:06 +00003187
Evan Cheng0b592c02010-04-01 06:04:33 +00003188/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3189/// to replace the memset / memcpy. Return true if the number of memory ops
3190/// is below the threshold. It returns the types of the sequence of
3191/// memory ops to perform memset / memcpy by reference.
3192static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng0b592c02010-04-01 06:04:33 +00003193 unsigned Limit, uint64_t Size,
3194 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng52ff54e2010-04-02 19:36:14 +00003195 bool NonScalarIntSafe,
Evan Cheng0b592c02010-04-01 06:04:33 +00003196 SelectionDAG &DAG,
3197 const TargetLowering &TLI) {
3198 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3199 "Expecting memcpy / memset source to meet alignment requirement!");
3200 // If 'SrcAlign' is zero, that means the memory operation does not need load
3201 // the value, i.e. memset or memcpy from constant string. Otherwise, it's
3202 // the inferred alignment of the source. 'DstAlign', on the other hand, is the
3203 // specified alignment of the memory operation. If it is zero, that means
3204 // it's possible to change the alignment of the destination.
Evan Cheng52ff54e2010-04-02 19:36:14 +00003205 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
3206 NonScalarIntSafe, DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003207
Chris Lattnerd700ed32010-03-07 07:45:08 +00003208 if (VT == MVT::Other) {
Evan Cheng0b592c02010-04-01 06:04:33 +00003209 VT = TLI.getPointerTy();
3210 const Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3211 if (DstAlign >= TLI.getTargetData()->getABITypeAlignment(Ty) ||
3212 TLI.allowsUnalignedMemoryAccesses(VT)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003213 VT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00003214 } else {
Evan Cheng0b592c02010-04-01 06:04:33 +00003215 switch (DstAlign & 7) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003216 case 0: VT = MVT::i64; break;
3217 case 4: VT = MVT::i32; break;
3218 case 2: VT = MVT::i16; break;
3219 default: VT = MVT::i8; break;
Evan Cheng8c590372008-05-15 08:39:06 +00003220 }
3221 }
3222
Owen Anderson2dd68a22009-08-11 21:59:30 +00003223 MVT LVT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00003224 while (!TLI.isTypeLegal(LVT))
Owen Anderson2dd68a22009-08-11 21:59:30 +00003225 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands92c43912008-06-06 12:08:01 +00003226 assert(LVT.isInteger());
Evan Cheng8c590372008-05-15 08:39:06 +00003227
Duncan Sandsec142ee2008-06-08 20:54:56 +00003228 if (VT.bitsGT(LVT))
Evan Cheng8c590372008-05-15 08:39:06 +00003229 VT = LVT;
3230 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003231
3232 unsigned NumMemOps = 0;
3233 while (Size != 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00003234 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003235 while (VTSize > Size) {
Evan Cheng8c590372008-05-15 08:39:06 +00003236 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng0b592c02010-04-01 06:04:33 +00003237 if (VT.isVector() || VT.isFloatingPoint()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003238 VT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00003239 while (!TLI.isTypeLegal(VT))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003240 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands92c43912008-06-06 12:08:01 +00003241 VTSize = VT.getSizeInBits() / 8;
Evan Cheng8c590372008-05-15 08:39:06 +00003242 } else {
Dale Johannesen5ebb9302009-06-22 20:59:07 +00003243 // This can result in a type that is not legal on the target, e.g.
3244 // 1 or 2 bytes on PPC.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003245 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Cheng8c590372008-05-15 08:39:06 +00003246 VTSize >>= 1;
3247 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003248 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003249
3250 if (++NumMemOps > Limit)
3251 return false;
3252 MemOps.push_back(VT);
3253 Size -= VTSize;
3254 }
3255
3256 return true;
3257}
3258
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003259static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1ac55a62010-03-30 18:08:53 +00003260 SDValue Chain, SDValue Dst,
3261 SDValue Src, uint64_t Size,
Mon P Wang483af3c2010-04-04 03:10:48 +00003262 unsigned Align, bool isVol,
3263 bool AlwaysInline,
Evan Cheng1ac55a62010-03-30 18:08:53 +00003264 const Value *DstSV, uint64_t DstSVOff,
3265 const Value *SrcSV, uint64_t SrcSVOff) {
Evan Cheng52ff54e2010-04-02 19:36:14 +00003266 // Turn a memcpy of undef to nop.
3267 if (Src.getOpcode() == ISD::UNDEF)
3268 return Chain;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003269
Dan Gohman42d311c2008-05-29 19:42:22 +00003270 // Expand memcpy to a series of load and store ops if the size operand falls
3271 // below a certain threshold.
Evan Cheng52ff54e2010-04-02 19:36:14 +00003272 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersonac9de032009-08-10 22:56:29 +00003273 std::vector<EVT> MemOps;
Dan Gohman88566ae2008-10-03 17:56:45 +00003274 uint64_t Limit = -1ULL;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003275 if (!AlwaysInline)
3276 Limit = TLI.getMaxStoresPerMemcpy();
Evan Cheng0b592c02010-04-01 06:04:33 +00003277 bool DstAlignCanChange = false;
3278 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3279 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3280 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3281 DstAlignCanChange = true;
3282 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3283 if (Align > SrcAlign)
3284 SrcAlign = Align;
Evan Cheng833501d2008-06-30 07:31:25 +00003285 std::string Str;
Evan Cheng0b592c02010-04-01 06:04:33 +00003286 bool CopyFromStr = isMemSrcFromString(Src, Str);
3287 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng7fed2682010-04-01 18:19:11 +00003288 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng0b592c02010-04-01 06:04:33 +00003289 (DstAlignCanChange ? 0 : Align),
Evan Cheng7fed2682010-04-01 18:19:11 +00003290 (isZeroStr ? 0 : SrcAlign), true, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003291 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003292
Evan Cheng0b592c02010-04-01 06:04:33 +00003293 if (DstAlignCanChange) {
3294 const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
3295 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3296 if (NewAlign > Align) {
3297 // Give the stack frame object a larger alignment if needed.
3298 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3299 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3300 Align = NewAlign;
3301 }
3302 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003303
Dan Gohman8181bd12008-07-27 21:46:04 +00003304 SmallVector<SDValue, 8> OutChains;
Evan Cheng8c590372008-05-15 08:39:06 +00003305 unsigned NumMemOps = MemOps.size();
Evan Cheng833501d2008-06-30 07:31:25 +00003306 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner9c220f62009-09-20 17:32:21 +00003307 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersonac9de032009-08-10 22:56:29 +00003308 EVT VT = MemOps[i];
Duncan Sands92c43912008-06-06 12:08:01 +00003309 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003310 SDValue Value, Store;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003311
Evan Cheng0b592c02010-04-01 06:04:33 +00003312 if (CopyFromStr &&
3313 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Cheng8c590372008-05-15 08:39:06 +00003314 // It's unlikely a store of a vector immediate can be done in a single
3315 // instruction. It would require a load from a constantpool first.
Evan Cheng0b592c02010-04-01 06:04:33 +00003316 // We only handle zero vectors here.
Evan Cheng833501d2008-06-30 07:31:25 +00003317 // FIXME: Handle other cases where store of vector immediate is done in
3318 // a single instruction.
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003319 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
3320 Store = DAG.getStore(Chain, dl, Value,
Evan Cheng8c590372008-05-15 08:39:06 +00003321 getMemBasePlusOffset(Dst, DstOff, DAG),
Mon P Wang483af3c2010-04-04 03:10:48 +00003322 DstSV, DstSVOff + DstOff, isVol, false, Align);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003323 } else {
Dale Johannesen5ebb9302009-06-22 20:59:07 +00003324 // The type might not be legal for the target. This should only happen
3325 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen6933df92009-06-24 17:11:31 +00003326 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3327 // to Load/Store if NVT==VT.
Dale Johannesen5ebb9302009-06-22 20:59:07 +00003328 // FIXME does the case above also need this?
Owen Anderson77f4eb52009-08-12 00:36:31 +00003329 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen6933df92009-06-24 17:11:31 +00003330 assert(NVT.bitsGE(VT));
3331 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
3332 getMemBasePlusOffset(Src, SrcOff, DAG),
Mon P Wang483af3c2010-04-04 03:10:48 +00003333 SrcSV, SrcSVOff + SrcOff, VT, isVol, false,
Evan Cheng0b592c02010-04-01 06:04:33 +00003334 MinAlign(SrcAlign, SrcOff));
Dale Johannesen6933df92009-06-24 17:11:31 +00003335 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene7c5a5062010-02-15 17:00:31 +00003336 getMemBasePlusOffset(Dst, DstOff, DAG),
Mon P Wang483af3c2010-04-04 03:10:48 +00003337 DstSV, DstSVOff + DstOff, VT, isVol, false,
Evan Cheng0b592c02010-04-01 06:04:33 +00003338 Align);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003339 }
3340 OutChains.push_back(Store);
3341 SrcOff += VTSize;
3342 DstOff += VTSize;
3343 }
3344
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003345 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003346 &OutChains[0], OutChains.size());
3347}
3348
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003349static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng0b592c02010-04-01 06:04:33 +00003350 SDValue Chain, SDValue Dst,
3351 SDValue Src, uint64_t Size,
Mon P Wang483af3c2010-04-04 03:10:48 +00003352 unsigned Align, bool isVol,
3353 bool AlwaysInline,
Evan Cheng0b592c02010-04-01 06:04:33 +00003354 const Value *DstSV, uint64_t DstSVOff,
3355 const Value *SrcSV, uint64_t SrcSVOff) {
Evan Cheng52ff54e2010-04-02 19:36:14 +00003356 // Turn a memmove of undef to nop.
3357 if (Src.getOpcode() == ISD::UNDEF)
3358 return Chain;
Dan Gohman42d311c2008-05-29 19:42:22 +00003359
3360 // Expand memmove to a series of load and store ops if the size operand falls
3361 // below a certain threshold.
Evan Cheng52ff54e2010-04-02 19:36:14 +00003362 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersonac9de032009-08-10 22:56:29 +00003363 std::vector<EVT> MemOps;
Dan Gohman88566ae2008-10-03 17:56:45 +00003364 uint64_t Limit = -1ULL;
Dan Gohman42d311c2008-05-29 19:42:22 +00003365 if (!AlwaysInline)
3366 Limit = TLI.getMaxStoresPerMemmove();
Evan Cheng0b592c02010-04-01 06:04:33 +00003367 bool DstAlignCanChange = false;
3368 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3369 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3370 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3371 DstAlignCanChange = true;
3372 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3373 if (Align > SrcAlign)
3374 SrcAlign = Align;
3375
Evan Cheng7fed2682010-04-01 18:19:11 +00003376 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng0b592c02010-04-01 06:04:33 +00003377 (DstAlignCanChange ? 0 : Align),
Evan Cheng7fed2682010-04-01 18:19:11 +00003378 SrcAlign, true, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003379 return SDValue();
Dan Gohman42d311c2008-05-29 19:42:22 +00003380
Evan Cheng0b592c02010-04-01 06:04:33 +00003381 if (DstAlignCanChange) {
3382 const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
3383 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3384 if (NewAlign > Align) {
3385 // Give the stack frame object a larger alignment if needed.
3386 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3387 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3388 Align = NewAlign;
3389 }
3390 }
Dan Gohman42d311c2008-05-29 19:42:22 +00003391
Evan Cheng0b592c02010-04-01 06:04:33 +00003392 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003393 SmallVector<SDValue, 8> LoadValues;
3394 SmallVector<SDValue, 8> LoadChains;
3395 SmallVector<SDValue, 8> OutChains;
Dan Gohman42d311c2008-05-29 19:42:22 +00003396 unsigned NumMemOps = MemOps.size();
3397 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersonac9de032009-08-10 22:56:29 +00003398 EVT VT = MemOps[i];
Duncan Sands92c43912008-06-06 12:08:01 +00003399 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003400 SDValue Value, Store;
Dan Gohman42d311c2008-05-29 19:42:22 +00003401
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003402 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman42d311c2008-05-29 19:42:22 +00003403 getMemBasePlusOffset(Src, SrcOff, DAG),
Mon P Wang483af3c2010-04-04 03:10:48 +00003404 SrcSV, SrcSVOff + SrcOff, isVol, false, SrcAlign);
Dan Gohman42d311c2008-05-29 19:42:22 +00003405 LoadValues.push_back(Value);
3406 LoadChains.push_back(Value.getValue(1));
3407 SrcOff += VTSize;
3408 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003409 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman42d311c2008-05-29 19:42:22 +00003410 &LoadChains[0], LoadChains.size());
3411 OutChains.clear();
3412 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersonac9de032009-08-10 22:56:29 +00003413 EVT VT = MemOps[i];
Duncan Sands92c43912008-06-06 12:08:01 +00003414 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003415 SDValue Value, Store;
Dan Gohman42d311c2008-05-29 19:42:22 +00003416
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003417 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman42d311c2008-05-29 19:42:22 +00003418 getMemBasePlusOffset(Dst, DstOff, DAG),
Mon P Wang483af3c2010-04-04 03:10:48 +00003419 DstSV, DstSVOff + DstOff, isVol, false, Align);
Dan Gohman42d311c2008-05-29 19:42:22 +00003420 OutChains.push_back(Store);
3421 DstOff += VTSize;
3422 }
3423
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003424 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman42d311c2008-05-29 19:42:22 +00003425 &OutChains[0], OutChains.size());
3426}
3427
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003428static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng0b592c02010-04-01 06:04:33 +00003429 SDValue Chain, SDValue Dst,
3430 SDValue Src, uint64_t Size,
Mon P Wang483af3c2010-04-04 03:10:48 +00003431 unsigned Align, bool isVol,
Evan Cheng0b592c02010-04-01 06:04:33 +00003432 const Value *DstSV, uint64_t DstSVOff) {
Evan Cheng52ff54e2010-04-02 19:36:14 +00003433 // Turn a memset of undef to nop.
3434 if (Src.getOpcode() == ISD::UNDEF)
3435 return Chain;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003436
3437 // Expand memset to a series of load/store ops if the size operand
3438 // falls below a certain threshold.
Evan Cheng52ff54e2010-04-02 19:36:14 +00003439 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersonac9de032009-08-10 22:56:29 +00003440 std::vector<EVT> MemOps;
Evan Cheng0b592c02010-04-01 06:04:33 +00003441 bool DstAlignCanChange = false;
3442 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3443 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3444 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3445 DstAlignCanChange = true;
Evan Cheng52ff54e2010-04-02 19:36:14 +00003446 bool NonScalarIntSafe =
3447 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng7fed2682010-04-01 18:19:11 +00003448 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(),
Evan Cheng0b592c02010-04-01 06:04:33 +00003449 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng52ff54e2010-04-02 19:36:14 +00003450 NonScalarIntSafe, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003451 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003452
Evan Cheng0b592c02010-04-01 06:04:33 +00003453 if (DstAlignCanChange) {
3454 const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
3455 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3456 if (NewAlign > Align) {
3457 // Give the stack frame object a larger alignment if needed.
3458 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3459 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3460 Align = NewAlign;
3461 }
3462 }
3463
Dan Gohman8181bd12008-07-27 21:46:04 +00003464 SmallVector<SDValue, 8> OutChains;
Dan Gohman65118f42008-04-28 17:15:20 +00003465 uint64_t DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003466 unsigned NumMemOps = MemOps.size();
3467 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersonac9de032009-08-10 22:56:29 +00003468 EVT VT = MemOps[i];
Duncan Sands92c43912008-06-06 12:08:01 +00003469 unsigned VTSize = VT.getSizeInBits() / 8;
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003470 SDValue Value = getMemsetValue(Src, VT, DAG, dl);
3471 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner23d3bdf2008-10-28 07:10:51 +00003472 getMemBasePlusOffset(Dst, DstOff, DAG),
Mon P Wang483af3c2010-04-04 03:10:48 +00003473 DstSV, DstSVOff + DstOff, isVol, false, 0);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003474 OutChains.push_back(Store);
3475 DstOff += VTSize;
3476 }
3477
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003478 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003479 &OutChains[0], OutChains.size());
3480}
3481
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003482SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003483 SDValue Src, SDValue Size,
Mon P Wang483af3c2010-04-04 03:10:48 +00003484 unsigned Align, bool isVol, bool AlwaysInline,
Dan Gohman8181bd12008-07-27 21:46:04 +00003485 const Value *DstSV, uint64_t DstSVOff,
3486 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003487
3488 // Check to see if we should lower the memcpy to loads and stores first.
3489 // For cases within the target-specified limits, this is the best choice.
3490 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3491 if (ConstantSize) {
3492 // Memcpy with size zero? Just return the original chain.
3493 if (ConstantSize->isNullValue())
3494 return Chain;
3495
Evan Cheng0b592c02010-04-01 06:04:33 +00003496 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3497 ConstantSize->getZExtValue(),Align,
Mon P Wang483af3c2010-04-04 03:10:48 +00003498 isVol, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003499 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003500 return Result;
3501 }
3502
3503 // Then check to see if we should lower the memcpy with target-specific
3504 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003505 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003506 TLI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang483af3c2010-04-04 03:10:48 +00003507 isVol, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00003508 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003509 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003510 return Result;
3511
3512 // If we really need inline code and the target declined to provide it,
3513 // use a (potentially long) sequence of loads and stores.
3514 if (AlwaysInline) {
3515 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003516 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang483af3c2010-04-04 03:10:48 +00003517 ConstantSize->getZExtValue(), Align, isVol,
3518 true, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003519 }
3520
3521 // Emit a library call.
3522 TargetLowering::ArgListTy Args;
3523 TargetLowering::ArgListEntry Entry;
Owen Anderson35b47072009-08-13 21:58:54 +00003524 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohmane8b391e2008-04-12 04:36:06 +00003525 Entry.Node = Dst; Args.push_back(Entry);
3526 Entry.Node = Src; Args.push_back(Entry);
3527 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003528 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003529 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00003530 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikovcdab71f2009-08-14 20:10:52 +00003531 false, false, false, false, 0,
3532 TLI.getLibcallCallingConv(RTLIB::MEMCPY), false,
Dan Gohman9178de12009-08-05 01:29:28 +00003533 /*isReturnValueUsed=*/false,
Eric Christopher440d9eb2009-08-22 00:40:45 +00003534 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaf4e64d62009-07-30 09:12:56 +00003535 TLI.getPointerTy()),
Bill Wendling1ca34452010-03-02 01:55:18 +00003536 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003537 return CallResult.second;
3538}
3539
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003540SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003541 SDValue Src, SDValue Size,
Mon P Wang483af3c2010-04-04 03:10:48 +00003542 unsigned Align, bool isVol,
Dan Gohman8181bd12008-07-27 21:46:04 +00003543 const Value *DstSV, uint64_t DstSVOff,
3544 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003545
Dan Gohman42d311c2008-05-29 19:42:22 +00003546 // Check to see if we should lower the memmove to loads and stores first.
3547 // For cases within the target-specified limits, this is the best choice.
3548 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3549 if (ConstantSize) {
3550 // Memmove with size zero? Just return the original chain.
3551 if (ConstantSize->isNullValue())
3552 return Chain;
3553
Dan Gohman8181bd12008-07-27 21:46:04 +00003554 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003555 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang483af3c2010-04-04 03:10:48 +00003556 ConstantSize->getZExtValue(), Align, isVol,
3557 false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003558 if (Result.getNode())
Dan Gohman42d311c2008-05-29 19:42:22 +00003559 return Result;
3560 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003561
3562 // Then check to see if we should lower the memmove with target-specific
3563 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003564 SDValue Result =
Mon P Wang483af3c2010-04-04 03:10:48 +00003565 TLI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Dan Gohman65118f42008-04-28 17:15:20 +00003566 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003567 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003568 return Result;
3569
3570 // Emit a library call.
Mon P Wang483af3c2010-04-04 03:10:48 +00003571 assert(!isVol && "library memmove does not support volatile");
Dan Gohmane8b391e2008-04-12 04:36:06 +00003572 TargetLowering::ArgListTy Args;
3573 TargetLowering::ArgListEntry Entry;
Owen Anderson35b47072009-08-13 21:58:54 +00003574 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohmane8b391e2008-04-12 04:36:06 +00003575 Entry.Node = Dst; Args.push_back(Entry);
3576 Entry.Node = Src; Args.push_back(Entry);
3577 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003578 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003579 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00003580 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikovcdab71f2009-08-14 20:10:52 +00003581 false, false, false, false, 0,
3582 TLI.getLibcallCallingConv(RTLIB::MEMMOVE), false,
Dan Gohman9178de12009-08-05 01:29:28 +00003583 /*isReturnValueUsed=*/false,
Eric Christopher440d9eb2009-08-22 00:40:45 +00003584 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaf4e64d62009-07-30 09:12:56 +00003585 TLI.getPointerTy()),
Bill Wendling1ca34452010-03-02 01:55:18 +00003586 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003587 return CallResult.second;
3588}
3589
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003590SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003591 SDValue Src, SDValue Size,
Mon P Wang483af3c2010-04-04 03:10:48 +00003592 unsigned Align, bool isVol,
Dan Gohman8181bd12008-07-27 21:46:04 +00003593 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003594
3595 // Check to see if we should lower the memset to stores first.
3596 // For cases within the target-specified limits, this is the best choice.
3597 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3598 if (ConstantSize) {
3599 // Memset with size zero? Just return the original chain.
3600 if (ConstantSize->isNullValue())
3601 return Chain;
3602
Mon P Wang483af3c2010-04-04 03:10:48 +00003603 SDValue Result =
3604 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
3605 Align, isVol, DstSV, DstSVOff);
3606
Gabor Greif1c80d112008-08-28 21:40:38 +00003607 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003608 return Result;
3609 }
3610
3611 // Then check to see if we should lower the memset with target-specific
3612 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003613 SDValue Result =
Mon P Wang483af3c2010-04-04 03:10:48 +00003614 TLI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Bill Wendling4b2e3782008-10-01 00:59:58 +00003615 DstSV, DstSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003616 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003617 return Result;
3618
3619 // Emit a library call.
Mon P Wang483af3c2010-04-04 03:10:48 +00003620 assert(!isVol && "library memset does not support volatile");
Owen Anderson35b47072009-08-13 21:58:54 +00003621 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohmane8b391e2008-04-12 04:36:06 +00003622 TargetLowering::ArgListTy Args;
3623 TargetLowering::ArgListEntry Entry;
3624 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3625 Args.push_back(Entry);
3626 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003627 if (Src.getValueType().bitsGT(MVT::i32))
3628 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003629 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003630 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson35b47072009-08-13 21:58:54 +00003631 Entry.Node = Src;
3632 Entry.Ty = Type::getInt32Ty(*getContext());
3633 Entry.isSExt = true;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003634 Args.push_back(Entry);
Owen Anderson35b47072009-08-13 21:58:54 +00003635 Entry.Node = Size;
3636 Entry.Ty = IntPtrTy;
3637 Entry.isSExt = false;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003638 Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003639 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003640 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00003641 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikovcdab71f2009-08-14 20:10:52 +00003642 false, false, false, false, 0,
3643 TLI.getLibcallCallingConv(RTLIB::MEMSET), false,
Dan Gohman9178de12009-08-05 01:29:28 +00003644 /*isReturnValueUsed=*/false,
Eric Christopher440d9eb2009-08-22 00:40:45 +00003645 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaf4e64d62009-07-30 09:12:56 +00003646 TLI.getPointerTy()),
Bill Wendling1ca34452010-03-02 01:55:18 +00003647 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003648 return CallResult.second;
Rafael Espindola80825902007-10-19 10:41:11 +00003649}
3650
Owen Andersonac9de032009-08-10 22:56:29 +00003651SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003652 SDValue Chain,
Scott Michel91099d62009-02-17 22:15:04 +00003653 SDValue Ptr, SDValue Cmp,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003654 SDValue Swp, const Value* PtrVal,
3655 unsigned Alignment) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003656 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3657 Alignment = getEVTAlignment(MemVT);
3658
3659 // Check if the memory reference references a frame index
3660 if (!PtrVal)
3661 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003662 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3663 PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003664
Evan Cheng174e2cf2009-10-18 18:16:27 +00003665 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003666 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3667
3668 // For now, atomics are considered to be volatile always.
3669 Flags |= MachineMemOperand::MOVolatile;
3670
3671 MachineMemOperand *MMO =
3672 MF.getMachineMemOperand(PtrVal, Flags, 0,
3673 MemVT.getStoreSize(), Alignment);
3674
3675 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO);
3676}
3677
3678SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3679 SDValue Chain,
3680 SDValue Ptr, SDValue Cmp,
3681 SDValue Swp, MachineMemOperand *MMO) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003682 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3683 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3684
Owen Andersonac9de032009-08-10 22:56:29 +00003685 EVT VT = Cmp.getValueType();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003686
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003687 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003688 FoldingSetNodeID ID;
Dan Gohman9d0f5022009-02-03 00:08:45 +00003689 ID.AddInteger(MemVT.getRawBits());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003690 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3691 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3692 void* IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003693 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3694 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003695 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003696 }
Dan Gohman7616a1e2010-03-18 18:49:47 +00003697 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
3698 Ptr, Cmp, Swp, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003699 CSEMap.InsertNode(N, IP);
3700 AllNodes.push_back(N);
3701 return SDValue(N, 0);
3702}
3703
Owen Andersonac9de032009-08-10 22:56:29 +00003704SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003705 SDValue Chain,
Scott Michel91099d62009-02-17 22:15:04 +00003706 SDValue Ptr, SDValue Val,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003707 const Value* PtrVal,
3708 unsigned Alignment) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003709 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3710 Alignment = getEVTAlignment(MemVT);
3711
3712 // Check if the memory reference references a frame index
3713 if (!PtrVal)
3714 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003715 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3716 PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003717
Evan Cheng174e2cf2009-10-18 18:16:27 +00003718 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003719 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3720
3721 // For now, atomics are considered to be volatile always.
3722 Flags |= MachineMemOperand::MOVolatile;
3723
3724 MachineMemOperand *MMO =
3725 MF.getMachineMemOperand(PtrVal, Flags, 0,
3726 MemVT.getStoreSize(), Alignment);
3727
3728 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO);
3729}
3730
3731SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3732 SDValue Chain,
3733 SDValue Ptr, SDValue Val,
3734 MachineMemOperand *MMO) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003735 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3736 Opcode == ISD::ATOMIC_LOAD_SUB ||
3737 Opcode == ISD::ATOMIC_LOAD_AND ||
3738 Opcode == ISD::ATOMIC_LOAD_OR ||
3739 Opcode == ISD::ATOMIC_LOAD_XOR ||
3740 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michel91099d62009-02-17 22:15:04 +00003741 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003742 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michel91099d62009-02-17 22:15:04 +00003743 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003744 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3745 Opcode == ISD::ATOMIC_SWAP) &&
3746 "Invalid Atomic Op");
3747
Owen Andersonac9de032009-08-10 22:56:29 +00003748 EVT VT = Val.getValueType();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003749
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003750 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003751 FoldingSetNodeID ID;
Dan Gohman9d0f5022009-02-03 00:08:45 +00003752 ID.AddInteger(MemVT.getRawBits());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003753 SDValue Ops[] = {Chain, Ptr, Val};
3754 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3755 void* IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003756 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3757 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003758 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003759 }
Dan Gohman7616a1e2010-03-18 18:49:47 +00003760 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
3761 Ptr, Val, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003762 CSEMap.InsertNode(N, IP);
3763 AllNodes.push_back(N);
3764 return SDValue(N, 0);
3765}
3766
Duncan Sands698842f2008-07-02 17:40:58 +00003767/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3768/// Allowed to return something different (and simpler) if Simplify is true.
Dale Johannesenf3b91132009-02-02 20:47:48 +00003769SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3770 DebugLoc dl) {
3771 if (NumOps == 1)
3772 return Ops[0];
3773
Owen Andersonac9de032009-08-10 22:56:29 +00003774 SmallVector<EVT, 4> VTs;
Dale Johannesenf3b91132009-02-02 20:47:48 +00003775 VTs.reserve(NumOps);
3776 for (unsigned i = 0; i < NumOps; ++i)
3777 VTs.push_back(Ops[i].getValueType());
Scott Michel91099d62009-02-17 22:15:04 +00003778 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesenf3b91132009-02-02 20:47:48 +00003779 Ops, NumOps);
3780}
3781
Dan Gohman8181bd12008-07-27 21:46:04 +00003782SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003783SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersonac9de032009-08-10 22:56:29 +00003784 const EVT *VTs, unsigned NumVTs,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003785 const SDValue *Ops, unsigned NumOps,
Owen Andersonac9de032009-08-10 22:56:29 +00003786 EVT MemVT, const Value *srcValue, int SVOff,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003787 unsigned Align, bool Vol,
3788 bool ReadMem, bool WriteMem) {
3789 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3790 MemVT, srcValue, SVOff, Align, Vol,
3791 ReadMem, WriteMem);
3792}
3793
3794SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003795SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3796 const SDValue *Ops, unsigned NumOps,
Owen Andersonac9de032009-08-10 22:56:29 +00003797 EVT MemVT, const Value *srcValue, int SVOff,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003798 unsigned Align, bool Vol,
3799 bool ReadMem, bool WriteMem) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003800 if (Align == 0) // Ensure that codegen never sees alignment 0
3801 Align = getEVTAlignment(MemVT);
3802
3803 MachineFunction &MF = getMachineFunction();
3804 unsigned Flags = 0;
3805 if (WriteMem)
3806 Flags |= MachineMemOperand::MOStore;
3807 if (ReadMem)
3808 Flags |= MachineMemOperand::MOLoad;
3809 if (Vol)
3810 Flags |= MachineMemOperand::MOVolatile;
3811 MachineMemOperand *MMO =
3812 MF.getMachineMemOperand(srcValue, Flags, SVOff,
3813 MemVT.getStoreSize(), Align);
3814
3815 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
3816}
3817
3818SDValue
3819SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3820 const SDValue *Ops, unsigned NumOps,
3821 EVT MemVT, MachineMemOperand *MMO) {
3822 assert((Opcode == ISD::INTRINSIC_VOID ||
3823 Opcode == ISD::INTRINSIC_W_CHAIN ||
3824 (Opcode <= INT_MAX &&
3825 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
3826 "Opcode is not a memory-accessing opcode!");
3827
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003828 // Memoize the node unless it returns a flag.
3829 MemIntrinsicSDNode *N;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003830 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003831 FoldingSetNodeID ID;
3832 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3833 void *IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003834 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3835 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003836 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003837 }
Scott Michel91099d62009-02-17 22:15:04 +00003838
Dan Gohman7616a1e2010-03-18 18:49:47 +00003839 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
3840 MemVT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003841 CSEMap.InsertNode(N, IP);
3842 } else {
Dan Gohman7616a1e2010-03-18 18:49:47 +00003843 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
3844 MemVT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003845 }
3846 AllNodes.push_back(N);
3847 return SDValue(N, 0);
3848}
3849
3850SDValue
Scott Michel91099d62009-02-17 22:15:04 +00003851SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
Owen Andersonac9de032009-08-10 22:56:29 +00003852 ISD::LoadExtType ExtType, EVT VT, SDValue Chain,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003853 SDValue Ptr, SDValue Offset,
Dan Gohman3bab1f72009-09-23 21:02:20 +00003854 const Value *SV, int SVOffset, EVT MemVT,
David Greene7c5a5062010-02-15 17:00:31 +00003855 bool isVolatile, bool isNonTemporal,
3856 unsigned Alignment) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003857 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Owen Andersonac9de032009-08-10 22:56:29 +00003858 Alignment = getEVTAlignment(VT);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003859
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003860 // Check if the memory reference references a frame index
3861 if (!SV)
3862 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003863 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3864 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003865
Evan Cheng174e2cf2009-10-18 18:16:27 +00003866 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003867 unsigned Flags = MachineMemOperand::MOLoad;
3868 if (isVolatile)
3869 Flags |= MachineMemOperand::MOVolatile;
David Greene7c5a5062010-02-15 17:00:31 +00003870 if (isNonTemporal)
3871 Flags |= MachineMemOperand::MONonTemporal;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003872 MachineMemOperand *MMO =
3873 MF.getMachineMemOperand(SV, Flags, SVOffset,
3874 MemVT.getStoreSize(), Alignment);
3875 return getLoad(AM, dl, ExtType, VT, Chain, Ptr, Offset, MemVT, MMO);
3876}
3877
3878SDValue
3879SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
3880 ISD::LoadExtType ExtType, EVT VT, SDValue Chain,
3881 SDValue Ptr, SDValue Offset, EVT MemVT,
3882 MachineMemOperand *MMO) {
Dan Gohman3bab1f72009-09-23 21:02:20 +00003883 if (VT == MemVT) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003884 ExtType = ISD::NON_EXTLOAD;
3885 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman3bab1f72009-09-23 21:02:20 +00003886 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003887 } else {
3888 // Extending load.
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00003889 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
3890 "Should only be an extending load, not truncating!");
Dan Gohman3bab1f72009-09-23 21:02:20 +00003891 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003892 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00003893 assert(VT.isVector() == MemVT.isVector() &&
3894 "Cannot use trunc store to convert to or from a vector!");
3895 assert((!VT.isVector() ||
3896 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
3897 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003898 }
3899
3900 bool Indexed = AM != ISD::UNINDEXED;
3901 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3902 "Unindexed load with an offset!");
3903
3904 SDVTList VTs = Indexed ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003905 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003906 SDValue Ops[] = { Chain, Ptr, Offset };
3907 FoldingSetNodeID ID;
3908 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman3bab1f72009-09-23 21:02:20 +00003909 ID.AddInteger(MemVT.getRawBits());
David Greene05a001f2010-02-17 20:21:42 +00003910 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
3911 MMO->isNonTemporal()));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003912 void *IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003913 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3914 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003915 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003916 }
Dan Gohman7616a1e2010-03-18 18:49:47 +00003917 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
3918 MemVT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003919 CSEMap.InsertNode(N, IP);
3920 AllNodes.push_back(N);
3921 return SDValue(N, 0);
3922}
3923
Owen Andersonac9de032009-08-10 22:56:29 +00003924SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003925 SDValue Chain, SDValue Ptr,
3926 const Value *SV, int SVOffset,
David Greene7c5a5062010-02-15 17:00:31 +00003927 bool isVolatile, bool isNonTemporal,
3928 unsigned Alignment) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003929 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003930 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
David Greene7c5a5062010-02-15 17:00:31 +00003931 SV, SVOffset, VT, isVolatile, isNonTemporal, Alignment);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003932}
3933
Owen Andersonac9de032009-08-10 22:56:29 +00003934SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003935 SDValue Chain, SDValue Ptr,
3936 const Value *SV,
Dan Gohman3bab1f72009-09-23 21:02:20 +00003937 int SVOffset, EVT MemVT,
David Greene7c5a5062010-02-15 17:00:31 +00003938 bool isVolatile, bool isNonTemporal,
3939 unsigned Alignment) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003940 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003941 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
David Greene7c5a5062010-02-15 17:00:31 +00003942 SV, SVOffset, MemVT, isVolatile, isNonTemporal, Alignment);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003943}
3944
Dan Gohman8181bd12008-07-27 21:46:04 +00003945SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003946SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3947 SDValue Offset, ISD::MemIndexedMode AM) {
3948 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3949 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3950 "Load is already a indexed load!");
3951 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3952 LD->getChain(), Base, Offset, LD->getSrcValue(),
3953 LD->getSrcValueOffset(), LD->getMemoryVT(),
David Greene7c5a5062010-02-15 17:00:31 +00003954 LD->isVolatile(), LD->isNonTemporal(), LD->getAlignment());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003955}
3956
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003957SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3958 SDValue Ptr, const Value *SV, int SVOffset,
David Greene7c5a5062010-02-15 17:00:31 +00003959 bool isVolatile, bool isNonTemporal,
3960 unsigned Alignment) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003961 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003962 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003963
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003964 // Check if the memory reference references a frame index
3965 if (!SV)
3966 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003967 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3968 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003969
Evan Cheng174e2cf2009-10-18 18:16:27 +00003970 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003971 unsigned Flags = MachineMemOperand::MOStore;
3972 if (isVolatile)
3973 Flags |= MachineMemOperand::MOVolatile;
David Greene7c5a5062010-02-15 17:00:31 +00003974 if (isNonTemporal)
3975 Flags |= MachineMemOperand::MONonTemporal;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003976 MachineMemOperand *MMO =
3977 MF.getMachineMemOperand(SV, Flags, SVOffset,
3978 Val.getValueType().getStoreSize(), Alignment);
3979
3980 return getStore(Chain, dl, Val, Ptr, MMO);
3981}
3982
3983SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3984 SDValue Ptr, MachineMemOperand *MMO) {
3985 EVT VT = Val.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003986 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003987 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003988 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3989 FoldingSetNodeID ID;
3990 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003991 ID.AddInteger(VT.getRawBits());
David Greene05a001f2010-02-17 20:21:42 +00003992 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
3993 MMO->isNonTemporal()));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003994 void *IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003995 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3996 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003997 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003998 }
Dan Gohman7616a1e2010-03-18 18:49:47 +00003999 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4000 false, VT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004001 CSEMap.InsertNode(N, IP);
4002 AllNodes.push_back(N);
4003 return SDValue(N, 0);
4004}
4005
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004006SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4007 SDValue Ptr, const Value *SV,
Owen Andersonac9de032009-08-10 22:56:29 +00004008 int SVOffset, EVT SVT,
David Greene7c5a5062010-02-15 17:00:31 +00004009 bool isVolatile, bool isNonTemporal,
4010 unsigned Alignment) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004011 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4012 Alignment = getEVTAlignment(SVT);
4013
4014 // Check if the memory reference references a frame index
4015 if (!SV)
4016 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00004017 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
4018 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004019
Evan Cheng174e2cf2009-10-18 18:16:27 +00004020 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004021 unsigned Flags = MachineMemOperand::MOStore;
4022 if (isVolatile)
4023 Flags |= MachineMemOperand::MOVolatile;
David Greene7c5a5062010-02-15 17:00:31 +00004024 if (isNonTemporal)
4025 Flags |= MachineMemOperand::MONonTemporal;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004026 MachineMemOperand *MMO =
4027 MF.getMachineMemOperand(SV, Flags, SVOffset, SVT.getStoreSize(), Alignment);
4028
4029 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4030}
4031
4032SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4033 SDValue Ptr, EVT SVT,
4034 MachineMemOperand *MMO) {
Owen Andersonac9de032009-08-10 22:56:29 +00004035 EVT VT = Val.getValueType();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004036
4037 if (VT == SVT)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004038 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004039
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00004040 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4041 "Should only be a truncating store, not extending!");
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004042 assert(VT.isInteger() == SVT.isInteger() &&
4043 "Can't do FP-INT conversion!");
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00004044 assert(VT.isVector() == SVT.isVector() &&
4045 "Cannot use trunc store to convert to or from a vector!");
4046 assert((!VT.isVector() ||
4047 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4048 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004049
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004050 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen9bfc0172009-02-06 23:05:02 +00004051 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004052 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4053 FoldingSetNodeID ID;
4054 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004055 ID.AddInteger(SVT.getRawBits());
David Greene05a001f2010-02-17 20:21:42 +00004056 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
4057 MMO->isNonTemporal()));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004058 void *IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004059 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4060 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004061 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004062 }
Dan Gohman7616a1e2010-03-18 18:49:47 +00004063 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4064 true, SVT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004065 CSEMap.InsertNode(N, IP);
4066 AllNodes.push_back(N);
4067 return SDValue(N, 0);
4068}
4069
Dan Gohman8181bd12008-07-27 21:46:04 +00004070SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004071SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4072 SDValue Offset, ISD::MemIndexedMode AM) {
4073 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4074 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4075 "Store is already a indexed store!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004076 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004077 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4078 FoldingSetNodeID ID;
4079 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004080 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00004081 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004082 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00004083 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004084 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00004085
Dan Gohman7616a1e2010-03-18 18:49:47 +00004086 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4087 ST->isTruncatingStore(),
4088 ST->getMemoryVT(),
4089 ST->getMemOperand());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004090 CSEMap.InsertNode(N, IP);
4091 AllNodes.push_back(N);
4092 return SDValue(N, 0);
4093}
4094
Owen Andersonac9de032009-08-10 22:56:29 +00004095SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00004096 SDValue Chain, SDValue Ptr,
4097 SDValue SV) {
4098 SDValue Ops[] = { Chain, Ptr, SV };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004099 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 3);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00004100}
4101
Owen Andersonac9de032009-08-10 22:56:29 +00004102SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00004103 const SDUse *Ops, unsigned NumOps) {
Dan Gohman703ce5d2008-07-07 18:26:29 +00004104 switch (NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00004105 case 0: return getNode(Opcode, DL, VT);
4106 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4107 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4108 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman703ce5d2008-07-07 18:26:29 +00004109 default: break;
4110 }
4111
Dan Gohman8181bd12008-07-27 21:46:04 +00004112 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman703ce5d2008-07-07 18:26:29 +00004113 // the regular getNode logic.
Dan Gohman8181bd12008-07-27 21:46:04 +00004114 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendlingdf4de112009-01-28 22:17:52 +00004115 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman703ce5d2008-07-07 18:26:29 +00004116}
4117
Owen Andersonac9de032009-08-10 22:56:29 +00004118SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00004119 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004120 switch (NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00004121 case 0: return getNode(Opcode, DL, VT);
4122 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4123 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4124 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125 default: break;
4126 }
Scott Michel91099d62009-02-17 22:15:04 +00004127
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004128 switch (Opcode) {
4129 default: break;
4130 case ISD::SELECT_CC: {
4131 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
4132 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4133 "LHS and RHS of condition must have same type!");
4134 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4135 "True and False arms of SelectCC must have same type!");
4136 assert(Ops[2].getValueType() == VT &&
4137 "select_cc node must be of same type as true and false value!");
4138 break;
4139 }
4140 case ISD::BR_CC: {
4141 assert(NumOps == 5 && "BR_CC takes 5 operands!");
4142 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4143 "LHS/RHS of comparison should match types!");
4144 break;
4145 }
4146 }
4147
4148 // Memoize nodes.
4149 SDNode *N;
4150 SDVTList VTs = getVTList(VT);
Bill Wendlingdf4de112009-01-28 22:17:52 +00004151
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004152 if (VT != MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004153 FoldingSetNodeID ID;
4154 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
4155 void *IP = 0;
Bill Wendlingdf4de112009-01-28 22:17:52 +00004156
Bill Wendling339b1532009-12-18 23:32:53 +00004157 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00004158 return SDValue(E, 0);
Bill Wendlingdf4de112009-01-28 22:17:52 +00004159
Dan Gohman7616a1e2010-03-18 18:49:47 +00004160 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004161 CSEMap.InsertNode(N, IP);
4162 } else {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004163 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164 }
Bill Wendlingdf4de112009-01-28 22:17:52 +00004165
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004166 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00004167#ifndef NDEBUG
4168 VerifyNode(N);
4169#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00004170 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171}
4172
Bill Wendlingdf4de112009-01-28 22:17:52 +00004173SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersonac9de032009-08-10 22:56:29 +00004174 const std::vector<EVT> &ResultTys,
Bill Wendlingdf4de112009-01-28 22:17:52 +00004175 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanee036282009-04-09 23:54:40 +00004176 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 Ops, NumOps);
4178}
4179
Bill Wendlingdf4de112009-01-28 22:17:52 +00004180SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersonac9de032009-08-10 22:56:29 +00004181 const EVT *VTs, unsigned NumVTs,
Bill Wendlingdf4de112009-01-28 22:17:52 +00004182 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004183 if (NumVTs == 1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004184 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4185 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michel91099d62009-02-17 22:15:04 +00004186}
4187
Bill Wendlingdf4de112009-01-28 22:17:52 +00004188SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4189 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 if (VTList.NumVTs == 1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004191 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192
Daniel Dunbar43e3a622009-07-19 01:38:38 +00004193#if 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194 switch (Opcode) {
4195 // FIXME: figure out how to safely handle things like
4196 // int foo(int x) { return 1 << (x & 255); }
4197 // int bar() { return foo(256); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198 case ISD::SRA_PARTS:
4199 case ISD::SRL_PARTS:
4200 case ISD::SHL_PARTS:
4201 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004202 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004203 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204 else if (N3.getOpcode() == ISD::AND)
4205 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4206 // If the and is only masking out bits that cannot effect the shift,
4207 // eliminate the and.
Dan Gohman7196cb12010-01-09 02:13:55 +00004208 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004209 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004210 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211 }
4212 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 }
Daniel Dunbar43e3a622009-07-19 01:38:38 +00004214#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215
4216 // Memoize the node unless it returns a flag.
4217 SDNode *N;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004218 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 FoldingSetNodeID ID;
4220 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4221 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00004222 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00004223 return SDValue(E, 0);
Bill Wendling339b1532009-12-18 23:32:53 +00004224
Dan Gohmaned825d12008-07-07 23:02:41 +00004225 if (NumOps == 1) {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004226 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004227 } else if (NumOps == 2) {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004228 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004229 } else if (NumOps == 3) {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004230 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4231 Ops[2]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004232 } else {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004233 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohmaned825d12008-07-07 23:02:41 +00004234 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 CSEMap.InsertNode(N, IP);
4236 } else {
Dan Gohmaned825d12008-07-07 23:02:41 +00004237 if (NumOps == 1) {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004238 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004239 } else if (NumOps == 2) {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004240 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004241 } else if (NumOps == 3) {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004242 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4243 Ops[2]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004244 } else {
Dan Gohman7616a1e2010-03-18 18:49:47 +00004245 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohmaned825d12008-07-07 23:02:41 +00004246 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004247 }
4248 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00004249#ifndef NDEBUG
4250 VerifyNode(N);
4251#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00004252 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253}
4254
Bill Wendlingdf4de112009-01-28 22:17:52 +00004255SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4256 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman798d1272007-10-08 15:49:58 +00004257}
4258
Bill Wendlingdf4de112009-01-28 22:17:52 +00004259SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4260 SDValue N1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004261 SDValue Ops[] = { N1 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004262 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman798d1272007-10-08 15:49:58 +00004263}
4264
Bill Wendlingdf4de112009-01-28 22:17:52 +00004265SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4266 SDValue N1, SDValue N2) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004267 SDValue Ops[] = { N1, N2 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004268 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman798d1272007-10-08 15:49:58 +00004269}
4270
Bill Wendlingdf4de112009-01-28 22:17:52 +00004271SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4272 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004273 SDValue Ops[] = { N1, N2, N3 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004274 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman798d1272007-10-08 15:49:58 +00004275}
4276
Bill Wendlingdf4de112009-01-28 22:17:52 +00004277SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4278 SDValue N1, SDValue N2, SDValue N3,
4279 SDValue N4) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004280 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004281 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman798d1272007-10-08 15:49:58 +00004282}
4283
Bill Wendlingdf4de112009-01-28 22:17:52 +00004284SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4285 SDValue N1, SDValue N2, SDValue N3,
4286 SDValue N4, SDValue N5) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004287 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004288 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman798d1272007-10-08 15:49:58 +00004289}
4290
Owen Andersonac9de032009-08-10 22:56:29 +00004291SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00004292 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004293}
4294
Owen Andersonac9de032009-08-10 22:56:29 +00004295SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004296 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4297 E = VTList.rend(); I != E; ++I)
4298 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4299 return *I;
4300
Owen Andersonac9de032009-08-10 22:56:29 +00004301 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004302 Array[0] = VT1;
4303 Array[1] = VT2;
4304 SDVTList Result = makeVTList(Array, 2);
4305 VTList.push_back(Result);
4306 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307}
Dan Gohmanbd68c792008-07-17 19:10:17 +00004308
Owen Andersonac9de032009-08-10 22:56:29 +00004309SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004310 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4311 E = VTList.rend(); I != E; ++I)
4312 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4313 I->VTs[2] == VT3)
4314 return *I;
4315
Owen Andersonac9de032009-08-10 22:56:29 +00004316 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004317 Array[0] = VT1;
4318 Array[1] = VT2;
4319 Array[2] = VT3;
4320 SDVTList Result = makeVTList(Array, 3);
4321 VTList.push_back(Result);
4322 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323}
4324
Owen Andersonac9de032009-08-10 22:56:29 +00004325SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling7a4076f2008-12-01 23:28:22 +00004326 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4327 E = VTList.rend(); I != E; ++I)
4328 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4329 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4330 return *I;
4331
Anton Korobeynikov7c29ae32009-12-13 01:00:59 +00004332 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling7a4076f2008-12-01 23:28:22 +00004333 Array[0] = VT1;
4334 Array[1] = VT2;
4335 Array[2] = VT3;
4336 Array[3] = VT4;
4337 SDVTList Result = makeVTList(Array, 4);
4338 VTList.push_back(Result);
4339 return Result;
4340}
4341
Owen Andersonac9de032009-08-10 22:56:29 +00004342SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 switch (NumVTs) {
Edwin Törökbd448e32009-07-14 16:55:14 +00004344 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004345 case 1: return getVTList(VTs[0]);
4346 case 2: return getVTList(VTs[0], VTs[1]);
4347 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikov957d2c32009-12-19 02:04:00 +00004348 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004349 default: break;
4350 }
4351
Dan Gohmanbd68c792008-07-17 19:10:17 +00004352 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4353 E = VTList.rend(); I != E; ++I) {
4354 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4355 continue;
Scott Michel91099d62009-02-17 22:15:04 +00004356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 bool NoMatch = false;
4358 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmanbd68c792008-07-17 19:10:17 +00004359 if (VTs[i] != I->VTs[i]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360 NoMatch = true;
4361 break;
4362 }
4363 if (!NoMatch)
Dan Gohmanbd68c792008-07-17 19:10:17 +00004364 return *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365 }
Scott Michel91099d62009-02-17 22:15:04 +00004366
Owen Andersonac9de032009-08-10 22:56:29 +00004367 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004368 std::copy(VTs, VTs+NumVTs, Array);
4369 SDVTList Result = makeVTList(Array, NumVTs);
4370 VTList.push_back(Result);
4371 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004372}
4373
4374
4375/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4376/// specified operands. If the resultant node already exists in the DAG,
4377/// this does not modify the specified node, instead it returns the node that
4378/// already exists. If the resultant node does not exist in the DAG, the
4379/// input node is returned. As a degenerate case, if you specify the same
4380/// input operands as the node already has, the input node is returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00004381SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004382 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004383 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michel91099d62009-02-17 22:15:04 +00004384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004385 // Check to see if there is no change.
4386 if (Op == N->getOperand(0)) return InN;
Scott Michel91099d62009-02-17 22:15:04 +00004387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 // See if the modified node already exists.
4389 void *InsertPos = 0;
4390 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004391 return SDValue(Existing, InN.getResNo());
Scott Michel91099d62009-02-17 22:15:04 +00004392
Dan Gohman7d919ea2008-07-21 22:38:59 +00004393 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004394 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004395 if (!RemoveNodeFromCSEMaps(N))
4396 InsertPos = 0;
Scott Michel91099d62009-02-17 22:15:04 +00004397
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004399 N->OperandList[0].set(Op);
Scott Michel91099d62009-02-17 22:15:04 +00004400
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004401 // If this gets put into a CSE map, add it.
4402 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4403 return InN;
4404}
4405
Dan Gohman8181bd12008-07-27 21:46:04 +00004406SDValue SelectionDAG::
4407UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004408 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michel91099d62009-02-17 22:15:04 +00004410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411 // Check to see if there is no change.
4412 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4413 return InN; // No operands changed, just return the input node.
Scott Michel91099d62009-02-17 22:15:04 +00004414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004415 // See if the modified node already exists.
4416 void *InsertPos = 0;
4417 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004418 return SDValue(Existing, InN.getResNo());
Scott Michel91099d62009-02-17 22:15:04 +00004419
Dan Gohman7d919ea2008-07-21 22:38:59 +00004420 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004422 if (!RemoveNodeFromCSEMaps(N))
4423 InsertPos = 0;
Scott Michel91099d62009-02-17 22:15:04 +00004424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004425 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004426 if (N->OperandList[0] != Op1)
4427 N->OperandList[0].set(Op1);
4428 if (N->OperandList[1] != Op2)
4429 N->OperandList[1].set(Op2);
Scott Michel91099d62009-02-17 22:15:04 +00004430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 // If this gets put into a CSE map, add it.
4432 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4433 return InN;
4434}
4435
Dan Gohman8181bd12008-07-27 21:46:04 +00004436SDValue SelectionDAG::
4437UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4438 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 return UpdateNodeOperands(N, Ops, 3);
4440}
4441
Dan Gohman8181bd12008-07-27 21:46:04 +00004442SDValue SelectionDAG::
Scott Michel91099d62009-02-17 22:15:04 +00004443UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004444 SDValue Op3, SDValue Op4) {
4445 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004446 return UpdateNodeOperands(N, Ops, 4);
4447}
4448
Dan Gohman8181bd12008-07-27 21:46:04 +00004449SDValue SelectionDAG::
4450UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4451 SDValue Op3, SDValue Op4, SDValue Op5) {
4452 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453 return UpdateNodeOperands(N, Ops, 5);
4454}
4455
Dan Gohman8181bd12008-07-27 21:46:04 +00004456SDValue SelectionDAG::
4457UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004458 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004459 assert(N->getNumOperands() == NumOps &&
4460 "Update with wrong number of operands");
Scott Michel91099d62009-02-17 22:15:04 +00004461
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 // Check to see if there is no change.
4463 bool AnyChange = false;
4464 for (unsigned i = 0; i != NumOps; ++i) {
4465 if (Ops[i] != N->getOperand(i)) {
4466 AnyChange = true;
4467 break;
4468 }
4469 }
Scott Michel91099d62009-02-17 22:15:04 +00004470
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 // No operands changed, just return the input node.
4472 if (!AnyChange) return InN;
Scott Michel91099d62009-02-17 22:15:04 +00004473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 // See if the modified node already exists.
4475 void *InsertPos = 0;
4476 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004477 return SDValue(Existing, InN.getResNo());
Scott Michel91099d62009-02-17 22:15:04 +00004478
Dan Gohman14a027d2008-05-02 00:05:03 +00004479 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004481 if (!RemoveNodeFromCSEMaps(N))
4482 InsertPos = 0;
Scott Michel91099d62009-02-17 22:15:04 +00004483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004485 for (unsigned i = 0; i != NumOps; ++i)
4486 if (N->OperandList[i] != Ops[i])
4487 N->OperandList[i].set(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488
4489 // If this gets put into a CSE map, add it.
4490 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4491 return InN;
4492}
4493
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004494/// DropOperands - Release the operands and set this node to have
Dan Gohmanbd68c792008-07-17 19:10:17 +00004495/// zero operands.
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004496void SDNode::DropOperands() {
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004497 // Unlike the code in MorphNodeTo that does this, we don't need to
4498 // watch for dead nodes here.
djgc2517d32009-01-26 04:35:06 +00004499 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4500 SDUse &Use = *I++;
4501 Use.set(SDValue());
4502 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503}
4504
Dan Gohmanbd68c792008-07-17 19:10:17 +00004505/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4506/// machine opcode.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507///
Dan Gohmanbd68c792008-07-17 19:10:17 +00004508SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004509 EVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510 SDVTList VTs = getVTList(VT);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004511 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512}
4513
Dan Gohmanbd68c792008-07-17 19:10:17 +00004514SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004515 EVT VT, SDValue Op1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004516 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004517 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004518 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519}
4520
Dan Gohmanbd68c792008-07-17 19:10:17 +00004521SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004522 EVT VT, SDValue Op1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004523 SDValue Op2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004524 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004525 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004526 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527}
4528
Dan Gohmanbd68c792008-07-17 19:10:17 +00004529SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004530 EVT VT, SDValue Op1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004531 SDValue Op2, SDValue Op3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004532 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004533 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004534 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004535}
4536
Dan Gohmanbd68c792008-07-17 19:10:17 +00004537SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004538 EVT VT, const SDValue *Ops,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539 unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540 SDVTList VTs = getVTList(VT);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004541 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004542}
4543
Dan Gohmanbd68c792008-07-17 19:10:17 +00004544SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004545 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohman7eced112008-07-02 23:23:19 +00004546 unsigned NumOps) {
4547 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004548 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004549}
4550
Dan Gohmanbd68c792008-07-17 19:10:17 +00004551SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004552 EVT VT1, EVT VT2) {
Dan Gohman7eced112008-07-02 23:23:19 +00004553 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004554 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohman7eced112008-07-02 23:23:19 +00004555}
4556
Dan Gohmanbd68c792008-07-17 19:10:17 +00004557SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004558 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004559 const SDValue *Ops, unsigned NumOps) {
Dan Gohman7eced112008-07-02 23:23:19 +00004560 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004561 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004562}
4563
Bill Wendling7a4076f2008-12-01 23:28:22 +00004564SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004565 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling7a4076f2008-12-01 23:28:22 +00004566 const SDValue *Ops, unsigned NumOps) {
4567 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4568 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4569}
4570
Scott Michel91099d62009-02-17 22:15:04 +00004571SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004572 EVT VT1, EVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004573 SDValue Op1) {
Dan Gohman7eced112008-07-02 23:23:19 +00004574 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004575 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004576 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004577}
4578
Scott Michel91099d62009-02-17 22:15:04 +00004579SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004580 EVT VT1, EVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004581 SDValue Op1, SDValue Op2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004583 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004584 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585}
4586
Dan Gohmanbd68c792008-07-17 19:10:17 +00004587SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004588 EVT VT1, EVT VT2,
Scott Michel91099d62009-02-17 22:15:04 +00004589 SDValue Op1, SDValue Op2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004590 SDValue Op3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004592 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004593 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman7eced112008-07-02 23:23:19 +00004594}
4595
Dan Gohmanbd68c792008-07-17 19:10:17 +00004596SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004597 EVT VT1, EVT VT2, EVT VT3,
Scott Michel91099d62009-02-17 22:15:04 +00004598 SDValue Op1, SDValue Op2,
Bill Wendling7a4076f2008-12-01 23:28:22 +00004599 SDValue Op3) {
4600 SDVTList VTs = getVTList(VT1, VT2, VT3);
4601 SDValue Ops[] = { Op1, Op2, Op3 };
4602 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4603}
4604
4605SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004606 SDVTList VTs, const SDValue *Ops,
Dan Gohman7eced112008-07-02 23:23:19 +00004607 unsigned NumOps) {
Chris Lattnerdef44712010-02-23 23:01:35 +00004608 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4609 // Reset the NodeID to -1.
4610 N->setNodeId(-1);
4611 return N;
Dan Gohmanbd68c792008-07-17 19:10:17 +00004612}
4613
Chris Lattner7a58f522010-02-28 21:36:14 +00004614/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmanbd68c792008-07-17 19:10:17 +00004615/// return type, opcode, and operands.
4616///
4617/// Note that MorphNodeTo returns the resultant node. If there is already a
4618/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesenb03cc3f2009-02-04 23:02:30 +00004619/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmanbd68c792008-07-17 19:10:17 +00004620///
4621/// Using MorphNodeTo is faster than creating a new node and swapping it in
4622/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif8b2235c2008-08-30 22:16:05 +00004623/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmanbd68c792008-07-17 19:10:17 +00004624/// the node's users.
4625///
4626SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004627 SDVTList VTs, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004628 unsigned NumOps) {
Dan Gohman7eced112008-07-02 23:23:19 +00004629 // If an identical node already exists, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630 void *IP = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004631 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004632 FoldingSetNodeID ID;
4633 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendling339b1532009-12-18 23:32:53 +00004634 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohmanbd68c792008-07-17 19:10:17 +00004635 return ON;
4636 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637
Dan Gohman705e3f72008-09-13 01:54:27 +00004638 if (!RemoveNodeFromCSEMaps(N))
4639 IP = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640
Dan Gohmanbd68c792008-07-17 19:10:17 +00004641 // Start the morphing.
4642 N->NodeType = Opc;
4643 N->ValueList = VTs.VTs;
4644 N->NumValues = VTs.NumVTs;
Scott Michel91099d62009-02-17 22:15:04 +00004645
Dan Gohmanbd68c792008-07-17 19:10:17 +00004646 // Clear the operands list, updating used nodes to remove this from their
4647 // use list. Keep track of any operands that become dead as a result.
4648 SmallPtrSet<SDNode*, 16> DeadNodeSet;
djgc2517d32009-01-26 04:35:06 +00004649 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4650 SDUse &Use = *I++;
4651 SDNode *Used = Use.getNode();
4652 Use.set(SDValue());
Dan Gohmanbd68c792008-07-17 19:10:17 +00004653 if (Used->use_empty())
4654 DeadNodeSet.insert(Used);
4655 }
4656
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004657 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
4658 // Initialize the memory references information.
4659 MN->setMemRefs(0, 0);
4660 // If NumOps is larger than the # of operands we can have in a
4661 // MachineSDNode, reallocate the operand list.
4662 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
4663 if (MN->OperandsNeedDelete)
4664 delete[] MN->OperandList;
4665 if (NumOps > array_lengthof(MN->LocalOperands))
4666 // We're creating a final node that will live unmorphed for the
4667 // remainder of the current SelectionDAG iteration, so we can allocate
4668 // the operands directly out of a pool with no recycling metadata.
4669 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman7616a1e2010-03-18 18:49:47 +00004670 Ops, NumOps);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004671 else
4672 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
4673 MN->OperandsNeedDelete = false;
4674 } else
4675 MN->InitOperands(MN->OperandList, Ops, NumOps);
4676 } else {
4677 // If NumOps is larger than the # of operands we currently have, reallocate
4678 // the operand list.
4679 if (NumOps > N->NumOperands) {
4680 if (N->OperandsNeedDelete)
4681 delete[] N->OperandList;
4682 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004683 N->OperandsNeedDelete = true;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004684 } else
Anton Korobeynikovfcfa3112009-10-22 00:15:17 +00004685 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004686 }
4687
4688 // Delete any nodes that are still dead after adding the uses for the
4689 // new operands.
Chris Lattnera9578c42010-03-01 07:43:08 +00004690 if (!DeadNodeSet.empty()) {
4691 SmallVector<SDNode *, 16> DeadNodes;
4692 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4693 E = DeadNodeSet.end(); I != E; ++I)
4694 if ((*I)->use_empty())
4695 DeadNodes.push_back(*I);
4696 RemoveDeadNodes(DeadNodes);
4697 }
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004698
Dan Gohmanbd68c792008-07-17 19:10:17 +00004699 if (IP)
4700 CSEMap.InsertNode(N, IP); // Memoize the new node.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 return N;
4702}
4703
4704
Dan Gohman61fda0d2009-09-25 18:54:59 +00004705/// getMachineNode - These are used for target selectors to create a new node
4706/// with specified return type(s), MachineInstr opcode, and operands.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004707///
Dan Gohman61fda0d2009-09-25 18:54:59 +00004708/// Note that getMachineNode returns the resultant node. If there is already a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004709/// node of the specified opcode and operands, it returns that node instead of
4710/// the current one.
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004711MachineSDNode *
4712SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004713 SDVTList VTs = getVTList(VT);
4714 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004715}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004716
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004717MachineSDNode *
4718SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004719 SDVTList VTs = getVTList(VT);
4720 SDValue Ops[] = { Op1 };
4721 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004722}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004723
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004724MachineSDNode *
4725SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4726 SDValue Op1, SDValue Op2) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004727 SDVTList VTs = getVTList(VT);
4728 SDValue Ops[] = { Op1, Op2 };
4729 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004730}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004731
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004732MachineSDNode *
4733SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4734 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004735 SDVTList VTs = getVTList(VT);
4736 SDValue Ops[] = { Op1, Op2, Op3 };
4737 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004738}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004739
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004740MachineSDNode *
4741SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4742 const SDValue *Ops, unsigned NumOps) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004743 SDVTList VTs = getVTList(VT);
4744 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004745}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004746
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004747MachineSDNode *
4748SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanee036282009-04-09 23:54:40 +00004749 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004750 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004751}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004752
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004753MachineSDNode *
4754SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4755 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanee036282009-04-09 23:54:40 +00004756 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004757 SDValue Ops[] = { Op1 };
4758 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004759}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004760
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004761MachineSDNode *
4762SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4763 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanee036282009-04-09 23:54:40 +00004764 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004765 SDValue Ops[] = { Op1, Op2 };
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004766 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling6ed1f082009-01-29 09:01:55 +00004767}
4768
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004769MachineSDNode *
4770SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4771 EVT VT1, EVT VT2, SDValue Op1,
4772 SDValue Op2, SDValue Op3) {
Dan Gohmanee036282009-04-09 23:54:40 +00004773 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004774 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004775 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling6ed1f082009-01-29 09:01:55 +00004776}
4777
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004778MachineSDNode *
4779SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4780 EVT VT1, EVT VT2,
4781 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanee036282009-04-09 23:54:40 +00004782 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004783 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004784}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004785
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004786MachineSDNode *
4787SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4788 EVT VT1, EVT VT2, EVT VT3,
4789 SDValue Op1, SDValue Op2) {
Dan Gohmanee036282009-04-09 23:54:40 +00004790 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004791 SDValue Ops[] = { Op1, Op2 };
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004792 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004793}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004794
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004795MachineSDNode *
4796SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4797 EVT VT1, EVT VT2, EVT VT3,
4798 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanee036282009-04-09 23:54:40 +00004799 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004800 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004801 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004802}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004803
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004804MachineSDNode *
4805SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4806 EVT VT1, EVT VT2, EVT VT3,
4807 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanee036282009-04-09 23:54:40 +00004808 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004809 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004810}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004811
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004812MachineSDNode *
4813SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
4814 EVT VT2, EVT VT3, EVT VT4,
4815 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanee036282009-04-09 23:54:40 +00004816 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004817 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004818}
4819
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004820MachineSDNode *
4821SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4822 const std::vector<EVT> &ResultTys,
4823 const SDValue *Ops, unsigned NumOps) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004824 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
4825 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
4826}
4827
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004828MachineSDNode *
4829SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
4830 const SDValue *Ops, unsigned NumOps) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004831 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Flag;
4832 MachineSDNode *N;
4833 void *IP;
4834
4835 if (DoCSE) {
4836 FoldingSetNodeID ID;
4837 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
4838 IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00004839 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004840 return cast<MachineSDNode>(E);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004841 }
4842
4843 // Allocate a new MachineSDNode.
Dan Gohman7616a1e2010-03-18 18:49:47 +00004844 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004845
4846 // Initialize the operands list.
4847 if (NumOps > array_lengthof(N->LocalOperands))
4848 // We're creating a final node that will live unmorphed for the
4849 // remainder of the current SelectionDAG iteration, so we can allocate
4850 // the operands directly out of a pool with no recycling metadata.
4851 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
4852 Ops, NumOps);
4853 else
4854 N->InitOperands(N->LocalOperands, Ops, NumOps);
4855 N->OperandsNeedDelete = false;
4856
4857 if (DoCSE)
4858 CSEMap.InsertNode(N, IP);
4859
4860 AllNodes.push_back(N);
4861#ifndef NDEBUG
4862 VerifyNode(N);
4863#endif
4864 return N;
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004865}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004866
Dan Gohman7ffdd432009-08-19 18:16:17 +00004867/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner4052b292010-02-09 19:54:29 +00004868/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman7ffdd432009-08-19 18:16:17 +00004869SDValue
4870SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
4871 SDValue Operand) {
4872 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner4052b292010-02-09 19:54:29 +00004873 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman61fda0d2009-09-25 18:54:59 +00004874 VT, Operand, SRIdxVal);
Dan Gohman7ffdd432009-08-19 18:16:17 +00004875 return SDValue(Subreg, 0);
4876}
4877
Bob Wilsonfcaa4cf2009-10-08 18:49:46 +00004878/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner4052b292010-02-09 19:54:29 +00004879/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilsonfcaa4cf2009-10-08 18:49:46 +00004880SDValue
4881SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
4882 SDValue Operand, SDValue Subreg) {
4883 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner4052b292010-02-09 19:54:29 +00004884 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilsonfcaa4cf2009-10-08 18:49:46 +00004885 VT, Operand, Subreg, SRIdxVal);
4886 return SDValue(Result, 0);
4887}
4888
Evan Chengd1113582008-03-22 01:55:50 +00004889/// getNodeIfExists - Get the specified node if it's already available, or
4890/// else return NULL.
4891SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman8181bd12008-07-27 21:46:04 +00004892 const SDValue *Ops, unsigned NumOps) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004893 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Evan Chengd1113582008-03-22 01:55:50 +00004894 FoldingSetNodeID ID;
4895 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4896 void *IP = 0;
Bill Wendling339b1532009-12-18 23:32:53 +00004897 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Chengd1113582008-03-22 01:55:50 +00004898 return E;
4899 }
4900 return NULL;
4901}
4902
Evan Chenge3922422010-03-29 20:48:30 +00004903/// getDbgValue - Creates a SDDbgValue node.
4904///
4905SDDbgValue *
4906SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
4907 DebugLoc DL, unsigned O) {
4908 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
4909}
4910
4911SDDbgValue *
4912SelectionDAG::getDbgValue(MDNode *MDPtr, Value *C, uint64_t Off,
4913 DebugLoc DL, unsigned O) {
4914 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
4915}
4916
4917SDDbgValue *
4918SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
4919 DebugLoc DL, unsigned O) {
4920 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
4921}
4922
Dan Gohman4f6bea02010-03-03 21:33:37 +00004923namespace {
4924
Dan Gohmandbd63882010-03-04 19:11:28 +00004925/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman4f6bea02010-03-03 21:33:37 +00004926/// pointed to by a use iterator is deleted, increment the use iterator
4927/// so that it doesn't dangle.
4928///
4929/// This class also manages a "downlink" DAGUpdateListener, to forward
4930/// messages to ReplaceAllUsesWith's callers.
4931///
4932class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
4933 SelectionDAG::DAGUpdateListener *DownLink;
4934 SDNode::use_iterator &UI;
4935 SDNode::use_iterator &UE;
4936
4937 virtual void NodeDeleted(SDNode *N, SDNode *E) {
4938 // Increment the iterator as needed.
4939 while (UI != UE && N == *UI)
4940 ++UI;
4941
4942 // Then forward the message.
4943 if (DownLink) DownLink->NodeDeleted(N, E);
4944 }
4945
4946 virtual void NodeUpdated(SDNode *N) {
4947 // Just forward the message.
4948 if (DownLink) DownLink->NodeUpdated(N);
4949 }
4950
4951public:
4952 RAUWUpdateListener(SelectionDAG::DAGUpdateListener *dl,
4953 SDNode::use_iterator &ui,
4954 SDNode::use_iterator &ue)
4955 : DownLink(dl), UI(ui), UE(ue) {}
4956};
4957
4958}
4959
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4961/// This can cause recursive merging of nodes in the DAG.
4962///
Chris Lattnerdca329f2008-02-03 03:35:22 +00004963/// This version assumes From has a single result value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004964///
Dan Gohman8181bd12008-07-27 21:46:04 +00004965void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004966 DAGUpdateListener *UpdateListener) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004967 SDNode *From = FromN.getNode();
Scott Michel91099d62009-02-17 22:15:04 +00004968 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969 "Cannot replace with this method!");
Gabor Greif1c80d112008-08-28 21:40:38 +00004970 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein05650fd2008-04-07 10:06:32 +00004971
djg943376a2009-01-25 16:29:12 +00004972 // Iterate over all the existing uses of From. New uses will be added
4973 // to the beginning of the use list, which we avoid visiting.
4974 // This specifically avoids visiting uses of From that arise while the
4975 // replacement is happening, because any such uses would be the result
4976 // of CSE: If an existing node looks like From after one of its operands
4977 // is replaced by To, we don't want to replace of all its users with To
4978 // too. See PR3018 for more info.
Dan Gohman86bf1392009-01-19 21:44:21 +00004979 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohman4f6bea02010-03-03 21:33:37 +00004980 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohman86bf1392009-01-19 21:44:21 +00004981 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004982 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004983
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004985 RemoveNodeFromCSEMaps(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986
djg943376a2009-01-25 16:29:12 +00004987 // A user can appear in a use list multiple times, and when this
4988 // happens the uses are usually next to each other in the list.
4989 // To help reduce the number of CSE recomputations, process all
4990 // the uses of this user that we can find this way.
4991 do {
djgc2517d32009-01-26 04:35:06 +00004992 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00004993 ++UI;
djgc2517d32009-01-26 04:35:06 +00004994 Use.set(To);
djg943376a2009-01-25 16:29:12 +00004995 } while (UI != UE && *UI == User);
4996
4997 // Now that we have modified User, add it back to the CSE maps. If it
4998 // already exists there, recursively merge the results together.
Dan Gohman4f6bea02010-03-03 21:33:37 +00004999 AddModifiedNodeToCSEMaps(User, &Listener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 }
5001}
5002
5003/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5004/// This can cause recursive merging of nodes in the DAG.
5005///
Dan Gohman3209ac32009-04-15 20:06:30 +00005006/// This version assumes that for each value of From, there is a
5007/// corresponding value in To in the same position with the same type.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005008///
5009void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005010 DAGUpdateListener *UpdateListener) {
Dan Gohman3209ac32009-04-15 20:06:30 +00005011#ifndef NDEBUG
5012 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5013 assert((!From->hasAnyUseOfValue(i) ||
5014 From->getValueType(i) == To->getValueType(i)) &&
5015 "Cannot use this version of ReplaceAllUsesWith!");
5016#endif
Dan Gohmanbd68c792008-07-17 19:10:17 +00005017
5018 // Handle the trivial case.
5019 if (From == To)
5020 return;
5021
Dan Gohman86bf1392009-01-19 21:44:21 +00005022 // Iterate over just the existing users of From. See the comments in
5023 // the ReplaceAllUsesWith above.
5024 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohman4f6bea02010-03-03 21:33:37 +00005025 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohman86bf1392009-01-19 21:44:21 +00005026 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00005027 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00005028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005029 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00005030 RemoveNodeFromCSEMaps(User);
Roman Levenstein05650fd2008-04-07 10:06:32 +00005031
djg943376a2009-01-25 16:29:12 +00005032 // A user can appear in a use list multiple times, and when this
5033 // happens the uses are usually next to each other in the list.
5034 // To help reduce the number of CSE recomputations, process all
5035 // the uses of this user that we can find this way.
5036 do {
djgc2517d32009-01-26 04:35:06 +00005037 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00005038 ++UI;
djgc2517d32009-01-26 04:35:06 +00005039 Use.setNode(To);
djg943376a2009-01-25 16:29:12 +00005040 } while (UI != UE && *UI == User);
5041
5042 // Now that we have modified User, add it back to the CSE maps. If it
5043 // already exists there, recursively merge the results together.
Dan Gohman4f6bea02010-03-03 21:33:37 +00005044 AddModifiedNodeToCSEMaps(User, &Listener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005045 }
5046}
5047
5048/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5049/// This can cause recursive merging of nodes in the DAG.
5050///
5051/// This version can replace From with any result values. To must match the
5052/// number and types of values returned by From.
5053void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman8181bd12008-07-27 21:46:04 +00005054 const SDValue *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005055 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00005056 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman8181bd12008-07-27 21:46:04 +00005057 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005058
Dan Gohman86bf1392009-01-19 21:44:21 +00005059 // Iterate over just the existing users of From. See the comments in
5060 // the ReplaceAllUsesWith above.
5061 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohman4f6bea02010-03-03 21:33:37 +00005062 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohman86bf1392009-01-19 21:44:21 +00005063 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00005064 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00005065
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005066 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00005067 RemoveNodeFromCSEMaps(User);
Roman Levenstein05650fd2008-04-07 10:06:32 +00005068
djg943376a2009-01-25 16:29:12 +00005069 // A user can appear in a use list multiple times, and when this
5070 // happens the uses are usually next to each other in the list.
5071 // To help reduce the number of CSE recomputations, process all
5072 // the uses of this user that we can find this way.
5073 do {
djgc2517d32009-01-26 04:35:06 +00005074 SDUse &Use = UI.getUse();
5075 const SDValue &ToOp = To[Use.getResNo()];
djg943376a2009-01-25 16:29:12 +00005076 ++UI;
djgc2517d32009-01-26 04:35:06 +00005077 Use.set(ToOp);
djg943376a2009-01-25 16:29:12 +00005078 } while (UI != UE && *UI == User);
5079
5080 // Now that we have modified User, add it back to the CSE maps. If it
5081 // already exists there, recursively merge the results together.
Dan Gohman4f6bea02010-03-03 21:33:37 +00005082 AddModifiedNodeToCSEMaps(User, &Listener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005083 }
5084}
5085
5086/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
djgc2517d32009-01-26 04:35:06 +00005087/// uses of other values produced by From.getNode() alone. The Deleted
5088/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman8181bd12008-07-27 21:46:04 +00005089void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005090 DAGUpdateListener *UpdateListener){
Dan Gohmanbd68c792008-07-17 19:10:17 +00005091 // Handle the really simple, really trivial case efficiently.
5092 if (From == To) return;
5093
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005094 // Handle the simple, trivial, case efficiently.
Gabor Greif1c80d112008-08-28 21:40:38 +00005095 if (From.getNode()->getNumValues() == 1) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005096 ReplaceAllUsesWith(From, To, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005097 return;
5098 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005099
djg943376a2009-01-25 16:29:12 +00005100 // Iterate over just the existing users of From. See the comments in
5101 // the ReplaceAllUsesWith above.
5102 SDNode::use_iterator UI = From.getNode()->use_begin(),
5103 UE = From.getNode()->use_end();
Dan Gohman4f6bea02010-03-03 21:33:37 +00005104 RAUWUpdateListener Listener(UpdateListener, UI, UE);
djg943376a2009-01-25 16:29:12 +00005105 while (UI != UE) {
5106 SDNode *User = *UI;
5107 bool UserRemovedFromCSEMaps = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005108
djg943376a2009-01-25 16:29:12 +00005109 // A user can appear in a use list multiple times, and when this
5110 // happens the uses are usually next to each other in the list.
5111 // To help reduce the number of CSE recomputations, process all
5112 // the uses of this user that we can find this way.
5113 do {
djgc2517d32009-01-26 04:35:06 +00005114 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00005115
5116 // Skip uses of different values from the same node.
djgc2517d32009-01-26 04:35:06 +00005117 if (Use.getResNo() != From.getResNo()) {
djg943376a2009-01-25 16:29:12 +00005118 ++UI;
5119 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005120 }
djg943376a2009-01-25 16:29:12 +00005121
5122 // If this node hasn't been modified yet, it's still in the CSE maps,
5123 // so remove its old self from the CSE maps.
5124 if (!UserRemovedFromCSEMaps) {
5125 RemoveNodeFromCSEMaps(User);
5126 UserRemovedFromCSEMaps = true;
5127 }
5128
5129 ++UI;
djgc2517d32009-01-26 04:35:06 +00005130 Use.set(To);
djg943376a2009-01-25 16:29:12 +00005131 } while (UI != UE && *UI == User);
5132
5133 // We are iterating over all uses of the From node, so if a use
5134 // doesn't use the specific value, no changes are made.
5135 if (!UserRemovedFromCSEMaps)
5136 continue;
5137
Chris Lattner8a258202007-10-15 06:10:22 +00005138 // Now that we have modified User, add it back to the CSE maps. If it
5139 // already exists there, recursively merge the results together.
Dan Gohman4f6bea02010-03-03 21:33:37 +00005140 AddModifiedNodeToCSEMaps(User, &Listener);
Dan Gohmanbd68c792008-07-17 19:10:17 +00005141 }
5142}
5143
djg943376a2009-01-25 16:29:12 +00005144namespace {
5145 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5146 /// to record information about a use.
5147 struct UseMemo {
5148 SDNode *User;
5149 unsigned Index;
djgc2517d32009-01-26 04:35:06 +00005150 SDUse *Use;
djg943376a2009-01-25 16:29:12 +00005151 };
djgc2517d32009-01-26 04:35:06 +00005152
5153 /// operator< - Sort Memos by User.
5154 bool operator<(const UseMemo &L, const UseMemo &R) {
5155 return (intptr_t)L.User < (intptr_t)R.User;
5156 }
djg943376a2009-01-25 16:29:12 +00005157}
5158
Dan Gohmanbd68c792008-07-17 19:10:17 +00005159/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
djgc2517d32009-01-26 04:35:06 +00005160/// uses of other values produced by From.getNode() alone. The same value
5161/// may appear in both the From and To list. The Deleted vector is
Dan Gohmanbd68c792008-07-17 19:10:17 +00005162/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman8181bd12008-07-27 21:46:04 +00005163void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5164 const SDValue *To,
Dan Gohmanbd68c792008-07-17 19:10:17 +00005165 unsigned Num,
5166 DAGUpdateListener *UpdateListener){
5167 // Handle the simple, trivial case efficiently.
5168 if (Num == 1)
5169 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5170
djg943376a2009-01-25 16:29:12 +00005171 // Read up all the uses and make records of them. This helps
5172 // processing new uses that are introduced during the
5173 // replacement process.
5174 SmallVector<UseMemo, 4> Uses;
5175 for (unsigned i = 0; i != Num; ++i) {
5176 unsigned FromResNo = From[i].getResNo();
5177 SDNode *FromNode = From[i].getNode();
Scott Michel91099d62009-02-17 22:15:04 +00005178 for (SDNode::use_iterator UI = FromNode->use_begin(),
djgc2517d32009-01-26 04:35:06 +00005179 E = FromNode->use_end(); UI != E; ++UI) {
5180 SDUse &Use = UI.getUse();
5181 if (Use.getResNo() == FromResNo) {
5182 UseMemo Memo = { *UI, i, &Use };
djg943376a2009-01-25 16:29:12 +00005183 Uses.push_back(Memo);
5184 }
djgc2517d32009-01-26 04:35:06 +00005185 }
djg943376a2009-01-25 16:29:12 +00005186 }
Dan Gohmanbd68c792008-07-17 19:10:17 +00005187
djgc2517d32009-01-26 04:35:06 +00005188 // Sort the uses, so that all the uses from a given User are together.
5189 std::sort(Uses.begin(), Uses.end());
5190
djg943376a2009-01-25 16:29:12 +00005191 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5192 UseIndex != UseIndexEnd; ) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00005193 // We know that this user uses some value of From. If it is the right
5194 // value, update it.
djg943376a2009-01-25 16:29:12 +00005195 SDNode *User = Uses[UseIndex].User;
5196
5197 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanbd68c792008-07-17 19:10:17 +00005198 RemoveNodeFromCSEMaps(User);
djg943376a2009-01-25 16:29:12 +00005199
djgc2517d32009-01-26 04:35:06 +00005200 // The Uses array is sorted, so all the uses for a given User
5201 // are next to each other in the list.
djg943376a2009-01-25 16:29:12 +00005202 // To help reduce the number of CSE recomputations, process all
5203 // the uses of this user that we can find this way.
5204 do {
5205 unsigned i = Uses[UseIndex].Index;
djgc2517d32009-01-26 04:35:06 +00005206 SDUse &Use = *Uses[UseIndex].Use;
djg943376a2009-01-25 16:29:12 +00005207 ++UseIndex;
5208
djgc2517d32009-01-26 04:35:06 +00005209 Use.set(To[i]);
djg943376a2009-01-25 16:29:12 +00005210 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5211
Dan Gohmanbd68c792008-07-17 19:10:17 +00005212 // Now that we have modified User, add it back to the CSE maps. If it
5213 // already exists there, recursively merge the results together.
djg943376a2009-01-25 16:29:12 +00005214 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005215 }
5216}
5217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005218/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
5219/// based on their topological order. It returns the maximum id and a vector
5220/// of the SDNodes* in assigned order by reference.
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005221unsigned SelectionDAG::AssignTopologicalOrder() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005222
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005223 unsigned DAGSize = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005224
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005225 // SortedPos tracks the progress of the algorithm. Nodes before it are
5226 // sorted, nodes after it are unsorted. When the algorithm completes
5227 // it is at the end of the list.
5228 allnodes_iterator SortedPos = allnodes_begin();
5229
Dan Gohman6e780312008-11-21 19:10:41 +00005230 // Visit all the nodes. Move nodes with no operands to the front of
5231 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005232 // operand count. Before we do this, the Node Id fields of the nodes
5233 // may contain arbitrary values. After, the Node Id fields for nodes
5234 // before SortedPos will contain the topological sort index, and the
5235 // Node Id fields for nodes At SortedPos and after will contain the
5236 // count of outstanding operands.
5237 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5238 SDNode *N = I++;
David Greenec25757c2010-01-20 20:13:31 +00005239 checkForCycles(N);
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005240 unsigned Degree = N->getNumOperands();
5241 if (Degree == 0) {
5242 // A node with no uses, add it to the result array immediately.
5243 N->setNodeId(DAGSize++);
5244 allnodes_iterator Q = N;
5245 if (Q != SortedPos)
5246 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greenea05f25b2010-01-20 00:59:23 +00005247 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005248 ++SortedPos;
5249 } else {
5250 // Temporarily use the Node Id as scratch space for the degree count.
5251 N->setNodeId(Degree);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005252 }
5253 }
5254
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005255 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5256 // such that by the time the end is reached all nodes will be sorted.
5257 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5258 SDNode *N = I;
David Greenec25757c2010-01-20 20:13:31 +00005259 checkForCycles(N);
David Greenea05f25b2010-01-20 00:59:23 +00005260 // N is in sorted position, so all its uses have one less operand
5261 // that needs to be sorted.
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005262 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5263 UI != UE; ++UI) {
5264 SDNode *P = *UI;
5265 unsigned Degree = P->getNodeId();
David Greenea05f25b2010-01-20 00:59:23 +00005266 assert(Degree != 0 && "Invalid node degree");
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005267 --Degree;
5268 if (Degree == 0) {
5269 // All of P's operands are sorted, so P may sorted now.
5270 P->setNodeId(DAGSize++);
5271 if (P != SortedPos)
5272 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greenea05f25b2010-01-20 00:59:23 +00005273 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005274 ++SortedPos;
5275 } else {
5276 // Update P's outstanding operand count.
5277 P->setNodeId(Degree);
5278 }
5279 }
David Greenea05f25b2010-01-20 00:59:23 +00005280 if (I == SortedPos) {
David Greene4e45d402010-02-09 23:03:05 +00005281#ifndef NDEBUG
5282 SDNode *S = ++I;
5283 dbgs() << "Overran sorted position:\n";
David Greenea05f25b2010-01-20 00:59:23 +00005284 S->dumprFull();
David Greene4e45d402010-02-09 23:03:05 +00005285#endif
5286 llvm_unreachable(0);
David Greenea05f25b2010-01-20 00:59:23 +00005287 }
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005288 }
5289
5290 assert(SortedPos == AllNodes.end() &&
5291 "Topological sort incomplete!");
5292 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5293 "First node in topological sort is not the entry token!");
5294 assert(AllNodes.front().getNodeId() == 0 &&
5295 "First node in topological sort has non-zero id!");
5296 assert(AllNodes.front().getNumOperands() == 0 &&
5297 "First node in topological sort has operands!");
5298 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5299 "Last node in topologic sort has unexpected id!");
5300 assert(AllNodes.back().use_empty() &&
5301 "Last node in topologic sort has users!");
Dan Gohman6e780312008-11-21 19:10:41 +00005302 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005303 return DAGSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005304}
5305
Bill Wendling339b1532009-12-18 23:32:53 +00005306/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling87589f72010-01-28 21:51:40 +00005307void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendling339b1532009-12-18 23:32:53 +00005308 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling0df2cd52010-01-23 10:26:57 +00005309 Ordering->add(SD, Order);
Bill Wendling339b1532009-12-18 23:32:53 +00005310}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005311
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00005312/// GetOrdering - Get the order for the SDNode.
5313unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5314 assert(SD && "Trying to get the order of a null node!");
Bill Wendling0df2cd52010-01-23 10:26:57 +00005315 return Ordering->getOrder(SD);
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00005316}
5317
Evan Chengfd976052010-03-25 01:38:16 +00005318/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5319/// value is produced by SD.
5320void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD) {
5321 DbgInfo->add(DB, SD);
5322 if (SD)
5323 SD->setHasDebugValue(true);
Dale Johannesenf4243fb2010-03-10 22:13:47 +00005324}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325
5326//===----------------------------------------------------------------------===//
5327// SDNode Class
5328//===----------------------------------------------------------------------===//
5329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330HandleSDNode::~HandleSDNode() {
Dan Gohmanb997a4e2008-07-07 20:57:48 +00005331 DropOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332}
5333
Chris Lattnerb31fb962009-06-26 21:14:05 +00005334GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, const GlobalValue *GA,
Owen Andersonac9de032009-08-10 22:56:29 +00005335 EVT VT, int64_t o, unsigned char TF)
Chris Lattner65657ae2010-04-02 20:17:23 +00005336 : SDNode(Opc, DebugLoc(), getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005337 TheGlobal = const_cast<GlobalValue*>(GA);
5338}
5339
Owen Andersonac9de032009-08-10 22:56:29 +00005340MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005341 MachineMemOperand *mmo)
5342 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene05a001f2010-02-17 20:21:42 +00005343 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
5344 MMO->isNonTemporal());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005345 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene05a001f2010-02-17 20:21:42 +00005346 assert(isNonTemporal() == MMO->isNonTemporal() &&
5347 "Non-temporal encoding error!");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005348 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen08f344e2009-01-28 21:18:29 +00005349}
5350
Scott Michel91099d62009-02-17 22:15:04 +00005351MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Nate Begemane22356d2009-09-15 00:13:12 +00005352 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005353 MachineMemOperand *mmo)
Dale Johannesen08f344e2009-01-28 21:18:29 +00005354 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005355 MemoryVT(memvt), MMO(mmo) {
David Greene05a001f2010-02-17 20:21:42 +00005356 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
5357 MMO->isNonTemporal());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005358 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5359 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005360}
5361
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005362/// Profile - Gather unique data for the node.
5363///
Dan Gohman98beebe2008-08-20 15:58:01 +00005364void SDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005365 AddNodeIDNode(ID, this);
5366}
5367
Owen Anderson97be9d32009-08-25 22:27:22 +00005368namespace {
5369 struct EVTArray {
5370 std::vector<EVT> VTs;
5371
5372 EVTArray() {
5373 VTs.reserve(MVT::LAST_VALUETYPE);
5374 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5375 VTs.push_back(MVT((MVT::SimpleValueType)i));
5376 }
5377 };
5378}
5379
Owen Andersonac9de032009-08-10 22:56:29 +00005380static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson97be9d32009-08-25 22:27:22 +00005381static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner28ab49b2009-08-22 04:07:34 +00005382static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonef31ae32009-06-25 17:09:00 +00005383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384/// getValueTypeList - Return a pointer to the specified value type.
5385///
Owen Andersonac9de032009-08-10 22:56:29 +00005386const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands92c43912008-06-06 12:08:01 +00005387 if (VT.isExtended()) {
Owen Anderson1a285c22009-08-22 06:32:36 +00005388 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonef31ae32009-06-25 17:09:00 +00005389 return &(*EVTs->insert(VT).first);
Duncan Sandsa9810f32007-10-16 09:56:48 +00005390 } else {
Owen Anderson97be9d32009-08-25 22:27:22 +00005391 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsa9810f32007-10-16 09:56:48 +00005392 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005393}
Duncan Sandsa9810f32007-10-16 09:56:48 +00005394
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5396/// indicated value. This method ignores uses of other values defined by this
5397/// operation.
5398bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
5399 assert(Value < getNumValues() && "Bad value!");
5400
Roman Levenstein05650fd2008-04-07 10:06:32 +00005401 // TODO: Only iterate over uses of a given value of the node
5402 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
djgc2517d32009-01-26 04:35:06 +00005403 if (UI.getUse().getResNo() == Value) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00005404 if (NUses == 0)
5405 return false;
5406 --NUses;
5407 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005408 }
5409
5410 // Found exactly the right number of uses?
5411 return NUses == 0;
5412}
5413
5414
Evan Cheng0af04f72007-08-02 05:29:38 +00005415/// hasAnyUseOfValue - Return true if there are any use of the indicated
5416/// value. This method ignores uses of other values defined by this operation.
5417bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5418 assert(Value < getNumValues() && "Bad value!");
5419
Dan Gohmana9651732008-07-09 22:39:01 +00005420 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
djgc2517d32009-01-26 04:35:06 +00005421 if (UI.getUse().getResNo() == Value)
Dan Gohmana9651732008-07-09 22:39:01 +00005422 return true;
Evan Cheng0af04f72007-08-02 05:29:38 +00005423
5424 return false;
5425}
5426
5427
Dan Gohman07fadb02008-07-27 18:06:42 +00005428/// isOnlyUserOf - Return true if this node is the only use of N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005429///
Dan Gohman07fadb02008-07-27 18:06:42 +00005430bool SDNode::isOnlyUserOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005431 bool Seen = false;
5432 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00005433 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 if (User == this)
5435 Seen = true;
5436 else
5437 return false;
5438 }
5439
5440 return Seen;
5441}
5442
5443/// isOperand - Return true if this node is an operand of N.
5444///
Dan Gohman8181bd12008-07-27 21:46:04 +00005445bool SDValue::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005446 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5447 if (*this == N->getOperand(i))
5448 return true;
5449 return false;
5450}
5451
Evan Chengd9387682008-03-04 00:41:45 +00005452bool SDNode::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005453 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
djgc2517d32009-01-26 04:35:06 +00005454 if (this == N->OperandList[i].getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455 return true;
5456 return false;
5457}
5458
Chris Lattner10d94f92008-01-16 05:49:24 +00005459/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michel91099d62009-02-17 22:15:04 +00005460/// be a chain) reaches the specified operand without crossing any
Chris Lattner10d94f92008-01-16 05:49:24 +00005461/// side-effecting instructions. In practice, this looks through token
5462/// factors and non-volatile loads. In order to remain efficient, this only
5463/// looks a couple of nodes in, it does not do an exhaustive search.
Scott Michel91099d62009-02-17 22:15:04 +00005464bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner10d94f92008-01-16 05:49:24 +00005465 unsigned Depth) const {
5466 if (*this == Dest) return true;
Scott Michel91099d62009-02-17 22:15:04 +00005467
Chris Lattner10d94f92008-01-16 05:49:24 +00005468 // Don't search too deeply, we just want to be able to see through
5469 // TokenFactor's etc.
5470 if (Depth == 0) return false;
Scott Michel91099d62009-02-17 22:15:04 +00005471
Chris Lattner10d94f92008-01-16 05:49:24 +00005472 // If this is a token factor, all inputs to the TF happen in parallel. If any
5473 // of the operands of the TF reach dest, then we can do the xform.
5474 if (getOpcode() == ISD::TokenFactor) {
5475 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5476 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5477 return true;
5478 return false;
5479 }
Scott Michel91099d62009-02-17 22:15:04 +00005480
Chris Lattner10d94f92008-01-16 05:49:24 +00005481 // Loads don't have side effects, look through them.
5482 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5483 if (!Ld->isVolatile())
5484 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5485 }
5486 return false;
5487}
5488
Evan Chengd9387682008-03-04 00:41:45 +00005489/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Dan Gohmaned7cc322009-10-28 03:44:30 +00005490/// is either an operand of N or it can be reached by traversing up the operands.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005491/// NOTE: this is an expensive method. Use it carefully.
Evan Chengd9387682008-03-04 00:41:45 +00005492bool SDNode::isPredecessorOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005493 SmallPtrSet<SDNode *, 32> Visited;
Dan Gohmaned7cc322009-10-28 03:44:30 +00005494 SmallVector<SDNode *, 16> Worklist;
5495 Worklist.push_back(N);
5496
5497 do {
5498 N = Worklist.pop_back_val();
5499 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5500 SDNode *Op = N->getOperand(i).getNode();
5501 if (Op == this)
5502 return true;
5503 if (Visited.insert(Op))
5504 Worklist.push_back(Op);
5505 }
5506 } while (!Worklist.empty());
5507
5508 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509}
5510
5511uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5512 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005513 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005514}
5515
5516std::string SDNode::getOperationName(const SelectionDAG *G) const {
5517 switch (getOpcode()) {
5518 default:
5519 if (getOpcode() < ISD::BUILTIN_OP_END)
5520 return "<<Unknown DAG Node>>";
Dan Gohmanbd68c792008-07-17 19:10:17 +00005521 if (isMachineOpcode()) {
5522 if (G)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005523 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmanbd68c792008-07-17 19:10:17 +00005524 if (getMachineOpcode() < TII->getNumOpcodes())
5525 return TII->get(getMachineOpcode()).getName();
Chris Lattner5a762642010-02-24 04:24:44 +00005526 return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>";
Dan Gohmanbd68c792008-07-17 19:10:17 +00005527 }
5528 if (G) {
Dan Gohmana0c429e2009-01-15 16:58:17 +00005529 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmanbd68c792008-07-17 19:10:17 +00005530 const char *Name = TLI.getTargetNodeName(getOpcode());
5531 if (Name) return Name;
Chris Lattner5a762642010-02-24 04:24:44 +00005532 return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005533 }
Chris Lattner5a762642010-02-24 04:24:44 +00005534 return "<<Unknown Node #" + utostr(getOpcode()) + ">>";
Scott Michel91099d62009-02-17 22:15:04 +00005535
Dan Gohmanbd68c792008-07-17 19:10:17 +00005536#ifndef NDEBUG
5537 case ISD::DELETED_NODE:
5538 return "<<Deleted Node!>>";
5539#endif
Evan Chengd1d68072008-03-08 00:58:38 +00005540 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth785610d2008-02-16 01:24:58 +00005541 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohmanbebba8d2008-12-23 21:37:04 +00005542 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5543 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5544 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5545 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5546 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5547 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5548 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5549 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5550 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5551 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5552 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5553 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554 case ISD::PCMARKER: return "PCMarker";
5555 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
5556 case ISD::SRCVALUE: return "SrcValue";
5557 case ISD::EntryToken: return "EntryToken";
5558 case ISD::TokenFactor: return "TokenFactor";
5559 case ISD::AssertSext: return "AssertSext";
5560 case ISD::AssertZext: return "AssertZext";
5561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005562 case ISD::BasicBlock: return "BasicBlock";
5563 case ISD::VALUETYPE: return "ValueType";
5564 case ISD::Register: return "Register";
5565
5566 case ISD::Constant: return "Constant";
5567 case ISD::ConstantFP: return "ConstantFP";
5568 case ISD::GlobalAddress: return "GlobalAddress";
5569 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
5570 case ISD::FrameIndex: return "FrameIndex";
5571 case ISD::JumpTable: return "JumpTable";
5572 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendlingfef06052008-09-16 21:48:12 +00005573 case ISD::RETURNADDR: return "RETURNADDR";
5574 case ISD::FRAMEADDR: return "FRAMEADDR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005575 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
5576 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Jim Grosbach29feb6a2009-08-11 00:09:57 +00005577 case ISD::LSDAADDR: return "LSDAADDR";
Bill Wendlingfef06052008-09-16 21:48:12 +00005578 case ISD::EHSELECTION: return "EHSELECTION";
5579 case ISD::EH_RETURN: return "EH_RETURN";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005580 case ISD::ConstantPool: return "ConstantPool";
Bill Wendlingfef06052008-09-16 21:48:12 +00005581 case ISD::ExternalSymbol: return "ExternalSymbol";
Dan Gohman91057512009-10-30 01:27:03 +00005582 case ISD::BlockAddress: return "BlockAddress";
Jakob Stoklund Olesen75dbdd12009-10-15 18:50:03 +00005583 case ISD::INTRINSIC_WO_CHAIN:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005584 case ISD::INTRINSIC_VOID:
5585 case ISD::INTRINSIC_W_CHAIN: {
Jakob Stoklund Olesen75dbdd12009-10-15 18:50:03 +00005586 unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
5587 unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
5588 if (IID < Intrinsic::num_intrinsics)
5589 return Intrinsic::getName((Intrinsic::ID)IID);
5590 else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
5591 return TII->getName(IID);
5592 llvm_unreachable("Invalid intrinsic ID");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005593 }
5594
5595 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
5596 case ISD::TargetConstant: return "TargetConstant";
5597 case ISD::TargetConstantFP:return "TargetConstantFP";
5598 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
5599 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
5600 case ISD::TargetFrameIndex: return "TargetFrameIndex";
5601 case ISD::TargetJumpTable: return "TargetJumpTable";
5602 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendlingfef06052008-09-16 21:48:12 +00005603 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Dan Gohman91057512009-10-30 01:27:03 +00005604 case ISD::TargetBlockAddress: return "TargetBlockAddress";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605
5606 case ISD::CopyToReg: return "CopyToReg";
5607 case ISD::CopyFromReg: return "CopyFromReg";
5608 case ISD::UNDEF: return "undef";
5609 case ISD::MERGE_VALUES: return "merge_values";
5610 case ISD::INLINEASM: return "inlineasm";
Dan Gohmanfa607c92008-07-01 00:05:16 +00005611 case ISD::EH_LABEL: return "eh_label";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005612 case ISD::HANDLENODE: return "handlenode";
Scott Michel91099d62009-02-17 22:15:04 +00005613
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614 // Unary operators
5615 case ISD::FABS: return "fabs";
5616 case ISD::FNEG: return "fneg";
5617 case ISD::FSQRT: return "fsqrt";
5618 case ISD::FSIN: return "fsin";
5619 case ISD::FCOS: return "fcos";
5620 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00005621 case ISD::FPOW: return "fpow";
Dan Gohmanc8b20e22008-08-21 17:55:02 +00005622 case ISD::FTRUNC: return "ftrunc";
5623 case ISD::FFLOOR: return "ffloor";
5624 case ISD::FCEIL: return "fceil";
5625 case ISD::FRINT: return "frint";
5626 case ISD::FNEARBYINT: return "fnearbyint";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627
5628 // Binary operators
5629 case ISD::ADD: return "add";
5630 case ISD::SUB: return "sub";
5631 case ISD::MUL: return "mul";
5632 case ISD::MULHU: return "mulhu";
5633 case ISD::MULHS: return "mulhs";
5634 case ISD::SDIV: return "sdiv";
5635 case ISD::UDIV: return "udiv";
5636 case ISD::SREM: return "srem";
5637 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00005638 case ISD::SMUL_LOHI: return "smul_lohi";
5639 case ISD::UMUL_LOHI: return "umul_lohi";
5640 case ISD::SDIVREM: return "sdivrem";
Dan Gohman2e66b052008-09-08 16:30:29 +00005641 case ISD::UDIVREM: return "udivrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005642 case ISD::AND: return "and";
5643 case ISD::OR: return "or";
5644 case ISD::XOR: return "xor";
5645 case ISD::SHL: return "shl";
5646 case ISD::SRA: return "sra";
5647 case ISD::SRL: return "srl";
5648 case ISD::ROTL: return "rotl";
5649 case ISD::ROTR: return "rotr";
5650 case ISD::FADD: return "fadd";
5651 case ISD::FSUB: return "fsub";
5652 case ISD::FMUL: return "fmul";
5653 case ISD::FDIV: return "fdiv";
5654 case ISD::FREM: return "frem";
5655 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00005656 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005657
5658 case ISD::SETCC: return "setcc";
Nate Begeman9a1ce152008-05-12 19:40:03 +00005659 case ISD::VSETCC: return "vsetcc";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005660 case ISD::SELECT: return "select";
5661 case ISD::SELECT_CC: return "select_cc";
5662 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
5663 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
5664 case ISD::CONCAT_VECTORS: return "concat_vectors";
5665 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
5666 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
5667 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Dale Johannesen747fe522009-06-02 03:12:52 +00005668 case ISD::CARRY_FALSE: return "carry_false";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005669 case ISD::ADDC: return "addc";
5670 case ISD::ADDE: return "adde";
Bill Wendlingea8b7c92008-11-21 02:12:42 +00005671 case ISD::SADDO: return "saddo";
5672 case ISD::UADDO: return "uaddo";
Bill Wendling7e04be62008-12-09 22:08:41 +00005673 case ISD::SSUBO: return "ssubo";
5674 case ISD::USUBO: return "usubo";
5675 case ISD::SMULO: return "smulo";
5676 case ISD::UMULO: return "umulo";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005677 case ISD::SUBC: return "subc";
5678 case ISD::SUBE: return "sube";
5679 case ISD::SHL_PARTS: return "shl_parts";
5680 case ISD::SRA_PARTS: return "sra_parts";
5681 case ISD::SRL_PARTS: return "srl_parts";
Scott Michel91099d62009-02-17 22:15:04 +00005682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005683 // Conversion operators.
5684 case ISD::SIGN_EXTEND: return "sign_extend";
5685 case ISD::ZERO_EXTEND: return "zero_extend";
5686 case ISD::ANY_EXTEND: return "any_extend";
5687 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
5688 case ISD::TRUNCATE: return "truncate";
5689 case ISD::FP_ROUND: return "fp_round";
Dan Gohman819574c2008-01-31 00:41:03 +00005690 case ISD::FLT_ROUNDS_: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
5692 case ISD::FP_EXTEND: return "fp_extend";
5693
5694 case ISD::SINT_TO_FP: return "sint_to_fp";
5695 case ISD::UINT_TO_FP: return "uint_to_fp";
5696 case ISD::FP_TO_SINT: return "fp_to_sint";
5697 case ISD::FP_TO_UINT: return "fp_to_uint";
5698 case ISD::BIT_CONVERT: return "bit_convert";
Anton Korobeynikov899c5cf2010-03-14 18:42:24 +00005699 case ISD::FP16_TO_FP32: return "fp16_to_fp32";
5700 case ISD::FP32_TO_FP16: return "fp32_to_fp16";
Scott Michel91099d62009-02-17 22:15:04 +00005701
Mon P Wang73d31542008-11-10 20:54:11 +00005702 case ISD::CONVERT_RNDSAT: {
5703 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005704 default: llvm_unreachable("Unknown cvt code!");
Mon P Wang73d31542008-11-10 20:54:11 +00005705 case ISD::CVT_FF: return "cvt_ff";
5706 case ISD::CVT_FS: return "cvt_fs";
5707 case ISD::CVT_FU: return "cvt_fu";
5708 case ISD::CVT_SF: return "cvt_sf";
5709 case ISD::CVT_UF: return "cvt_uf";
5710 case ISD::CVT_SS: return "cvt_ss";
5711 case ISD::CVT_SU: return "cvt_su";
5712 case ISD::CVT_US: return "cvt_us";
5713 case ISD::CVT_UU: return "cvt_uu";
5714 }
5715 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005716
5717 // Control flow instructions
5718 case ISD::BR: return "br";
5719 case ISD::BRIND: return "brind";
5720 case ISD::BR_JT: return "br_jt";
5721 case ISD::BRCOND: return "brcond";
5722 case ISD::BR_CC: return "br_cc";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005723 case ISD::CALLSEQ_START: return "callseq_start";
5724 case ISD::CALLSEQ_END: return "callseq_end";
5725
5726 // Other operators
5727 case ISD::LOAD: return "load";
5728 case ISD::STORE: return "store";
5729 case ISD::VAARG: return "vaarg";
5730 case ISD::VACOPY: return "vacopy";
5731 case ISD::VAEND: return "vaend";
5732 case ISD::VASTART: return "vastart";
5733 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
5734 case ISD::EXTRACT_ELEMENT: return "extract_element";
5735 case ISD::BUILD_PAIR: return "build_pair";
5736 case ISD::STACKSAVE: return "stacksave";
5737 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00005738 case ISD::TRAP: return "trap";
5739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005740 // Bit manipulation
5741 case ISD::BSWAP: return "bswap";
5742 case ISD::CTPOP: return "ctpop";
5743 case ISD::CTTZ: return "cttz";
5744 case ISD::CTLZ: return "ctlz";
5745
Duncan Sands38947cd2007-07-27 12:58:54 +00005746 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00005747 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00005748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005749 case ISD::CONDCODE:
5750 switch (cast<CondCodeSDNode>(this)->get()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005751 default: llvm_unreachable("Unknown setcc condition!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005752 case ISD::SETOEQ: return "setoeq";
5753 case ISD::SETOGT: return "setogt";
5754 case ISD::SETOGE: return "setoge";
5755 case ISD::SETOLT: return "setolt";
5756 case ISD::SETOLE: return "setole";
5757 case ISD::SETONE: return "setone";
5758
5759 case ISD::SETO: return "seto";
5760 case ISD::SETUO: return "setuo";
5761 case ISD::SETUEQ: return "setue";
5762 case ISD::SETUGT: return "setugt";
5763 case ISD::SETUGE: return "setuge";
5764 case ISD::SETULT: return "setult";
5765 case ISD::SETULE: return "setule";
5766 case ISD::SETUNE: return "setune";
5767
5768 case ISD::SETEQ: return "seteq";
5769 case ISD::SETGT: return "setgt";
5770 case ISD::SETGE: return "setge";
5771 case ISD::SETLT: return "setlt";
5772 case ISD::SETLE: return "setle";
5773 case ISD::SETNE: return "setne";
5774 }
5775 }
5776}
5777
5778const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
5779 switch (AM) {
5780 default:
5781 return "";
5782 case ISD::PRE_INC:
5783 return "<pre-inc>";
5784 case ISD::PRE_DEC:
5785 return "<pre-dec>";
5786 case ISD::POST_INC:
5787 return "<post-inc>";
5788 case ISD::POST_DEC:
5789 return "<post-dec>";
5790 }
5791}
5792
Duncan Sandsc93fae32008-03-21 09:14:45 +00005793std::string ISD::ArgFlagsTy::getArgFlagsString() {
5794 std::string S = "< ";
5795
5796 if (isZExt())
5797 S += "zext ";
5798 if (isSExt())
5799 S += "sext ";
5800 if (isInReg())
5801 S += "inreg ";
5802 if (isSRet())
5803 S += "sret ";
5804 if (isByVal())
5805 S += "byval ";
5806 if (isNest())
5807 S += "nest ";
5808 if (getByValAlign())
5809 S += "byval-align:" + utostr(getByValAlign()) + " ";
5810 if (getOrigAlign())
5811 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5812 if (getByValSize())
5813 S += "byval-size:" + utostr(getByValSize()) + " ";
5814 return S + ">";
5815}
5816
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005817void SDNode::dump() const { dump(0); }
5818void SDNode::dump(const SelectionDAG *G) const {
David Greened44636d2010-01-05 01:24:36 +00005819 print(dbgs(), G);
Chris Lattner1fefaac2008-08-23 22:23:09 +00005820}
5821
Stuart Hastingsc6250282009-02-04 16:46:19 +00005822void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005823 OS << (void*)this << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005824
5825 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005826 if (i) OS << ",";
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005827 if (getValueType(i) == MVT::Other)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005828 OS << "ch";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005829 else
Owen Andersonac9de032009-08-10 22:56:29 +00005830 OS << getValueType(i).getEVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005831 }
Chris Lattner1fefaac2008-08-23 22:23:09 +00005832 OS << " = " << getOperationName(G);
Stuart Hastingsc6250282009-02-04 16:46:19 +00005833}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005834
Stuart Hastingsc6250282009-02-04 16:46:19 +00005835void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005836 if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
5837 if (!MN->memoperands_empty()) {
5838 OS << "<";
5839 OS << "Mem:";
5840 for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
5841 e = MN->memoperands_end(); i != e; ++i) {
5842 OS << **i;
5843 if (next(i) != e)
5844 OS << " ";
5845 }
5846 OS << ">";
5847 }
5848 } else if (const ShuffleVectorSDNode *SVN =
5849 dyn_cast<ShuffleVectorSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005850 OS << "<";
Nate Begeman543d2142009-04-27 18:41:29 +00005851 for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
5852 int Idx = SVN->getMaskElt(i);
Chris Lattner1fefaac2008-08-23 22:23:09 +00005853 if (i) OS << ",";
Nate Begeman543d2142009-04-27 18:41:29 +00005854 if (Idx < 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005855 OS << "u";
Evan Chengaad43a02007-12-11 02:08:35 +00005856 else
Nate Begeman543d2142009-04-27 18:41:29 +00005857 OS << Idx;
Evan Chengaad43a02007-12-11 02:08:35 +00005858 }
Chris Lattner1fefaac2008-08-23 22:23:09 +00005859 OS << ">";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005860 } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005861 OS << '<' << CSDN->getAPIntValue() << '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005862 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00005863 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005864 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen2fc20782007-09-14 22:26:36 +00005865 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005866 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen2fc20782007-09-14 22:26:36 +00005867 else {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005868 OS << "<APFloat(";
Dale Johannesen6e547b42008-10-09 23:00:39 +00005869 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005870 OS << ")>";
Dale Johannesen2fc20782007-09-14 22:26:36 +00005871 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005872 } else if (const GlobalAddressSDNode *GADN =
5873 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohmancfe43232008-10-18 18:22:42 +00005874 int64_t offset = GADN->getOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005875 OS << '<';
5876 WriteAsOperand(OS, GADN->getGlobal());
5877 OS << '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878 if (offset > 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005879 OS << " + " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005880 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005881 OS << " " << offset;
Dan Gohman36251262009-08-01 19:13:38 +00005882 if (unsigned int TF = GADN->getTargetFlags())
Chris Lattner7123ef72009-06-25 21:21:14 +00005883 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005884 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005885 OS << "<" << FIDN->getIndex() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005886 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005887 OS << "<" << JTDN->getIndex() << ">";
Dan Gohman36251262009-08-01 19:13:38 +00005888 if (unsigned int TF = JTDN->getTargetFlags())
Chris Lattner583c7962009-06-25 21:35:31 +00005889 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005890 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
5891 int offset = CP->getOffset();
5892 if (CP->isMachineConstantPoolEntry())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005893 OS << "<" << *CP->getMachineCPVal() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005894 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005895 OS << "<" << *CP->getConstVal() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005896 if (offset > 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005897 OS << " + " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005898 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005899 OS << " " << offset;
Dan Gohman36251262009-08-01 19:13:38 +00005900 if (unsigned int TF = CP->getTargetFlags())
Chris Lattner583c7962009-06-25 21:35:31 +00005901 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005902 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005903 OS << "<";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005904 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5905 if (LBB)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005906 OS << LBB->getName() << " ";
5907 OS << (const void*)BBDN->getBasicBlock() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005908 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman1e57df32008-02-10 18:45:23 +00005909 if (G && R->getReg() &&
5910 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Dan Gohman57b31652009-10-31 20:19:03 +00005911 OS << " %" << G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005912 } else {
Dan Gohman57b31652009-10-31 20:19:03 +00005913 OS << " %reg" << R->getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005914 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005915 } else if (const ExternalSymbolSDNode *ES =
5916 dyn_cast<ExternalSymbolSDNode>(this)) {
5917 OS << "'" << ES->getSymbol() << "'";
Dan Gohman36251262009-08-01 19:13:38 +00005918 if (unsigned int TF = ES->getTargetFlags())
Chris Lattner7123ef72009-06-25 21:21:14 +00005919 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005920 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5921 if (M->getValue())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005922 OS << "<" << M->getValue() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005923 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005924 OS << "<null>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005925 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Owen Andersonac9de032009-08-10 22:56:29 +00005926 OS << ":" << N->getVT().getEVTString();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005927 }
5928 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Dan Gohman438d4102009-10-29 23:30:06 +00005929 OS << "<" << *LD->getMemOperand();
Evan Cheng034c4f82007-12-18 19:06:30 +00005930
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005931 bool doExt = true;
5932 switch (LD->getExtensionType()) {
5933 default: doExt = false; break;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005934 case ISD::EXTLOAD: OS << ", anyext"; break;
5935 case ISD::SEXTLOAD: OS << ", sext"; break;
5936 case ISD::ZEXTLOAD: OS << ", zext"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005937 }
5938 if (doExt)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005939 OS << " from " << LD->getMemoryVT().getEVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005940
5941 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00005942 if (*AM)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005943 OS << ", " << AM;
5944
5945 OS << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005946 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Dan Gohman438d4102009-10-29 23:30:06 +00005947 OS << "<" << *ST->getMemOperand();
Evan Cheng034c4f82007-12-18 19:06:30 +00005948
5949 if (ST->isTruncatingStore())
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005950 OS << ", trunc to " << ST->getMemoryVT().getEVTString();
Evan Cheng034c4f82007-12-18 19:06:30 +00005951
5952 const char *AM = getIndexedModeName(ST->getAddressingMode());
5953 if (*AM)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005954 OS << ", " << AM;
5955
5956 OS << ">";
5957 } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
Dan Gohman438d4102009-10-29 23:30:06 +00005958 OS << "<" << *M->getMemOperand() << ">";
Dan Gohman91057512009-10-30 01:27:03 +00005959 } else if (const BlockAddressSDNode *BA =
5960 dyn_cast<BlockAddressSDNode>(this)) {
5961 OS << "<";
5962 WriteAsOperand(OS, BA->getBlockAddress()->getFunction(), false);
5963 OS << ", ";
5964 WriteAsOperand(OS, BA->getBlockAddress()->getBasicBlock(), false);
5965 OS << ">";
Dan Gohman885793b2009-11-20 23:18:13 +00005966 if (unsigned int TF = BA->getTargetFlags())
5967 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005968 }
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00005969
5970 if (G)
5971 if (unsigned Order = G->GetOrdering(this))
5972 OS << " [ORD=" << Order << ']';
Dale Johannesenf4243fb2010-03-10 22:13:47 +00005973
Chris Lattner91d5e062010-02-23 19:31:18 +00005974 if (getNodeId() != -1)
5975 OS << " [ID=" << getNodeId() << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005976}
5977
Stuart Hastingsc6250282009-02-04 16:46:19 +00005978void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5979 print_types(OS, G);
Stuart Hastingsc6250282009-02-04 16:46:19 +00005980 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohman63e2b7e2009-11-05 18:49:11 +00005981 if (i) OS << ", "; else OS << " ";
Stuart Hastingsc6250282009-02-04 16:46:19 +00005982 OS << (void*)getOperand(i).getNode();
5983 if (unsigned RN = getOperand(i).getResNo())
5984 OS << ":" << RN;
5985 }
5986 print_details(OS, G);
5987}
5988
David Greeneb43b7322010-01-19 20:37:34 +00005989static void printrWithDepthHelper(raw_ostream &OS, const SDNode *N,
5990 const SelectionDAG *G, unsigned depth,
5991 unsigned indent)
5992{
5993 if (depth == 0)
David Greene2fecfc62010-01-15 19:43:23 +00005994 return;
David Greeneb43b7322010-01-19 20:37:34 +00005995
5996 OS.indent(indent);
5997
5998 N->print(OS, G);
5999
6000 if (depth < 1)
6001 return;
6002
6003 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6004 OS << '\n';
6005 printrWithDepthHelper(OS, N->getOperand(i).getNode(), G, depth-1, indent+2);
David Greene2fecfc62010-01-15 19:43:23 +00006006 }
David Greene2fecfc62010-01-15 19:43:23 +00006007}
6008
David Greeneb43b7322010-01-19 20:37:34 +00006009void SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G,
6010 unsigned depth) const {
6011 printrWithDepthHelper(OS, this, G, depth, 0);
6012}
6013
6014void SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const {
David Greene2fecfc62010-01-15 19:43:23 +00006015 // Don't print impossibly deep things.
David Greeneb43b7322010-01-19 20:37:34 +00006016 printrWithDepth(OS, G, 100);
6017}
6018
6019void SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const {
6020 printrWithDepth(dbgs(), G, depth);
6021}
6022
6023void SDNode::dumprFull(const SelectionDAG *G) const {
6024 // Don't print impossibly deep things.
6025 dumprWithDepth(G, 100);
David Greene2fecfc62010-01-15 19:43:23 +00006026}
6027
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006028static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
6029 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00006030 if (N->getOperand(i).getNode()->hasOneUse())
6031 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006032 else
David Greened44636d2010-01-05 01:24:36 +00006033 dbgs() << "\n" << std::string(indent+2, ' ')
6034 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006035
6036
David Greened44636d2010-01-05 01:24:36 +00006037 dbgs() << "\n";
6038 dbgs().indent(indent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006039 N->dump(G);
6040}
6041
Mon P Wangc707f3f2009-11-30 02:42:02 +00006042SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6043 assert(N->getNumValues() == 1 &&
6044 "Can't unroll a vector with multiple results!");
6045
6046 EVT VT = N->getValueType(0);
6047 unsigned NE = VT.getVectorNumElements();
6048 EVT EltVT = VT.getVectorElementType();
6049 DebugLoc dl = N->getDebugLoc();
6050
6051 SmallVector<SDValue, 8> Scalars;
6052 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6053
6054 // If ResNE is 0, fully unroll the vector op.
6055 if (ResNE == 0)
6056 ResNE = NE;
6057 else if (NE > ResNE)
6058 NE = ResNE;
6059
6060 unsigned i;
6061 for (i= 0; i != NE; ++i) {
6062 for (unsigned j = 0; j != N->getNumOperands(); ++j) {
6063 SDValue Operand = N->getOperand(j);
6064 EVT OperandVT = Operand.getValueType();
6065 if (OperandVT.isVector()) {
6066 // A vector operand; extract a single element.
6067 EVT OperandEltVT = OperandVT.getVectorElementType();
6068 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6069 OperandEltVT,
6070 Operand,
6071 getConstant(i, MVT::i32));
6072 } else {
6073 // A scalar operand; just use it as is.
6074 Operands[j] = Operand;
6075 }
6076 }
6077
6078 switch (N->getOpcode()) {
6079 default:
6080 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6081 &Operands[0], Operands.size()));
6082 break;
6083 case ISD::SHL:
6084 case ISD::SRA:
6085 case ISD::SRL:
6086 case ISD::ROTL:
6087 case ISD::ROTR:
6088 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
6089 getShiftAmountOperand(Operands[1])));
6090 break;
Dan Gohman7196cb12010-01-09 02:13:55 +00006091 case ISD::SIGN_EXTEND_INREG:
6092 case ISD::FP_ROUND_INREG: {
6093 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6094 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6095 Operands[0],
6096 getValueType(ExtVT)));
6097 }
Mon P Wangc707f3f2009-11-30 02:42:02 +00006098 }
6099 }
6100
6101 for (; i < ResNE; ++i)
6102 Scalars.push_back(getUNDEF(EltVT));
6103
6104 return getNode(ISD::BUILD_VECTOR, dl,
6105 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6106 &Scalars[0], Scalars.size());
6107}
6108
Evan Cheng1a029cb2009-12-09 01:36:00 +00006109
6110/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6111/// location that is 'Dist' units away from the location that the 'Base' load
6112/// is loading from.
6113bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
6114 unsigned Bytes, int Dist) const {
6115 if (LD->getChain() != Base->getChain())
6116 return false;
6117 EVT VT = LD->getValueType(0);
6118 if (VT.getSizeInBits() / 8 != Bytes)
6119 return false;
6120
6121 SDValue Loc = LD->getOperand(1);
6122 SDValue BaseLoc = Base->getOperand(1);
6123 if (Loc.getOpcode() == ISD::FrameIndex) {
6124 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6125 return false;
6126 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6127 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6128 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6129 int FS = MFI->getObjectSize(FI);
6130 int BFS = MFI->getObjectSize(BFI);
6131 if (FS != BFS || FS != (int)Bytes) return false;
6132 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6133 }
6134 if (Loc.getOpcode() == ISD::ADD && Loc.getOperand(0) == BaseLoc) {
6135 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Loc.getOperand(1));
6136 if (V && (V->getSExtValue() == Dist*Bytes))
6137 return true;
6138 }
6139
6140 GlobalValue *GV1 = NULL;
6141 GlobalValue *GV2 = NULL;
6142 int64_t Offset1 = 0;
6143 int64_t Offset2 = 0;
6144 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6145 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6146 if (isGA1 && isGA2 && GV1 == GV2)
6147 return Offset1 == (Offset2 + Dist*Bytes);
6148 return false;
6149}
6150
6151
Evan Cheng684647d2009-12-09 01:04:59 +00006152/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6153/// it cannot be inferred.
Evan Chengd3caa132009-12-09 01:10:37 +00006154unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng76ebe862009-12-09 01:53:58 +00006155 // If this is a GlobalAddress + cst, return the alignment.
6156 GlobalValue *GV;
6157 int64_t GVOffset = 0;
Evan Cheng0b592c02010-04-01 06:04:33 +00006158 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
6159 // If GV has specified alignment, then use it. Otherwise, use the preferred
6160 // alignment.
6161 unsigned Align = GV->getAlignment();
6162 if (!Align) {
6163 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
Evan Cheng4a5fdcb2010-04-01 20:13:28 +00006164 if (GVar->hasInitializer()) {
Evan Cheng7fed2682010-04-01 18:19:11 +00006165 const TargetData *TD = TLI.getTargetData();
6166 Align = TD->getPreferredAlignment(GVar);
6167 }
Evan Cheng0b592c02010-04-01 06:04:33 +00006168 }
6169 }
6170 return MinAlign(Align, GVOffset);
6171 }
Evan Cheng76ebe862009-12-09 01:53:58 +00006172
Evan Cheng684647d2009-12-09 01:04:59 +00006173 // If this is a direct reference to a stack slot, use information about the
6174 // stack slot's alignment.
6175 int FrameIdx = 1 << 31;
6176 int64_t FrameOffset = 0;
6177 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6178 FrameIdx = FI->getIndex();
6179 } else if (Ptr.getOpcode() == ISD::ADD &&
6180 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
6181 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
6182 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6183 FrameOffset = Ptr.getConstantOperandVal(1);
6184 }
6185
6186 if (FrameIdx != (1 << 31)) {
6187 // FIXME: Handle FI+CST.
6188 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chenga711e262009-12-09 01:17:24 +00006189 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6190 FrameOffset);
Evan Cheng684647d2009-12-09 01:04:59 +00006191 if (MFI.isFixedObjectIndex(FrameIdx)) {
6192 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
6193
6194 // The alignment of the frame index can be determined from its offset from
6195 // the incoming frame position. If the frame object is at offset 32 and
6196 // the stack is guaranteed to be 16-byte aligned, then we know that the
6197 // object is 16-byte aligned.
6198 unsigned StackAlign = getTarget().getFrameInfo()->getStackAlignment();
6199 unsigned Align = MinAlign(ObjectOffset, StackAlign);
6200
6201 // Finally, the frame object itself may have a known alignment. Factor
6202 // the alignment + offset into a new alignment. For example, if we know
Evan Chenga711e262009-12-09 01:17:24 +00006203 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
Evan Cheng684647d2009-12-09 01:04:59 +00006204 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
6205 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
Evan Cheng684647d2009-12-09 01:04:59 +00006206 return std::max(Align, FIInfoAlign);
6207 }
Evan Chenga711e262009-12-09 01:17:24 +00006208 return FIInfoAlign;
Evan Cheng684647d2009-12-09 01:04:59 +00006209 }
6210
6211 return 0;
6212}
6213
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006214void SelectionDAG::dump() const {
David Greened44636d2010-01-05 01:24:36 +00006215 dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
Scott Michel91099d62009-02-17 22:15:04 +00006216
Dan Gohman5fafd422008-07-15 18:18:54 +00006217 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
6218 I != E; ++I) {
6219 const SDNode *N = I;
Gabor Greif1c80d112008-08-28 21:40:38 +00006220 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman5fafd422008-07-15 18:18:54 +00006221 DumpNodes(N, 2, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006222 }
6223
Gabor Greif1c80d112008-08-28 21:40:38 +00006224 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006225
David Greened44636d2010-01-05 01:24:36 +00006226 dbgs() << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006227}
6228
Stuart Hastingsc6250282009-02-04 16:46:19 +00006229void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
6230 print_types(OS, G);
6231 print_details(OS, G);
6232}
6233
6234typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
Stuart Hastings620a4d12009-02-04 20:30:10 +00006235static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
Bill Wendlingc1946742009-05-30 01:09:53 +00006236 const SelectionDAG *G, VisitedSDNodeSet &once) {
6237 if (!once.insert(N)) // If we've been here before, return now.
Stuart Hastingsc6250282009-02-04 16:46:19 +00006238 return;
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00006239
Stuart Hastingsc6250282009-02-04 16:46:19 +00006240 // Dump the current SDNode, but don't end the line yet.
6241 OS << std::string(indent, ' ');
6242 N->printr(OS, G);
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00006243
Stuart Hastingsc6250282009-02-04 16:46:19 +00006244 // Having printed this SDNode, walk the children:
6245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6246 const SDNode *child = N->getOperand(i).getNode();
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00006247
Stuart Hastingsc6250282009-02-04 16:46:19 +00006248 if (i) OS << ",";
6249 OS << " ";
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00006250
Stuart Hastingsc6250282009-02-04 16:46:19 +00006251 if (child->getNumOperands() == 0) {
6252 // This child has no grandchildren; print it inline right here.
6253 child->printr(OS, G);
6254 once.insert(child);
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00006255 } else { // Just the address. FIXME: also print the child's opcode.
Stuart Hastingsc6250282009-02-04 16:46:19 +00006256 OS << (void*)child;
6257 if (unsigned RN = N->getOperand(i).getResNo())
Bill Wendlingc1946742009-05-30 01:09:53 +00006258 OS << ":" << RN;
Stuart Hastingsc6250282009-02-04 16:46:19 +00006259 }
6260 }
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00006261
Stuart Hastingsc6250282009-02-04 16:46:19 +00006262 OS << "\n";
Bill Wendlingdb23bbb2009-12-21 21:59:52 +00006263
Stuart Hastingsc6250282009-02-04 16:46:19 +00006264 // Dump children that have grandchildren on their own line(s).
6265 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6266 const SDNode *child = N->getOperand(i).getNode();
6267 DumpNodesr(OS, child, indent+2, G, once);
6268 }
6269}
6270
6271void SDNode::dumpr() const {
6272 VisitedSDNodeSet once;
David Greened44636d2010-01-05 01:24:36 +00006273 DumpNodesr(dbgs(), this, 0, 0, once);
Stuart Hastingsc6250282009-02-04 16:46:19 +00006274}
6275
Dan Gohman594bb862009-09-25 00:34:34 +00006276void SDNode::dumpr(const SelectionDAG *G) const {
6277 VisitedSDNodeSet once;
David Greened44636d2010-01-05 01:24:36 +00006278 DumpNodesr(dbgs(), this, 0, G, once);
Dan Gohman594bb862009-09-25 00:34:34 +00006279}
6280
Sanjiv Guptababc5c42009-04-29 04:43:24 +00006281
6282// getAddressSpace - Return the address space this GlobalAddress belongs to.
6283unsigned GlobalAddressSDNode::getAddressSpace() const {
6284 return getGlobal()->getType()->getAddressSpace();
6285}
6286
6287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288const Type *ConstantPoolSDNode::getType() const {
6289 if (isMachineConstantPoolEntry())
6290 return Val.MachineCPVal->getType();
6291 return Val.ConstVal->getType();
6292}
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006293
Bob Wilsone6539682009-03-02 23:24:16 +00006294bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6295 APInt &SplatUndef,
6296 unsigned &SplatBitSize,
6297 bool &HasAnyUndefs,
Dale Johannesen48fd1e42009-11-13 01:45:18 +00006298 unsigned MinSplatBits,
6299 bool isBigEndian) {
Owen Andersonac9de032009-08-10 22:56:29 +00006300 EVT VT = getValueType(0);
Bob Wilsone6539682009-03-02 23:24:16 +00006301 assert(VT.isVector() && "Expected a vector type");
6302 unsigned sz = VT.getSizeInBits();
6303 if (MinSplatBits > sz)
6304 return false;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006305
Bob Wilsone6539682009-03-02 23:24:16 +00006306 SplatValue = APInt(sz, 0);
6307 SplatUndef = APInt(sz, 0);
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006308
Bob Wilsone6539682009-03-02 23:24:16 +00006309 // Get the bits. Bits with undefined values (when the corresponding element
6310 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6311 // in SplatValue. If any of the values are not constant, give up and return
6312 // false.
6313 unsigned int nOps = getNumOperands();
6314 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6315 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen48fd1e42009-11-13 01:45:18 +00006316
6317 for (unsigned j = 0; j < nOps; ++j) {
6318 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006319 SDValue OpVal = getOperand(i);
Dale Johannesen48fd1e42009-11-13 01:45:18 +00006320 unsigned BitPos = j * EltBitSize;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006321
Bob Wilsone6539682009-03-02 23:24:16 +00006322 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen48fd1e42009-11-13 01:45:18 +00006323 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilsone6539682009-03-02 23:24:16 +00006324 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Bob Wilson2b570bb2009-04-13 22:05:19 +00006325 SplatValue |= (APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize).
6326 zextOrTrunc(sz) << BitPos);
Bob Wilsone6539682009-03-02 23:24:16 +00006327 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson0e94e912009-03-04 17:47:01 +00006328 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilsone6539682009-03-02 23:24:16 +00006329 else
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006330 return false;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006331 }
6332
Bob Wilsone6539682009-03-02 23:24:16 +00006333 // The build_vector is all constants or undefs. Find the smallest element
6334 // size that splats the vector.
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006335
Bob Wilsone6539682009-03-02 23:24:16 +00006336 HasAnyUndefs = (SplatUndef != 0);
6337 while (sz > 8) {
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006338
Bob Wilsone6539682009-03-02 23:24:16 +00006339 unsigned HalfSize = sz / 2;
6340 APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize);
6341 APInt LowValue = APInt(SplatValue).trunc(HalfSize);
6342 APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize);
6343 APInt LowUndef = APInt(SplatUndef).trunc(HalfSize);
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006344
Bob Wilsone6539682009-03-02 23:24:16 +00006345 // If the two halves do not match (ignoring undef bits), stop here.
6346 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6347 MinSplatBits > HalfSize)
6348 break;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006349
Bob Wilsone6539682009-03-02 23:24:16 +00006350 SplatValue = HighValue | LowValue;
6351 SplatUndef = HighUndef & LowUndef;
Eric Christopher440d9eb2009-08-22 00:40:45 +00006352
Bob Wilsone6539682009-03-02 23:24:16 +00006353 sz = HalfSize;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006354 }
6355
Bob Wilsone6539682009-03-02 23:24:16 +00006356 SplatBitSize = sz;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006357 return true;
6358}
Nate Begeman543d2142009-04-27 18:41:29 +00006359
Owen Andersonac9de032009-08-10 22:56:29 +00006360bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begemane8f61cb2009-04-29 05:20:52 +00006361 // Find the first non-undef value in the shuffle mask.
6362 unsigned i, e;
6363 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6364 /* search */;
6365
Nate Begeman4da76202009-04-29 18:13:31 +00006366 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher440d9eb2009-08-22 00:40:45 +00006367
Nate Begemane8f61cb2009-04-29 05:20:52 +00006368 // Make sure all remaining elements are either undef or the same as the first
6369 // non-undef value.
6370 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00006371 if (Mask[i] >= 0 && Mask[i] != Idx)
6372 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00006373 return true;
6374}
David Greenec25757c2010-01-20 20:13:31 +00006375
Chris Lattner7de87352010-02-24 21:34:04 +00006376#ifdef XDEBUG
David Greenec25757c2010-01-20 20:13:31 +00006377static void checkForCyclesHelper(const SDNode *N,
Chris Lattner7de87352010-02-24 21:34:04 +00006378 SmallPtrSet<const SDNode*, 32> &Visited,
6379 SmallPtrSet<const SDNode*, 32> &Checked) {
6380 // If this node has already been checked, don't check it again.
6381 if (Checked.count(N))
David Greene923912c2010-02-23 17:37:50 +00006382 return;
Chris Lattner7de87352010-02-24 21:34:04 +00006383
6384 // If a node has already been visited on this depth-first walk, reject it as
6385 // a cycle.
6386 if (!Visited.insert(N)) {
David Greenec25757c2010-01-20 20:13:31 +00006387 dbgs() << "Offending node:\n";
6388 N->dumprFull();
Chris Lattner7de87352010-02-24 21:34:04 +00006389 errs() << "Detected cycle in SelectionDAG\n";
6390 abort();
David Greenec25757c2010-01-20 20:13:31 +00006391 }
Chris Lattner7de87352010-02-24 21:34:04 +00006392
6393 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6394 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
6395
6396 Checked.insert(N);
6397 Visited.erase(N);
David Greenec25757c2010-01-20 20:13:31 +00006398}
Chris Lattner7de87352010-02-24 21:34:04 +00006399#endif
David Greenec25757c2010-01-20 20:13:31 +00006400
6401void llvm::checkForCycles(const llvm::SDNode *N) {
6402#ifdef XDEBUG
6403 assert(N && "Checking nonexistant SDNode");
Chris Lattner7de87352010-02-24 21:34:04 +00006404 SmallPtrSet<const SDNode*, 32> visited;
6405 SmallPtrSet<const SDNode*, 32> checked;
David Greene923912c2010-02-23 17:37:50 +00006406 checkForCyclesHelper(N, visited, checked);
David Greenec25757c2010-01-20 20:13:31 +00006407#endif
6408}
6409
6410void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6411 checkForCycles(DAG->getRoot().getNode());
6412}