blob: 9466f4dd060a2dcb62c3eb64ed04d990084d3514 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendlinge36025e2009-12-21 19:34:59 +000013
Chris Lattner78ec3112003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Chenga8efe282010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070029#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalAlias.h"
33#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/Intrinsics.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
David Greene55d146e2010-01-05 01:24:36 +000036#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000040#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000041#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetIntrinsicInfo.h"
44#include "llvm/Target/TargetLowering.h"
45#include "llvm/Target/TargetMachine.h"
46#include "llvm/Target/TargetOptions.h"
47#include "llvm/Target/TargetRegisterInfo.h"
48#include "llvm/Target/TargetSelectionDAGInfo.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080049#include "llvm/Target/TargetSubtargetInfo.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000050#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000051#include <cmath>
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070052
Chris Lattnere25738c2004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000054
Chris Lattner0b3e5252006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Andersone50ed302009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000065
Jim Laskey58b968b2005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000076}
77
Owen Andersone50ed302009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000081
Dale Johannesenf04afdb2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover0a29cb02013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000089}
90
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000094
Evan Chenga8df1662006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000098 // Look through a bit convert.
Stephen Hines37ed9c12014-12-01 14:51:49 -080099 while (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000101
Evan Chenga8df1662006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000103
Chris Lattner61d43992006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000105
Chris Lattner61d43992006-03-25 22:57:01 +0000106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000109
Chris Lattner61d43992006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Chris Lattner61d43992006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000114 // elements. We have to be a bit careful here, as the type of the constant
115 // may not be the same as the type of the vector elements due to type
116 // legalization (the elements are promoted to a legal type for the target and
117 // a vector of a type may be legal when the base element type is not).
118 // We only want to check enough bits to cover the vector elements, because
119 // we care if the resultant vector is all ones, not whether the individual
120 // constants are.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszak554c6762012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000125 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000128 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000129 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000130 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
Chris Lattner61d43992006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000133 // undefs. Even with the above element type twiddling, this should be OK, as
134 // the same type legalization should have applied to all the elements.
Chris Lattner61d43992006-03-25 22:57:01 +0000135 for (++i; i != e; ++i)
136 if (N->getOperand(i) != NotZero &&
137 N->getOperand(i).getOpcode() != ISD::UNDEF)
138 return false;
139 return true;
140}
141
142
Evan Cheng4a147842006-03-26 09:50:58 +0000143/// isBuildVectorAllZeros - Return true if the specified node is a
144/// BUILD_VECTOR where all of the elements are 0 or undef.
145bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000146 // Look through a bit convert.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800147 while (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000149
Evan Cheng4a147842006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000151
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700152 bool IsAllUndef = true;
153 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i) {
154 if (N->getOperand(i).getOpcode() == ISD::UNDEF)
155 continue;
156 IsAllUndef = false;
157 // Do not accept build_vectors that aren't all constants or which have non-0
158 // elements. We have to be a bit careful here, as the type of the constant
159 // may not be the same as the type of the vector elements due to type
160 // legalization (the elements are promoted to a legal type for the target
161 // and a vector of a type may be legal when the base element type is not).
162 // We only want to check enough bits to cover the vector elements, because
163 // we care if the resultant vector is all zeros, not whether the individual
164 // constants are.
165 SDValue Zero = N->getOperand(i);
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
168 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
169 return false;
170 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
171 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
172 return false;
173 } else
174 return false;
175 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000176
Evan Chenga8df1662006-03-27 06:58:47 +0000177 // Do not accept an all-undef vector.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700178 if (IsAllUndef)
Evan Chenga8df1662006-03-27 06:58:47 +0000179 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000180 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000181}
182
Stephen Hines36b56882014-04-23 16:57:46 -0700183/// \brief Return true if the specified node is a BUILD_VECTOR node of
184/// all ConstantSDNode or undef.
185bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
186 if (N->getOpcode() != ISD::BUILD_VECTOR)
187 return false;
188
189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
190 SDValue Op = N->getOperand(i);
191 if (Op.getOpcode() == ISD::UNDEF)
192 continue;
193 if (!isa<ConstantSDNode>(Op))
194 return false;
195 }
196 return true;
197}
198
Evan Chengefec7512008-02-18 23:04:32 +0000199/// isScalarToVector - Return true if the specified node is a
200/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
201/// element is not an undef.
202bool ISD::isScalarToVector(const SDNode *N) {
203 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
204 return true;
205
206 if (N->getOpcode() != ISD::BUILD_VECTOR)
207 return false;
208 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
209 return false;
210 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000211 if (NumElems == 1)
212 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000213 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000214 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000215 if (V.getOpcode() != ISD::UNDEF)
216 return false;
217 }
218 return true;
219}
220
Nadav Rotemb87bdac2012-07-15 08:38:23 +0000221/// allOperandsUndef - Return true if the node has at least one operand
222/// and all operands of the specified node are ISD::UNDEF.
223bool ISD::allOperandsUndef(const SDNode *N) {
224 // Return false if the node has no operands.
225 // This is "logically inconsistent" with the definition of "all" but
226 // is probably the desired behavior.
227 if (N->getNumOperands() == 0)
228 return false;
229
230 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
231 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
232 return false;
233
234 return true;
235}
236
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700237ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
Stephen Hines36b56882014-04-23 16:57:46 -0700238 switch (ExtType) {
239 case ISD::EXTLOAD:
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700240 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
Stephen Hines36b56882014-04-23 16:57:46 -0700241 case ISD::SEXTLOAD:
242 return ISD::SIGN_EXTEND;
243 case ISD::ZEXTLOAD:
244 return ISD::ZERO_EXTEND;
245 default:
246 break;
247 }
248
249 llvm_unreachable("Invalid LoadExtType");
250}
251
Chris Lattnerc3aae252005-01-07 07:46:32 +0000252/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
253/// when given the operation for (X op Y).
254ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
255 // To perform this operation, we just need to swap the L and G bits of the
256 // operation.
257 unsigned OldL = (Operation >> 2) & 1;
258 unsigned OldG = (Operation >> 1) & 1;
259 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
260 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000261 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000262}
263
264/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
265/// 'op' is a valid SetCC operation.
266ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
267 unsigned Operation = Op;
268 if (isInteger)
269 Operation ^= 7; // Flip L, G, E bits, but not U.
270 else
271 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000272
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000274 Operation &= ~8; // Don't let N and U bits get set.
275
Chris Lattnerc3aae252005-01-07 07:46:32 +0000276 return ISD::CondCode(Operation);
277}
278
279
280/// isSignedOp - For an integer comparison, return 1 if the comparison is a
281/// signed operation and 2 if the result is an unsigned comparison. Return zero
282/// if the operation does not depend on the sign of the input (setne and seteq).
283static int isSignedOp(ISD::CondCode Opcode) {
284 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000285 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000286 case ISD::SETEQ:
287 case ISD::SETNE: return 0;
288 case ISD::SETLT:
289 case ISD::SETLE:
290 case ISD::SETGT:
291 case ISD::SETGE: return 1;
292 case ISD::SETULT:
293 case ISD::SETULE:
294 case ISD::SETUGT:
295 case ISD::SETUGE: return 2;
296 }
297}
298
299/// getSetCCOrOperation - Return the result of a logical OR between different
300/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
301/// returns SETCC_INVALID if it is not possible to represent the resultant
302/// comparison.
303ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
304 bool isInteger) {
305 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
306 // Cannot fold a signed integer setcc with an unsigned integer setcc.
307 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000308
Chris Lattnerc3aae252005-01-07 07:46:32 +0000309 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000310
Chris Lattnerc3aae252005-01-07 07:46:32 +0000311 // If the N and U bits get set then the resultant comparison DOES suddenly
312 // care about orderedness, and is true when ordered.
313 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000314 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Chris Lattnere41102b2006-05-12 17:03:46 +0000316 // Canonicalize illegal integer setcc's.
317 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
318 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000319
Chris Lattnerc3aae252005-01-07 07:46:32 +0000320 return ISD::CondCode(Op);
321}
322
323/// getSetCCAndOperation - Return the result of a logical AND between different
324/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
325/// function returns zero if it is not possible to represent the resultant
326/// comparison.
327ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
328 bool isInteger) {
329 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
330 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000331 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000332
333 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000334 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000335
Chris Lattnera83385f2006-04-27 05:01:07 +0000336 // Canonicalize illegal integer setcc's.
337 if (isInteger) {
338 switch (Result) {
339 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000340 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000341 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000342 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
343 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
344 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000345 }
346 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000347
Chris Lattnera83385f2006-04-27 05:01:07 +0000348 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000349}
350
Jim Laskey58b968b2005-08-17 20:08:02 +0000351//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000352// SDNode Profile Support
353//===----------------------------------------------------------------------===//
354
Jim Laskeydef69b92006-10-27 23:52:51 +0000355/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
356///
Jim Laskey583bd472006-10-27 23:46:08 +0000357static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
358 ID.AddInteger(OpC);
359}
360
361/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
362/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000363static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000364 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000365}
366
Jim Laskeydef69b92006-10-27 23:52:51 +0000367/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
368///
Jim Laskey583bd472006-10-27 23:46:08 +0000369static void AddNodeIDOperands(FoldingSetNodeID &ID,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700370 ArrayRef<SDValue> Ops) {
371 for (auto& Op : Ops) {
372 ID.AddPointer(Op.getNode());
373 ID.AddInteger(Op.getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000374 }
Jim Laskey583bd472006-10-27 23:46:08 +0000375}
376
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000377/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
378///
379static void AddNodeIDOperands(FoldingSetNodeID &ID,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700380 ArrayRef<SDUse> Ops) {
381 for (auto& Op : Ops) {
382 ID.AddPointer(Op.getNode());
383 ID.AddInteger(Op.getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000384 }
385}
386
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700387static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, bool nuw, bool nsw,
388 bool exact) {
389 ID.AddBoolean(nuw);
390 ID.AddBoolean(nsw);
391 ID.AddBoolean(exact);
392}
393
394/// AddBinaryNodeIDCustom - Add BinarySDNodes special infos
395static void AddBinaryNodeIDCustom(FoldingSetNodeID &ID, unsigned Opcode,
396 bool nuw, bool nsw, bool exact) {
397 if (isBinOpWithFlags(Opcode))
398 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
399}
400
Stephen Hinesdce4a402014-05-29 02:49:00 -0700401static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
402 SDVTList VTList, ArrayRef<SDValue> OpList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000403 AddNodeIDOpcode(ID, OpC);
404 AddNodeIDValueTypes(ID, VTList);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700405 AddNodeIDOperands(ID, OpList);
Jim Laskey583bd472006-10-27 23:46:08 +0000406}
407
Duncan Sands0dc40452008-10-27 15:30:53 +0000408/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
409/// the NodeID data.
410static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000411 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000412 case ISD::TargetExternalSymbol:
413 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000414 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000415 default: break; // Normal nodes don't need extra info.
416 case ISD::TargetConstant:
Stephen Hines36b56882014-04-23 16:57:46 -0700417 case ISD::Constant: {
418 const ConstantSDNode *C = cast<ConstantSDNode>(N);
419 ID.AddPointer(C->getConstantIntValue());
420 ID.AddBoolean(C->isOpaque());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000421 break;
Stephen Hines36b56882014-04-23 16:57:46 -0700422 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000423 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000424 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000425 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000426 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000427 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000428 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000429 case ISD::GlobalAddress:
430 case ISD::TargetGlobalTLSAddress:
431 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000432 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000433 ID.AddPointer(GA->getGlobal());
434 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000435 ID.AddInteger(GA->getTargetFlags());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000436 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000437 break;
438 }
439 case ISD::BasicBlock:
440 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
441 break;
442 case ISD::Register:
443 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
444 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000445 case ISD::RegisterMask:
446 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
447 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000448 case ISD::SRCVALUE:
449 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
450 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000451 case ISD::FrameIndex:
452 case ISD::TargetFrameIndex:
453 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
454 break;
455 case ISD::JumpTable:
456 case ISD::TargetJumpTable:
457 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000458 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000459 break;
460 case ISD::ConstantPool:
461 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000462 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000463 ID.AddInteger(CP->getAlignment());
464 ID.AddInteger(CP->getOffset());
465 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000466 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000467 else
468 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000469 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000470 break;
471 }
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +0000472 case ISD::TargetIndex: {
473 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
474 ID.AddInteger(TI->getIndex());
475 ID.AddInteger(TI->getOffset());
476 ID.AddInteger(TI->getTargetFlags());
477 break;
478 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000479 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000480 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000481 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000482 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000483 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000484 break;
485 }
486 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000487 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000488 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000489 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000490 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000491 break;
492 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700493 case ISD::SDIV:
494 case ISD::UDIV:
495 case ISD::SRA:
496 case ISD::SRL:
497 case ISD::MUL:
498 case ISD::ADD:
499 case ISD::SUB:
500 case ISD::SHL: {
501 const BinaryWithFlagsSDNode *BinNode = cast<BinaryWithFlagsSDNode>(N);
502 AddBinaryNodeIDCustom(ID, N->getOpcode(), BinNode->hasNoUnsignedWrap(),
503 BinNode->hasNoSignedWrap(), BinNode->isExact());
504 break;
505 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000506 case ISD::ATOMIC_CMP_SWAP:
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700507 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000508 case ISD::ATOMIC_SWAP:
509 case ISD::ATOMIC_LOAD_ADD:
510 case ISD::ATOMIC_LOAD_SUB:
511 case ISD::ATOMIC_LOAD_AND:
512 case ISD::ATOMIC_LOAD_OR:
513 case ISD::ATOMIC_LOAD_XOR:
514 case ISD::ATOMIC_LOAD_NAND:
515 case ISD::ATOMIC_LOAD_MIN:
516 case ISD::ATOMIC_LOAD_MAX:
517 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000518 case ISD::ATOMIC_LOAD_UMAX:
519 case ISD::ATOMIC_LOAD:
520 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000521 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000522 ID.AddInteger(AT->getMemoryVT().getRawBits());
523 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000524 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
525 break;
526 }
527 case ISD::PREFETCH: {
528 const MemSDNode *PF = cast<MemSDNode>(N);
529 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000530 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000531 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000532 case ISD::VECTOR_SHUFFLE: {
533 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000534 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000535 i != e; ++i)
536 ID.AddInteger(SVN->getMaskElt(i));
537 break;
538 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000539 case ISD::TargetBlockAddress:
540 case ISD::BlockAddress: {
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000541 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
542 ID.AddPointer(BA->getBlockAddress());
543 ID.AddInteger(BA->getOffset());
544 ID.AddInteger(BA->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000545 break;
546 }
Mon P Wang28873102008-06-25 08:15:39 +0000547 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000548
549 // Target specific memory nodes could also have address spaces to check.
550 if (N->isTargetMemoryOpcode())
551 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000552}
553
Duncan Sands0dc40452008-10-27 15:30:53 +0000554/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
555/// data.
556static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
557 AddNodeIDOpcode(ID, N->getOpcode());
558 // Add the return value info.
559 AddNodeIDValueTypes(ID, N->getVTList());
560 // Add the operand info.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700561 AddNodeIDOperands(ID, N->ops());
Duncan Sands0dc40452008-10-27 15:30:53 +0000562
563 // Handle SDNode leafs with special info.
564 AddNodeIDCustom(ID, N);
565}
566
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000567/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000568/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000569/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000570///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000571static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000572encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000573 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000574 assert((ConvType & 3) == ConvType &&
575 "ConvType may not require more than 2 bits!");
576 assert((AM & 7) == AM &&
577 "AM may not require more than 3 bits!");
578 return ConvType |
579 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000580 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000581 (isNonTemporal << 6) |
582 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000583}
584
Jim Laskey583bd472006-10-27 23:46:08 +0000585//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000586// SelectionDAG Class
587//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000588
Duncan Sands0dc40452008-10-27 15:30:53 +0000589/// doNotCSE - Return true if CSE should not be performed for this node.
590static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000591 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000592 return true; // Never CSE anything that produces a flag.
593
594 switch (N->getOpcode()) {
595 default: break;
596 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000597 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000598 return true; // Never CSE these nodes.
599 }
600
601 // Check that remaining values produced are not flags.
602 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000603 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000604 return true; // Never CSE anything that produces a flag.
605
606 return false;
607}
608
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000609/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000610/// SelectionDAG.
611void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000612 // Create a dummy node (which is not added to allnodes), that adds a reference
613 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000614 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000615
Chris Lattner190a4182006-08-04 17:45:20 +0000616 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000617
Chris Lattner190a4182006-08-04 17:45:20 +0000618 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000619 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000620 if (I->use_empty())
621 DeadNodes.push_back(I);
622
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000623 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000624
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000625 // If the root changed (e.g. it was a dead load, update the root).
626 setRoot(Dummy.getValue());
627}
628
629/// RemoveDeadNodes - This method deletes the unreachable nodes in the
630/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000631void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000632
Chris Lattner190a4182006-08-04 17:45:20 +0000633 // Process the worklist, deleting the nodes and adding their uses to the
634 // worklist.
635 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000636 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000637
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000638 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700639 DUL->NodeDeleted(N, nullptr);
Scott Michelfdc40a02009-02-17 22:15:04 +0000640
Chris Lattner190a4182006-08-04 17:45:20 +0000641 // Take the node out of the appropriate CSE map.
642 RemoveNodeFromCSEMaps(N);
643
644 // Next, brutally remove the operand list. This is safe to do, as there are
645 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000646 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
647 SDUse &Use = *I++;
648 SDNode *Operand = Use.getNode();
649 Use.set(SDValue());
650
Chris Lattner190a4182006-08-04 17:45:20 +0000651 // Now that we removed this operand, see if there are no uses of it left.
652 if (Operand->use_empty())
653 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000654 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000655
Dan Gohmanc5336122009-01-19 22:39:36 +0000656 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000657 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000658}
659
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000660void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000661 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000662
663 // Create a dummy node that adds a reference to the root node, preventing
664 // it from being deleted. (This matters if the root is an operand of the
665 // dead node.)
666 HandleSDNode Dummy(getRoot());
667
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000668 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000669}
670
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000671void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000672 // First take this out of the appropriate CSE map.
673 RemoveNodeFromCSEMaps(N);
674
Scott Michelfdc40a02009-02-17 22:15:04 +0000675 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000676 // AllNodes list, and delete the node.
677 DeleteNodeNotInCSEMaps(N);
678}
679
680void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000681 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
682 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000683
Dan Gohmanf06c8352008-09-30 18:30:35 +0000684 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000685 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000686
Dan Gohmanc5336122009-01-19 22:39:36 +0000687 DeallocateNode(N);
688}
689
Stephen Hines37ed9c12014-12-01 14:51:49 -0800690void SDDbgInfo::erase(const SDNode *Node) {
691 DbgValMapType::iterator I = DbgValMap.find(Node);
692 if (I == DbgValMap.end())
693 return;
694 for (auto &Val: I->second)
695 Val->setIsInvalidated();
696 DbgValMap.erase(I);
697}
698
Dan Gohmanc5336122009-01-19 22:39:36 +0000699void SelectionDAG::DeallocateNode(SDNode *N) {
700 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000701 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000702
Dan Gohmanc5336122009-01-19 22:39:36 +0000703 // Set the opcode to DELETED_NODE to help catch bugs when node
704 // memory is reallocated.
705 N->NodeType = ISD::DELETED_NODE;
706
Dan Gohman11467282008-08-26 01:44:34 +0000707 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000708
Stephen Hines37ed9c12014-12-01 14:51:49 -0800709 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
710 // them and forget about that node.
711 DbgInfo->erase(N);
712}
713
714#ifndef NDEBUG
715/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
716static void VerifySDNode(SDNode *N) {
717 switch (N->getOpcode()) {
718 default:
719 break;
720 case ISD::BUILD_PAIR: {
721 EVT VT = N->getValueType(0);
722 assert(N->getNumValues() == 1 && "Too many results!");
723 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
724 "Wrong return type!");
725 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
726 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
727 "Mismatched operand types!");
728 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
729 "Wrong operand type!");
730 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
731 "Wrong return type size");
732 break;
733 }
734 case ISD::BUILD_VECTOR: {
735 assert(N->getNumValues() == 1 && "Too many results!");
736 assert(N->getValueType(0).isVector() && "Wrong return type!");
737 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
738 "Wrong number of operands!");
739 EVT EltVT = N->getValueType(0).getVectorElementType();
740 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
741 assert((I->getValueType() == EltVT ||
742 (EltVT.isInteger() && I->getValueType().isInteger() &&
743 EltVT.bitsLE(I->getValueType()))) &&
744 "Wrong operand type!");
745 assert(I->getValueType() == N->getOperand(0).getValueType() &&
746 "Operands must all have the same type");
747 }
748 break;
749 }
750 }
751}
752#endif // NDEBUG
753
754/// \brief Insert a newly allocated node into the DAG.
755///
756/// Handles insertion into the all nodes list and CSE map, as well as
757/// verification and other common operations when a new node is allocated.
758void SelectionDAG::InsertNode(SDNode *N) {
759 AllNodes.push_back(N);
760#ifndef NDEBUG
761 VerifySDNode(N);
762#endif
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000763}
764
Chris Lattner149c58c2005-08-16 18:17:10 +0000765/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
766/// correspond to it. This is useful when we're about to delete or repurpose
767/// the node. We don't want future request for structurally identical nodes
768/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000769bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000770 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000771 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000772 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000773 case ISD::CONDCODE:
774 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
775 "Cond code doesn't exist!");
Stephen Hinesdce4a402014-05-29 02:49:00 -0700776 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
777 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000778 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000779 case ISD::ExternalSymbol:
780 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000781 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000782 case ISD::TargetExternalSymbol: {
783 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
784 Erased = TargetExternalSymbols.erase(
785 std::pair<std::string,unsigned char>(ESN->getSymbol(),
786 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000787 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000788 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000789 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000790 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000791 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000792 Erased = ExtendedValueTypeNodes.erase(VT);
793 } else {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700794 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
795 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
Duncan Sandsf411b832007-10-17 13:49:58 +0000796 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000797 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000798 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000799 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000800 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000801 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
802 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000803 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000804 break;
805 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000806#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000807 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000808 // flag result (which cannot be CSE'd) or is one of the special cases that are
809 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000810 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000811 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000812 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000813 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000814 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000815 }
816#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000817 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000818}
819
Dan Gohman39946102009-01-25 16:29:12 +0000820/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
821/// maps and modified in place. Add it back to the CSE maps, unless an identical
822/// node already exists, in which case transfer all its users to the existing
823/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000824///
Dan Gohman39946102009-01-25 16:29:12 +0000825void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000826SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000827 // For node types that aren't CSE'd, just act as if no identical node
828 // already exists.
829 if (!doNotCSE(N)) {
830 SDNode *Existing = CSEMap.GetOrInsertNode(N);
831 if (Existing != N) {
832 // If there was already an existing matching node, use ReplaceAllUsesWith
833 // to replace the dead one with the existing one. This can cause
834 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000835 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000836
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000837 // N is now dead. Inform the listeners and delete it.
838 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
839 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000840 DeleteNodeNotInCSEMaps(N);
841 return;
842 }
843 }
Evan Cheng71e86852008-07-08 20:06:39 +0000844
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000845 // If the node doesn't already exist, we updated it. Inform listeners.
846 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
847 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000848}
849
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000850/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000851/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000852/// return null, otherwise return a pointer to the slot it would take. If a
853/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000854SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000855 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000856 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700857 return nullptr;
Evan Cheng71e86852008-07-08 20:06:39 +0000858
Dan Gohman475871a2008-07-27 21:46:04 +0000859 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000860 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700861 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000862 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000863 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000864 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000865}
866
867/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000868/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000869/// return null, otherwise return a pointer to the slot it would take. If a
870/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000871SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000872 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000873 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000874 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700875 return nullptr;
Duncan Sands0dc40452008-10-27 15:30:53 +0000876
Dan Gohman475871a2008-07-27 21:46:04 +0000877 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000878 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700879 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000880 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000881 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000882 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000883}
884
885
886/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000887/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000888/// return null, otherwise return a pointer to the slot it would take. If a
889/// node already exists with these operands, the slot will be non-null.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700890SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
Chris Lattnera5682852006-08-07 23:03:03 +0000891 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000892 if (doNotCSE(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700893 return nullptr;
Evan Cheng71e86852008-07-08 20:06:39 +0000894
Jim Laskey583bd472006-10-27 23:46:08 +0000895 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700896 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
Duncan Sands0dc40452008-10-27 15:30:53 +0000897 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000898 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000899 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000900}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000901
Owen Andersone50ed302009-08-10 22:56:29 +0000902/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000903/// given type.
904///
Owen Andersone50ed302009-08-10 22:56:29 +0000905unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000906 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000907 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000908 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000909
Stephen Hines37ed9c12014-12-01 14:51:49 -0800910 return TLI->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000911}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000912
Dale Johannesen92570c42009-02-07 02:15:05 +0000913// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000914SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800915 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
916 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
917 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
918 UpdateListeners(nullptr) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000919 AllNodes.push_back(&EntryNode);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000920 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000921}
922
Stephen Hines37ed9c12014-12-01 14:51:49 -0800923void SelectionDAG::init(MachineFunction &mf) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000924 MF = &mf;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800925 TLI = getSubtarget().getTargetLowering();
926 TSI = getSubtarget().getSelectionDAGInfo();
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000927 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000928}
929
Chris Lattner78ec3112003-08-11 14:57:33 +0000930SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000931 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000932 allnodes_clear();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000933 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000934}
935
936void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000937 assert(&*AllNodes.begin() == &EntryNode);
938 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000939 while (!AllNodes.empty())
940 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000941}
942
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700943BinarySDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, SDLoc DL,
944 SDVTList VTs, SDValue N1,
945 SDValue N2, bool nuw, bool nsw,
946 bool exact) {
947 if (isBinOpWithFlags(Opcode)) {
948 BinaryWithFlagsSDNode *FN = new (NodeAllocator) BinaryWithFlagsSDNode(
949 Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
950 FN->setHasNoUnsignedWrap(nuw);
951 FN->setHasNoSignedWrap(nsw);
952 FN->setIsExact(exact);
953
954 return FN;
955 }
956
957 BinarySDNode *N = new (NodeAllocator)
958 BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
959 return N;
960}
961
Dan Gohman7c3234c2008-08-27 23:52:12 +0000962void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000963 allnodes_clear();
964 OperandAllocator.Reset();
965 CSEMap.clear();
966
967 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000968 ExternalSymbols.clear();
969 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000970 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700971 static_cast<CondCodeSDNode*>(nullptr));
Dan Gohmanf350b272008-08-23 02:25:05 +0000972 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700973 static_cast<SDNode*>(nullptr));
Dan Gohmanf350b272008-08-23 02:25:05 +0000974
Stephen Hinesdce4a402014-05-29 02:49:00 -0700975 EntryNode.UseList = nullptr;
Dan Gohmanf350b272008-08-23 02:25:05 +0000976 AllNodes.push_back(&EntryNode);
977 Root = getEntryNode();
Evan Cheng31441b72010-03-29 20:48:30 +0000978 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000979}
980
Andrew Trickac6d9be2013-05-25 02:42:55 +0000981SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotema3c42f32011-09-27 11:16:47 +0000982 return VT.bitsGT(Op.getValueType()) ?
983 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
984 getNode(ISD::TRUNCATE, DL, VT, Op);
985}
986
Andrew Trickac6d9be2013-05-25 02:42:55 +0000987SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000988 return VT.bitsGT(Op.getValueType()) ?
989 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
990 getNode(ISD::TRUNCATE, DL, VT, Op);
991}
992
Andrew Trickac6d9be2013-05-25 02:42:55 +0000993SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000994 return VT.bitsGT(Op.getValueType()) ?
995 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
996 getNode(ISD::TRUNCATE, DL, VT, Op);
997}
998
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700999SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, SDLoc SL, EVT VT,
1000 EVT OpVT) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001001 if (VT.bitsLE(Op.getValueType()))
1002 return getNode(ISD::TRUNCATE, SL, VT, Op);
1003
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001004 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001005 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1006}
1007
Andrew Trickac6d9be2013-05-25 02:42:55 +00001008SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +00001009 assert(!VT.isVector() &&
1010 "getZeroExtendInReg should use the vector element type instead of "
1011 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +00001012 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +00001013 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1014 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +00001015 VT.getSizeInBits());
1016 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1017 getConstant(Imm, Op.getValueType()));
1018}
1019
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001020SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1021 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1022 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1023 "The sizes of the input and result must match in order to perform the "
1024 "extend in-register.");
1025 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1026 "The destination vector type must have fewer lanes than the input.");
1027 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1028}
1029
1030SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1031 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1032 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1033 "The sizes of the input and result must match in order to perform the "
1034 "extend in-register.");
1035 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1036 "The destination vector type must have fewer lanes than the input.");
1037 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1038}
1039
1040SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, SDLoc DL, EVT VT) {
1041 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1042 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1043 "The sizes of the input and result must match in order to perform the "
1044 "extend in-register.");
1045 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1046 "The destination vector type must have fewer lanes than the input.");
1047 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1048}
1049
Bob Wilson4c245462009-01-22 17:39:32 +00001050/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1051///
Andrew Trickac6d9be2013-05-25 02:42:55 +00001052SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001053 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +00001054 SDValue NegOne =
1055 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +00001056 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1057}
1058
Stephen Hinesdce4a402014-05-29 02:49:00 -07001059SDValue SelectionDAG::getLogicalNOT(SDLoc DL, SDValue Val, EVT VT) {
1060 EVT EltVT = VT.getScalarType();
1061 SDValue TrueValue;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001062 switch (TLI->getBooleanContents(VT)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001063 case TargetLowering::ZeroOrOneBooleanContent:
1064 case TargetLowering::UndefinedBooleanContent:
1065 TrueValue = getConstant(1, VT);
1066 break;
1067 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1068 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()),
1069 VT);
1070 break;
1071 }
1072 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1073}
1074
Stephen Hines36b56882014-04-23 16:57:46 -07001075SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001076 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +00001077 assert((EltVT.getSizeInBits() >= 64 ||
1078 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1079 "getConstant with a uint64_t value that doesn't fit in the type!");
Stephen Hines36b56882014-04-23 16:57:46 -07001080 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO);
Dan Gohman6394b092008-02-08 22:59:30 +00001081}
1082
Stephen Hines36b56882014-04-23 16:57:46 -07001083SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO)
1084{
1085 return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001086}
1087
Stephen Hines36b56882014-04-23 16:57:46 -07001088SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT,
1089 bool isO) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001090 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +00001091
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001092 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +00001093 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +00001094
Duncan Sands28b77e92011-09-06 19:07:46 +00001095 // In some cases the vector type is legal but the element type is illegal and
1096 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1097 // inserted value (the type does not need to match the vector element type).
1098 // Any extra bits introduced will be truncated away.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001099 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sands28b77e92011-09-06 19:07:46 +00001100 TargetLowering::TypePromoteInteger) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001101 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sands28b77e92011-09-06 19:07:46 +00001102 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1103 Elt = ConstantInt::get(*getContext(), NewVal);
1104 }
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001105 // In other cases the element type is illegal and needs to be expanded, for
1106 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1107 // the value into n parts and use a vector type with n-times the elements.
1108 // Then bitcast to the type requested.
1109 // Legalizing constants too early makes the DAGCombiner's job harder so we
1110 // only legalize if the DAG tells us we must produce legal types.
1111 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1112 TLI->getTypeAction(*getContext(), EltVT) ==
1113 TargetLowering::TypeExpandInteger) {
1114 APInt NewVal = Elt->getValue();
1115 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1116 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1117 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1118 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1119
1120 // Check the temporary vector is the correct size. If this fails then
1121 // getTypeToTransformTo() probably returned a type whose size (in bits)
1122 // isn't a power-of-2 factor of the requested type size.
1123 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1124
1125 SmallVector<SDValue, 2> EltParts;
1126 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1127 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1128 .trunc(ViaEltSizeInBits),
Stephen Hines36b56882014-04-23 16:57:46 -07001129 ViaEltVT, isT, isO));
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001130 }
1131
1132 // EltParts is currently in little endian order. If we actually want
1133 // big-endian order then reverse it now.
1134 if (TLI->isBigEndian())
1135 std::reverse(EltParts.begin(), EltParts.end());
1136
1137 // The elements must be reversed when the element order is different
1138 // to the endianness of the elements (because the BITCAST is itself a
1139 // vector shuffle in this situation). However, we do not need any code to
1140 // perform this reversal because getConstant() is producing a vector
1141 // splat.
1142 // This situation occurs in MIPS MSA.
1143
1144 SmallVector<SDValue, 8> Ops;
1145 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1146 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1147
1148 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1149 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07001150 Ops));
Daniel Sandersea28aaf2013-11-15 12:56:49 +00001151 return Result;
1152 }
Duncan Sands28b77e92011-09-06 19:07:46 +00001153
1154 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1155 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001156 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001157 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001158 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Duncan Sands28b77e92011-09-06 19:07:46 +00001159 ID.AddPointer(Elt);
Stephen Hines36b56882014-04-23 16:57:46 -07001160 ID.AddBoolean(isO);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001161 void *IP = nullptr;
1162 SDNode *N = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001163 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001164 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001165 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001166
Dan Gohman89081322007-12-12 22:21:26 +00001167 if (!N) {
Stephen Hines36b56882014-04-23 16:57:46 -07001168 N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001169 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001170 InsertNode(N);
Dan Gohman89081322007-12-12 22:21:26 +00001171 }
1172
Dan Gohman475871a2008-07-27 21:46:04 +00001173 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001174 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001175 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001176 Ops.assign(VT.getVectorNumElements(), Result);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001177 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman89081322007-12-12 22:21:26 +00001178 }
1179 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001180}
1181
Dan Gohman475871a2008-07-27 21:46:04 +00001182SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001183 return getConstant(Val, TLI->getPointerTy(), isTarget);
Chris Lattner0bd48932008-01-17 07:00:52 +00001184}
1185
1186
Owen Andersone50ed302009-08-10 22:56:29 +00001187SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001188 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001189}
1190
Owen Andersone50ed302009-08-10 22:56:29 +00001191SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001192 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001193
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001194 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195
Chris Lattnerd8658612005-02-17 20:17:32 +00001196 // Do the map lookup using the actual bit pattern for the floating point
1197 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1198 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001199 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001200 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001201 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001202 ID.AddPointer(&V);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001203 void *IP = nullptr;
1204 SDNode *N = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001205 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001206 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001207 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001208
Evan Chengc908dcd2007-06-29 21:36:04 +00001209 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001210 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001211 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001212 InsertNode(N);
Evan Chengc908dcd2007-06-29 21:36:04 +00001213 }
1214
Dan Gohman475871a2008-07-27 21:46:04 +00001215 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001216 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001217 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001218 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001219 // FIXME SDLoc info might be appropriate here
Stephen Hinesdce4a402014-05-29 02:49:00 -07001220 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Ops);
Dan Gohman7f321562007-06-25 16:23:39 +00001221 }
1222 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001223}
1224
Owen Andersone50ed302009-08-10 22:56:29 +00001225SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001226 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001227 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001228 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001229 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001230 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6eb7a422012-12-30 19:03:32 +00001231 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1232 EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001233 bool ignored;
1234 APFloat apf = APFloat(Val);
Tim Northover0a29cb02013-01-22 09:46:31 +00001235 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001236 &ignored);
1237 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001238 } else
1239 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001240}
1241
Andrew Trickac6d9be2013-05-25 02:42:55 +00001242SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001243 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001244 bool isTargetGA,
1245 unsigned char TargetFlags) {
1246 assert((TargetFlags == 0 || isTargetGA) &&
1247 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001248
Dan Gohman6520e202008-10-18 02:06:02 +00001249 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault94437c92013-11-17 00:06:39 +00001250 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman6520e202008-10-18 02:06:02 +00001251 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001252 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001253
Chris Lattner2a4ed822009-06-25 21:21:14 +00001254 unsigned Opc;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001255 if (GV->isThreadLocal())
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001256 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1257 else
1258 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001259
Jim Laskey583bd472006-10-27 23:46:08 +00001260 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001261 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001262 ID.AddPointer(GV);
1263 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001264 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001265 ID.AddInteger(GV->getType()->getAddressSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001266 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001267 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001268 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001269
Andrew Trickac6d9be2013-05-25 02:42:55 +00001270 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1271 DL.getDebugLoc(), GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001272 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001273 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001274 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001275 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001276}
1277
Owen Andersone50ed302009-08-10 22:56:29 +00001278SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001279 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001280 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001281 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001282 ID.AddInteger(FI);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001283 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001284 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001285 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001286
Dan Gohman95531882010-03-18 18:49:47 +00001287 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001288 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001289 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001290 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001291}
1292
Owen Andersone50ed302009-08-10 22:56:29 +00001293SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001294 unsigned char TargetFlags) {
1295 assert((TargetFlags == 0 || isTarget) &&
1296 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001297 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001298 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001299 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001300 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001301 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001302 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001304 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001305
Dan Gohman95531882010-03-18 18:49:47 +00001306 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1307 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001308 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001309 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001310 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001311}
1312
Dan Gohman46510a72010-04-15 01:51:59 +00001313SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001314 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001315 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001316 unsigned char TargetFlags) {
1317 assert((TargetFlags == 0 || isTarget) &&
1318 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001319 if (Alignment == 0)
Stephen Hines37ed9c12014-12-01 14:51:49 -08001320 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001321 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001322 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001323 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001324 ID.AddInteger(Alignment);
1325 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001326 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001327 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001328 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001329 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001330 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001331
Dan Gohman95531882010-03-18 18:49:47 +00001332 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1333 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001334 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001335 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001336 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001337}
1338
Chris Lattnerc3aae252005-01-07 07:46:32 +00001339
Owen Andersone50ed302009-08-10 22:56:29 +00001340SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001341 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001342 bool isTarget,
1343 unsigned char TargetFlags) {
1344 assert((TargetFlags == 0 || isTarget) &&
1345 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001346 if (Alignment == 0)
Stephen Hines37ed9c12014-12-01 14:51:49 -08001347 Alignment = TLI->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001348 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001349 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001350 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Evan Chengd6594ae2006-09-12 21:00:35 +00001351 ID.AddInteger(Alignment);
1352 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001353 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001354 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001355 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001356 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001357 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001358
Dan Gohman95531882010-03-18 18:49:47 +00001359 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1360 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001361 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001362 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001363 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001364}
1365
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001366SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1367 unsigned char TargetFlags) {
1368 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001369 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001370 ID.AddInteger(Index);
1371 ID.AddInteger(Offset);
1372 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001373 void *IP = nullptr;
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001374 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1375 return SDValue(E, 0);
1376
1377 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1378 TargetFlags);
1379 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001380 InsertNode(N);
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001381 return SDValue(N, 0);
1382}
1383
Dan Gohman475871a2008-07-27 21:46:04 +00001384SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001385 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001386 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001387 ID.AddPointer(MBB);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001388 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001389 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001390 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001391
Dan Gohman95531882010-03-18 18:49:47 +00001392 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001393 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001394 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001395 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001396}
1397
Owen Andersone50ed302009-08-10 22:56:29 +00001398SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001399 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1400 ValueTypeNodes.size())
1401 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001402
Duncan Sands83ec4b62008-06-06 12:08:01 +00001403 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001404 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001405
Dan Gohman475871a2008-07-27 21:46:04 +00001406 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001407 N = new (NodeAllocator) VTSDNode(VT);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001408 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001409 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001410}
1411
Owen Andersone50ed302009-08-10 22:56:29 +00001412SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001413 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001414 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001415 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001416 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001417 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001418}
1419
Owen Andersone50ed302009-08-10 22:56:29 +00001420SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001421 unsigned char TargetFlags) {
1422 SDNode *&N =
1423 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1424 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001425 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001426 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001427 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001428 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001429}
1430
Dan Gohman475871a2008-07-27 21:46:04 +00001431SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001432 if ((unsigned)Cond >= CondCodeNodes.size())
1433 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001434
Stephen Hinesdce4a402014-05-29 02:49:00 -07001435 if (!CondCodeNodes[Cond]) {
Dan Gohman95531882010-03-18 18:49:47 +00001436 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001437 CondCodeNodes[Cond] = N;
Stephen Hines37ed9c12014-12-01 14:51:49 -08001438 InsertNode(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001439 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001440
Dan Gohman475871a2008-07-27 21:46:04 +00001441 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001442}
1443
Nate Begeman5a5ca152009-04-29 05:20:52 +00001444// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1445// the shuffle mask M that point at N1 to point at N2, and indices that point
1446// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001447static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1448 std::swap(N1, N2);
1449 int NElts = M.size();
1450 for (int i = 0; i != NElts; ++i) {
1451 if (M[i] >= NElts)
1452 M[i] -= NElts;
1453 else if (M[i] >= 0)
1454 M[i] += NElts;
1455 }
1456}
1457
Andrew Trickac6d9be2013-05-25 02:42:55 +00001458SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001459 SDValue N2, const int *Mask) {
Craig Topperad445a62013-08-09 04:37:24 +00001460 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1461 "Invalid VECTOR_SHUFFLE");
Nate Begeman9008ca62009-04-27 18:41:29 +00001462
1463 // Canonicalize shuffle undef, undef -> undef
1464 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001465 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001466
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001467 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001468 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001469 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001470 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001471 for (unsigned i = 0; i != NElts; ++i) {
1472 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001473 MaskVec.push_back(Mask[i]);
1474 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001475
Nate Begeman9008ca62009-04-27 18:41:29 +00001476 // Canonicalize shuffle v, v -> v, undef
1477 if (N1 == N2) {
1478 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001479 for (unsigned i = 0; i != NElts; ++i)
1480 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001481 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001482
Nate Begeman9008ca62009-04-27 18:41:29 +00001483 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1484 if (N1.getOpcode() == ISD::UNDEF)
1485 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001486
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001487 // If shuffling a splat, try to blend the splat instead. We do this here so
1488 // that even when this arises during lowering we don't have to re-handle it.
1489 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1490 BitVector UndefElements;
1491 SDValue Splat = BV->getSplatValue(&UndefElements);
1492 if (!Splat)
1493 return;
1494
1495 for (int i = 0; i < (int)NElts; ++i) {
1496 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + (int)NElts))
1497 continue;
1498
1499 // If this input comes from undef, mark it as such.
1500 if (UndefElements[MaskVec[i] - Offset]) {
1501 MaskVec[i] = -1;
1502 continue;
1503 }
1504
1505 // If we can blend a non-undef lane, use that instead.
1506 if (!UndefElements[i])
1507 MaskVec[i] = i + Offset;
1508 }
1509 };
1510 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1511 BlendSplat(N1BV, 0);
1512 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1513 BlendSplat(N2BV, NElts);
1514
Nate Begeman9008ca62009-04-27 18:41:29 +00001515 // Canonicalize all index into lhs, -> shuffle lhs, undef
1516 // Canonicalize all index into rhs, -> shuffle rhs, undef
1517 bool AllLHS = true, AllRHS = true;
1518 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001519 for (unsigned i = 0; i != NElts; ++i) {
1520 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001521 if (N2Undef)
1522 MaskVec[i] = -1;
1523 else
1524 AllLHS = false;
1525 } else if (MaskVec[i] >= 0) {
1526 AllRHS = false;
1527 }
1528 }
1529 if (AllLHS && AllRHS)
1530 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001531 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001532 N2 = getUNDEF(VT);
1533 if (AllRHS) {
1534 N1 = getUNDEF(VT);
1535 commuteShuffle(N1, N2, MaskVec);
1536 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001537 // Reset our undef status after accounting for the mask.
1538 N2Undef = N2.getOpcode() == ISD::UNDEF;
1539 // Re-check whether both sides ended up undef.
1540 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1541 return getUNDEF(VT);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001542
Craig Toppereee2a112013-08-08 08:03:12 +00001543 // If Identity shuffle return that node.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001544 bool Identity = true, AllSame = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001545 for (unsigned i = 0; i != NElts; ++i) {
1546 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001547 if (MaskVec[i] != MaskVec[0]) AllSame = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001548 }
Craig Topperad445a62013-08-09 04:37:24 +00001549 if (Identity && NElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00001550 return N1;
Nate Begeman9008ca62009-04-27 18:41:29 +00001551
Stephen Hinesdce4a402014-05-29 02:49:00 -07001552 // Shuffling a constant splat doesn't change the result.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001553 if (N2Undef) {
1554 SDValue V = N1;
1555
1556 // Look through any bitcasts. We check that these don't change the number
1557 // (and size) of elements and just changes their types.
1558 while (V.getOpcode() == ISD::BITCAST)
1559 V = V->getOperand(0);
1560
1561 // A splat should always show up as a build vector node.
1562 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
1563 BitVector UndefElements;
1564 SDValue Splat = BV->getSplatValue(&UndefElements);
1565 // If this is a splat of an undef, shuffling it is also undef.
1566 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1567 return getUNDEF(VT);
1568
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001569 bool SameNumElts =
1570 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1571
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001572 // We only have a splat which can skip shuffles if there is a splatted
1573 // value and no undef lanes rearranged by the shuffle.
1574 if (Splat && UndefElements.none()) {
1575 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1576 // number of elements match or the value splatted is a zero constant.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001577 if (SameNumElts)
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001578 return N1;
1579 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1580 if (C->isNullValue())
1581 return N1;
1582 }
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001583
1584 // If the shuffle itself creates a splat, build the vector directly.
1585 if (AllSame && SameNumElts) {
1586 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1587 SmallVector<SDValue, 8> Ops(NElts, Splatted);
1588
1589 EVT BuildVT = BV->getValueType(0);
1590 SDValue NewBV = getNode(ISD::BUILD_VECTOR, dl, BuildVT, Ops);
1591
1592 // We may have jumped through bitcasts, so the type of the
1593 // BUILD_VECTOR may not match the type of the shuffle.
1594 if (BuildVT != VT)
1595 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1596 return NewBV;
1597 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001598 }
1599 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001600
Nate Begeman9008ca62009-04-27 18:41:29 +00001601 FoldingSetNodeID ID;
1602 SDValue Ops[2] = { N1, N2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001603 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001604 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001605 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001606
Stephen Hinesdce4a402014-05-29 02:49:00 -07001607 void* IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001608 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001609 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001610
Nate Begeman9008ca62009-04-27 18:41:29 +00001611 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1612 // SDNode doesn't have access to it. This memory will be "leaked" when
1613 // the node is deallocated, but recovered when the NodeAllocator is released.
1614 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1615 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001616
Dan Gohman95531882010-03-18 18:49:47 +00001617 ShuffleVectorSDNode *N =
Jack Carter3af4d252013-09-09 22:02:08 +00001618 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1619 dl.getDebugLoc(), N1, N2,
1620 MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001621 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001622 InsertNode(N);
Nate Begeman9008ca62009-04-27 18:41:29 +00001623 return SDValue(N, 0);
1624}
1625
Stephen Hines37ed9c12014-12-01 14:51:49 -08001626SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1627 MVT VT = SV.getSimpleValueType(0);
1628 unsigned NumElems = VT.getVectorNumElements();
1629 SmallVector<int, 8> MaskVec;
1630
1631 for (unsigned i = 0; i != NumElems; ++i) {
1632 int Idx = SV.getMaskElt(i);
1633 if (Idx >= 0) {
1634 if (Idx < (int)NumElems)
1635 Idx += NumElems;
1636 else
1637 Idx -= NumElems;
1638 }
1639 MaskVec.push_back(Idx);
1640 }
1641
1642 SDValue Op0 = SV.getOperand(0);
1643 SDValue Op1 = SV.getOperand(1);
1644 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1645}
1646
Andrew Trickac6d9be2013-05-25 02:42:55 +00001647SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001648 SDValue Val, SDValue DTy,
1649 SDValue STy, SDValue Rnd, SDValue Sat,
1650 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001651 // If the src and dest types are the same and the conversion is between
1652 // integer types of the same sign or two floats, no conversion is necessary.
1653 if (DTy == STy &&
1654 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001655 return Val;
1656
1657 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001658 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001659 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
1660 void* IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001661 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001662 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001663
Jack Carter3af4d252013-09-09 22:02:08 +00001664 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1665 dl.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07001666 Ops, Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001667 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001668 InsertNode(N);
Dale Johannesena04b7572009-02-03 23:04:43 +00001669 return SDValue(N, 0);
1670}
1671
Owen Andersone50ed302009-08-10 22:56:29 +00001672SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001673 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001674 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001675 ID.AddInteger(RegNo);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001676 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001677 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001678 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001679
Dan Gohman95531882010-03-18 18:49:47 +00001680 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001681 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001682 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001683 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001684}
1685
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001686SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1687 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001688 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001689 ID.AddPointer(RegMask);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001690 void *IP = nullptr;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001691 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1692 return SDValue(E, 0);
1693
1694 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1695 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001696 InsertNode(N);
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001697 return SDValue(N, 0);
1698}
1699
Andrew Trickac6d9be2013-05-25 02:42:55 +00001700SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001701 FoldingSetNodeID ID;
1702 SDValue Ops[] = { Root };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001703 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattner7561d482010-03-14 02:33:54 +00001704 ID.AddPointer(Label);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001705 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001706 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001707 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001708
Jack Carter3af4d252013-09-09 22:02:08 +00001709 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1710 dl.getDebugLoc(), Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001711 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001712 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00001713 return SDValue(N, 0);
1714}
1715
Chris Lattner7561d482010-03-14 02:33:54 +00001716
Dan Gohman46510a72010-04-15 01:51:59 +00001717SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001718 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001719 bool isTarget,
1720 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001721 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1722
1723 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001724 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001725 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001726 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001727 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001728 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001729 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001730 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001731
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001732 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1733 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001734 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001735 InsertNode(N);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001736 return SDValue(N, 0);
1737}
1738
Dan Gohman475871a2008-07-27 21:46:04 +00001739SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001740 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001741 "SrcValue is not a pointer?");
1742
Jim Laskey583bd472006-10-27 23:46:08 +00001743 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001744 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001745 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001746
Stephen Hinesdce4a402014-05-29 02:49:00 -07001747 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001748 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001749 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001750
Dan Gohman95531882010-03-18 18:49:47 +00001751 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001752 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001753 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001754 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001755}
1756
Chris Lattnerdecc2672010-04-07 05:20:54 +00001757/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1758SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1759 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001760 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattnerdecc2672010-04-07 05:20:54 +00001761 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001762
Stephen Hinesdce4a402014-05-29 02:49:00 -07001763 void *IP = nullptr;
Chris Lattnerdecc2672010-04-07 05:20:54 +00001764 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1765 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001766
Chris Lattnerdecc2672010-04-07 05:20:54 +00001767 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1768 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001769 InsertNode(N);
Chris Lattnerdecc2672010-04-07 05:20:54 +00001770 return SDValue(N, 0);
1771}
1772
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001773/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1774SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1775 unsigned SrcAS, unsigned DestAS) {
1776 SDValue Ops[] = {Ptr};
1777 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001778 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001779 ID.AddInteger(SrcAS);
1780 ID.AddInteger(DestAS);
1781
Stephen Hinesdce4a402014-05-29 02:49:00 -07001782 void *IP = nullptr;
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001783 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1784 return SDValue(E, 0);
1785
1786 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1787 dl.getDebugLoc(),
1788 VT, Ptr, SrcAS, DestAS);
1789 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001790 InsertNode(N);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001791 return SDValue(N, 0);
1792}
Chris Lattnerdecc2672010-04-07 05:20:54 +00001793
Duncan Sands92abc622009-01-31 15:50:11 +00001794/// getShiftAmountOperand - Return the specified value casted to
1795/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001796SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001797 EVT OpTy = Op.getValueType();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001798 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001799 if (OpTy == ShTy || OpTy.isVector()) return Op;
1800
1801 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001802 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001803}
1804
Chris Lattner37ce9df2007-10-15 17:47:20 +00001805/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1806/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001807SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001808 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001809 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001810 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001811 unsigned StackAlign =
Bill Wendlingba54bca2013-06-19 21:36:55 +00001812 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001813
David Greene3f2bf852009-11-12 20:49:22 +00001814 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001815 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner37ce9df2007-10-15 17:47:20 +00001816}
1817
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001818/// CreateStackTemporary - Create a stack temporary suitable for holding
1819/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001820SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001821 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1822 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001823 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1824 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001825 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001826 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1827 TD->getPrefTypeAlignment(Ty2));
1828
1829 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001830 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001831 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001832}
1833
Owen Andersone50ed302009-08-10 22:56:29 +00001834SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001835 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001836 // These setcc operations always fold.
1837 switch (Cond) {
1838 default: break;
1839 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001840 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001841 case ISD::SETTRUE:
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001842 case ISD::SETTRUE2: {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001843 TargetLowering::BooleanContent Cnt =
1844 TLI->getBooleanContents(N1->getValueType(0));
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001845 return getConstant(
1846 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1847 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001848
Chris Lattnera83385f2006-04-27 05:01:07 +00001849 case ISD::SETOEQ:
1850 case ISD::SETOGT:
1851 case ISD::SETOGE:
1852 case ISD::SETOLT:
1853 case ISD::SETOLE:
1854 case ISD::SETONE:
1855 case ISD::SETO:
1856 case ISD::SETUO:
1857 case ISD::SETUEQ:
1858 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001859 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001860 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001861 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001862
Gabor Greifba36cb52008-08-28 21:40:38 +00001863 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001864 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001865 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001866 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001867
Chris Lattnerc3aae252005-01-07 07:46:32 +00001868 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001869 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001870 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1871 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001872 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1873 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1874 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1875 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1876 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1877 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1878 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1879 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001880 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001881 }
Chris Lattner67255a12005-04-07 18:14:58 +00001882 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001883 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1884 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001885 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001886 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001887 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001888 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001889 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001890 // fall through
1891 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001892 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001893 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001894 // fall through
1895 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001896 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001897 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001898 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001899 // fall through
1900 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001901 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001902 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001903 // fall through
1904 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001905 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001906 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001907 // fall through
1908 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001909 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001910 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001911 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001912 // fall through
1913 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001914 R==APFloat::cmpEqual, VT);
1915 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1916 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1917 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1918 R==APFloat::cmpEqual, VT);
1919 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1920 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1921 R==APFloat::cmpLessThan, VT);
1922 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1923 R==APFloat::cmpUnordered, VT);
1924 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1925 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001926 }
1927 } else {
1928 // Ensure that the constant occurs on the RHS.
Tom Stellard12d43f92013-09-28 02:50:38 +00001929 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1930 MVT CompVT = N1.getValueType().getSimpleVT();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001931 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellard12d43f92013-09-28 02:50:38 +00001932 return SDValue();
1933
1934 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001935 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001936 }
1937
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001938 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001939 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001940}
1941
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001942/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1943/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001944bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001945 // This predicate is not safe for vector operations.
1946 if (Op.getValueType().isVector())
1947 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001948
Dan Gohman87862e72009-12-11 21:31:27 +00001949 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001950 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1951}
1952
Dan Gohmanea859be2007-06-22 14:59:07 +00001953/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1954/// this predicate to simplify operations downstream. Mask is known to be zero
1955/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001956bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001957 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001958 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001959 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001960 return (KnownZero & Mask) == Mask;
1961}
1962
Stephen Hinesdce4a402014-05-29 02:49:00 -07001963/// Determine which bits of Op are known to be either zero or one and return
1964/// them in the KnownZero/KnownOne bitsets.
1965void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1966 APInt &KnownOne, unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001967 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001968
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001969 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001970 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001971 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001972
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001973 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001974
1975 switch (Op.getOpcode()) {
1976 case ISD::Constant:
1977 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001978 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1979 KnownZero = ~KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001980 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001981 case ISD::AND:
1982 // If either the LHS or the RHS are Zero, the result is zero.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001983 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1984 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001985
1986 // Output known-1 bits are only known if set in both the LHS & RHS.
1987 KnownOne &= KnownOne2;
1988 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1989 KnownZero |= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001990 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001991 case ISD::OR:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001992 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1993 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Dan Gohmanea859be2007-06-22 14:59:07 +00001995 // Output known-0 bits are only known if clear in both the LHS & RHS.
1996 KnownZero &= KnownZero2;
1997 // Output known-1 are known to be set if set in either the LHS | RHS.
1998 KnownOne |= KnownOne2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001999 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002000 case ISD::XOR: {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002001 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2002 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002003
Dan Gohmanea859be2007-06-22 14:59:07 +00002004 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002005 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00002006 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2007 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2008 KnownZero = KnownZeroOut;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002009 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002010 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002011 case ISD::MUL: {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002012 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2013 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002014
2015 // If low bits are zero in either operand, output low known-0 bits.
2016 // Also compute a conserative estimate for high known-0 bits.
2017 // More trickiness is possible, but this is sufficient for the
2018 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00002019 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00002020 unsigned TrailZ = KnownZero.countTrailingOnes() +
2021 KnownZero2.countTrailingOnes();
2022 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00002023 KnownZero2.countLeadingOnes(),
2024 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00002025
2026 TrailZ = std::min(TrailZ, BitWidth);
2027 LeadZ = std::min(LeadZ, BitWidth);
2028 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2029 APInt::getHighBitsSet(BitWidth, LeadZ);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002030 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00002031 }
2032 case ISD::UDIV: {
2033 // For the purposes of computing leading zeros we can conservatively
2034 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00002035 // be less than the denominator.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002036 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002037 unsigned LeadZ = KnownZero2.countLeadingOnes();
2038
Jay Foad7a874dd2010-12-01 08:53:58 +00002039 KnownOne2.clearAllBits();
2040 KnownZero2.clearAllBits();
Stephen Hinesdce4a402014-05-29 02:49:00 -07002041 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00002042 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2043 if (RHSUnknownLeadingOnes != BitWidth)
2044 LeadZ = std::min(BitWidth,
2045 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002046
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002047 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002048 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00002049 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002050 case ISD::SELECT:
Stephen Hinesdce4a402014-05-29 02:49:00 -07002051 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2052 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002053
Dan Gohmanea859be2007-06-22 14:59:07 +00002054 // Only known if known in both the LHS and RHS.
2055 KnownOne &= KnownOne2;
2056 KnownZero &= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002057 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002058 case ISD::SELECT_CC:
Stephen Hinesdce4a402014-05-29 02:49:00 -07002059 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2060 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002061
Dan Gohmanea859be2007-06-22 14:59:07 +00002062 // Only known if known in both the LHS and RHS.
2063 KnownOne &= KnownOne2;
2064 KnownZero &= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002065 break;
Bill Wendling253174b2008-11-22 07:24:01 +00002066 case ISD::SADDO:
2067 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002068 case ISD::SSUBO:
2069 case ISD::USUBO:
2070 case ISD::SMULO:
2071 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002072 if (Op.getResNo() != 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002073 break;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002074 // The boolean result conforms to getBooleanContents.
2075 // If we know the result of a setcc has the top bits zero, use this info.
2076 // We know that we have an integer-based boolean since these operations
2077 // are only available for integer.
2078 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2079 TargetLowering::ZeroOrOneBooleanContent &&
2080 BitWidth > 1)
2081 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2082 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002083 case ISD::SETCC:
2084 // If we know the result of a setcc has the top bits zero, use this info.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002085 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2086 TargetLowering::ZeroOrOneBooleanContent &&
2087 BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002088 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002089 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002090 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002091 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002092 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002093 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00002094
2095 // If the shift count is an invalid immediate, don't do anything.
2096 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002097 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002098
Stephen Hinesdce4a402014-05-29 02:49:00 -07002099 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmand4cf9922008-02-26 18:50:50 +00002100 KnownZero <<= ShAmt;
2101 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002102 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00002103 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002104 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002105 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002106 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002107 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002108 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002109 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002110
Dan Gohmand4cf9922008-02-26 18:50:50 +00002111 // If the shift count is an invalid immediate, don't do anything.
2112 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002113 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002114
Stephen Hinesdce4a402014-05-29 02:49:00 -07002115 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002116 KnownZero = KnownZero.lshr(ShAmt);
2117 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002118
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002119 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002120 KnownZero |= HighBits; // High bits known zero.
2121 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002122 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002123 case ISD::SRA:
2124 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002125 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002126
Dan Gohmand4cf9922008-02-26 18:50:50 +00002127 // If the shift count is an invalid immediate, don't do anything.
2128 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002129 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002130
Dan Gohmanea859be2007-06-22 14:59:07 +00002131 // If any of the demanded bits are produced by the sign extension, we also
2132 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002133 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002134
Stephen Hinesdce4a402014-05-29 02:49:00 -07002135 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002136 KnownZero = KnownZero.lshr(ShAmt);
2137 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002138
Dan Gohmanea859be2007-06-22 14:59:07 +00002139 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002140 APInt SignBit = APInt::getSignBit(BitWidth);
2141 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00002142
Dan Gohmanca93a432008-02-20 16:30:17 +00002143 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002144 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00002145 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002146 KnownOne |= HighBits; // New bits are known one.
2147 }
2148 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002149 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002150 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002151 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002152 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002153
2154 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00002155 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002156 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002157
Dan Gohman977a76f2008-02-13 22:28:48 +00002158 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002159 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002160
Dan Gohmanea859be2007-06-22 14:59:07 +00002161 // If the sign extended bits are demanded, we know that the sign
2162 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00002163 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00002164 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002165 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00002166
Stephen Hinesdce4a402014-05-29 02:49:00 -07002167 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002168 KnownOne &= InputDemandedBits;
2169 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002170
Dan Gohmanea859be2007-06-22 14:59:07 +00002171 // If the sign bit of the input is known set or clear, then we know the
2172 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00002173 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00002174 KnownZero |= NewBits;
2175 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00002176 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00002177 KnownOne |= NewBits;
2178 KnownZero &= ~NewBits;
2179 } else { // Input sign bit unknown
2180 KnownZero &= ~NewBits;
2181 KnownOne &= ~NewBits;
2182 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002183 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002184 }
2185 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002186 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00002187 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002188 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00002189 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002190 unsigned LowBits = Log2_32(BitWidth)+1;
2191 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00002192 KnownOne.clearAllBits();
Stephen Hinesdce4a402014-05-29 02:49:00 -07002193 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002194 }
2195 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00002196 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem77451752013-03-20 22:53:44 +00002197 // If this is a ZEXTLoad and we are looking at the loaded value.
2198 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00002199 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002200 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002201 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00002202 } else if (const MDNode *Ranges = LD->getRanges()) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002203 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002204 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002205 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002206 }
2207 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002208 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002209 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002210 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00002211 KnownZero = KnownZero.trunc(InBits);
2212 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002213 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002214 KnownZero = KnownZero.zext(BitWidth);
2215 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002216 KnownZero |= NewBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002217 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002218 }
2219 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002220 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002221 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002222 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002223
Jay Foad40f8f622010-12-07 08:25:19 +00002224 KnownZero = KnownZero.trunc(InBits);
2225 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002226 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00002227
2228 // Note if the sign bit is known to be zero or one.
2229 bool SignBitKnownZero = KnownZero.isNegative();
2230 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohman977a76f2008-02-13 22:28:48 +00002231
Jay Foad40f8f622010-12-07 08:25:19 +00002232 KnownZero = KnownZero.zext(BitWidth);
2233 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002234
Dan Gohman977a76f2008-02-13 22:28:48 +00002235 // If the sign bit is known zero or one, the top bits match.
2236 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00002237 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00002238 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00002239 KnownOne |= NewBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002240 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002241 }
2242 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00002243 EVT InVT = Op.getOperand(0).getValueType();
2244 unsigned InBits = InVT.getScalarType().getSizeInBits();
2245 KnownZero = KnownZero.trunc(InBits);
2246 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002247 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng2766a472012-12-06 19:13:27 +00002248 KnownZero = KnownZero.zext(BitWidth);
2249 KnownOne = KnownOne.zext(BitWidth);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002250 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002251 }
2252 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00002253 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002254 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00002255 KnownZero = KnownZero.zext(InBits);
2256 KnownOne = KnownOne.zext(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002257 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002258 KnownZero = KnownZero.trunc(BitWidth);
2259 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002260 break;
2261 }
2262 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002263 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002264 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Stephen Hinesdce4a402014-05-29 02:49:00 -07002265 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002266 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002267 KnownOne &= (~KnownZero);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002268 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002269 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002270 case ISD::FGETSIGN:
2271 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002272 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002273 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002274
Dan Gohman23e8b712008-04-28 17:02:21 +00002275 case ISD::SUB: {
2276 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2277 // We know that the top bits of C-X are clear if X contains less bits
2278 // than C (i.e. no wrap-around can happen). For example, 20-X is
2279 // positive if we can prove that X is >= 0 and < 16.
2280 if (CLHS->getAPIntValue().isNonNegative()) {
2281 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2282 // NLZ can't be BitWidth with no sign bit
2283 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002284 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002285
2286 // If all of the MaskV bits are known to be zero, then we know the
2287 // output top bits are zero, because we now know that the output is
2288 // from [0-C].
2289 if ((KnownZero2 & MaskV) == MaskV) {
2290 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2291 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002292 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002293 }
2294 }
2295 }
2296 }
2297 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002298 case ISD::ADD:
2299 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002300 // Output known-0 bits are known if clear or set in both the low clear bits
2301 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2302 // low 3 bits clear.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002303 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002304 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2305
Stephen Hinesdce4a402014-05-29 02:49:00 -07002306 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002307 KnownZeroOut = std::min(KnownZeroOut,
2308 KnownZero2.countTrailingOnes());
2309
Chris Lattnerda605882010-12-19 20:38:28 +00002310 if (Op.getOpcode() == ISD::ADD) {
2311 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002312 break;
Chris Lattnerda605882010-12-19 20:38:28 +00002313 }
2314
2315 // With ADDE, a carry bit may be added in, so we can only use this
2316 // information if we know (at least) that the low two bits are clear. We
2317 // then return to the caller that the low bit is unknown but that other bits
2318 // are known zero.
2319 if (KnownZeroOut >= 2) // ADDE
2320 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002321 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002322 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002323 case ISD::SREM:
2324 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002325 const APInt &RA = Rem->getAPIntValue().abs();
2326 if (RA.isPowerOf2()) {
2327 APInt LowBits = RA - 1;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002328 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002329
Duncan Sands5c2873a2010-01-29 09:45:26 +00002330 // The low bits of the first operand are unchanged by the srem.
2331 KnownZero = KnownZero2 & LowBits;
2332 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002333
Duncan Sands5c2873a2010-01-29 09:45:26 +00002334 // If the first operand is non-negative or has all low bits zero, then
2335 // the upper bits are all zero.
2336 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2337 KnownZero |= ~LowBits;
2338
2339 // If the first operand is negative and not all low bits are zero, then
2340 // the upper bits are all one.
2341 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2342 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002343 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002344 }
2345 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002346 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00002347 case ISD::UREM: {
2348 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002349 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002350 if (RA.isPowerOf2()) {
2351 APInt LowBits = (RA - 1);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002352 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2353
2354 // The upper bits are all zero, the lower ones are unchanged.
2355 KnownZero = KnownZero2 | ~LowBits;
2356 KnownOne = KnownOne2 & LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002357 break;
2358 }
2359 }
2360
2361 // Since the result is less than or equal to either operand, any leading
2362 // zero bits in either operand must also exist in the result.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002363 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2364 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002365
2366 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2367 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002368 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002369 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002370 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002371 }
Stephen Hinesebe69fe2015-03-23 12:10:34 -07002372 case ISD::EXTRACT_ELEMENT: {
2373 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2374 const unsigned Index =
2375 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2376 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2377
2378 // Remove low part of known bits mask
2379 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2380 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2381
2382 // Remove high part of known bit mask
2383 KnownZero = KnownZero.trunc(BitWidth);
2384 KnownOne = KnownOne.trunc(BitWidth);
2385 break;
2386 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002387 case ISD::FrameIndex:
2388 case ISD::TargetFrameIndex:
2389 if (unsigned Align = InferPtrAlignment(Op)) {
2390 // The low bits are known zero if the pointer is aligned.
2391 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002392 break;
Chris Lattner0a9481f2011-02-13 22:25:43 +00002393 }
2394 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002395
Dan Gohmanea859be2007-06-22 14:59:07 +00002396 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002397 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2398 break;
2399 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002400 case ISD::INTRINSIC_WO_CHAIN:
2401 case ISD::INTRINSIC_W_CHAIN:
2402 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002403 // Allow the target to implement this method for its nodes.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002404 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
2405 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002406 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002407
2408 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002409}
2410
2411/// ComputeNumSignBits - Return the number of times the sign bit of the
2412/// register is replicated into the other bits. We know that at least 1 bit
2413/// is always equal to the sign bit (itself), but other cases can give us
2414/// information. For example, immediately after an "SRA X, 2", we know that
2415/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002416unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002417 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002418 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002419 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002420 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002421 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002422
Dan Gohmanea859be2007-06-22 14:59:07 +00002423 if (Depth == 6)
2424 return 1; // Limit search depth.
2425
2426 switch (Op.getOpcode()) {
2427 default: break;
2428 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002429 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002430 return VTBits-Tmp+1;
2431 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002432 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002433 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002434
Dan Gohmanea859be2007-06-22 14:59:07 +00002435 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002436 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002437 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002438 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002439
Dan Gohmanea859be2007-06-22 14:59:07 +00002440 case ISD::SIGN_EXTEND:
Jack Carter3af4d252013-09-09 22:02:08 +00002441 Tmp =
2442 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002443 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002444
Dan Gohmanea859be2007-06-22 14:59:07 +00002445 case ISD::SIGN_EXTEND_INREG:
2446 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002447 Tmp =
2448 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002449 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002450
Dan Gohmanea859be2007-06-22 14:59:07 +00002451 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2452 return std::max(Tmp, Tmp2);
2453
2454 case ISD::SRA:
2455 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2456 // SRA X, C -> adds C sign bits.
2457 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002458 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002459 if (Tmp > VTBits) Tmp = VTBits;
2460 }
2461 return Tmp;
2462 case ISD::SHL:
2463 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2464 // shl destroys sign bits.
2465 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002466 if (C->getZExtValue() >= VTBits || // Bad shift.
2467 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2468 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002469 }
2470 break;
2471 case ISD::AND:
2472 case ISD::OR:
2473 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002474 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002475 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002476 if (Tmp != 1) {
2477 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2478 FirstAnswer = std::min(Tmp, Tmp2);
2479 // We computed what we know about the sign bits as our first
2480 // answer. Now proceed to the generic code that uses
Stephen Hinesdce4a402014-05-29 02:49:00 -07002481 // computeKnownBits, and pick whichever answer is better.
Dan Gohmana332f172008-05-23 02:28:01 +00002482 }
2483 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002484
2485 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002486 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002487 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002488 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002489 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002490
2491 case ISD::SADDO:
2492 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002493 case ISD::SSUBO:
2494 case ISD::USUBO:
2495 case ISD::SMULO:
2496 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002497 if (Op.getResNo() != 1)
2498 break;
Duncan Sands03228082008-11-23 15:47:28 +00002499 // The boolean result conforms to getBooleanContents. Fall through.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002500 // If setcc returns 0/-1, all bits are sign bits.
2501 // We know that we have an integer-based boolean since these operations
2502 // are only available for integer.
2503 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2504 TargetLowering::ZeroOrNegativeOneBooleanContent)
2505 return VTBits;
2506 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002507 case ISD::SETCC:
2508 // If setcc returns 0/-1, all bits are sign bits.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002509 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002510 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002511 return VTBits;
2512 break;
2513 case ISD::ROTL:
2514 case ISD::ROTR:
2515 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002516 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002517
Dan Gohmanea859be2007-06-22 14:59:07 +00002518 // Handle rotate right by N like a rotate left by 32-N.
2519 if (Op.getOpcode() == ISD::ROTR)
2520 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2521
2522 // If we aren't rotating out all of the known-in sign bits, return the
2523 // number that are left. This handles rotl(sext(x), 1) for example.
2524 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2525 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2526 }
2527 break;
2528 case ISD::ADD:
2529 // Add can have at most one carry bit. Thus we know that the output
2530 // is, at worst, one more bit than the inputs.
2531 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2532 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002533
Dan Gohmanea859be2007-06-22 14:59:07 +00002534 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002535 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002536 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002537 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002538 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002539
Dan Gohmanea859be2007-06-22 14:59:07 +00002540 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2541 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002542 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002543 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002544
Dan Gohmanea859be2007-06-22 14:59:07 +00002545 // If we are subtracting one from a positive number, there is no carry
2546 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002547 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002548 return Tmp;
2549 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002550
Dan Gohmanea859be2007-06-22 14:59:07 +00002551 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2552 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002553 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002554
Dan Gohmanea859be2007-06-22 14:59:07 +00002555 case ISD::SUB:
2556 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2557 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002558
Dan Gohmanea859be2007-06-22 14:59:07 +00002559 // Handle NEG.
2560 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002561 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002562 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002563 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002564 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2565 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002566 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002567 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002568
Dan Gohmanea859be2007-06-22 14:59:07 +00002569 // If the input is known to be positive (the sign bit is known clear),
2570 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002571 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002572 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002573
Dan Gohmanea859be2007-06-22 14:59:07 +00002574 // Otherwise, we treat this like a SUB.
2575 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002576
Dan Gohmanea859be2007-06-22 14:59:07 +00002577 // Sub can have at most one carry bit. Thus we know that the output
2578 // is, at worst, one more bit than the inputs.
2579 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2580 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002581 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002582 case ISD::TRUNCATE:
2583 // FIXME: it's tricky to do anything useful for this, but it is an important
2584 // case for targets like X86.
2585 break;
Stephen Hinesebe69fe2015-03-23 12:10:34 -07002586 case ISD::EXTRACT_ELEMENT: {
2587 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2588 const int BitWidth = Op.getValueType().getSizeInBits();
2589 const int Items =
2590 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2591
2592 // Get reverse index (starting from 1), Op1 value indexes elements from
2593 // little end. Sign starts at big end.
2594 const int rIndex = Items - 1 -
2595 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2596
2597 // If the sign portion ends in our element the substraction gives correct
2598 // result. Otherwise it gives either negative or > bitwidth result
2599 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2600 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002602
Nadav Rotem77451752013-03-20 22:53:44 +00002603 // If we are looking at the loaded value of the SDNode.
2604 if (Op.getResNo() == 0) {
2605 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2606 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2607 unsigned ExtType = LD->getExtensionType();
2608 switch (ExtType) {
2609 default: break;
2610 case ISD::SEXTLOAD: // '17' bits known
2611 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2612 return VTBits-Tmp+1;
2613 case ISD::ZEXTLOAD: // '16' bits known
2614 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2615 return VTBits-Tmp;
2616 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002617 }
2618 }
2619
2620 // Allow the target to implement this method for its nodes.
2621 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002622 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002623 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2624 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002625 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002626 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002627 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002628
Dan Gohmanea859be2007-06-22 14:59:07 +00002629 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2630 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002631 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002632 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002633
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002634 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002635 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002636 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002637 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002638 Mask = KnownOne;
2639 } else {
2640 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002641 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002642 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002643
Dan Gohmanea859be2007-06-22 14:59:07 +00002644 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2645 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002646 Mask = ~Mask;
2647 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002648 // Return # leading zeros. We use 'min' here in case Val was zero before
2649 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002650 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002651}
2652
Chris Lattner0a9481f2011-02-13 22:25:43 +00002653/// isBaseWithConstantOffset - Return true if the specified operand is an
2654/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2655/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2656/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002657/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002658bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2659 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2660 !isa<ConstantSDNode>(Op.getOperand(1)))
2661 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002662
2663 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002664 !MaskedValueIsZero(Op.getOperand(0),
2665 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2666 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002667
Chris Lattner0a9481f2011-02-13 22:25:43 +00002668 return true;
2669}
2670
2671
Dan Gohman8d44b282009-09-03 20:34:31 +00002672bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2673 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002674 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002675 return true;
2676
2677 // If the value is a constant, we can obviously see if it is a NaN or not.
2678 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2679 return !C->getValueAPF().isNaN();
2680
2681 // TODO: Recognize more cases here.
2682
2683 return false;
2684}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002685
Dan Gohmane8326932010-02-24 06:52:40 +00002686bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2687 // If the value is a constant, we can obviously see if it is a zero or not.
2688 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2689 return !C->isZero();
2690
2691 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002692 switch (Op.getOpcode()) {
2693 default: break;
2694 case ISD::OR:
2695 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2696 return !C->isNullValue();
2697 break;
2698 }
Dan Gohmane8326932010-02-24 06:52:40 +00002699
2700 return false;
2701}
2702
2703bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2704 // Check the obvious case.
2705 if (A == B) return true;
2706
2707 // For for negative and positive zero.
2708 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2709 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2710 if (CA->isZero() && CB->isZero()) return true;
2711
2712 // Otherwise they may not be equal.
2713 return false;
2714}
2715
Chris Lattnerc3aae252005-01-07 07:46:32 +00002716/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002717///
Andrew Trickac6d9be2013-05-25 02:42:55 +00002718SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002719 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002720 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
2721 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002722 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002723 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002724
Jack Carter3af4d252013-09-09 22:02:08 +00002725 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2726 DL.getDebugLoc(), getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002727 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002728
Stephen Hines37ed9c12014-12-01 14:51:49 -08002729 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00002730 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002731}
2732
Andrew Trickac6d9be2013-05-25 02:42:55 +00002733SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002734 EVT VT, SDValue Operand) {
Stephen Hines36b56882014-04-23 16:57:46 -07002735 // Constant fold unary operations with an integer constant operand. Even
2736 // opaque constant will be folded, because the folding of unary operations
2737 // doesn't create new constants with different values. Nevertheless, the
2738 // opaque flag is preserved during folding to prevent future folding with
2739 // other constants.
Gabor Greifba36cb52008-08-28 21:40:38 +00002740 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002741 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002742 switch (Opcode) {
2743 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002744 case ISD::SIGN_EXTEND:
Stephen Hines36b56882014-04-23 16:57:46 -07002745 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2746 C->isTargetOpcode(), C->isOpaque());
Chris Lattner4ed11b42005-09-02 00:17:32 +00002747 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002748 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002749 case ISD::TRUNCATE:
Stephen Hines36b56882014-04-23 16:57:46 -07002750 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2751 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen73328d12007-09-19 23:55:34 +00002752 case ISD::UINT_TO_FP:
2753 case ISD::SINT_TO_FP: {
Tim Northover0a29cb02013-01-22 09:46:31 +00002754 APFloat apf(EVTToAPFloatSemantics(VT),
2755 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002756 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002757 Opcode==ISD::SINT_TO_FP,
2758 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002759 return getConstantFP(apf, VT);
2760 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002761 case ISD::BITCAST:
Stephen Hinesebe69fe2015-03-23 12:10:34 -07002762 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
2763 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002764 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover0a29cb02013-01-22 09:46:31 +00002765 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002766 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover0a29cb02013-01-22 09:46:31 +00002767 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002768 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002769 case ISD::BSWAP:
Stephen Hines36b56882014-04-23 16:57:46 -07002770 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2771 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002772 case ISD::CTPOP:
Stephen Hines36b56882014-04-23 16:57:46 -07002773 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2774 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002775 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002776 case ISD::CTLZ_ZERO_UNDEF:
Stephen Hines36b56882014-04-23 16:57:46 -07002777 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2778 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002779 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002780 case ISD::CTTZ_ZERO_UNDEF:
Stephen Hines36b56882014-04-23 16:57:46 -07002781 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2782 C->isOpaque());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002783 }
2784 }
2785
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002786 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002787 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002788 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002789 switch (Opcode) {
2790 case ISD::FNEG:
2791 V.changeSign();
2792 return getConstantFP(V, VT);
2793 case ISD::FABS:
2794 V.clearSign();
2795 return getConstantFP(V, VT);
2796 case ISD::FCEIL: {
2797 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2798 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002799 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002800 break;
2801 }
2802 case ISD::FTRUNC: {
2803 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2804 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002805 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002806 break;
2807 }
2808 case ISD::FFLOOR: {
2809 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2810 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002811 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002812 break;
2813 }
2814 case ISD::FP_EXTEND: {
2815 bool ignored;
2816 // This can return overflow, underflow, or inexact; we don't care.
2817 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00002818 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigande669c932012-10-29 18:35:49 +00002819 APFloat::rmNearestTiesToEven, &ignored);
2820 return getConstantFP(V, VT);
2821 }
2822 case ISD::FP_TO_SINT:
2823 case ISD::FP_TO_UINT: {
2824 integerPart x[2];
2825 bool ignored;
Stephen Hines37ed9c12014-12-01 14:51:49 -08002826 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigande669c932012-10-29 18:35:49 +00002827 // FIXME need to be more flexible about rounding mode.
2828 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2829 Opcode==ISD::FP_TO_SINT,
2830 APFloat::rmTowardZero, &ignored);
2831 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002832 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002833 APInt api(VT.getSizeInBits(), x);
2834 return getConstant(api, VT);
2835 }
2836 case ISD::BITCAST:
Stephen Hinesebe69fe2015-03-23 12:10:34 -07002837 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
2838 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), VT);
2839 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Ulrich Weigande669c932012-10-29 18:35:49 +00002840 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2841 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2842 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2843 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002844 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002845 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002846
Stephen Hines37ed9c12014-12-01 14:51:49 -08002847 // Constant fold unary operations with a vector integer operand.
2848 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
2849 if (BV->isConstant()) {
2850 switch (Opcode) {
2851 default:
2852 // FIXME: Entirely reasonable to perform folding of other unary
2853 // operations here as the need arises.
2854 break;
2855 case ISD::UINT_TO_FP:
2856 case ISD::SINT_TO_FP: {
2857 SmallVector<SDValue, 8> Ops;
2858 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2859 SDValue OpN = BV->getOperand(i);
2860 // Let the above scalar folding handle the conversion of each
2861 // element.
2862 OpN = getNode(ISD::SINT_TO_FP, DL, VT.getVectorElementType(),
2863 OpN);
2864 Ops.push_back(OpN);
2865 }
2866 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
2867 }
2868 }
2869 }
2870 }
2871
Gabor Greifba36cb52008-08-28 21:40:38 +00002872 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002873 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002874 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002875 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002876 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002877 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002878 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002879 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002880 assert(VT.isFloatingPoint() &&
2881 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002882 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002883 assert((!VT.isVector() ||
2884 VT.getVectorNumElements() ==
2885 Operand.getValueType().getVectorNumElements()) &&
2886 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002887 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002888 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002889 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002890 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002891 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002892 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002893 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002894 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2895 "Invalid sext node, dst < src!");
2896 assert((!VT.isVector() ||
2897 VT.getVectorNumElements() ==
2898 Operand.getValueType().getVectorNumElements()) &&
2899 "Vector element count mismatch!");
Nadav Rotem0c8607b2013-01-20 08:35:56 +00002900 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2901 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2902 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002903 // sext(undef) = 0, because the top bits will all be the same.
2904 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002905 break;
2906 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002907 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002908 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002909 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002910 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2911 "Invalid zext node, dst < src!");
2912 assert((!VT.isVector() ||
2913 VT.getVectorNumElements() ==
2914 Operand.getValueType().getVectorNumElements()) &&
2915 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002916 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002917 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002918 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002919 else if (OpOpcode == ISD::UNDEF)
2920 // zext(undef) = 0, because the top bits will be zero.
2921 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002922 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002923 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002924 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002925 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002926 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002927 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2928 "Invalid anyext node, dst < src!");
2929 assert((!VT.isVector() ||
2930 VT.getVectorNumElements() ==
2931 Operand.getValueType().getVectorNumElements()) &&
2932 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002933
Dan Gohman9e86a732010-06-18 00:08:30 +00002934 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2935 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002936 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002937 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002938 else if (OpOpcode == ISD::UNDEF)
2939 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002940
2941 // (ext (trunx x)) -> x
2942 if (OpOpcode == ISD::TRUNCATE) {
2943 SDValue OpOp = Operand.getNode()->getOperand(0);
2944 if (OpOp.getValueType() == VT)
2945 return OpOp;
2946 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002947 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002948 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002949 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002950 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002951 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002952 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2953 "Invalid truncate node, src < dst!");
2954 assert((!VT.isVector() ||
2955 VT.getVectorNumElements() ==
2956 Operand.getValueType().getVectorNumElements()) &&
2957 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002958 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002959 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002960 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2961 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002962 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002963 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2964 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002965 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002966 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002967 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002968 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002969 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002970 if (OpOpcode == ISD::UNDEF)
2971 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002972 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002973 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002974 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002975 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002976 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002977 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002978 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2979 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002980 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002981 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002982 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002983 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002984 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002985 (VT.getVectorElementType() == Operand.getValueType() ||
2986 (VT.getVectorElementType().isInteger() &&
2987 Operand.getValueType().isInteger() &&
2988 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002989 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002990 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002991 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002992 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2993 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2994 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2995 Operand.getConstantOperandVal(1) == 0 &&
2996 Operand.getOperand(0).getValueType() == VT)
2997 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002998 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002999 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00003000 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003001 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003002 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00003003 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00003004 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00003005 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00003006 break;
3007 case ISD::FABS:
3008 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003009 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00003010 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003011 }
3012
Chris Lattner43247a12005-08-25 19:12:10 +00003013 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003014 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003015 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00003016 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003017 SDValue Ops[1] = { Operand };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003018 AddNodeIDNode(ID, Opcode, VTs, Ops);
3019 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003020 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003021 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003022
Jack Carter3af4d252013-09-09 22:02:08 +00003023 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3024 DL.getDebugLoc(), VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00003025 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003026 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003027 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
3028 DL.getDebugLoc(), VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00003029 }
Duncan Sandsd038e042008-07-21 10:20:31 +00003030
Stephen Hines37ed9c12014-12-01 14:51:49 -08003031 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003032 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00003033}
3034
Benjamin Kramer49693102013-02-04 15:19:18 +00003035SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
3036 SDNode *Cst1, SDNode *Cst2) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07003037 // If the opcode is a target-specific ISD node, there's nothing we can
3038 // do here and the operand rules may not line up with the below, so
3039 // bail early.
3040 if (Opcode >= ISD::BUILTIN_OP_END)
3041 return SDValue();
3042
Benjamin Kramer49693102013-02-04 15:19:18 +00003043 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
3044 SmallVector<SDValue, 4> Outputs;
3045 EVT SVT = VT.getScalarType();
Bill Wendling688d1c42008-09-24 10:16:24 +00003046
Benjamin Kramer49693102013-02-04 15:19:18 +00003047 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
3048 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Stephen Hines36b56882014-04-23 16:57:46 -07003049 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
3050 return SDValue();
3051
3052 if (Scalar1 && Scalar2)
Benjamin Kramer49693102013-02-04 15:19:18 +00003053 // Scalar instruction.
3054 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Stephen Hines36b56882014-04-23 16:57:46 -07003055 else {
Benjamin Kramer49693102013-02-04 15:19:18 +00003056 // For vectors extract each constant element into Inputs so we can constant
3057 // fold them individually.
3058 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3059 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3060 if (!BV1 || !BV2)
3061 return SDValue();
3062
3063 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3064
3065 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3066 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3067 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3068 if (!V1 || !V2) // Not a constant, bail.
3069 return SDValue();
3070
Stephen Hines36b56882014-04-23 16:57:46 -07003071 if (V1->isOpaque() || V2->isOpaque())
3072 return SDValue();
3073
Benjamin Kramer49693102013-02-04 15:19:18 +00003074 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3075 // FIXME: This is valid and could be handled by truncating the APInts.
3076 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3077 return SDValue();
3078
3079 Inputs.push_back(std::make_pair(V1, V2));
3080 }
Bill Wendling688d1c42008-09-24 10:16:24 +00003081 }
3082
Benjamin Kramer49693102013-02-04 15:19:18 +00003083 // We have a number of constant values, constant fold them element by element.
3084 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3085 const APInt &C1 = Inputs[I].first->getAPIntValue();
3086 const APInt &C2 = Inputs[I].second->getAPIntValue();
3087
3088 switch (Opcode) {
3089 case ISD::ADD:
3090 Outputs.push_back(getConstant(C1 + C2, SVT));
3091 break;
3092 case ISD::SUB:
3093 Outputs.push_back(getConstant(C1 - C2, SVT));
3094 break;
3095 case ISD::MUL:
3096 Outputs.push_back(getConstant(C1 * C2, SVT));
3097 break;
3098 case ISD::UDIV:
3099 if (!C2.getBoolValue())
3100 return SDValue();
3101 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3102 break;
3103 case ISD::UREM:
3104 if (!C2.getBoolValue())
3105 return SDValue();
3106 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3107 break;
3108 case ISD::SDIV:
3109 if (!C2.getBoolValue())
3110 return SDValue();
3111 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3112 break;
3113 case ISD::SREM:
3114 if (!C2.getBoolValue())
3115 return SDValue();
3116 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3117 break;
3118 case ISD::AND:
3119 Outputs.push_back(getConstant(C1 & C2, SVT));
3120 break;
3121 case ISD::OR:
3122 Outputs.push_back(getConstant(C1 | C2, SVT));
3123 break;
3124 case ISD::XOR:
3125 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3126 break;
3127 case ISD::SHL:
3128 Outputs.push_back(getConstant(C1 << C2, SVT));
3129 break;
3130 case ISD::SRL:
3131 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3132 break;
3133 case ISD::SRA:
3134 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3135 break;
3136 case ISD::ROTL:
3137 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3138 break;
3139 case ISD::ROTR:
3140 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3141 break;
3142 default:
3143 return SDValue();
3144 }
3145 }
3146
Stephen Hinesdce4a402014-05-29 02:49:00 -07003147 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3148 "Expected a scalar or vector!"));
3149
Benjamin Kramer49693102013-02-04 15:19:18 +00003150 // Handle the scalar case first.
Stephen Hinesdce4a402014-05-29 02:49:00 -07003151 if (!VT.isVector())
Benjamin Kramer49693102013-02-04 15:19:18 +00003152 return Outputs.back();
3153
Stephen Hinesdce4a402014-05-29 02:49:00 -07003154 // We may have a vector type but a scalar result. Create a splat.
3155 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3156
3157 // Build a big vector out of the scalar elements we generated.
3158 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling688d1c42008-09-24 10:16:24 +00003159}
3160
Andrew Trickac6d9be2013-05-25 02:42:55 +00003161SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003162 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003163 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3164 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00003165 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003166 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00003167 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3169 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003170 // Fold trivial token factors.
3171 if (N1.getOpcode() == ISD::EntryToken) return N2;
3172 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00003173 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00003174 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003175 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00003176 // Concat of UNDEFs is UNDEF.
3177 if (N1.getOpcode() == ISD::UNDEF &&
3178 N2.getOpcode() == ISD::UNDEF)
3179 return getUNDEF(VT);
3180
Dan Gohman7f8613e2008-08-14 20:04:46 +00003181 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3182 // one big BUILD_VECTOR.
3183 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3184 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003185 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3186 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003187 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07003188 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman7f8613e2008-08-14 20:04:46 +00003189 }
3190 break;
Chris Lattner76365122005-01-16 02:23:22 +00003191 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00003192 assert(VT.isInteger() && "This operator does not apply to FP types!");
3193 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003194 N1.getValueType() == VT && "Binary operator types must match!");
3195 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3196 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00003197 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003198 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00003199 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3200 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003201 break;
Chris Lattner76365122005-01-16 02:23:22 +00003202 case ISD::OR:
3203 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00003204 case ISD::ADD:
3205 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00003206 assert(VT.isInteger() && "This operator does not apply to FP types!");
3207 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003208 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00003209 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3210 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00003211 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003212 return N1;
3213 break;
Chris Lattner76365122005-01-16 02:23:22 +00003214 case ISD::UDIV:
3215 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00003216 case ISD::MULHU:
3217 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00003218 case ISD::MUL:
3219 case ISD::SDIV:
3220 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00003221 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00003222 assert(N1.getValueType() == N2.getValueType() &&
3223 N1.getValueType() == VT && "Binary operator types must match!");
3224 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003225 case ISD::FADD:
3226 case ISD::FSUB:
3227 case ISD::FMUL:
3228 case ISD::FDIV:
3229 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003230 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00003231 if (Opcode == ISD::FADD) {
3232 // 0+x --> x
3233 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3234 if (CFP->getValueAPF().isZero())
3235 return N2;
3236 // x+0 --> x
3237 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3238 if (CFP->getValueAPF().isZero())
3239 return N1;
3240 } else if (Opcode == ISD::FSUB) {
3241 // x-0 --> x
3242 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3243 if (CFP->getValueAPF().isZero())
3244 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00003245 } else if (Opcode == ISD::FMUL) {
3246 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3247 SDValue V = N2;
3248
3249 // If the first operand isn't the constant, try the second
3250 if (!CFP) {
3251 CFP = dyn_cast<ConstantFPSDNode>(N2);
3252 V = N1;
3253 }
3254
3255 if (CFP) {
3256 // 0*x --> 0
3257 if (CFP->isZero())
3258 return SDValue(CFP,0);
3259 // 1*x --> x
3260 if (CFP->isExactlyValue(1.0))
3261 return V;
3262 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00003263 }
Dan Gohman760f86f2009-01-22 21:58:43 +00003264 }
Dale Johannesen78995512010-05-15 18:38:02 +00003265 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00003266 assert(N1.getValueType() == N2.getValueType() &&
3267 N1.getValueType() == VT && "Binary operator types must match!");
3268 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003269 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3270 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003271 N1.getValueType().isFloatingPoint() &&
3272 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00003273 "Invalid FCOPYSIGN!");
3274 break;
Chris Lattner76365122005-01-16 02:23:22 +00003275 case ISD::SHL:
3276 case ISD::SRA:
3277 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00003278 case ISD::ROTL:
3279 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00003280 assert(VT == N1.getValueType() &&
3281 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003282 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00003283 "Shifts only work on integers");
Michael Liaoa6b20ce2013-03-01 18:40:30 +00003284 assert((!VT.isVector() || VT == N2.getValueType()) &&
3285 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere0751182011-02-13 19:09:16 +00003286 // Verify that the shift amount VT is bit enough to hold valid shift
3287 // amounts. This catches things like trying to shift an i1024 value by an
3288 // i8, which is easy to fall into in generic code that uses
3289 // TLI.getShiftAmount().
3290 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00003291 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00003292 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00003293
3294 // Always fold shifts of i1 values so the code generator doesn't need to
3295 // handle them. Since we know the size of the shift has to be less than the
3296 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00003298 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00003299 if (N2C && N2C->isNullValue())
3300 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00003301 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00003302 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003303 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003304 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003305 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003306 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00003307 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003308 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003309 "type is vector!");
3310 assert((!EVT.isVector() ||
3311 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3312 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003313 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00003314 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003315 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00003316 break;
3317 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003318 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003319 assert(VT.isFloatingPoint() &&
3320 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003321 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00003322 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003323 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00003324 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00003325 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003326 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00003327 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003328 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003330 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00003331 assert(!EVT.isVector() &&
3332 "AssertSExt/AssertZExt type should be the vector element type "
3333 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003334 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00003335 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003336 break;
3337 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003338 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003339 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003340 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003341 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003342 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00003343 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003344 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003345 "type is vector!");
3346 assert((!EVT.isVector() ||
3347 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3348 "Vector element counts must match in SIGN_EXTEND_INREG");
3349 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003350 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00003351
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003352 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00003353 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003354 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00003355 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00003356 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00003357 return getConstant(Val, VT);
3358 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003359 break;
3360 }
3361 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00003362 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3363 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00003364 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003365
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003366 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3367 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00003368 if (N2C &&
3369 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003370 N1.getNumOperands() > 0) {
3371 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00003372 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003373 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003374 N1.getOperand(N2C->getZExtValue() / Factor),
3375 getConstant(N2C->getZExtValue() % Factor,
3376 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003377 }
3378
3379 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3380 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00003381 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3382 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00003383
3384 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00003385 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00003386 // are promoted and implicitly truncated, and the result implicitly
3387 // extended. Make that explicit here.
3388 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00003389
Bob Wilsonb1303d02009-04-13 22:05:19 +00003390 return Elt;
3391 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003392
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003393 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3394 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00003395 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00003396 // If the indices are the same, return the inserted element else
3397 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00003398 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00003399 SDValue N1Op2 = N1.getOperand(2);
3400 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3401
3402 if (N1Op2C && N2C) {
3403 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3404 if (VT == N1.getOperand(1).getValueType())
3405 return N1.getOperand(1);
3406 else
3407 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3408 }
3409
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003410 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00003411 }
Dan Gohman6ab64222008-08-13 21:51:37 +00003412 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003413 break;
3414 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003415 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00003416 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3417 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00003418 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00003419 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00003420
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003421 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3422 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00003423 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003424 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003425 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00003426
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003427 // EXTRACT_ELEMENT of a constant int is also very common.
3428 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003429 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003430 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00003431 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3432 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003433 }
3434 break;
David Greene91585092011-01-26 15:38:49 +00003435 case ISD::EXTRACT_SUBVECTOR: {
3436 SDValue Index = N2;
3437 if (VT.isSimple() && N1.getValueType().isSimple()) {
3438 assert(VT.isVector() && N1.getValueType().isVector() &&
3439 "Extract subvector VTs must be a vectors!");
Jack Carter3af4d252013-09-09 22:02:08 +00003440 assert(VT.getVectorElementType() ==
3441 N1.getValueType().getVectorElementType() &&
David Greene91585092011-01-26 15:38:49 +00003442 "Extract subvector VTs must have the same element type!");
Craig Topper0ff11902013-08-15 02:44:19 +00003443 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greene91585092011-01-26 15:38:49 +00003444 "Extract subvector must be from larger vector to smaller vector!");
3445
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003446 if (isa<ConstantSDNode>(Index.getNode())) {
3447 assert((VT.getVectorNumElements() +
3448 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003449 <= N1.getValueType().getVectorNumElements())
3450 && "Extract subvector overflow!");
3451 }
3452
3453 // Trivial extraction.
Craig Topper0ff11902013-08-15 02:44:19 +00003454 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greene91585092011-01-26 15:38:49 +00003455 return N1;
3456 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003457 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003458 }
David Greene91585092011-01-26 15:38:49 +00003459 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003460
Benjamin Kramer49693102013-02-04 15:19:18 +00003461 // Perform trivial constant folding.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07003462 if (SDValue SV =
3463 FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode()))
3464 return SV;
Benjamin Kramer49693102013-02-04 15:19:18 +00003465
3466 // Canonicalize constant to RHS if commutative.
3467 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3468 std::swap(N1C, N2C);
3469 std::swap(N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003470 }
3471
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003472 // Constant fold FP operations.
Stephen Hines37ed9c12014-12-01 14:51:49 -08003473 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greifba36cb52008-08-28 21:40:38 +00003474 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3475 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003476 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003477 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer49693102013-02-04 15:19:18 +00003478 // Canonicalize constant to RHS if commutative.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003479 std::swap(N1CFP, N2CFP);
3480 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003481 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003482 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3483 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003484 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003485 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003486 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003487 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003488 return getConstantFP(V1, VT);
3489 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003490 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003491 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003492 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003493 return getConstantFP(V1, VT);
3494 break;
3495 case ISD::FMUL:
3496 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003497 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003498 return getConstantFP(V1, VT);
3499 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003500 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003501 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003502 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3503 s!=APFloat::opDivByZero)) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003504 return getConstantFP(V1, VT);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003505 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003506 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003507 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003508 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003509 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3510 s!=APFloat::opDivByZero)) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003511 return getConstantFP(V1, VT);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003512 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003513 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003514 case ISD::FCOPYSIGN:
3515 V1.copySign(V2);
3516 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003517 default: break;
3518 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003519 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003520
3521 if (Opcode == ISD::FP_ROUND) {
3522 APFloat V = N1CFP->getValueAPF(); // make copy
3523 bool ignored;
3524 // This can return overflow, underflow, or inexact; we don't care.
3525 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00003526 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson06886aa2012-04-10 22:46:53 +00003527 APFloat::rmNearestTiesToEven, &ignored);
3528 return getConstantFP(V, VT);
3529 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003530 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003531
Chris Lattner62b57722006-04-20 05:39:12 +00003532 // Canonicalize an UNDEF to the RHS, even over a constant.
3533 if (N1.getOpcode() == ISD::UNDEF) {
3534 if (isCommutativeBinOp(Opcode)) {
3535 std::swap(N1, N2);
3536 } else {
3537 switch (Opcode) {
3538 case ISD::FP_ROUND_INREG:
3539 case ISD::SIGN_EXTEND_INREG:
3540 case ISD::SUB:
3541 case ISD::FSUB:
3542 case ISD::FDIV:
3543 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003544 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003545 return N1; // fold op(undef, arg2) -> undef
3546 case ISD::UDIV:
3547 case ISD::SDIV:
3548 case ISD::UREM:
3549 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003550 case ISD::SRL:
3551 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003552 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003553 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3554 // For vectors, we can't easily build an all zero vector, just return
3555 // the LHS.
3556 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003557 }
3558 }
3559 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003560
3561 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003562 if (N2.getOpcode() == ISD::UNDEF) {
3563 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003564 case ISD::XOR:
3565 if (N1.getOpcode() == ISD::UNDEF)
3566 // Handle undef ^ undef -> 0 special case. This is a common
3567 // idiom (misuse).
3568 return getConstant(0, VT);
3569 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003570 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003571 case ISD::ADDC:
3572 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003573 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003574 case ISD::UDIV:
3575 case ISD::SDIV:
3576 case ISD::UREM:
3577 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003578 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003579 case ISD::FADD:
3580 case ISD::FSUB:
3581 case ISD::FMUL:
3582 case ISD::FDIV:
3583 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003584 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003585 return N2;
3586 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003587 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003588 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003589 case ISD::SRL:
3590 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003591 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003592 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3593 // For vectors, we can't easily build an all zero vector, just return
3594 // the LHS.
3595 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003596 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003597 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003598 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003599 // For vectors, we can't easily build an all one vector, just return
3600 // the LHS.
3601 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003602 case ISD::SRA:
3603 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003604 }
3605 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003606
Chris Lattner27e9b412005-05-11 18:57:39 +00003607 // Memoize this node if possible.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003608 BinarySDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003609 SDVTList VTs = getVTList(VT);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003610 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003611 if (VT != MVT::Glue) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003612 SDValue Ops[] = {N1, N2};
Jim Laskey583bd472006-10-27 23:46:08 +00003613 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003614 AddNodeIDNode(ID, Opcode, VTs, Ops);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003615 if (BinOpHasFlags)
3616 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Stephen Hinesdce4a402014-05-29 02:49:00 -07003617 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003618 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003619 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003620
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003621 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3622
Chris Lattnera5682852006-08-07 23:03:03 +00003623 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003624 } else {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003625
3626 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner27e9b412005-05-11 18:57:39 +00003627 }
3628
Stephen Hines37ed9c12014-12-01 14:51:49 -08003629 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003630 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003631}
3632
Andrew Trickac6d9be2013-05-25 02:42:55 +00003633SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003634 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003635 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003636 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003637 switch (Opcode) {
Owen Anderson58dcd202013-05-09 22:27:13 +00003638 case ISD::FMA: {
3639 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3640 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3641 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3642 if (N1CFP && N2CFP && N3CFP) {
3643 APFloat V1 = N1CFP->getValueAPF();
3644 const APFloat &V2 = N2CFP->getValueAPF();
3645 const APFloat &V3 = N3CFP->getValueAPF();
3646 APFloat::opStatus s =
3647 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07003648 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
Owen Anderson58dcd202013-05-09 22:27:13 +00003649 return getConstantFP(V1, VT);
3650 }
3651 break;
3652 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00003653 case ISD::CONCAT_VECTORS:
3654 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3655 // one big BUILD_VECTOR.
3656 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3657 N2.getOpcode() == ISD::BUILD_VECTOR &&
3658 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003659 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3660 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003661 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3662 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07003663 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman7f8613e2008-08-14 20:04:46 +00003664 }
3665 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003666 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003667 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003668 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003669 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003670 break;
3671 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003672 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003673 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003674 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003675 return N2; // select true, X, Y -> X
3676 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003677 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003678
3679 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003680 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003681 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003682 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003683 case ISD::INSERT_SUBVECTOR: {
3684 SDValue Index = N3;
3685 if (VT.isSimple() && N1.getValueType().isSimple()
3686 && N2.getValueType().isSimple()) {
3687 assert(VT.isVector() && N1.getValueType().isVector() &&
3688 N2.getValueType().isVector() &&
3689 "Insert subvector VTs must be a vectors");
3690 assert(VT == N1.getValueType() &&
3691 "Dest and insert subvector source types must match!");
Craig Topper0ff11902013-08-15 02:44:19 +00003692 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenecfe33c42011-01-26 19:13:22 +00003693 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003694 if (isa<ConstantSDNode>(Index.getNode())) {
3695 assert((N2.getValueType().getVectorNumElements() +
3696 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003697 <= VT.getVectorNumElements())
3698 && "Insert subvector overflow!");
3699 }
3700
3701 // Trivial insertion.
Craig Topper0ff11902013-08-15 02:44:19 +00003702 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenecfe33c42011-01-26 19:13:22 +00003703 return N2;
3704 }
3705 break;
3706 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003707 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003708 // Fold bit_convert nodes from a type to themselves.
3709 if (N1.getValueType() == VT)
3710 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003711 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003712 }
3713
Chris Lattner43247a12005-08-25 19:12:10 +00003714 // Memoize node if it doesn't produce a flag.
3715 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003716 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003717 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003718 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003719 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003720 AddNodeIDNode(ID, Opcode, VTs, Ops);
3721 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003722 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003723 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003724
Jack Carter3af4d252013-09-09 22:02:08 +00003725 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3726 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003727 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003728 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003729 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3730 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003731 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003732
Stephen Hines37ed9c12014-12-01 14:51:49 -08003733 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003734 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003735}
3736
Andrew Trickac6d9be2013-05-25 02:42:55 +00003737SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003738 SDValue N1, SDValue N2, SDValue N3,
3739 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003740 SDValue Ops[] = { N1, N2, N3, N4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003741 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003742}
3743
Andrew Trickac6d9be2013-05-25 02:42:55 +00003744SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003745 SDValue N1, SDValue N2, SDValue N3,
3746 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003747 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003748 return getNode(Opcode, DL, VT, Ops);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003749}
3750
Dan Gohman98ca4f22009-08-05 01:29:28 +00003751/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3752/// the incoming stack arguments to be loaded from the stack.
3753SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3754 SmallVector<SDValue, 8> ArgChains;
3755
3756 // Include the original chain at the beginning of the list. When this is
3757 // used by target LowerCall hooks, this helps legalize find the
3758 // CALLSEQ_BEGIN node.
3759 ArgChains.push_back(Chain);
3760
3761 // Add a chain value for each stack argument.
3762 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3763 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3764 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3765 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3766 if (FI->getIndex() < 0)
3767 ArgChains.push_back(SDValue(L, 1));
3768
3769 // Build a tokenfactor for all the chains.
Stephen Hinesdce4a402014-05-29 02:49:00 -07003770 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003771}
3772
Dan Gohman707e0182008-04-12 04:36:06 +00003773/// getMemsetValue - Vectorized representation of the memset value
3774/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003775static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003776 SDLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003777 assert(Value.getOpcode() != ISD::UNDEF);
3778
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003779 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003780 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003781 assert(C->getAPIntValue().getBitWidth() == 8);
3782 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands83ec4b62008-06-06 12:08:01 +00003783 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003784 return DAG.getConstant(Val, VT);
Tim Northover0a29cb02013-01-22 09:46:31 +00003785 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003786 }
Evan Chengf0df0312008-05-15 08:39:06 +00003787
Dale Johannesen0f502f62009-02-03 22:26:09 +00003788 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003789 if (NumBits > 8) {
3790 // Use a multiplication with 0x010101... to extend the input to the
3791 // required length.
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003792 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003793 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003794 }
3795
3796 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003797}
3798
Dan Gohman707e0182008-04-12 04:36:06 +00003799/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3800/// used when a memcpy is turned into a memset when the source is a constant
3801/// string ptr.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003802static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003803 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003804 // Handle vector with all elements zero.
3805 if (Str.empty()) {
3806 if (VT.isInteger())
3807 return DAG.getConstant(0, VT);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003808 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng255f20f2010-04-01 06:04:33 +00003809 return DAG.getConstantFP(0.0, VT);
3810 else if (VT.isVector()) {
3811 unsigned NumElts = VT.getVectorNumElements();
3812 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003813 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003814 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3815 EltVT, NumElts)));
3816 } else
3817 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003818 }
3819
Duncan Sands83ec4b62008-06-06 12:08:01 +00003820 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Cheng4ff23d02013-01-10 22:13:27 +00003821 unsigned NumVTBits = VT.getSizeInBits();
3822 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattner18c7f802012-02-05 02:29:43 +00003823 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3824
Evan Cheng4ff23d02013-01-10 22:13:27 +00003825 APInt Val(NumVTBits, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003826 if (TLI.isLittleEndian()) {
3827 for (unsigned i = 0; i != NumBytes; ++i)
3828 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3829 } else {
3830 for (unsigned i = 0; i != NumBytes; ++i)
3831 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003832 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003833
Stephen Hines36b56882014-04-23 16:57:46 -07003834 // If the "cost" of materializing the integer immediate is less than the cost
3835 // of a load, then it is cost effective to turn the load into the immediate.
3836 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3837 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng376642e2012-12-10 23:21:26 +00003838 return DAG.getConstant(Val, VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07003839 return SDValue(nullptr, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003840}
3841
Scott Michelfdc40a02009-02-17 22:15:04 +00003842/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003843///
Andrew Trickdd0fb012013-05-25 03:08:10 +00003844static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman707e0182008-04-12 04:36:06 +00003845 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003846 EVT VT = Base.getValueType();
Andrew Trickdd0fb012013-05-25 03:08:10 +00003847 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003848 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003849}
3850
Evan Chengf0df0312008-05-15 08:39:06 +00003851/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3852///
Chris Lattner18c7f802012-02-05 02:29:43 +00003853static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003854 unsigned SrcDelta = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003855 GlobalAddressSDNode *G = nullptr;
Evan Chengf0df0312008-05-15 08:39:06 +00003856 if (Src.getOpcode() == ISD::GlobalAddress)
3857 G = cast<GlobalAddressSDNode>(Src);
3858 else if (Src.getOpcode() == ISD::ADD &&
3859 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3860 Src.getOperand(1).getOpcode() == ISD::Constant) {
3861 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003862 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003863 }
3864 if (!G)
3865 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003866
Chris Lattner18c7f802012-02-05 02:29:43 +00003867 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003868}
Dan Gohman707e0182008-04-12 04:36:06 +00003869
Evan Cheng255f20f2010-04-01 06:04:33 +00003870/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3871/// to replace the memset / memcpy. Return true if the number of memory ops
3872/// is below the threshold. It returns the types of the sequence of
3873/// memory ops to perform memset / memcpy by reference.
3874static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003875 unsigned Limit, uint64_t Size,
3876 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003877 bool IsMemset,
3878 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003879 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003880 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003881 SelectionDAG &DAG,
3882 const TargetLowering &TLI) {
3883 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3884 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003885 // If 'SrcAlign' is zero, that means the memory operation does not need to
3886 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3887 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3888 // is the specified alignment of the memory operation. If it is zero, that
3889 // means it's possible to change the alignment of the destination.
3890 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3891 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003892 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003893 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003894 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003895
Chris Lattneracee6472010-03-07 07:45:08 +00003896 if (VT == MVT::Other) {
Stephen Hines36b56882014-04-23 16:57:46 -07003897 unsigned AS = 0;
3898 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Stephen Hines37ed9c12014-12-01 14:51:49 -08003899 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003900 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003901 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003902 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003903 case 0: VT = MVT::i64; break;
3904 case 4: VT = MVT::i32; break;
3905 case 2: VT = MVT::i16; break;
3906 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003907 }
3908 }
3909
Owen Anderson766b5ef2009-08-11 21:59:30 +00003910 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003911 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003912 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003913 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003914
Duncan Sands8e4eb092008-06-08 20:54:56 +00003915 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003916 VT = LVT;
3917 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003918
Dan Gohman707e0182008-04-12 04:36:06 +00003919 unsigned NumMemOps = 0;
3920 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003921 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003922 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003923 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003924 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003925 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003926
3927 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003928 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003929 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003930 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003931 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003932 Found = true;
3933 else if (NewVT == MVT::i64 &&
3934 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003935 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003936 // i64 is usually not legal on 32-bit targets, but f64 may be.
3937 NewVT = MVT::f64;
3938 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003939 }
Evan Cheng376642e2012-12-10 23:21:26 +00003940 }
3941
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003942 if (!Found) {
3943 do {
3944 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3945 if (NewVT == MVT::i8)
3946 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003947 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003948 }
3949 NewVTSize = NewVT.getSizeInBits() / 8;
3950
Evan Cheng376642e2012-12-10 23:21:26 +00003951 // If the new VT cannot cover all of the remaining bits, then consider
3952 // issuing a (or a pair of) unaligned and overlapping load / store.
3953 // FIXME: Only does this for 64-bit or more since we don't have proper
3954 // cost model for unaligned load / store.
3955 bool Fast;
Stephen Hines36b56882014-04-23 16:57:46 -07003956 unsigned AS = 0;
Evan Chenga16e49d2012-12-12 20:43:23 +00003957 if (NumMemOps && AllowOverlap &&
3958 VTSize >= 8 && NewVTSize < Size &&
Stephen Hines37ed9c12014-12-01 14:51:49 -08003959 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng376642e2012-12-10 23:21:26 +00003960 VTSize = Size;
3961 else {
3962 VT = NewVT;
3963 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003964 }
Dan Gohman707e0182008-04-12 04:36:06 +00003965 }
Dan Gohman707e0182008-04-12 04:36:06 +00003966
Evan Chenga16e49d2012-12-12 20:43:23 +00003967 if (++NumMemOps > Limit)
3968 return false;
3969
Dan Gohman707e0182008-04-12 04:36:06 +00003970 MemOps.push_back(VT);
3971 Size -= VTSize;
3972 }
3973
3974 return true;
3975}
3976
Andrew Trickac6d9be2013-05-25 02:42:55 +00003977static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003978 SDValue Chain, SDValue Dst,
3979 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003980 unsigned Align, bool isVol,
3981 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003982 MachinePointerInfo DstPtrInfo,
3983 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003984 // Turn a memcpy of undef to nop.
3985 if (Src.getOpcode() == ISD::UNDEF)
3986 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003987
Dan Gohman21323f32008-05-29 19:42:22 +00003988 // Expand memcpy to a series of load and store ops if the size operand falls
3989 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003990 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3991 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003992 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003993 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003994 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003995 MachineFunction &MF = DAG.getMachineFunction();
3996 MachineFrameInfo *MFI = MF.getFrameInfo();
Stephen Hinesebe69fe2015-03-23 12:10:34 -07003997 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003998 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3999 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4000 DstAlignCanChange = true;
4001 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4002 if (Align > SrcAlign)
4003 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00004004 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00004005 bool CopyFromStr = isMemSrcFromString(Src, Str);
4006 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00004007 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004008
Evan Cheng94107ba2010-04-01 18:19:11 +00004009 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00004010 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00004011 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00004012 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00004013 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00004014
Evan Cheng255f20f2010-04-01 06:04:33 +00004015 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004016 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00004017 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hames2d95e432013-01-31 20:23:43 +00004018
4019 // Don't promote to an alignment that would require dynamic stack
Stephen Lin155615d2013-07-08 00:37:03 +00004020 // realignment.
Stephen Hines37ed9c12014-12-01 14:51:49 -08004021 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hames2d95e432013-01-31 20:23:43 +00004022 if (!TRI->needsStackRealignment(MF))
4023 while (NewAlign > Align &&
4024 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
4025 NewAlign /= 2;
4026
Evan Cheng255f20f2010-04-01 06:04:33 +00004027 if (NewAlign > Align) {
4028 // Give the stack frame object a larger alignment if needed.
4029 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4030 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4031 Align = NewAlign;
4032 }
4033 }
Dan Gohman707e0182008-04-12 04:36:06 +00004034
Dan Gohman475871a2008-07-27 21:46:04 +00004035 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00004036 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00004037 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00004038 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00004039 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00004040 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00004041 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00004042
Evan Cheng376642e2012-12-10 23:21:26 +00004043 if (VTSize > Size) {
4044 // Issuing an unaligned load / store pair that overlaps with the previous
4045 // pair. Adjust the offset accordingly.
4046 assert(i == NumMemOps-1 && i != 0);
4047 SrcOff -= VTSize - Size;
4048 DstOff -= VTSize - Size;
4049 }
4050
Evan Cheng255f20f2010-04-01 06:04:33 +00004051 if (CopyFromStr &&
4052 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00004053 // It's unlikely a store of a vector immediate can be done in a single
4054 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00004055 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00004056 // FIXME: Handle other cases where store of vector immediate is done in
4057 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00004058 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00004059 if (Value.getNode())
4060 Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004061 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng376642e2012-12-10 23:21:26 +00004062 DstPtrInfo.getWithOffset(DstOff), isVol,
4063 false, Align);
4064 }
4065
4066 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00004067 // The type might not be legal for the target. This should only happen
4068 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00004069 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4070 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00004071 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00004072 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00004073 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00004074 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004075 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004076 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004077 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00004078 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004079 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004080 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4081 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00004082 }
4083 OutChains.push_back(Store);
4084 SrcOff += VTSize;
4085 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00004086 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00004087 }
4088
Stephen Hinesdce4a402014-05-29 02:49:00 -07004089 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman707e0182008-04-12 04:36:06 +00004090}
4091
Andrew Trickac6d9be2013-05-25 02:42:55 +00004092static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00004093 SDValue Chain, SDValue Dst,
4094 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004095 unsigned Align, bool isVol,
4096 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004097 MachinePointerInfo DstPtrInfo,
4098 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00004099 // Turn a memmove of undef to nop.
4100 if (Src.getOpcode() == ISD::UNDEF)
4101 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00004102
4103 // Expand memmove to a series of load and store ops if the size operand falls
4104 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00004105 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004106 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00004107 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00004108 MachineFunction &MF = DAG.getMachineFunction();
4109 MachineFrameInfo *MFI = MF.getFrameInfo();
Stephen Hinesebe69fe2015-03-23 12:10:34 -07004110 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004111 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4112 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4113 DstAlignCanChange = true;
4114 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4115 if (Align > SrcAlign)
4116 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00004117 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004118
Evan Cheng94107ba2010-04-01 18:19:11 +00004119 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00004120 (DstAlignCanChange ? 0 : Align), SrcAlign,
4121 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00004122 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00004123
Evan Cheng255f20f2010-04-01 06:04:33 +00004124 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004125 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00004126 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00004127 if (NewAlign > Align) {
4128 // Give the stack frame object a larger alignment if needed.
4129 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4130 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4131 Align = NewAlign;
4132 }
4133 }
Dan Gohman21323f32008-05-29 19:42:22 +00004134
Evan Cheng255f20f2010-04-01 06:04:33 +00004135 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004136 SmallVector<SDValue, 8> LoadValues;
4137 SmallVector<SDValue, 8> LoadChains;
4138 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00004139 unsigned NumMemOps = MemOps.size();
4140 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004141 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00004142 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00004143 SDValue Value;
Dan Gohman21323f32008-05-29 19:42:22 +00004144
Dale Johannesen0f502f62009-02-03 22:26:09 +00004145 Value = DAG.getLoad(VT, dl, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004146 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004147 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004148 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00004149 LoadValues.push_back(Value);
4150 LoadChains.push_back(Value.getValue(1));
4151 SrcOff += VTSize;
4152 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07004153 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman21323f32008-05-29 19:42:22 +00004154 OutChains.clear();
4155 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004156 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00004157 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00004158 SDValue Store;
Dan Gohman21323f32008-05-29 19:42:22 +00004159
Dale Johannesen0f502f62009-02-03 22:26:09 +00004160 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Trickdd0fb012013-05-25 03:08:10 +00004161 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004162 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00004163 OutChains.push_back(Store);
4164 DstOff += VTSize;
4165 }
4166
Stephen Hinesdce4a402014-05-29 02:49:00 -07004167 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman21323f32008-05-29 19:42:22 +00004168}
4169
Serge Pavlov496f0242013-09-17 16:24:42 +00004170/// \brief Lower the call to 'memset' intrinsic function into a series of store
4171/// operations.
4172///
4173/// \param DAG Selection DAG where lowered code is placed.
4174/// \param dl Link to corresponding IR location.
4175/// \param Chain Control flow dependency.
4176/// \param Dst Pointer to destination memory location.
4177/// \param Src Value of byte to write into the memory.
4178/// \param Size Number of bytes to write.
4179/// \param Align Alignment of the destination in bytes.
4180/// \param isVol True if destination is volatile.
4181/// \param DstPtrInfo IR information on the memory pointer.
4182/// \returns New head in the control flow, if lowering was successful, empty
4183/// SDValue otherwise.
4184///
4185/// The function tries to replace 'llvm.memset' intrinsic with several store
4186/// operations and value calculation code. This is usually profitable for small
4187/// memory size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004188static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00004189 SDValue Chain, SDValue Dst,
4190 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004191 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004192 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00004193 // Turn a memset of undef to nop.
4194 if (Src.getOpcode() == ISD::UNDEF)
4195 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00004196
4197 // Expand memset to a series of load/store ops if the size operand
4198 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00004199 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004200 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00004201 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00004202 MachineFunction &MF = DAG.getMachineFunction();
4203 MachineFrameInfo *MFI = MF.getFrameInfo();
Stephen Hinesebe69fe2015-03-23 12:10:34 -07004204 bool OptSize = MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004205 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4206 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4207 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00004208 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00004209 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00004210 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00004211 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00004212 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00004213 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00004214
Evan Cheng255f20f2010-04-01 06:04:33 +00004215 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004216 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00004217 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00004218 if (NewAlign > Align) {
4219 // Give the stack frame object a larger alignment if needed.
4220 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4221 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4222 Align = NewAlign;
4223 }
4224 }
4225
Dan Gohman475871a2008-07-27 21:46:04 +00004226 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00004227 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00004228 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00004229
4230 // Find the largest store and generate the bit pattern for it.
4231 EVT LargestVT = MemOps[0];
4232 for (unsigned i = 1; i < NumMemOps; i++)
4233 if (MemOps[i].bitsGT(LargestVT))
4234 LargestVT = MemOps[i];
4235 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4236
Dan Gohman707e0182008-04-12 04:36:06 +00004237 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004238 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00004239 unsigned VTSize = VT.getSizeInBits() / 8;
4240 if (VTSize > Size) {
4241 // Issuing an unaligned load / store pair that overlaps with the previous
4242 // pair. Adjust the offset accordingly.
4243 assert(i == NumMemOps-1 && i != 0);
4244 DstOff -= VTSize - Size;
4245 }
Benjamin Kramer80220362011-01-02 19:57:05 +00004246
4247 // If this store is smaller than the largest store see whether we can get
4248 // the smaller value for free with a truncate.
4249 SDValue Value = MemSetValue;
4250 if (VT.bitsLT(LargestVT)) {
4251 if (!LargestVT.isVector() && !VT.isVector() &&
4252 TLI.isTruncateFree(LargestVT, VT))
4253 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4254 else
4255 Value = getMemsetValue(Src, VT, DAG, dl);
4256 }
4257 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004258 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004259 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004260 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00004261 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00004262 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00004263 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00004264 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00004265 }
4266
Stephen Hinesdce4a402014-05-29 02:49:00 -07004267 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman707e0182008-04-12 04:36:06 +00004268}
4269
Andrew Trickac6d9be2013-05-25 02:42:55 +00004270SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004271 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004272 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004273 MachinePointerInfo DstPtrInfo,
4274 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004275 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004276
4277 // Check to see if we should lower the memcpy to loads and stores first.
4278 // For cases within the target-specified limits, this is the best choice.
4279 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4280 if (ConstantSize) {
4281 // Memcpy with size zero? Just return the original chain.
4282 if (ConstantSize->isNullValue())
4283 return Chain;
4284
Evan Cheng255f20f2010-04-01 06:04:33 +00004285 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4286 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00004287 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004288 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004289 return Result;
4290 }
4291
4292 // Then check to see if we should lower the memcpy with target-specific
4293 // code. If the target chooses to do this, this is the next best.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07004294 if (TSI) {
4295 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4296 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4297 DstPtrInfo, SrcPtrInfo);
4298 if (Result.getNode())
4299 return Result;
4300 }
Dan Gohman707e0182008-04-12 04:36:06 +00004301
4302 // If we really need inline code and the target declined to provide it,
4303 // use a (potentially long) sequence of loads and stores.
4304 if (AlwaysInline) {
4305 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004306 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004307 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004308 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00004309 }
4310
Dan Gohman7b55d362010-04-05 20:24:08 +00004311 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4312 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4313 // respect volatile, so they may do things like read or write memory
4314 // beyond the given memory regions. But fixing this isn't easy, and most
4315 // people don't care.
4316
Dan Gohman707e0182008-04-12 04:36:06 +00004317 // Emit a library call.
4318 TargetLowering::ArgListTy Args;
4319 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004320 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004321 Entry.Node = Dst; Args.push_back(Entry);
4322 Entry.Node = Src; Args.push_back(Entry);
4323 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004324 // FIXME: pass in SDLoc
Stephen Hinesdce4a402014-05-29 02:49:00 -07004325 TargetLowering::CallLoweringInfo CLI(*this);
4326 CLI.setDebugLoc(dl).setChain(Chain)
4327 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4328 Type::getVoidTy(*getContext()),
4329 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004330 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004331 .setDiscardResult();
Bill Wendlingba54bca2013-06-19 21:36:55 +00004332 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004333
Dan Gohman707e0182008-04-12 04:36:06 +00004334 return CallResult.second;
4335}
4336
Andrew Trickac6d9be2013-05-25 02:42:55 +00004337SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004338 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004339 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004340 MachinePointerInfo DstPtrInfo,
4341 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004342 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004343
Dan Gohman21323f32008-05-29 19:42:22 +00004344 // Check to see if we should lower the memmove to loads and stores first.
4345 // For cases within the target-specified limits, this is the best choice.
4346 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4347 if (ConstantSize) {
4348 // Memmove with size zero? Just return the original chain.
4349 if (ConstantSize->isNullValue())
4350 return Chain;
4351
Dan Gohman475871a2008-07-27 21:46:04 +00004352 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00004353 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004354 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004355 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004356 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00004357 return Result;
4358 }
Dan Gohman707e0182008-04-12 04:36:06 +00004359
4360 // Then check to see if we should lower the memmove with target-specific
4361 // code. If the target chooses to do this, this is the next best.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07004362 if (TSI) {
4363 SDValue Result = TSI->EmitTargetCodeForMemmove(
4364 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4365 if (Result.getNode())
4366 return Result;
4367 }
Dan Gohman707e0182008-04-12 04:36:06 +00004368
Mon P Wang01f0e852010-04-06 08:27:51 +00004369 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4370 // not be safe. See memcpy above for more details.
4371
Dan Gohman707e0182008-04-12 04:36:06 +00004372 // Emit a library call.
4373 TargetLowering::ArgListTy Args;
4374 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004375 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004376 Entry.Node = Dst; Args.push_back(Entry);
4377 Entry.Node = Src; Args.push_back(Entry);
4378 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004379 // FIXME: pass in SDLoc
Stephen Hinesdce4a402014-05-29 02:49:00 -07004380 TargetLowering::CallLoweringInfo CLI(*this);
4381 CLI.setDebugLoc(dl).setChain(Chain)
4382 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4383 Type::getVoidTy(*getContext()),
4384 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004385 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004386 .setDiscardResult();
Bill Wendlingba54bca2013-06-19 21:36:55 +00004387 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004388
Dan Gohman707e0182008-04-12 04:36:06 +00004389 return CallResult.second;
4390}
4391
Andrew Trickac6d9be2013-05-25 02:42:55 +00004392SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004393 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004394 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004395 MachinePointerInfo DstPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004396 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004397
4398 // Check to see if we should lower the memset to stores first.
4399 // For cases within the target-specified limits, this is the best choice.
4400 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4401 if (ConstantSize) {
4402 // Memset with size zero? Just return the original chain.
4403 if (ConstantSize->isNullValue())
4404 return Chain;
4405
Mon P Wang20adc9d2010-04-04 03:10:48 +00004406 SDValue Result =
4407 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00004408 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00004409
Gabor Greifba36cb52008-08-28 21:40:38 +00004410 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004411 return Result;
4412 }
4413
4414 // Then check to see if we should lower the memset with target-specific
4415 // code. If the target chooses to do this, this is the next best.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07004416 if (TSI) {
4417 SDValue Result = TSI->EmitTargetCodeForMemset(
4418 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4419 if (Result.getNode())
4420 return Result;
4421 }
Dan Gohman707e0182008-04-12 04:36:06 +00004422
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004423 // Emit a library call.
Bill Wendlingba54bca2013-06-19 21:36:55 +00004424 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004425 TargetLowering::ArgListTy Args;
4426 TargetLowering::ArgListEntry Entry;
4427 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4428 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004429 Entry.Node = Src;
Stephen Hines37ed9c12014-12-01 14:51:49 -08004430 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004431 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004432 Entry.Node = Size;
4433 Entry.Ty = IntPtrTy;
Dan Gohman707e0182008-04-12 04:36:06 +00004434 Args.push_back(Entry);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004435
Stephen Hinesdce4a402014-05-29 02:49:00 -07004436 // FIXME: pass in SDLoc
4437 TargetLowering::CallLoweringInfo CLI(*this);
4438 CLI.setDebugLoc(dl).setChain(Chain)
4439 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4440 Type::getVoidTy(*getContext()),
4441 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004442 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004443 .setDiscardResult();
4444
4445 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman707e0182008-04-12 04:36:06 +00004446 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004447}
4448
Andrew Trickac6d9be2013-05-25 02:42:55 +00004449SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004450 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emerson268c7432013-09-26 12:22:36 +00004451 MachineMemOperand *MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004452 AtomicOrdering SuccessOrdering,
4453 AtomicOrdering FailureOrdering,
Amara Emerson268c7432013-09-26 12:22:36 +00004454 SynchronizationScope SynchScope) {
4455 FoldingSetNodeID ID;
4456 ID.AddInteger(MemVT.getRawBits());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004457 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emerson268c7432013-09-26 12:22:36 +00004458 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004459 void* IP = nullptr;
Amara Emerson268c7432013-09-26 12:22:36 +00004460 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4461 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4462 return SDValue(E, 0);
4463 }
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004464
4465 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4466 // SDNode doesn't have access to it. This memory will be "leaked" when
4467 // the node is deallocated, but recovered when the allocator is released.
4468 // If the number of operands is less than 5 we use AtomicSDNode's internal
4469 // storage.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004470 unsigned NumOps = Ops.size();
4471 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4472 : nullptr;
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004473
Amara Emerson268c7432013-09-26 12:22:36 +00004474 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4475 dl.getDebugLoc(), VTList, MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004476 Ops.data(), DynOps, NumOps, MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004477 SuccessOrdering, FailureOrdering,
4478 SynchScope);
Amara Emerson268c7432013-09-26 12:22:36 +00004479 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004480 InsertNode(N);
Amara Emerson268c7432013-09-26 12:22:36 +00004481 return SDValue(N, 0);
4482}
4483
4484SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004485 SDVTList VTList, ArrayRef<SDValue> Ops,
Stephen Hines36b56882014-04-23 16:57:46 -07004486 MachineMemOperand *MMO,
4487 AtomicOrdering Ordering,
4488 SynchronizationScope SynchScope) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07004489 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Stephen Hines36b56882014-04-23 16:57:46 -07004490 Ordering, SynchScope);
4491}
4492
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004493SDValue SelectionDAG::getAtomicCmpSwap(
4494 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4495 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4496 unsigned Alignment, AtomicOrdering SuccessOrdering,
4497 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4498 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4499 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4500 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4501
Dan Gohmanc76909a2009-09-25 20:36:54 +00004502 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4503 Alignment = getEVTAlignment(MemVT);
4504
Evan Chengff89dcb2009-10-18 18:16:27 +00004505 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004506
Eli Friedman981a0102011-09-07 02:23:42 +00004507 // FIXME: Volatile isn't really correct; we should keep track of atomic
4508 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004509 unsigned Flags = MachineMemOperand::MOVolatile;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004510 Flags |= MachineMemOperand::MOLoad;
4511 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004512
4513 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004514 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004515
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004516 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4517 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004518}
4519
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004520SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4521 SDVTList VTs, SDValue Chain, SDValue Ptr,
4522 SDValue Cmp, SDValue Swp,
4523 MachineMemOperand *MMO,
4524 AtomicOrdering SuccessOrdering,
4525 AtomicOrdering FailureOrdering,
4526 SynchronizationScope SynchScope) {
4527 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4528 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesene8c17332009-01-29 00:47:48 +00004529 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4530
Dale Johannesene8c17332009-01-29 00:47:48 +00004531 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004532 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4533 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004534}
4535
Andrew Trickac6d9be2013-05-25 02:42:55 +00004536SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004537 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004538 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004539 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004540 unsigned Alignment,
4541 AtomicOrdering Ordering,
4542 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004543 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4544 Alignment = getEVTAlignment(MemVT);
4545
Evan Chengff89dcb2009-10-18 18:16:27 +00004546 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004547 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004548 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004549 // For now, atomics are considered to be volatile always, and they are
4550 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004551 // FIXME: Volatile isn't really correct; we should keep track of atomic
4552 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004553 unsigned Flags = MachineMemOperand::MOVolatile;
4554 if (Opcode != ISD::ATOMIC_STORE)
4555 Flags |= MachineMemOperand::MOLoad;
4556 if (Opcode != ISD::ATOMIC_LOAD)
4557 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004558
4559 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004560 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004561 MemVT.getStoreSize(), Alignment);
4562
Eli Friedman55ba8162011-07-29 03:05:32 +00004563 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4564 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004565}
4566
Andrew Trickac6d9be2013-05-25 02:42:55 +00004567SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004568 SDValue Chain,
4569 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004570 MachineMemOperand *MMO,
4571 AtomicOrdering Ordering,
4572 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004573 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4574 Opcode == ISD::ATOMIC_LOAD_SUB ||
4575 Opcode == ISD::ATOMIC_LOAD_AND ||
4576 Opcode == ISD::ATOMIC_LOAD_OR ||
4577 Opcode == ISD::ATOMIC_LOAD_XOR ||
4578 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004579 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004580 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004581 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004582 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004583 Opcode == ISD::ATOMIC_SWAP ||
4584 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004585 "Invalid Atomic Op");
4586
Owen Andersone50ed302009-08-10 22:56:29 +00004587 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004588
Eli Friedman327236c2011-08-24 20:50:09 +00004589 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4590 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004591 SDValue Ops[] = {Chain, Ptr, Val};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004592 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004593}
4594
Andrew Trickac6d9be2013-05-25 02:42:55 +00004595SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004596 EVT VT, SDValue Chain,
4597 SDValue Ptr,
4598 MachineMemOperand *MMO,
4599 AtomicOrdering Ordering,
4600 SynchronizationScope SynchScope) {
4601 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4602
4603 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman327236c2011-08-24 20:50:09 +00004604 SDValue Ops[] = {Chain, Ptr};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004605 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004606}
4607
Duncan Sands4bdcb612008-07-02 17:40:58 +00004608/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004609SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4610 if (Ops.size() == 1)
Dale Johannesen54c94522009-02-02 20:47:48 +00004611 return Ops[0];
4612
Owen Andersone50ed302009-08-10 22:56:29 +00004613 SmallVector<EVT, 4> VTs;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004614 VTs.reserve(Ops.size());
4615 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesen54c94522009-02-02 20:47:48 +00004616 VTs.push_back(Ops[i].getValueType());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004617 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004618}
4619
4620SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004621SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004622 ArrayRef<SDValue> Ops,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004623 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004624 unsigned Align, bool Vol,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004625 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004626 if (Align == 0) // Ensure that codegen never sees alignment 0
4627 Align = getEVTAlignment(MemVT);
4628
4629 MachineFunction &MF = getMachineFunction();
4630 unsigned Flags = 0;
4631 if (WriteMem)
4632 Flags |= MachineMemOperand::MOStore;
4633 if (ReadMem)
4634 Flags |= MachineMemOperand::MOLoad;
4635 if (Vol)
4636 Flags |= MachineMemOperand::MOVolatile;
Stephen Hines37ed9c12014-12-01 14:51:49 -08004637 if (!Size)
4638 Size = MemVT.getStoreSize();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004639 MachineMemOperand *MMO =
Stephen Hines37ed9c12014-12-01 14:51:49 -08004640 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004641
Stephen Hinesdce4a402014-05-29 02:49:00 -07004642 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004643}
4644
4645SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004646SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004647 ArrayRef<SDValue> Ops, EVT MemVT,
4648 MachineMemOperand *MMO) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004649 assert((Opcode == ISD::INTRINSIC_VOID ||
4650 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004651 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004652 Opcode == ISD::LIFETIME_START ||
4653 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004654 (Opcode <= INT_MAX &&
4655 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4656 "Opcode is not a memory-accessing opcode!");
4657
Dale Johannesene8c17332009-01-29 00:47:48 +00004658 // Memoize the node unless it returns a flag.
4659 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004660 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004661 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004662 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004663 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004664 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004665 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4666 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004667 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004668 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004669
Jack Carter3af4d252013-09-09 22:02:08 +00004670 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4671 dl.getDebugLoc(), VTList, Ops,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004672 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004673 CSEMap.InsertNode(N, IP);
4674 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004675 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4676 dl.getDebugLoc(), VTList, Ops,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004677 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004678 }
Stephen Hines37ed9c12014-12-01 14:51:49 -08004679 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004680 return SDValue(N, 0);
4681}
4682
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004683/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4684/// MachinePointerInfo record from it. This is particularly useful because the
4685/// code generator has many cases where it doesn't bother passing in a
4686/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4687static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4688 // If this is FI+Offset, we can model it.
4689 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4690 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4691
4692 // If this is (FI+Offset1)+Offset2, we can model it.
4693 if (Ptr.getOpcode() != ISD::ADD ||
4694 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4695 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4696 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004697
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004698 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4699 return MachinePointerInfo::getFixedStack(FI, Offset+
4700 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4701}
4702
4703/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4704/// MachinePointerInfo record from it. This is particularly useful because the
4705/// code generator has many cases where it doesn't bother passing in a
4706/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4707static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4708 // If the 'Offset' value isn't a constant, we can't handle this.
4709 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4710 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4711 if (OffsetOp.getOpcode() == ISD::UNDEF)
4712 return InferPointerInfo(Ptr);
4713 return MachinePointerInfo();
4714}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004715
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004716
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004717SDValue
4718SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004719 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004720 SDValue Ptr, SDValue Offset,
4721 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004722 bool isVolatile, bool isNonTemporal, bool isInvariant,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004723 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004724 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004725 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004726 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004727 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4728 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004729
Dan Gohmanc76909a2009-09-25 20:36:54 +00004730 unsigned Flags = MachineMemOperand::MOLoad;
4731 if (isVolatile)
4732 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004733 if (isNonTemporal)
4734 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004735 if (isInvariant)
4736 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004737
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004738 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4739 // clients.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004740 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004741 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004742
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004743 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004744 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004745 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004746 AAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004747 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004748}
4749
4750SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004751SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004752 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004753 SDValue Ptr, SDValue Offset, EVT MemVT,
4754 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004755 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004756 ExtType = ISD::NON_EXTLOAD;
4757 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004758 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004759 } else {
4760 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004761 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4762 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004763 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004764 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004765 assert(VT.isVector() == MemVT.isVector() &&
Stephen Hinesebe69fe2015-03-23 12:10:34 -07004766 "Cannot use an ext load to convert to or from a vector!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004767 assert((!VT.isVector() ||
4768 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
Stephen Hinesebe69fe2015-03-23 12:10:34 -07004769 "Cannot use an ext load to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004770 }
4771
4772 bool Indexed = AM != ISD::UNINDEXED;
4773 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4774 "Unindexed load with an offset!");
4775
4776 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004777 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004778 SDValue Ops[] = { Chain, Ptr, Offset };
4779 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004780 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004781 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004782 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004783 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004784 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004785 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004786 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4788 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004789 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004790 }
Jack Carter3af4d252013-09-09 22:02:08 +00004791 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4792 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman95531882010-03-18 18:49:47 +00004793 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004794 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004795 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004796 return SDValue(N, 0);
4797}
4798
Andrew Trickac6d9be2013-05-25 02:42:55 +00004799SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004800 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004801 MachinePointerInfo PtrInfo,
4802 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004803 bool isInvariant, unsigned Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004804 const AAMDNodes &AAInfo,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004805 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004806 SDValue Undef = getUNDEF(Ptr.getValueType());
4807 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004808 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004809 AAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004810}
Dale Johannesene8c17332009-01-29 00:47:48 +00004811
Richard Sandiford66589dc2013-10-28 11:17:59 +00004812SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4813 SDValue Chain, SDValue Ptr,
4814 MachineMemOperand *MMO) {
4815 SDValue Undef = getUNDEF(Ptr.getValueType());
4816 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4817 VT, MMO);
4818}
4819
Andrew Trickac6d9be2013-05-25 02:42:55 +00004820SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004821 SDValue Chain, SDValue Ptr,
4822 MachinePointerInfo PtrInfo, EVT MemVT,
4823 bool isVolatile, bool isNonTemporal,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004824 bool isInvariant, unsigned Alignment,
4825 const AAMDNodes &AAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004826 SDValue Undef = getUNDEF(Ptr.getValueType());
4827 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004828 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4829 Alignment, AAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004830}
4831
4832
Richard Sandiford66589dc2013-10-28 11:17:59 +00004833SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4834 SDValue Chain, SDValue Ptr, EVT MemVT,
4835 MachineMemOperand *MMO) {
4836 SDValue Undef = getUNDEF(Ptr.getValueType());
4837 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4838 MemVT, MMO);
4839}
4840
Dan Gohman475871a2008-07-27 21:46:04 +00004841SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004842SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004843 SDValue Offset, ISD::MemIndexedMode AM) {
4844 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4845 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4846 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004847 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004848 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004849 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004850 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004851}
4852
Andrew Trickac6d9be2013-05-25 02:42:55 +00004853SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004854 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004855 bool isVolatile, bool isNonTemporal,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004856 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004857 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004858 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004859 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004860 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004861
Dan Gohmanc76909a2009-09-25 20:36:54 +00004862 unsigned Flags = MachineMemOperand::MOStore;
4863 if (isVolatile)
4864 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004865 if (isNonTemporal)
4866 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004867
Stephen Hinesdce4a402014-05-29 02:49:00 -07004868 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004869 PtrInfo = InferPointerInfo(Ptr);
4870
4871 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004872 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004873 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004874 Val.getValueType().getStoreSize(), Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004875 AAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004876
4877 return getStore(Chain, dl, Val, Ptr, MMO);
4878}
4879
Andrew Trickac6d9be2013-05-25 02:42:55 +00004880SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004881 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004882 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004883 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004884 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004885 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004886 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004887 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4888 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004889 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004890 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004891 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004892 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004893 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004894 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004895 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4896 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004897 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004898 }
Jack Carter3af4d252013-09-09 22:02:08 +00004899 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4900 dl.getDebugLoc(), VTs,
4901 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004902 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004903 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004904 return SDValue(N, 0);
4905}
4906
Andrew Trickac6d9be2013-05-25 02:42:55 +00004907SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004908 SDValue Ptr, MachinePointerInfo PtrInfo,
4909 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004910 unsigned Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004911 const AAMDNodes &AAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004912 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004913 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004914 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4915 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004916
Dan Gohmanc76909a2009-09-25 20:36:54 +00004917 unsigned Flags = MachineMemOperand::MOStore;
4918 if (isVolatile)
4919 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004920 if (isNonTemporal)
4921 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004922
Stephen Hinesdce4a402014-05-29 02:49:00 -07004923 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004924 PtrInfo = InferPointerInfo(Ptr);
4925
4926 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004927 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004928 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004929 AAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004930
4931 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4932}
4933
Andrew Trickac6d9be2013-05-25 02:42:55 +00004934SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004935 SDValue Ptr, EVT SVT,
4936 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004937 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004938
Michael Ilseman06b96902012-09-10 16:56:31 +00004939 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004940 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004941 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004942 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004943
Dan Gohman2e141d72009-12-14 23:40:38 +00004944 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4945 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004946 assert(VT.isInteger() == SVT.isInteger() &&
4947 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004948 assert(VT.isVector() == SVT.isVector() &&
4949 "Cannot use trunc store to convert to or from a vector!");
4950 assert((!VT.isVector() ||
4951 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4952 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004953
Owen Anderson825b72b2009-08-11 20:47:22 +00004954 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004955 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004956 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4957 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004958 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004959 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004960 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004961 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004962 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004963 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004964 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4965 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004966 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004967 }
Jack Carter3af4d252013-09-09 22:02:08 +00004968 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4969 dl.getDebugLoc(), VTs,
4970 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004971 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004972 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004973 return SDValue(N, 0);
4974}
4975
Dan Gohman475871a2008-07-27 21:46:04 +00004976SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004977SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004978 SDValue Offset, ISD::MemIndexedMode AM) {
4979 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4980 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4981 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004982 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004983 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4984 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004985 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004986 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004987 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004988 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004989 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004990 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004991 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004992
Jack Carter3af4d252013-09-09 22:02:08 +00004993 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4994 dl.getDebugLoc(), VTs, AM,
Dan Gohman95531882010-03-18 18:49:47 +00004995 ST->isTruncatingStore(),
4996 ST->getMemoryVT(),
4997 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004998 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004999 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00005000 return SDValue(N, 0);
5001}
5002
Stephen Hinesebe69fe2015-03-23 12:10:34 -07005003SDValue
5004SelectionDAG::getMaskedLoad(EVT VT, SDLoc dl, SDValue Chain,
5005 SDValue Ptr, SDValue Mask, SDValue Src0, EVT MemVT,
5006 MachineMemOperand *MMO, ISD::LoadExtType ExtTy) {
5007
5008 SDVTList VTs = getVTList(VT, MVT::Other);
5009 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5010 FoldingSetNodeID ID;
5011 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5012 ID.AddInteger(VT.getRawBits());
5013 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
5014 MMO->isVolatile(),
5015 MMO->isNonTemporal(),
5016 MMO->isInvariant()));
5017 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5018 void *IP = nullptr;
5019 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5020 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5021 return SDValue(E, 0);
5022 }
5023 SDNode *N = new (NodeAllocator) MaskedLoadSDNode(dl.getIROrder(),
5024 dl.getDebugLoc(), Ops, 4, VTs,
5025 ExtTy, MemVT, MMO);
5026 CSEMap.InsertNode(N, IP);
5027 InsertNode(N);
5028 return SDValue(N, 0);
5029}
5030
5031SDValue SelectionDAG::getMaskedStore(SDValue Chain, SDLoc dl, SDValue Val,
5032 SDValue Ptr, SDValue Mask, EVT MemVT,
5033 MachineMemOperand *MMO, bool isTrunc) {
5034 assert(Chain.getValueType() == MVT::Other &&
5035 "Invalid chain type");
5036 EVT VT = Val.getValueType();
5037 SDVTList VTs = getVTList(MVT::Other);
5038 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5039 FoldingSetNodeID ID;
5040 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5041 ID.AddInteger(VT.getRawBits());
5042 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5043 MMO->isNonTemporal(), MMO->isInvariant()));
5044 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5045 void *IP = nullptr;
5046 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5047 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5048 return SDValue(E, 0);
5049 }
5050 SDNode *N = new (NodeAllocator) MaskedStoreSDNode(dl.getIROrder(),
5051 dl.getDebugLoc(), Ops, 4,
5052 VTs, isTrunc, MemVT, MMO);
5053 CSEMap.InsertNode(N, IP);
5054 InsertNode(N);
5055 return SDValue(N, 0);
5056}
5057
Andrew Trickac6d9be2013-05-25 02:42:55 +00005058SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005059 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00005060 SDValue SV,
5061 unsigned Align) {
5062 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005063 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005064}
5065
Andrew Trickac6d9be2013-05-25 02:42:55 +00005066SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005067 ArrayRef<SDUse> Ops) {
5068 switch (Ops.size()) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00005069 case 0: return getNode(Opcode, DL, VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005070 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling7ade28c2009-01-28 22:17:52 +00005071 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5072 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00005073 default: break;
5074 }
5075
Dan Gohman475871a2008-07-27 21:46:04 +00005076 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00005077 // the regular getNode logic.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005078 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
5079 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00005080}
5081
Andrew Trickac6d9be2013-05-25 02:42:55 +00005082SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005083 ArrayRef<SDValue> Ops) {
5084 unsigned NumOps = Ops.size();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005085 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00005086 case 0: return getNode(Opcode, DL, VT);
5087 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5088 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5089 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00005090 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005091 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005092
Chris Lattneref847df2005-04-09 03:27:28 +00005093 switch (Opcode) {
5094 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00005095 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005096 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00005097 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5098 "LHS and RHS of condition must have same type!");
5099 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5100 "True and False arms of SelectCC must have same type!");
5101 assert(Ops[2].getValueType() == VT &&
5102 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00005103 break;
5104 }
5105 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005106 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00005107 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5108 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00005109 break;
5110 }
Chris Lattneref847df2005-04-09 03:27:28 +00005111 }
5112
Chris Lattner385328c2005-05-14 07:42:29 +00005113 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00005114 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00005115 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00005116
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005117 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00005118 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005119 AddNodeIDNode(ID, Opcode, VTs, Ops);
5120 void *IP = nullptr;
Bill Wendling7ade28c2009-01-28 22:17:52 +00005121
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005122 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00005123 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00005124
Jack Carter3af4d252013-09-09 22:02:08 +00005125 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005126 VTs, Ops);
Chris Lattnera5682852006-08-07 23:03:03 +00005127 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00005128 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00005129 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005130 VTs, Ops);
Chris Lattner43247a12005-08-25 19:12:10 +00005131 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00005132
Stephen Hines37ed9c12014-12-01 14:51:49 -08005133 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005134 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005135}
5136
Andrew Trickac6d9be2013-05-25 02:42:55 +00005137SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005138 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5139 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Scott Michelfdc40a02009-02-17 22:15:04 +00005140}
5141
Andrew Trickac6d9be2013-05-25 02:42:55 +00005142SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005143 ArrayRef<SDValue> Ops) {
Chris Lattnerbe384162006-08-16 22:57:46 +00005144 if (VTList.NumVTs == 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -07005145 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattner89c34632005-05-14 06:20:26 +00005146
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00005147#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00005148 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00005149 // FIXME: figure out how to safely handle things like
5150 // int foo(int x) { return 1 << (x & 255); }
5151 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00005152 case ISD::SRA_PARTS:
5153 case ISD::SRL_PARTS:
5154 case ISD::SHL_PARTS:
5155 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005156 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00005157 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00005158 else if (N3.getOpcode() == ISD::AND)
5159 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5160 // If the and is only masking out bits that cannot effect the shift,
5161 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00005162 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00005163 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00005164 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00005165 }
5166 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00005167 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00005168#endif
Chris Lattner89c34632005-05-14 06:20:26 +00005169
Chris Lattner43247a12005-08-25 19:12:10 +00005170 // Memoize the node unless it returns a flag.
5171 SDNode *N;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005172 unsigned NumOps = Ops.size();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005173 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00005174 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005175 AddNodeIDNode(ID, Opcode, VTList, Ops);
5176 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005177 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00005178 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005179
Dan Gohman0e5f1302008-07-07 23:02:41 +00005180 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00005181 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5182 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005183 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00005184 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5185 DL.getDebugLoc(), VTList, Ops[0],
5186 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005187 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00005188 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5189 DL.getDebugLoc(), VTList, Ops[0],
5190 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005191 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00005192 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005193 VTList, Ops);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005194 }
Chris Lattnera5682852006-08-07 23:03:03 +00005195 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00005196 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00005197 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00005198 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5199 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005200 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00005201 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5202 DL.getDebugLoc(), VTList, Ops[0],
5203 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005204 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00005205 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5206 DL.getDebugLoc(), VTList, Ops[0],
5207 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005208 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00005209 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005210 VTList, Ops);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005211 }
Chris Lattner43247a12005-08-25 19:12:10 +00005212 }
Stephen Hines37ed9c12014-12-01 14:51:49 -08005213 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005214 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00005215}
5216
Andrew Trickac6d9be2013-05-25 02:42:55 +00005217SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08005218 return getNode(Opcode, DL, VTList, None);
Dan Gohman08ce9762007-10-08 15:49:58 +00005219}
5220
Andrew Trickac6d9be2013-05-25 02:42:55 +00005221SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005222 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00005223 SDValue Ops[] = { N1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005224 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005225}
5226
Andrew Trickac6d9be2013-05-25 02:42:55 +00005227SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005228 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00005229 SDValue Ops[] = { N1, N2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005230 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005231}
5232
Andrew Trickac6d9be2013-05-25 02:42:55 +00005233SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005234 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005235 SDValue Ops[] = { N1, N2, N3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005236 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005237}
5238
Andrew Trickac6d9be2013-05-25 02:42:55 +00005239SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005240 SDValue N1, SDValue N2, SDValue N3,
5241 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00005242 SDValue Ops[] = { N1, N2, N3, N4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005243 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005244}
5245
Andrew Trickac6d9be2013-05-25 02:42:55 +00005246SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005247 SDValue N1, SDValue N2, SDValue N3,
5248 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00005249 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005250 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005251}
5252
Owen Andersone50ed302009-08-10 22:56:29 +00005253SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00005254 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00005255}
5256
Owen Andersone50ed302009-08-10 22:56:29 +00005257SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005258 FoldingSetNodeID ID;
5259 ID.AddInteger(2U);
5260 ID.AddInteger(VT1.getRawBits());
5261 ID.AddInteger(VT2.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005262
Stephen Hinesdce4a402014-05-29 02:49:00 -07005263 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005264 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005265 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005266 EVT *Array = Allocator.Allocate<EVT>(2);
5267 Array[0] = VT1;
5268 Array[1] = VT2;
5269 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5270 VTListMap.InsertNode(Result, IP);
5271 }
5272 return Result->getSDVTList();
Chris Lattnera3255112005-11-08 23:30:28 +00005273}
Dan Gohmane8be6c62008-07-17 19:10:17 +00005274
Owen Andersone50ed302009-08-10 22:56:29 +00005275SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005276 FoldingSetNodeID ID;
5277 ID.AddInteger(3U);
5278 ID.AddInteger(VT1.getRawBits());
5279 ID.AddInteger(VT2.getRawBits());
5280 ID.AddInteger(VT3.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005281
Stephen Hinesdce4a402014-05-29 02:49:00 -07005282 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005283 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005284 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005285 EVT *Array = Allocator.Allocate<EVT>(3);
5286 Array[0] = VT1;
5287 Array[1] = VT2;
5288 Array[2] = VT3;
5289 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5290 VTListMap.InsertNode(Result, IP);
5291 }
5292 return Result->getSDVTList();
Chris Lattner70046e92006-08-15 17:46:01 +00005293}
5294
Owen Andersone50ed302009-08-10 22:56:29 +00005295SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005296 FoldingSetNodeID ID;
5297 ID.AddInteger(4U);
5298 ID.AddInteger(VT1.getRawBits());
5299 ID.AddInteger(VT2.getRawBits());
5300 ID.AddInteger(VT3.getRawBits());
5301 ID.AddInteger(VT4.getRawBits());
Bill Wendling13d6d442008-12-01 23:28:22 +00005302
Stephen Hinesdce4a402014-05-29 02:49:00 -07005303 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005304 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005305 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005306 EVT *Array = Allocator.Allocate<EVT>(4);
5307 Array[0] = VT1;
5308 Array[1] = VT2;
5309 Array[2] = VT3;
5310 Array[3] = VT4;
5311 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5312 VTListMap.InsertNode(Result, IP);
5313 }
5314 return Result->getSDVTList();
Bill Wendling13d6d442008-12-01 23:28:22 +00005315}
5316
Stephen Hinesdce4a402014-05-29 02:49:00 -07005317SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5318 unsigned NumVTs = VTs.size();
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005319 FoldingSetNodeID ID;
5320 ID.AddInteger(NumVTs);
5321 for (unsigned index = 0; index < NumVTs; index++) {
5322 ID.AddInteger(VTs[index].getRawBits());
Chris Lattner70046e92006-08-15 17:46:01 +00005323 }
5324
Stephen Hinesdce4a402014-05-29 02:49:00 -07005325 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005326 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005327 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005328 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005329 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005330 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5331 VTListMap.InsertNode(Result, IP);
Chris Lattner70046e92006-08-15 17:46:01 +00005332 }
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005333 return Result->getSDVTList();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00005334}
5335
5336
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005337/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5338/// specified operands. If the resultant node already exists in the DAG,
5339/// this does not modify the specified node, instead it returns the node that
5340/// already exists. If the resultant node does not exist in the DAG, the
5341/// input node is returned. As a degenerate case, if you specify the same
5342/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00005343SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005344 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005345
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005346 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00005347 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005348
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005349 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005350 void *InsertPos = nullptr;
Chris Lattnera5682852006-08-07 23:03:03 +00005351 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005352 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005353
Dan Gohman79acd2b2008-07-21 22:38:59 +00005354 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005355 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005356 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005357 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005358
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005359 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005360 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00005361
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005362 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005363 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005364 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005365}
5366
Dan Gohman027657d2010-06-18 15:30:29 +00005367SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005368 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005369
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005370 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005371 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00005372 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00005373
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005374 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005375 void *InsertPos = nullptr;
Chris Lattnera5682852006-08-07 23:03:03 +00005376 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005377 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005378
Dan Gohman79acd2b2008-07-21 22:38:59 +00005379 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005380 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005381 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005382 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005383
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005384 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005385 if (N->OperandList[0] != Op1)
5386 N->OperandList[0].set(Op1);
5387 if (N->OperandList[1] != Op2)
5388 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005389
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005390 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005391 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005392 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005393}
5394
Dan Gohman027657d2010-06-18 15:30:29 +00005395SDNode *SelectionDAG::
5396UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005397 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005398 return UpdateNodeOperands(N, Ops);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005399}
5400
Dan Gohman027657d2010-06-18 15:30:29 +00005401SDNode *SelectionDAG::
5402UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue Op3, SDValue Op4) {
5404 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005405 return UpdateNodeOperands(N, Ops);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005406}
5407
Dan Gohman027657d2010-06-18 15:30:29 +00005408SDNode *SelectionDAG::
5409UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005410 SDValue Op3, SDValue Op4, SDValue Op5) {
5411 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005412 return UpdateNodeOperands(N, Ops);
Chris Lattner809ec112006-01-28 10:09:25 +00005413}
5414
Dan Gohman027657d2010-06-18 15:30:29 +00005415SDNode *SelectionDAG::
Stephen Hinesdce4a402014-05-29 02:49:00 -07005416UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5417 unsigned NumOps = Ops.size();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005418 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005419 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005420
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005421 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005422 bool AnyChange = false;
5423 for (unsigned i = 0; i != NumOps; ++i) {
5424 if (Ops[i] != N->getOperand(i)) {
5425 AnyChange = true;
5426 break;
5427 }
5428 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005429
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005430 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00005431 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005432
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005433 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005434 void *InsertPos = nullptr;
5435 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005436 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005437
Dan Gohman7ceda162008-05-02 00:05:03 +00005438 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005439 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005440 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005441 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005442
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005443 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005444 for (unsigned i = 0; i != NumOps; ++i)
5445 if (N->OperandList[i] != Ops[i])
5446 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005447
5448 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005449 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005450 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005451}
5452
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005453/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00005454/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005455void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005456 // Unlike the code in MorphNodeTo that does this, we don't need to
5457 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00005458 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5459 SDUse &Use = *I++;
5460 Use.set(SDValue());
5461 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00005462}
Chris Lattner149c58c2005-08-16 18:17:10 +00005463
Dan Gohmane8be6c62008-07-17 19:10:17 +00005464/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5465/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005466///
Dan Gohmane8be6c62008-07-17 19:10:17 +00005467SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005468 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005469 SDVTList VTs = getVTList(VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005470 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner7651fa42005-08-24 23:00:29 +00005471}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005472
Dan Gohmane8be6c62008-07-17 19:10:17 +00005473SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005474 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005475 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005476 SDValue Ops[] = { Op1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005477 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005478}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005479
Dan Gohmane8be6c62008-07-17 19:10:17 +00005480SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005481 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005482 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005483 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005484 SDValue Ops[] = { Op1, Op2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005485 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005486}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005487
Dan Gohmane8be6c62008-07-17 19:10:17 +00005488SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005489 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005490 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005491 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005492 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005493 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005494}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00005495
Dan Gohmane8be6c62008-07-17 19:10:17 +00005496SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005497 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005498 SDVTList VTs = getVTList(VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005499 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005500}
5501
Dan Gohmane8be6c62008-07-17 19:10:17 +00005502SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005503 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005504 SDVTList VTs = getVTList(VT1, VT2);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005505 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005506}
5507
Dan Gohmane8be6c62008-07-17 19:10:17 +00005508SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005509 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005510 SDVTList VTs = getVTList(VT1, VT2);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005511 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohmancd920d92008-07-02 23:23:19 +00005512}
5513
Dan Gohmane8be6c62008-07-17 19:10:17 +00005514SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005515 EVT VT1, EVT VT2, EVT VT3,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005516 ArrayRef<SDValue> Ops) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005517 SDVTList VTs = getVTList(VT1, VT2, VT3);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005518 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005519}
5520
Bill Wendling13d6d442008-12-01 23:28:22 +00005521SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005522 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005523 ArrayRef<SDValue> Ops) {
Bill Wendling13d6d442008-12-01 23:28:22 +00005524 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005525 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling13d6d442008-12-01 23:28:22 +00005526}
5527
Scott Michelfdc40a02009-02-17 22:15:04 +00005528SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005529 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005530 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005531 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005532 SDValue Ops[] = { Op1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005533 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005534}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005535
Scott Michelfdc40a02009-02-17 22:15:04 +00005536SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005537 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005538 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005539 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005540 SDValue Ops[] = { Op1, Op2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005541 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005542}
5543
Dan Gohmane8be6c62008-07-17 19:10:17 +00005544SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005545 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005546 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005547 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005548 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005549 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005550 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005551}
5552
Dan Gohmane8be6c62008-07-17 19:10:17 +00005553SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005554 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005555 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005556 SDValue Op3) {
5557 SDVTList VTs = getVTList(VT1, VT2, VT3);
5558 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005559 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling13d6d442008-12-01 23:28:22 +00005560}
5561
5562SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005563 SDVTList VTs,ArrayRef<SDValue> Ops) {
5564 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner5857e0a2010-02-23 23:01:35 +00005565 // Reset the NodeID to -1.
5566 N->setNodeId(-1);
5567 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005568}
5569
Andrew Trickac6d9be2013-05-25 02:42:55 +00005570/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel0508d042011-12-15 18:21:18 +00005571/// the line number information on the merged node since it is not possible to
5572/// preserve the information that operation is associated with multiple lines.
5573/// This will make the debugger working better at -O0, were there is a higher
5574/// probability having other instructions associated with that line.
5575///
Andrew Trickac6d9be2013-05-25 02:42:55 +00005576/// For IROrder, we keep the smaller of the two
5577SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel0508d042011-12-15 18:21:18 +00005578 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005579 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5580 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel0508d042011-12-15 18:21:18 +00005581 N->setDebugLoc(DebugLoc());
5582 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005583 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5584 N->setIROrder(Order);
Devang Patel0508d042011-12-15 18:21:18 +00005585 return N;
5586}
5587
Chris Lattner21221e32010-02-28 21:36:14 +00005588/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005589/// return type, opcode, and operands.
5590///
5591/// Note that MorphNodeTo returns the resultant node. If there is already a
5592/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickac6d9be2013-05-25 02:42:55 +00005593/// the current one. Note that the SDLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005594///
5595/// Using MorphNodeTo is faster than creating a new node and swapping it in
5596/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005597/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005598/// the node's users.
5599///
Stephen Hines37ed9c12014-12-01 14:51:49 -08005600/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5601/// As a consequence it isn't appropriate to use from within the DAG combiner or
5602/// the legalizer which maintain worklists that would need to be updated when
5603/// deleting things.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005604SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005605 SDVTList VTs, ArrayRef<SDValue> Ops) {
5606 unsigned NumOps = Ops.size();
Dan Gohmancd920d92008-07-02 23:23:19 +00005607 // If an identical node already exists, use it.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005608 void *IP = nullptr;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005609 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005610 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005611 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005612 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005613 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohmane8be6c62008-07-17 19:10:17 +00005614 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005615
Dan Gohman095cc292008-09-13 01:54:27 +00005616 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005617 IP = nullptr;
Chris Lattner67612a12007-02-04 02:32:44 +00005618
Dan Gohmane8be6c62008-07-17 19:10:17 +00005619 // Start the morphing.
5620 N->NodeType = Opc;
5621 N->ValueList = VTs.VTs;
5622 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005623
Dan Gohmane8be6c62008-07-17 19:10:17 +00005624 // Clear the operands list, updating used nodes to remove this from their
5625 // use list. Keep track of any operands that become dead as a result.
5626 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005627 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5628 SDUse &Use = *I++;
5629 SDNode *Used = Use.getNode();
5630 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005631 if (Used->use_empty())
5632 DeadNodeSet.insert(Used);
5633 }
5634
Dan Gohmanc76909a2009-09-25 20:36:54 +00005635 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5636 // Initialize the memory references information.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005637 MN->setMemRefs(nullptr, nullptr);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005638 // If NumOps is larger than the # of operands we can have in a
5639 // MachineSDNode, reallocate the operand list.
5640 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5641 if (MN->OperandsNeedDelete)
5642 delete[] MN->OperandList;
5643 if (NumOps > array_lengthof(MN->LocalOperands))
5644 // We're creating a final node that will live unmorphed for the
5645 // remainder of the current SelectionDAG iteration, so we can allocate
5646 // the operands directly out of a pool with no recycling metadata.
5647 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005648 Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005649 else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005650 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005651 MN->OperandsNeedDelete = false;
5652 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005653 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005654 } else {
5655 // If NumOps is larger than the # of operands we currently have, reallocate
5656 // the operand list.
5657 if (NumOps > N->NumOperands) {
5658 if (N->OperandsNeedDelete)
5659 delete[] N->OperandList;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005660 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005661 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005662 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005663 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005664 }
5665
5666 // Delete any nodes that are still dead after adding the uses for the
5667 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005668 if (!DeadNodeSet.empty()) {
5669 SmallVector<SDNode *, 16> DeadNodes;
Stephen Hines37ed9c12014-12-01 14:51:49 -08005670 for (SDNode *N : DeadNodeSet)
5671 if (N->use_empty())
5672 DeadNodes.push_back(N);
Chris Lattner7d892d62010-03-01 07:43:08 +00005673 RemoveDeadNodes(DeadNodes);
5674 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005675
Dan Gohmane8be6c62008-07-17 19:10:17 +00005676 if (IP)
5677 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005678 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005679}
5680
Chris Lattner0fb094f2005-11-19 01:44:53 +00005681
Dan Gohman602b0c82009-09-25 18:54:59 +00005682/// getMachineNode - These are used for target selectors to create a new node
5683/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005684///
Dan Gohman602b0c82009-09-25 18:54:59 +00005685/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005686/// node of the specified opcode and operands, it returns that node instead of
5687/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005688MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005689SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005690 SDVTList VTs = getVTList(VT);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005691 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005692}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005693
Dan Gohmanc81b7832009-10-10 01:29:16 +00005694MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005695SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005696 SDVTList VTs = getVTList(VT);
5697 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005698 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005699}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005700
Dan Gohmanc81b7832009-10-10 01:29:16 +00005701MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005702SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005703 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005704 SDVTList VTs = getVTList(VT);
5705 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005706 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005707}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005708
Dan Gohmanc81b7832009-10-10 01:29:16 +00005709MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005710SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005711 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005712 SDVTList VTs = getVTList(VT);
5713 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005714 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005715}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005716
Dan Gohmanc81b7832009-10-10 01:29:16 +00005717MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005718SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liao2a8bea72013-04-19 22:22:57 +00005719 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005720 SDVTList VTs = getVTList(VT);
Michael Liao2a8bea72013-04-19 22:22:57 +00005721 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005722}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005723
Dan Gohmanc81b7832009-10-10 01:29:16 +00005724MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005725SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005726 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005727 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005728}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005729
Dan Gohmanc81b7832009-10-10 01:29:16 +00005730MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005731SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005732 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005733 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005734 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005735 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005736}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005737
Dan Gohmanc81b7832009-10-10 01:29:16 +00005738MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005739SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005740 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005741 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005742 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005743 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005744}
5745
Dan Gohmanc81b7832009-10-10 01:29:16 +00005746MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005747SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005748 EVT VT1, EVT VT2, SDValue Op1,
5749 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005750 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005751 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005752 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005753}
5754
Dan Gohmanc81b7832009-10-10 01:29:16 +00005755MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005756SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005757 EVT VT1, EVT VT2,
Michael Liao2a8bea72013-04-19 22:22:57 +00005758 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005759 SDVTList VTs = getVTList(VT1, VT2);
Michael Liao2a8bea72013-04-19 22:22:57 +00005760 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005761}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005762
Dan Gohmanc81b7832009-10-10 01:29:16 +00005763MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005764SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005765 EVT VT1, EVT VT2, EVT VT3,
5766 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005767 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005768 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005769 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005770}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005771
Dan Gohmanc81b7832009-10-10 01:29:16 +00005772MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005773SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005774 EVT VT1, EVT VT2, EVT VT3,
5775 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005776 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005777 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005778 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Cheng6ae46c42006-02-09 07:15:23 +00005779}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005780
Dan Gohmanc81b7832009-10-10 01:29:16 +00005781MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005782SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005783 EVT VT1, EVT VT2, EVT VT3,
Michael Liao2a8bea72013-04-19 22:22:57 +00005784 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005785 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liao2a8bea72013-04-19 22:22:57 +00005786 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005787}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005788
Dan Gohmanc81b7832009-10-10 01:29:16 +00005789MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005790SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005791 EVT VT2, EVT VT3, EVT VT4,
Michael Liao2a8bea72013-04-19 22:22:57 +00005792 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005793 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liao2a8bea72013-04-19 22:22:57 +00005794 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005795}
5796
Dan Gohmanc81b7832009-10-10 01:29:16 +00005797MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005798SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramer3853f742013-03-07 20:33:29 +00005799 ArrayRef<EVT> ResultTys,
Michael Liao2a8bea72013-04-19 22:22:57 +00005800 ArrayRef<SDValue> Ops) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07005801 SDVTList VTs = getVTList(ResultTys);
Michael Liao2a8bea72013-04-19 22:22:57 +00005802 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005803}
5804
Dan Gohmanc81b7832009-10-10 01:29:16 +00005805MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005806SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liao2a8bea72013-04-19 22:22:57 +00005807 ArrayRef<SDValue> OpsArray) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005808 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005809 MachineSDNode *N;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005810 void *IP = nullptr;
Michael Liao2a8bea72013-04-19 22:22:57 +00005811 const SDValue *Ops = OpsArray.data();
5812 unsigned NumOps = OpsArray.size();
Dan Gohmanc76909a2009-09-25 20:36:54 +00005813
5814 if (DoCSE) {
5815 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005816 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
5817 IP = nullptr;
Devang Patel0508d042011-12-15 18:21:18 +00005818 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005819 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel0508d042011-12-15 18:21:18 +00005820 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005821 }
5822
5823 // Allocate a new MachineSDNode.
Jack Carter3af4d252013-09-09 22:02:08 +00005824 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5825 DL.getDebugLoc(), VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005826
5827 // Initialize the operands list.
5828 if (NumOps > array_lengthof(N->LocalOperands))
5829 // We're creating a final node that will live unmorphed for the
5830 // remainder of the current SelectionDAG iteration, so we can allocate
5831 // the operands directly out of a pool with no recycling metadata.
5832 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5833 Ops, NumOps);
5834 else
5835 N->InitOperands(N->LocalOperands, Ops, NumOps);
5836 N->OperandsNeedDelete = false;
5837
5838 if (DoCSE)
5839 CSEMap.InsertNode(N, IP);
5840
Stephen Hines37ed9c12014-12-01 14:51:49 -08005841 InsertNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005842 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005843}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005844
Dan Gohman6a402dc2009-08-19 18:16:17 +00005845/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005846/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005847SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005848SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohman6a402dc2009-08-19 18:16:17 +00005849 SDValue Operand) {
5850 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005851 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005852 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005853 return SDValue(Subreg, 0);
5854}
5855
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005856/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005857/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005858SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005859SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005860 SDValue Operand, SDValue Subreg) {
5861 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005862 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005863 VT, Operand, Subreg, SRIdxVal);
5864 return SDValue(Result, 0);
5865}
5866
Evan Cheng08b11732008-03-22 01:55:50 +00005867/// getNodeIfExists - Get the specified node if it's already available, or
5868/// else return NULL.
5869SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07005870 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5871 bool exact) {
5872 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005873 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005874 AddNodeIDNode(ID, Opcode, VTList, Ops);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07005875 if (isBinOpWithFlags(Opcode))
5876 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005877 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005878 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005879 return E;
5880 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07005881 return nullptr;
Evan Cheng08b11732008-03-22 01:55:50 +00005882}
5883
Evan Cheng31441b72010-03-29 20:48:30 +00005884/// getDbgValue - Creates a SDDbgValue node.
5885///
Stephen Hinesdce4a402014-05-29 02:49:00 -07005886/// SDNode
Stephen Hines37ed9c12014-12-01 14:51:49 -08005887SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
5888 unsigned R, bool IsIndirect, uint64_t Off,
5889 DebugLoc DL, unsigned O) {
5890 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng31441b72010-03-29 20:48:30 +00005891}
5892
Stephen Hinesdce4a402014-05-29 02:49:00 -07005893/// Constant
Stephen Hines37ed9c12014-12-01 14:51:49 -08005894SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
5895 const Value *C, uint64_t Off,
5896 DebugLoc DL, unsigned O) {
5897 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng31441b72010-03-29 20:48:30 +00005898}
5899
Stephen Hinesdce4a402014-05-29 02:49:00 -07005900/// FrameIndex
Stephen Hines37ed9c12014-12-01 14:51:49 -08005901SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
5902 unsigned FI, uint64_t Off,
5903 DebugLoc DL, unsigned O) {
5904 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng31441b72010-03-29 20:48:30 +00005905}
5906
Dan Gohmana72d2a22010-03-03 21:33:37 +00005907namespace {
5908
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005909/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005910/// pointed to by a use iterator is deleted, increment the use iterator
5911/// so that it doesn't dangle.
5912///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005913class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005914 SDNode::use_iterator &UI;
5915 SDNode::use_iterator &UE;
5916
Stephen Hines36b56882014-04-23 16:57:46 -07005917 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005918 // Increment the iterator as needed.
5919 while (UI != UE && N == *UI)
5920 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005921 }
5922
5923public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005924 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005925 SDNode::use_iterator &ui,
5926 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005927 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005928};
5929
5930}
5931
Evan Cheng99157a02006-08-07 22:13:29 +00005932/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005933/// This can cause recursive merging of nodes in the DAG.
5934///
Chris Lattner11d049c2008-02-03 03:35:22 +00005935/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005936///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005937void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005938 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005939 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005940 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005941 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005942
Dan Gohman39946102009-01-25 16:29:12 +00005943 // Iterate over all the existing uses of From. New uses will be added
5944 // to the beginning of the use list, which we avoid visiting.
5945 // This specifically avoids visiting uses of From that arise while the
5946 // replacement is happening, because any such uses would be the result
5947 // of CSE: If an existing node looks like From after one of its operands
5948 // is replaced by To, we don't want to replace of all its users with To
5949 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005950 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005951 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005952 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005953 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005954
Chris Lattner8b8749f2005-08-17 19:00:20 +00005955 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005956 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005957
Dan Gohman39946102009-01-25 16:29:12 +00005958 // A user can appear in a use list multiple times, and when this
5959 // happens the uses are usually next to each other in the list.
5960 // To help reduce the number of CSE recomputations, process all
5961 // the uses of this user that we can find this way.
5962 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005963 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005964 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005965 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005966 } while (UI != UE && *UI == User);
5967
5968 // Now that we have modified User, add it back to the CSE maps. If it
5969 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005970 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005971 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005972
5973 // If we just RAUW'd the root, take note.
5974 if (FromN == getRoot())
5975 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005976}
5977
5978/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5979/// This can cause recursive merging of nodes in the DAG.
5980///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005981/// This version assumes that for each value of From, there is a
5982/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005983///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005984void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005985#ifndef NDEBUG
5986 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5987 assert((!From->hasAnyUseOfValue(i) ||
5988 From->getValueType(i) == To->getValueType(i)) &&
5989 "Cannot use this version of ReplaceAllUsesWith!");
5990#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005991
5992 // Handle the trivial case.
5993 if (From == To)
5994 return;
5995
Dan Gohmandbe664a2009-01-19 21:44:21 +00005996 // Iterate over just the existing users of From. See the comments in
5997 // the ReplaceAllUsesWith above.
5998 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005999 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00006000 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00006001 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00006002
Chris Lattner8b52f212005-08-26 18:36:28 +00006003 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00006004 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00006005
Dan Gohman39946102009-01-25 16:29:12 +00006006 // A user can appear in a use list multiple times, and when this
6007 // happens the uses are usually next to each other in the list.
6008 // To help reduce the number of CSE recomputations, process all
6009 // the uses of this user that we can find this way.
6010 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00006011 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00006012 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00006013 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00006014 } while (UI != UE && *UI == User);
6015
6016 // Now that we have modified User, add it back to the CSE maps. If it
6017 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006018 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00006019 }
Dan Gohman65fd6562011-11-03 21:49:52 +00006020
6021 // If we just RAUW'd the root, take note.
6022 if (From == getRoot().getNode())
6023 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00006024}
6025
Chris Lattner8b52f212005-08-26 18:36:28 +00006026/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6027/// This can cause recursive merging of nodes in the DAG.
6028///
6029/// This version can replace From with any result values. To must match the
6030/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006031void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00006032 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006033 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00006034
Dan Gohmandbe664a2009-01-19 21:44:21 +00006035 // Iterate over just the existing users of From. See the comments in
6036 // the ReplaceAllUsesWith above.
6037 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006038 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00006039 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00006040 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00006041
Chris Lattner7b2880c2005-08-24 22:44:39 +00006042 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00006043 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00006044
Dan Gohman39946102009-01-25 16:29:12 +00006045 // A user can appear in a use list multiple times, and when this
6046 // happens the uses are usually next to each other in the list.
6047 // To help reduce the number of CSE recomputations, process all
6048 // the uses of this user that we can find this way.
6049 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00006050 SDUse &Use = UI.getUse();
6051 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00006052 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00006053 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00006054 } while (UI != UE && *UI == User);
6055
6056 // Now that we have modified User, add it back to the CSE maps. If it
6057 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006058 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00006059 }
Dan Gohman65fd6562011-11-03 21:49:52 +00006060
6061 // If we just RAUW'd the root, take note.
6062 if (From == getRoot().getNode())
6063 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00006064}
6065
Chris Lattner012f2412006-02-17 21:58:01 +00006066/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00006067/// uses of other values produced by From.getNode() alone. The Deleted
6068/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006069void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00006070 // Handle the really simple, really trivial case efficiently.
6071 if (From == To) return;
6072
Chris Lattner012f2412006-02-17 21:58:01 +00006073 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00006074 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006075 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00006076 return;
6077 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006078
Dan Gohman39946102009-01-25 16:29:12 +00006079 // Iterate over just the existing users of From. See the comments in
6080 // the ReplaceAllUsesWith above.
6081 SDNode::use_iterator UI = From.getNode()->use_begin(),
6082 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006083 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00006084 while (UI != UE) {
6085 SDNode *User = *UI;
6086 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00006087
Dan Gohman39946102009-01-25 16:29:12 +00006088 // A user can appear in a use list multiple times, and when this
6089 // happens the uses are usually next to each other in the list.
6090 // To help reduce the number of CSE recomputations, process all
6091 // the uses of this user that we can find this way.
6092 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00006093 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00006094
6095 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00006096 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00006097 ++UI;
6098 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00006099 }
Dan Gohman39946102009-01-25 16:29:12 +00006100
6101 // If this node hasn't been modified yet, it's still in the CSE maps,
6102 // so remove its old self from the CSE maps.
6103 if (!UserRemovedFromCSEMaps) {
6104 RemoveNodeFromCSEMaps(User);
6105 UserRemovedFromCSEMaps = true;
6106 }
6107
6108 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00006109 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00006110 } while (UI != UE && *UI == User);
6111
6112 // We are iterating over all uses of the From node, so if a use
6113 // doesn't use the specific value, no changes are made.
6114 if (!UserRemovedFromCSEMaps)
6115 continue;
6116
Chris Lattner01d029b2007-10-15 06:10:22 +00006117 // Now that we have modified User, add it back to the CSE maps. If it
6118 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006119 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00006120 }
Dan Gohman65fd6562011-11-03 21:49:52 +00006121
6122 // If we just RAUW'd the root, take note.
6123 if (From == getRoot())
6124 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00006125}
6126
Dan Gohman39946102009-01-25 16:29:12 +00006127namespace {
6128 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6129 /// to record information about a use.
6130 struct UseMemo {
6131 SDNode *User;
6132 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00006133 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00006134 };
Dan Gohmane7852d02009-01-26 04:35:06 +00006135
6136 /// operator< - Sort Memos by User.
6137 bool operator<(const UseMemo &L, const UseMemo &R) {
6138 return (intptr_t)L.User < (intptr_t)R.User;
6139 }
Dan Gohman39946102009-01-25 16:29:12 +00006140}
6141
Dan Gohmane8be6c62008-07-17 19:10:17 +00006142/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00006143/// uses of other values produced by From.getNode() alone. The same value
6144/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00006145/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00006146void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6147 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006148 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00006149 // Handle the simple, trivial case efficiently.
6150 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006151 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00006152
Dan Gohman39946102009-01-25 16:29:12 +00006153 // Read up all the uses and make records of them. This helps
6154 // processing new uses that are introduced during the
6155 // replacement process.
6156 SmallVector<UseMemo, 4> Uses;
6157 for (unsigned i = 0; i != Num; ++i) {
6158 unsigned FromResNo = From[i].getResNo();
6159 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00006160 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00006161 E = FromNode->use_end(); UI != E; ++UI) {
6162 SDUse &Use = UI.getUse();
6163 if (Use.getResNo() == FromResNo) {
6164 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00006165 Uses.push_back(Memo);
6166 }
Dan Gohmane7852d02009-01-26 04:35:06 +00006167 }
Dan Gohman39946102009-01-25 16:29:12 +00006168 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00006169
Dan Gohmane7852d02009-01-26 04:35:06 +00006170 // Sort the uses, so that all the uses from a given User are together.
6171 std::sort(Uses.begin(), Uses.end());
6172
Dan Gohman39946102009-01-25 16:29:12 +00006173 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6174 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00006175 // We know that this user uses some value of From. If it is the right
6176 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00006177 SDNode *User = Uses[UseIndex].User;
6178
6179 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00006180 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00006181
Dan Gohmane7852d02009-01-26 04:35:06 +00006182 // The Uses array is sorted, so all the uses for a given User
6183 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00006184 // To help reduce the number of CSE recomputations, process all
6185 // the uses of this user that we can find this way.
6186 do {
6187 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00006188 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00006189 ++UseIndex;
6190
Dan Gohmane7852d02009-01-26 04:35:06 +00006191 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00006192 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6193
Dan Gohmane8be6c62008-07-17 19:10:17 +00006194 // Now that we have modified User, add it back to the CSE maps. If it
6195 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006196 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00006197 }
6198}
6199
Evan Chenge6f35d82006-08-01 08:20:41 +00006200/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00006201/// based on their topological order. It returns the maximum id and a vector
6202/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00006203unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00006204
Dan Gohmanf06c8352008-09-30 18:30:35 +00006205 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00006206
Dan Gohmanf06c8352008-09-30 18:30:35 +00006207 // SortedPos tracks the progress of the algorithm. Nodes before it are
6208 // sorted, nodes after it are unsorted. When the algorithm completes
6209 // it is at the end of the list.
6210 allnodes_iterator SortedPos = allnodes_begin();
6211
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006212 // Visit all the nodes. Move nodes with no operands to the front of
6213 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00006214 // operand count. Before we do this, the Node Id fields of the nodes
6215 // may contain arbitrary values. After, the Node Id fields for nodes
6216 // before SortedPos will contain the topological sort index, and the
6217 // Node Id fields for nodes At SortedPos and after will contain the
6218 // count of outstanding operands.
6219 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6220 SDNode *N = I++;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006221 checkForCycles(N, this);
Dan Gohmanf06c8352008-09-30 18:30:35 +00006222 unsigned Degree = N->getNumOperands();
6223 if (Degree == 0) {
6224 // A node with no uses, add it to the result array immediately.
6225 N->setNodeId(DAGSize++);
6226 allnodes_iterator Q = N;
6227 if (Q != SortedPos)
6228 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00006229 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006230 ++SortedPos;
6231 } else {
6232 // Temporarily use the Node Id as scratch space for the degree count.
6233 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00006234 }
6235 }
6236
Chad Rosierf496dea2012-05-21 17:13:41 +00006237 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00006238 // such that by the time the end is reached all nodes will be sorted.
6239 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6240 SDNode *N = I;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006241 checkForCycles(N, this);
David Greene221925e2010-01-20 00:59:23 +00006242 // N is in sorted position, so all its uses have one less operand
6243 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00006244 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6245 UI != UE; ++UI) {
6246 SDNode *P = *UI;
6247 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00006248 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006249 --Degree;
6250 if (Degree == 0) {
6251 // All of P's operands are sorted, so P may sorted now.
6252 P->setNodeId(DAGSize++);
6253 if (P != SortedPos)
6254 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00006255 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006256 ++SortedPos;
6257 } else {
6258 // Update P's outstanding operand count.
6259 P->setNodeId(Degree);
6260 }
6261 }
David Greene221925e2010-01-20 00:59:23 +00006262 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00006263#ifndef NDEBUG
6264 SDNode *S = ++I;
6265 dbgs() << "Overran sorted position:\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006266 S->dumprFull(this); dbgs() << "\n";
6267 dbgs() << "Checking if this is due to cycles\n";
6268 checkForCycles(this, true);
David Greene39143702010-02-09 23:03:05 +00006269#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -07006270 llvm_unreachable(nullptr);
David Greene221925e2010-01-20 00:59:23 +00006271 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00006272 }
6273
6274 assert(SortedPos == AllNodes.end() &&
6275 "Topological sort incomplete!");
6276 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6277 "First node in topological sort is not the entry token!");
6278 assert(AllNodes.front().getNodeId() == 0 &&
6279 "First node in topological sort has non-zero id!");
6280 assert(AllNodes.front().getNumOperands() == 0 &&
6281 "First node in topological sort has operands!");
6282 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6283 "Last node in topologic sort has unexpected id!");
6284 assert(AllNodes.back().use_empty() &&
6285 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006286 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006287 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00006288}
6289
Evan Chengbfcb3052010-03-25 01:38:16 +00006290/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6291/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00006292void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08006293 if (SD) {
6294 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Chengbfcb3052010-03-25 01:38:16 +00006295 SD->setHasDebugValue(true);
Stephen Hines37ed9c12014-12-01 14:51:49 -08006296 }
6297 DbgInfo->add(DB, SD, isParameter);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00006298}
Evan Cheng091cba12006-07-27 06:39:06 +00006299
Devang Patela2e868d2011-01-25 23:27:42 +00006300/// TransferDbgValues - Transfer SDDbgValues.
6301void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6302 if (From == To || !From.getNode()->getHasDebugValue())
6303 return;
6304 SDNode *FromNode = From.getNode();
6305 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006306 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00006307 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006308 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00006309 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00006310 SDDbgValue *Dbg = *I;
6311 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08006312 SDDbgValue *Clone =
6313 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6314 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6315 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patela778f5c2011-02-18 22:43:42 +00006316 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00006317 }
6318 }
Craig Topperf22fd3f2013-07-03 05:11:49 +00006319 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patela778f5c2011-02-18 22:43:42 +00006320 E = ClonedDVs.end(); I != E; ++I)
6321 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00006322}
6323
Jim Laskey58b968b2005-08-17 20:08:02 +00006324//===----------------------------------------------------------------------===//
6325// SDNode Class
6326//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00006327
Chris Lattner48b85922007-02-04 02:41:42 +00006328HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00006329 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00006330}
6331
Andrew Trickac6d9be2013-05-25 02:42:55 +00006332GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6333 DebugLoc DL, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006334 EVT VT, int64_t o, unsigned char TF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006335 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00006336 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00006337}
Chris Lattner48b85922007-02-04 02:41:42 +00006338
Matt Arsenault59d3ae62013-11-15 01:34:59 +00006339AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6340 SDValue X, unsigned SrcAS,
6341 unsigned DestAS)
6342 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6343 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6344
Andrew Trickac6d9be2013-05-25 02:42:55 +00006345MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6346 EVT memvt, MachineMemOperand *mmo)
6347 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006348 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006349 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006350 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00006351 assert(isNonTemporal() == MMO->isNonTemporal() &&
6352 "Non-temporal encoding error!");
Stephen Hines37ed9c12014-12-01 14:51:49 -08006353 // We check here that the size of the memory operand fits within the size of
6354 // the MMO. This is because the MMO might indicate only a possible address
6355 // range instead of specifying the affected memory addresses precisely.
6356 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00006357}
6358
Andrew Trickac6d9be2013-05-25 02:42:55 +00006359MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Stephen Hinesdce4a402014-05-29 02:49:00 -07006360 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6361 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohmanc76909a2009-09-25 20:36:54 +00006362 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006363 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006364 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006365 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Stephen Hines37ed9c12014-12-01 14:51:49 -08006366 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00006367}
6368
Jim Laskey583bd472006-10-27 23:46:08 +00006369/// Profile - Gather unique data for the node.
6370///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00006371void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00006372 AddNodeIDNode(ID, this);
6373}
6374
Owen Andersond8110fb2009-08-25 22:27:22 +00006375namespace {
6376 struct EVTArray {
6377 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006378
Owen Andersond8110fb2009-08-25 22:27:22 +00006379 EVTArray() {
6380 VTs.reserve(MVT::LAST_VALUETYPE);
6381 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6382 VTs.push_back(MVT((MVT::SimpleValueType)i));
6383 }
6384 };
6385}
6386
Owen Andersone50ed302009-08-10 22:56:29 +00006387static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00006388static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00006389static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00006390
Chris Lattnera3255112005-11-08 23:30:28 +00006391/// getValueTypeList - Return a pointer to the specified value type.
6392///
Owen Andersone50ed302009-08-10 22:56:29 +00006393const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006394 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00006395 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00006396 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00006397 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00006398 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00006399 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00006400 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00006401 }
Chris Lattnera3255112005-11-08 23:30:28 +00006402}
Duncan Sandsaf47b112007-10-16 09:56:48 +00006403
Chris Lattner5c884562005-01-12 18:37:47 +00006404/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6405/// indicated value. This method ignores uses of other values defined by this
6406/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00006407bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00006408 assert(Value < getNumValues() && "Bad value!");
6409
Roman Levensteindc1adac2008-04-07 10:06:32 +00006410 // TODO: Only iterate over uses of a given value of the node
6411 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00006412 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00006413 if (NUses == 0)
6414 return false;
6415 --NUses;
6416 }
Chris Lattner5c884562005-01-12 18:37:47 +00006417 }
6418
6419 // Found exactly the right number of uses?
6420 return NUses == 0;
6421}
6422
6423
Evan Cheng33d55952007-08-02 05:29:38 +00006424/// hasAnyUseOfValue - Return true if there are any use of the indicated
6425/// value. This method ignores uses of other values defined by this operation.
6426bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6427 assert(Value < getNumValues() && "Bad value!");
6428
Dan Gohman1373c1c2008-07-09 22:39:01 +00006429 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00006430 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00006431 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00006432
6433 return false;
6434}
6435
6436
Dan Gohman2a629952008-07-27 18:06:42 +00006437/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00006438///
Dan Gohman2a629952008-07-27 18:06:42 +00006439bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00006440 bool Seen = false;
6441 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006442 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00006443 if (User == this)
6444 Seen = true;
6445 else
6446 return false;
6447 }
6448
6449 return Seen;
6450}
6451
Evan Chenge6e97e62006-11-03 07:31:32 +00006452/// isOperand - Return true if this node is an operand of N.
6453///
Dan Gohman475871a2008-07-27 21:46:04 +00006454bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00006455 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6456 if (*this == N->getOperand(i))
6457 return true;
6458 return false;
6459}
6460
Evan Cheng917be682008-03-04 00:41:45 +00006461bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006462 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00006463 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006464 return true;
6465 return false;
6466}
Evan Cheng4ee62112006-02-05 06:29:23 +00006467
Chris Lattner572dee72008-01-16 05:49:24 +00006468/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00006469/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006470/// side-effecting instructions on any chain path. In practice, this looks
6471/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006472/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00006473bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00006474 unsigned Depth) const {
6475 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006476
Chris Lattner572dee72008-01-16 05:49:24 +00006477 // Don't search too deeply, we just want to be able to see through
6478 // TokenFactor's etc.
6479 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006480
Chris Lattner572dee72008-01-16 05:49:24 +00006481 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006482 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00006483 if (getOpcode() == ISD::TokenFactor) {
6484 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006485 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6486 return false;
6487 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00006488 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006489
Chris Lattner572dee72008-01-16 05:49:24 +00006490 // Loads don't have side effects, look through them.
6491 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6492 if (!Ld->isVolatile())
6493 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6494 }
6495 return false;
6496}
6497
Lang Hames944520f2011-07-07 04:31:51 +00006498/// hasPredecessor - Return true if N is a predecessor of this node.
6499/// N is either an operand of this node, or can be reached by recursively
6500/// traversing up the operands.
6501/// NOTE: This is an expensive method. Use it carefully.
6502bool SDNode::hasPredecessor(const SDNode *N) const {
6503 SmallPtrSet<const SDNode *, 32> Visited;
6504 SmallVector<const SDNode *, 16> Worklist;
6505 return hasPredecessorHelper(N, Visited, Worklist);
6506}
Dan Gohman6688d612009-10-28 03:44:30 +00006507
Craig Toppera0ec3f92013-07-14 04:42:23 +00006508bool
6509SDNode::hasPredecessorHelper(const SDNode *N,
Stephen Hines37ed9c12014-12-01 14:51:49 -08006510 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Toppera0ec3f92013-07-14 04:42:23 +00006511 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames944520f2011-07-07 04:31:51 +00006512 if (Visited.empty()) {
6513 Worklist.push_back(this);
6514 } else {
6515 // Take a look in the visited set. If we've already encountered this node
6516 // we needn't search further.
6517 if (Visited.count(N))
6518 return true;
6519 }
6520
6521 // Haven't visited N yet. Continue the search.
6522 while (!Worklist.empty()) {
6523 const SDNode *M = Worklist.pop_back_val();
6524 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6525 SDNode *Op = M->getOperand(i).getNode();
Stephen Hines37ed9c12014-12-01 14:51:49 -08006526 if (Visited.insert(Op).second)
Dan Gohman6688d612009-10-28 03:44:30 +00006527 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006528 if (Op == N)
6529 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006530 }
Lang Hames944520f2011-07-07 04:31:51 +00006531 }
Dan Gohman6688d612009-10-28 03:44:30 +00006532
6533 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006534}
6535
Evan Chengc5484282006-10-04 00:56:09 +00006536uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6537 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006538 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006539}
6540
Mon P Wangcd6e7252009-11-30 02:42:02 +00006541SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6542 assert(N->getNumValues() == 1 &&
6543 "Can't unroll a vector with multiple results!");
6544
6545 EVT VT = N->getValueType(0);
6546 unsigned NE = VT.getVectorNumElements();
6547 EVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006548 SDLoc dl(N);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006549
6550 SmallVector<SDValue, 8> Scalars;
6551 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6552
6553 // If ResNE is 0, fully unroll the vector op.
6554 if (ResNE == 0)
6555 ResNE = NE;
6556 else if (NE > ResNE)
6557 NE = ResNE;
6558
6559 unsigned i;
6560 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006561 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006562 SDValue Operand = N->getOperand(j);
6563 EVT OperandVT = Operand.getValueType();
6564 if (OperandVT.isVector()) {
6565 // A vector operand; extract a single element.
6566 EVT OperandEltVT = OperandVT.getVectorElementType();
6567 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6568 OperandEltVT,
6569 Operand,
Tom Stellard425b76c2013-08-05 22:22:01 +00006570 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006571 } else {
6572 // A scalar operand; just use it as is.
6573 Operands[j] = Operand;
6574 }
6575 }
6576
6577 switch (N->getOpcode()) {
6578 default:
Stephen Hinesdce4a402014-05-29 02:49:00 -07006579 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006580 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006581 case ISD::VSELECT:
Stephen Hinesdce4a402014-05-29 02:49:00 -07006582 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotemaec58612011-09-13 19:17:42 +00006583 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006584 case ISD::SHL:
6585 case ISD::SRA:
6586 case ISD::SRL:
6587 case ISD::ROTL:
6588 case ISD::ROTR:
6589 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter3af4d252013-09-09 22:02:08 +00006590 getShiftAmountOperand(Operands[0].getValueType(),
6591 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006592 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006593 case ISD::SIGN_EXTEND_INREG:
6594 case ISD::FP_ROUND_INREG: {
6595 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6596 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6597 Operands[0],
6598 getValueType(ExtVT)));
6599 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006600 }
6601 }
6602
6603 for (; i < ResNE; ++i)
6604 Scalars.push_back(getUNDEF(EltVT));
6605
6606 return getNode(ISD::BUILD_VECTOR, dl,
Stephen Hinesdce4a402014-05-29 02:49:00 -07006607 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006608}
6609
Evan Cheng64fa4a92009-12-09 01:36:00 +00006610
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006611/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6612/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006613/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006614bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006615 unsigned Bytes, int Dist) const {
6616 if (LD->getChain() != Base->getChain())
6617 return false;
6618 EVT VT = LD->getValueType(0);
6619 if (VT.getSizeInBits() / 8 != Bytes)
6620 return false;
6621
6622 SDValue Loc = LD->getOperand(1);
6623 SDValue BaseLoc = Base->getOperand(1);
6624 if (Loc.getOpcode() == ISD::FrameIndex) {
6625 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6626 return false;
6627 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6628 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6629 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6630 int FS = MFI->getObjectSize(FI);
6631 int BFS = MFI->getObjectSize(BFI);
6632 if (FS != BFS || FS != (int)Bytes) return false;
6633 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6634 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006635
Stephen Hinesebe69fe2015-03-23 12:10:34 -07006636 // Handle X + C.
6637 if (isBaseWithConstantOffset(Loc)) {
6638 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6639 if (Loc.getOperand(0) == BaseLoc) {
6640 // If the base location is a simple address with no offset itself, then
6641 // the second load's first add operand should be the base address.
6642 if (LocOffset == Dist * (int)Bytes)
6643 return true;
6644 } else if (isBaseWithConstantOffset(BaseLoc)) {
6645 // The base location itself has an offset, so subtract that value from the
6646 // second load's offset before comparing to distance * size.
6647 int64_t BOffset =
6648 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
6649 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
6650 if ((LocOffset - BOffset) == Dist * (int)Bytes)
6651 return true;
6652 }
6653 }
6654 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07006655 const GlobalValue *GV1 = nullptr;
6656 const GlobalValue *GV2 = nullptr;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006657 int64_t Offset1 = 0;
6658 int64_t Offset2 = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006659 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6660 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Cheng64fa4a92009-12-09 01:36:00 +00006661 if (isGA1 && isGA2 && GV1 == GV2)
6662 return Offset1 == (Offset2 + Dist*Bytes);
6663 return false;
6664}
6665
6666
Evan Chengf2dc5c72009-12-09 01:04:59 +00006667/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6668/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006669unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006670 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006671 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006672 int64_t GVOffset = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006673 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaulte6e81122013-11-16 20:50:54 +00006674 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006675 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Stephen Hinesdce4a402014-05-29 02:49:00 -07006676 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6677 TLI->getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006678 unsigned AlignBits = KnownZero.countTrailingOnes();
6679 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6680 if (Align)
6681 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006682 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006683
Evan Chengf2dc5c72009-12-09 01:04:59 +00006684 // If this is a direct reference to a stack slot, use information about the
6685 // stack slot's alignment.
6686 int FrameIdx = 1 << 31;
6687 int64_t FrameOffset = 0;
6688 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6689 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006690 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006691 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006692 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006693 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6694 FrameOffset = Ptr.getConstantOperandVal(1);
6695 }
6696
6697 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006698 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006699 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6700 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006701 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006702 }
6703
6704 return 0;
6705}
6706
Bill Wendlingee287ca2013-11-22 05:17:44 +00006707/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6708/// which is split (or expanded) into two not necessarily identical pieces.
6709std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6710 // Currently all types are split in half.
6711 EVT LoVT, HiVT;
6712 if (!VT.isVector()) {
6713 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6714 } else {
6715 unsigned NumElements = VT.getVectorNumElements();
6716 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6717 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6718 NumElements/2);
6719 }
6720 return std::make_pair(LoVT, HiVT);
6721}
6722
6723/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6724/// low/high part.
6725std::pair<SDValue, SDValue>
6726SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6727 const EVT &HiVT) {
6728 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6729 N.getValueType().getVectorNumElements() &&
6730 "More vector elements requested than available!");
6731 SDValue Lo, Hi;
6732 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6733 getConstant(0, TLI->getVectorIdxTy()));
6734 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6735 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6736 return std::make_pair(Lo, Hi);
6737}
6738
Stephen Hinesdce4a402014-05-29 02:49:00 -07006739void SelectionDAG::ExtractVectorElements(SDValue Op,
6740 SmallVectorImpl<SDValue> &Args,
6741 unsigned Start, unsigned Count) {
6742 EVT VT = Op.getValueType();
6743 if (Count == 0)
6744 Count = VT.getVectorNumElements();
6745
6746 EVT EltVT = VT.getVectorElementType();
6747 EVT IdxTy = TLI->getVectorIdxTy();
6748 SDLoc SL(Op);
6749 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6750 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6751 Op, getConstant(i, IdxTy)));
6752 }
6753}
6754
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006755// getAddressSpace - Return the address space this GlobalAddress belongs to.
6756unsigned GlobalAddressSDNode::getAddressSpace() const {
6757 return getGlobal()->getType()->getAddressSpace();
6758}
6759
6760
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006761Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006762 if (isMachineConstantPoolEntry())
6763 return Val.MachineCPVal->getType();
6764 return Val.ConstVal->getType();
6765}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006766
Bob Wilson24e338e2009-03-02 23:24:16 +00006767bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6768 APInt &SplatUndef,
6769 unsigned &SplatBitSize,
6770 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006771 unsigned MinSplatBits,
Stephen Hines36b56882014-04-23 16:57:46 -07006772 bool isBigEndian) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006773 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006774 assert(VT.isVector() && "Expected a vector type");
6775 unsigned sz = VT.getSizeInBits();
6776 if (MinSplatBits > sz)
6777 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006778
Bob Wilson24e338e2009-03-02 23:24:16 +00006779 SplatValue = APInt(sz, 0);
6780 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006781
Bob Wilson24e338e2009-03-02 23:24:16 +00006782 // Get the bits. Bits with undefined values (when the corresponding element
6783 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6784 // in SplatValue. If any of the values are not constant, give up and return
6785 // false.
6786 unsigned int nOps = getNumOperands();
6787 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6788 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006789
6790 for (unsigned j = 0; j < nOps; ++j) {
6791 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006792 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006793 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006794
Bob Wilson24e338e2009-03-02 23:24:16 +00006795 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006796 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006797 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006798 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006799 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006800 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006801 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006802 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006803 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006804 }
6805
Bob Wilson24e338e2009-03-02 23:24:16 +00006806 // The build_vector is all constants or undefs. Find the smallest element
6807 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006808
Bob Wilson24e338e2009-03-02 23:24:16 +00006809 HasAnyUndefs = (SplatUndef != 0);
6810 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006811
Bob Wilson24e338e2009-03-02 23:24:16 +00006812 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006813 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6814 APInt LowValue = SplatValue.trunc(HalfSize);
6815 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6816 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006817
Bob Wilson24e338e2009-03-02 23:24:16 +00006818 // If the two halves do not match (ignoring undef bits), stop here.
6819 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6820 MinSplatBits > HalfSize)
6821 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006822
Bob Wilson24e338e2009-03-02 23:24:16 +00006823 SplatValue = HighValue | LowValue;
6824 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006825
Bob Wilson24e338e2009-03-02 23:24:16 +00006826 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006827 }
6828
Bob Wilson24e338e2009-03-02 23:24:16 +00006829 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006830 return true;
6831}
Nate Begeman9008ca62009-04-27 18:41:29 +00006832
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006833SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6834 if (UndefElements) {
6835 UndefElements->clear();
6836 UndefElements->resize(getNumOperands());
6837 }
6838 SDValue Splatted;
6839 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6840 SDValue Op = getOperand(i);
6841 if (Op.getOpcode() == ISD::UNDEF) {
6842 if (UndefElements)
6843 (*UndefElements)[i] = true;
6844 } else if (!Splatted) {
6845 Splatted = Op;
6846 } else if (Splatted != Op) {
6847 return SDValue();
6848 }
6849 }
Stephen Hines36b56882014-04-23 16:57:46 -07006850
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006851 if (!Splatted) {
6852 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6853 "Can only have a splat without a constant for all undefs.");
6854 return getOperand(0);
6855 }
Stephen Hines36b56882014-04-23 16:57:46 -07006856
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006857 return Splatted;
6858}
6859
6860ConstantSDNode *
6861BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
6862 return dyn_cast_or_null<ConstantSDNode>(
6863 getSplatValue(UndefElements).getNode());
6864}
6865
6866ConstantFPSDNode *
6867BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
6868 return dyn_cast_or_null<ConstantFPSDNode>(
6869 getSplatValue(UndefElements).getNode());
Stephen Hines36b56882014-04-23 16:57:46 -07006870}
6871
6872bool BuildVectorSDNode::isConstant() const {
6873 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6874 unsigned Opc = getOperand(i).getOpcode();
6875 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6876 return false;
6877 }
6878 return true;
6879}
6880
Owen Andersone50ed302009-08-10 22:56:29 +00006881bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006882 // Find the first non-undef value in the shuffle mask.
6883 unsigned i, e;
6884 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6885 /* search */;
6886
Nate Begemana6415752009-04-29 18:13:31 +00006887 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006888
Nate Begeman5a5ca152009-04-29 05:20:52 +00006889 // Make sure all remaining elements are either undef or the same as the first
6890 // non-undef value.
6891 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006892 if (Mask[i] >= 0 && Mask[i] != Idx)
6893 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006894 return true;
6895}
David Greenecf495bc2010-01-20 20:13:31 +00006896
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006897#ifndef NDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006898static void checkForCyclesHelper(const SDNode *N,
Stephen Hines37ed9c12014-12-01 14:51:49 -08006899 SmallPtrSetImpl<const SDNode*> &Visited,
6900 SmallPtrSetImpl<const SDNode*> &Checked,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006901 const llvm::SelectionDAG *DAG) {
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006902 // If this node has already been checked, don't check it again.
6903 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006904 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006905
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006906 // If a node has already been visited on this depth-first walk, reject it as
6907 // a cycle.
Stephen Hines37ed9c12014-12-01 14:51:49 -08006908 if (!Visited.insert(N).second) {
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006909 errs() << "Detected cycle in SelectionDAG\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006910 dbgs() << "Offending node:\n";
6911 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006912 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006913 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006914
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006915 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006916 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006917
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006918 Checked.insert(N);
6919 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006920}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006921#endif
David Greenecf495bc2010-01-20 20:13:31 +00006922
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006923void llvm::checkForCycles(const llvm::SDNode *N,
6924 const llvm::SelectionDAG *DAG,
6925 bool force) {
6926#ifndef NDEBUG
6927 bool check = force;
David Greenecf495bc2010-01-20 20:13:31 +00006928#ifdef XDEBUG
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006929 check = true;
6930#endif // XDEBUG
6931 if (check) {
6932 assert(N && "Checking nonexistent SDNode");
6933 SmallPtrSet<const SDNode*, 32> visited;
6934 SmallPtrSet<const SDNode*, 32> checked;
6935 checkForCyclesHelper(N, visited, checked, DAG);
6936 }
6937#endif // !NDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006938}
6939
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006940void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6941 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greenecf495bc2010-01-20 20:13:31 +00006942}