blob: 7961e66d8c8d6892f609d74bb17d949ef718e515 [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 Hines36b56882014-04-23 16:57:46 -0700237ISD::NodeType ISD::getExtForLoadExtType(ISD::LoadExtType ExtType) {
238 switch (ExtType) {
239 case ISD::EXTLOAD:
240 return ISD::ANY_EXTEND;
241 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
Nate Begeman9008ca62009-04-27 18:41:29 +00001487 // Canonicalize all index into lhs, -> shuffle lhs, undef
1488 // Canonicalize all index into rhs, -> shuffle rhs, undef
1489 bool AllLHS = true, AllRHS = true;
1490 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001491 for (unsigned i = 0; i != NElts; ++i) {
1492 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001493 if (N2Undef)
1494 MaskVec[i] = -1;
1495 else
1496 AllLHS = false;
1497 } else if (MaskVec[i] >= 0) {
1498 AllRHS = false;
1499 }
1500 }
1501 if (AllLHS && AllRHS)
1502 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001503 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001504 N2 = getUNDEF(VT);
1505 if (AllRHS) {
1506 N1 = getUNDEF(VT);
1507 commuteShuffle(N1, N2, MaskVec);
1508 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001509 // Reset our undef status after accounting for the mask.
1510 N2Undef = N2.getOpcode() == ISD::UNDEF;
1511 // Re-check whether both sides ended up undef.
1512 if (N1.getOpcode() == ISD::UNDEF && N2Undef)
1513 return getUNDEF(VT);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001514
Craig Toppereee2a112013-08-08 08:03:12 +00001515 // If Identity shuffle return that node.
Nate Begeman9008ca62009-04-27 18:41:29 +00001516 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001517 for (unsigned i = 0; i != NElts; ++i) {
1518 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001519 }
Craig Topperad445a62013-08-09 04:37:24 +00001520 if (Identity && NElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00001521 return N1;
Nate Begeman9008ca62009-04-27 18:41:29 +00001522
Stephen Hinesdce4a402014-05-29 02:49:00 -07001523 // Shuffling a constant splat doesn't change the result.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001524 if (N2Undef) {
1525 SDValue V = N1;
1526
1527 // Look through any bitcasts. We check that these don't change the number
1528 // (and size) of elements and just changes their types.
1529 while (V.getOpcode() == ISD::BITCAST)
1530 V = V->getOperand(0);
1531
1532 // A splat should always show up as a build vector node.
1533 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
1534 BitVector UndefElements;
1535 SDValue Splat = BV->getSplatValue(&UndefElements);
1536 // If this is a splat of an undef, shuffling it is also undef.
1537 if (Splat && Splat.getOpcode() == ISD::UNDEF)
1538 return getUNDEF(VT);
1539
1540 // We only have a splat which can skip shuffles if there is a splatted
1541 // value and no undef lanes rearranged by the shuffle.
1542 if (Splat && UndefElements.none()) {
1543 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1544 // number of elements match or the value splatted is a zero constant.
1545 if (V.getValueType().getVectorNumElements() ==
1546 VT.getVectorNumElements())
1547 return N1;
1548 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1549 if (C->isNullValue())
1550 return N1;
1551 }
1552 }
1553 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001554
Nate Begeman9008ca62009-04-27 18:41:29 +00001555 FoldingSetNodeID ID;
1556 SDValue Ops[2] = { N1, N2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001557 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001558 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001559 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001560
Stephen Hinesdce4a402014-05-29 02:49:00 -07001561 void* IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001562 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001563 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001564
Nate Begeman9008ca62009-04-27 18:41:29 +00001565 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1566 // SDNode doesn't have access to it. This memory will be "leaked" when
1567 // the node is deallocated, but recovered when the NodeAllocator is released.
1568 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1569 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001570
Dan Gohman95531882010-03-18 18:49:47 +00001571 ShuffleVectorSDNode *N =
Jack Carter3af4d252013-09-09 22:02:08 +00001572 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1573 dl.getDebugLoc(), N1, N2,
1574 MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001575 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001576 InsertNode(N);
Nate Begeman9008ca62009-04-27 18:41:29 +00001577 return SDValue(N, 0);
1578}
1579
Stephen Hines37ed9c12014-12-01 14:51:49 -08001580SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1581 MVT VT = SV.getSimpleValueType(0);
1582 unsigned NumElems = VT.getVectorNumElements();
1583 SmallVector<int, 8> MaskVec;
1584
1585 for (unsigned i = 0; i != NumElems; ++i) {
1586 int Idx = SV.getMaskElt(i);
1587 if (Idx >= 0) {
1588 if (Idx < (int)NumElems)
1589 Idx += NumElems;
1590 else
1591 Idx -= NumElems;
1592 }
1593 MaskVec.push_back(Idx);
1594 }
1595
1596 SDValue Op0 = SV.getOperand(0);
1597 SDValue Op1 = SV.getOperand(1);
1598 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, &MaskVec[0]);
1599}
1600
Andrew Trickac6d9be2013-05-25 02:42:55 +00001601SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001602 SDValue Val, SDValue DTy,
1603 SDValue STy, SDValue Rnd, SDValue Sat,
1604 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001605 // If the src and dest types are the same and the conversion is between
1606 // integer types of the same sign or two floats, no conversion is necessary.
1607 if (DTy == STy &&
1608 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001609 return Val;
1610
1611 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001612 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001613 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
1614 void* IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001616 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001617
Jack Carter3af4d252013-09-09 22:02:08 +00001618 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1619 dl.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07001620 Ops, Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001621 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001622 InsertNode(N);
Dale Johannesena04b7572009-02-03 23:04:43 +00001623 return SDValue(N, 0);
1624}
1625
Owen Andersone50ed302009-08-10 22:56:29 +00001626SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001627 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001628 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001629 ID.AddInteger(RegNo);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001630 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001631 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001632 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001633
Dan Gohman95531882010-03-18 18:49:47 +00001634 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001635 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001636 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001637 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001638}
1639
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001640SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1641 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001642 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001643 ID.AddPointer(RegMask);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001644 void *IP = nullptr;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001645 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1646 return SDValue(E, 0);
1647
1648 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1649 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001650 InsertNode(N);
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001651 return SDValue(N, 0);
1652}
1653
Andrew Trickac6d9be2013-05-25 02:42:55 +00001654SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001655 FoldingSetNodeID ID;
1656 SDValue Ops[] = { Root };
Stephen Hinesdce4a402014-05-29 02:49:00 -07001657 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
Chris Lattner7561d482010-03-14 02:33:54 +00001658 ID.AddPointer(Label);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001659 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001660 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001661 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001662
Jack Carter3af4d252013-09-09 22:02:08 +00001663 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1664 dl.getDebugLoc(), Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001665 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001666 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00001667 return SDValue(N, 0);
1668}
1669
Chris Lattner7561d482010-03-14 02:33:54 +00001670
Dan Gohman46510a72010-04-15 01:51:59 +00001671SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001672 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001673 bool isTarget,
1674 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001675 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1676
1677 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001678 AddNodeIDNode(ID, Opc, getVTList(VT), None);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001679 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001680 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001681 ID.AddInteger(TargetFlags);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001682 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001683 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001684 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001685
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001686 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1687 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001688 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001689 InsertNode(N);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001690 return SDValue(N, 0);
1691}
1692
Dan Gohman475871a2008-07-27 21:46:04 +00001693SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001694 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001695 "SrcValue is not a pointer?");
1696
Jim Laskey583bd472006-10-27 23:46:08 +00001697 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001698 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
Chris Lattner61b09412006-08-11 21:01:22 +00001699 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001700
Stephen Hinesdce4a402014-05-29 02:49:00 -07001701 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001702 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001703 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001704
Dan Gohman95531882010-03-18 18:49:47 +00001705 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001706 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001707 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001708 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001709}
1710
Chris Lattnerdecc2672010-04-07 05:20:54 +00001711/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1712SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1713 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001714 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
Chris Lattnerdecc2672010-04-07 05:20:54 +00001715 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001716
Stephen Hinesdce4a402014-05-29 02:49:00 -07001717 void *IP = nullptr;
Chris Lattnerdecc2672010-04-07 05:20:54 +00001718 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1719 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001720
Chris Lattnerdecc2672010-04-07 05:20:54 +00001721 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1722 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001723 InsertNode(N);
Chris Lattnerdecc2672010-04-07 05:20:54 +00001724 return SDValue(N, 0);
1725}
1726
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001727/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1728SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1729 unsigned SrcAS, unsigned DestAS) {
1730 SDValue Ops[] = {Ptr};
1731 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001732 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001733 ID.AddInteger(SrcAS);
1734 ID.AddInteger(DestAS);
1735
Stephen Hinesdce4a402014-05-29 02:49:00 -07001736 void *IP = nullptr;
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001737 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1738 return SDValue(E, 0);
1739
1740 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1741 dl.getDebugLoc(),
1742 VT, Ptr, SrcAS, DestAS);
1743 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001744 InsertNode(N);
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001745 return SDValue(N, 0);
1746}
Chris Lattnerdecc2672010-04-07 05:20:54 +00001747
Duncan Sands92abc622009-01-31 15:50:11 +00001748/// getShiftAmountOperand - Return the specified value casted to
1749/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001750SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001751 EVT OpTy = Op.getValueType();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001752 EVT ShTy = TLI->getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001753 if (OpTy == ShTy || OpTy.isVector()) return Op;
1754
1755 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001756 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001757}
1758
Chris Lattner37ce9df2007-10-15 17:47:20 +00001759/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1760/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001761SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001762 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001763 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001764 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001765 unsigned StackAlign =
Bill Wendlingba54bca2013-06-19 21:36:55 +00001766 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
David Greene3f2bf852009-11-12 20:49:22 +00001768 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001769 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner37ce9df2007-10-15 17:47:20 +00001770}
1771
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001772/// CreateStackTemporary - Create a stack temporary suitable for holding
1773/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001774SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001775 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1776 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001777 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1778 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001779 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001780 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1781 TD->getPrefTypeAlignment(Ty2));
1782
1783 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001784 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001785 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001786}
1787
Owen Andersone50ed302009-08-10 22:56:29 +00001788SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001789 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001790 // These setcc operations always fold.
1791 switch (Cond) {
1792 default: break;
1793 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001794 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001795 case ISD::SETTRUE:
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001796 case ISD::SETTRUE2: {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001797 TargetLowering::BooleanContent Cnt =
1798 TLI->getBooleanContents(N1->getValueType(0));
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001799 return getConstant(
1800 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1801 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001802
Chris Lattnera83385f2006-04-27 05:01:07 +00001803 case ISD::SETOEQ:
1804 case ISD::SETOGT:
1805 case ISD::SETOGE:
1806 case ISD::SETOLT:
1807 case ISD::SETOLE:
1808 case ISD::SETONE:
1809 case ISD::SETO:
1810 case ISD::SETUO:
1811 case ISD::SETUEQ:
1812 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001813 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001814 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001815 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001816
Gabor Greifba36cb52008-08-28 21:40:38 +00001817 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001818 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001819 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001820 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001821
Chris Lattnerc3aae252005-01-07 07:46:32 +00001822 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001823 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001824 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1825 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001826 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1827 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1828 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1829 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1830 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1831 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1832 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1833 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001834 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001835 }
Chris Lattner67255a12005-04-07 18:14:58 +00001836 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001837 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1838 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001839 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001840 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001841 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001842 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001843 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001844 // fall through
1845 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001846 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001847 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001848 // fall through
1849 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001850 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001851 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001852 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001853 // fall through
1854 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001855 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001856 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001857 // fall through
1858 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001859 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001860 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001861 // fall through
1862 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001863 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001864 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001865 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001866 // fall through
1867 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001868 R==APFloat::cmpEqual, VT);
1869 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1870 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1871 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1872 R==APFloat::cmpEqual, VT);
1873 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1874 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1875 R==APFloat::cmpLessThan, VT);
1876 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1877 R==APFloat::cmpUnordered, VT);
1878 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1879 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001880 }
1881 } else {
1882 // Ensure that the constant occurs on the RHS.
Tom Stellard12d43f92013-09-28 02:50:38 +00001883 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1884 MVT CompVT = N1.getValueType().getSimpleVT();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001885 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
Tom Stellard12d43f92013-09-28 02:50:38 +00001886 return SDValue();
1887
1888 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001889 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001890 }
1891
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001892 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001893 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001894}
1895
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001896/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1897/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001898bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001899 // This predicate is not safe for vector operations.
1900 if (Op.getValueType().isVector())
1901 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001902
Dan Gohman87862e72009-12-11 21:31:27 +00001903 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001904 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1905}
1906
Dan Gohmanea859be2007-06-22 14:59:07 +00001907/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1908/// this predicate to simplify operations downstream. Mask is known to be zero
1909/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001910bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001911 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001912 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001913 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001914 return (KnownZero & Mask) == Mask;
1915}
1916
Stephen Hinesdce4a402014-05-29 02:49:00 -07001917/// Determine which bits of Op are known to be either zero or one and return
1918/// them in the KnownZero/KnownOne bitsets.
1919void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
1920 APInt &KnownOne, unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001921 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001922
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001923 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001924 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001925 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001926
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001927 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001928
1929 switch (Op.getOpcode()) {
1930 case ISD::Constant:
1931 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001932 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1933 KnownZero = ~KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001934 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001935 case ISD::AND:
1936 // If either the LHS or the RHS are Zero, the result is zero.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001937 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1938 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001939
1940 // Output known-1 bits are only known if set in both the LHS & RHS.
1941 KnownOne &= KnownOne2;
1942 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1943 KnownZero |= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001944 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001945 case ISD::OR:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001946 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1947 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001948
Dan Gohmanea859be2007-06-22 14:59:07 +00001949 // Output known-0 bits are only known if clear in both the LHS & RHS.
1950 KnownZero &= KnownZero2;
1951 // Output known-1 are known to be set if set in either the LHS | RHS.
1952 KnownOne |= KnownOne2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001953 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001954 case ISD::XOR: {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001955 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1956 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Dan Gohmanea859be2007-06-22 14:59:07 +00001958 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001959 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001960 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1961 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1962 KnownZero = KnownZeroOut;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001963 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001964 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001965 case ISD::MUL: {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001966 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1967 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001968
1969 // If low bits are zero in either operand, output low known-0 bits.
1970 // Also compute a conserative estimate for high known-0 bits.
1971 // More trickiness is possible, but this is sufficient for the
1972 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001973 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001974 unsigned TrailZ = KnownZero.countTrailingOnes() +
1975 KnownZero2.countTrailingOnes();
1976 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001977 KnownZero2.countLeadingOnes(),
1978 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001979
1980 TrailZ = std::min(TrailZ, BitWidth);
1981 LeadZ = std::min(LeadZ, BitWidth);
1982 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1983 APInt::getHighBitsSet(BitWidth, LeadZ);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001984 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00001985 }
1986 case ISD::UDIV: {
1987 // For the purposes of computing leading zeros we can conservatively
1988 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001989 // be less than the denominator.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001990 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001991 unsigned LeadZ = KnownZero2.countLeadingOnes();
1992
Jay Foad7a874dd2010-12-01 08:53:58 +00001993 KnownOne2.clearAllBits();
1994 KnownZero2.clearAllBits();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001995 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001996 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1997 if (RHSUnknownLeadingOnes != BitWidth)
1998 LeadZ = std::min(BitWidth,
1999 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002000
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002001 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002002 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00002003 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002004 case ISD::SELECT:
Stephen Hinesdce4a402014-05-29 02:49:00 -07002005 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2006 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002007
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 // Only known if known in both the LHS and RHS.
2009 KnownOne &= KnownOne2;
2010 KnownZero &= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002011 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002012 case ISD::SELECT_CC:
Stephen Hinesdce4a402014-05-29 02:49:00 -07002013 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2014 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002015
Dan Gohmanea859be2007-06-22 14:59:07 +00002016 // Only known if known in both the LHS and RHS.
2017 KnownOne &= KnownOne2;
2018 KnownZero &= KnownZero2;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002019 break;
Bill Wendling253174b2008-11-22 07:24:01 +00002020 case ISD::SADDO:
2021 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002022 case ISD::SSUBO:
2023 case ISD::USUBO:
2024 case ISD::SMULO:
2025 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002026 if (Op.getResNo() != 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002027 break;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002028 // The boolean result conforms to getBooleanContents.
2029 // If we know the result of a setcc has the top bits zero, use this info.
2030 // We know that we have an integer-based boolean since these operations
2031 // are only available for integer.
2032 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2033 TargetLowering::ZeroOrOneBooleanContent &&
2034 BitWidth > 1)
2035 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2036 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002037 case ISD::SETCC:
2038 // If we know the result of a setcc has the top bits zero, use this info.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002039 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2040 TargetLowering::ZeroOrOneBooleanContent &&
2041 BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002042 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002043 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002044 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002045 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002046 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002047 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00002048
2049 // If the shift count is an invalid immediate, don't do anything.
2050 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002051 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002052
Stephen Hinesdce4a402014-05-29 02:49:00 -07002053 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmand4cf9922008-02-26 18:50:50 +00002054 KnownZero <<= ShAmt;
2055 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002056 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00002057 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002058 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002059 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002060 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002061 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002062 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002063 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002064
Dan Gohmand4cf9922008-02-26 18:50:50 +00002065 // If the shift count is an invalid immediate, don't do anything.
2066 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002067 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002068
Stephen Hinesdce4a402014-05-29 02:49:00 -07002069 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002070 KnownZero = KnownZero.lshr(ShAmt);
2071 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002072
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002073 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00002074 KnownZero |= HighBits; // High bits known zero.
2075 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002076 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002077 case ISD::SRA:
2078 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002079 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002080
Dan Gohmand4cf9922008-02-26 18:50:50 +00002081 // If the shift count is an invalid immediate, don't do anything.
2082 if (ShAmt >= BitWidth)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002083 break;
Dan Gohmand4cf9922008-02-26 18:50:50 +00002084
Dan Gohmanea859be2007-06-22 14:59:07 +00002085 // If any of the demanded bits are produced by the sign extension, we also
2086 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002087 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002088
Stephen Hinesdce4a402014-05-29 02:49:00 -07002089 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002090 KnownZero = KnownZero.lshr(ShAmt);
2091 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00002092
Dan Gohmanea859be2007-06-22 14:59:07 +00002093 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002094 APInt SignBit = APInt::getSignBit(BitWidth);
2095 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00002096
Dan Gohmanca93a432008-02-20 16:30:17 +00002097 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002098 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00002099 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002100 KnownOne |= HighBits; // New bits are known one.
2101 }
2102 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002103 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002104 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002105 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002106 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002107
2108 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00002109 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002110 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002111
Dan Gohman977a76f2008-02-13 22:28:48 +00002112 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002113 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00002114
Dan Gohmanea859be2007-06-22 14:59:07 +00002115 // If the sign extended bits are demanded, we know that the sign
2116 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00002117 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00002118 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002119 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00002120
Stephen Hinesdce4a402014-05-29 02:49:00 -07002121 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002122 KnownOne &= InputDemandedBits;
2123 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002124
Dan Gohmanea859be2007-06-22 14:59:07 +00002125 // If the sign bit of the input is known set or clear, then we know the
2126 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00002127 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00002128 KnownZero |= NewBits;
2129 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00002130 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00002131 KnownOne |= NewBits;
2132 KnownZero &= ~NewBits;
2133 } else { // Input sign bit unknown
2134 KnownZero &= ~NewBits;
2135 KnownOne &= ~NewBits;
2136 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002137 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002138 }
2139 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002140 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002142 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00002143 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002144 unsigned LowBits = Log2_32(BitWidth)+1;
2145 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00002146 KnownOne.clearAllBits();
Stephen Hinesdce4a402014-05-29 02:49:00 -07002147 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002148 }
2149 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00002150 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem77451752013-03-20 22:53:44 +00002151 // If this is a ZEXTLoad and we are looking at the loaded value.
2152 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00002153 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002154 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002155 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00002156 } else if (const MDNode *Ranges = LD->getRanges()) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002157 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002158 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002159 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002160 }
2161 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002162 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002163 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002164 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00002165 KnownZero = KnownZero.trunc(InBits);
2166 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002167 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002168 KnownZero = KnownZero.zext(BitWidth);
2169 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002170 KnownZero |= NewBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002171 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002172 }
2173 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002174 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002175 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002176 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002177
Jay Foad40f8f622010-12-07 08:25:19 +00002178 KnownZero = KnownZero.trunc(InBits);
2179 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002180 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00002181
2182 // Note if the sign bit is known to be zero or one.
2183 bool SignBitKnownZero = KnownZero.isNegative();
2184 bool SignBitKnownOne = KnownOne.isNegative();
Dan Gohman977a76f2008-02-13 22:28:48 +00002185
Jay Foad40f8f622010-12-07 08:25:19 +00002186 KnownZero = KnownZero.zext(BitWidth);
2187 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002188
Dan Gohman977a76f2008-02-13 22:28:48 +00002189 // If the sign bit is known zero or one, the top bits match.
2190 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00002191 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00002192 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00002193 KnownOne |= NewBits;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002194 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002195 }
2196 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00002197 EVT InVT = Op.getOperand(0).getValueType();
2198 unsigned InBits = InVT.getScalarType().getSizeInBits();
2199 KnownZero = KnownZero.trunc(InBits);
2200 KnownOne = KnownOne.trunc(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002201 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng2766a472012-12-06 19:13:27 +00002202 KnownZero = KnownZero.zext(BitWidth);
2203 KnownOne = KnownOne.zext(BitWidth);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002204 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002205 }
2206 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00002207 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002208 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00002209 KnownZero = KnownZero.zext(InBits);
2210 KnownOne = KnownOne.zext(InBits);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002211 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002212 KnownZero = KnownZero.trunc(BitWidth);
2213 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002214 break;
2215 }
2216 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002217 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002218 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Stephen Hinesdce4a402014-05-29 02:49:00 -07002219 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002220 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002221 KnownOne &= (~KnownZero);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002222 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002223 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002224 case ISD::FGETSIGN:
2225 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002226 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002227 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002228
Dan Gohman23e8b712008-04-28 17:02:21 +00002229 case ISD::SUB: {
2230 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2231 // We know that the top bits of C-X are clear if X contains less bits
2232 // than C (i.e. no wrap-around can happen). For example, 20-X is
2233 // positive if we can prove that X is >= 0 and < 16.
2234 if (CLHS->getAPIntValue().isNonNegative()) {
2235 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2236 // NLZ can't be BitWidth with no sign bit
2237 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002238 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002239
2240 // If all of the MaskV bits are known to be zero, then we know the
2241 // output top bits are zero, because we now know that the output is
2242 // from [0-C].
2243 if ((KnownZero2 & MaskV) == MaskV) {
2244 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2245 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002246 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002247 }
2248 }
2249 }
2250 }
2251 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002252 case ISD::ADD:
2253 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002254 // Output known-0 bits are known if clear or set in both the low clear bits
2255 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2256 // low 3 bits clear.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002257 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002258 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2259
Stephen Hinesdce4a402014-05-29 02:49:00 -07002260 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002261 KnownZeroOut = std::min(KnownZeroOut,
2262 KnownZero2.countTrailingOnes());
2263
Chris Lattnerda605882010-12-19 20:38:28 +00002264 if (Op.getOpcode() == ISD::ADD) {
2265 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002266 break;
Chris Lattnerda605882010-12-19 20:38:28 +00002267 }
2268
2269 // With ADDE, a carry bit may be added in, so we can only use this
2270 // information if we know (at least) that the low two bits are clear. We
2271 // then return to the caller that the low bit is unknown but that other bits
2272 // are known zero.
2273 if (KnownZeroOut >= 2) // ADDE
2274 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002275 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002276 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002277 case ISD::SREM:
2278 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002279 const APInt &RA = Rem->getAPIntValue().abs();
2280 if (RA.isPowerOf2()) {
2281 APInt LowBits = RA - 1;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002282 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002283
Duncan Sands5c2873a2010-01-29 09:45:26 +00002284 // The low bits of the first operand are unchanged by the srem.
2285 KnownZero = KnownZero2 & LowBits;
2286 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002287
Duncan Sands5c2873a2010-01-29 09:45:26 +00002288 // If the first operand is non-negative or has all low bits zero, then
2289 // the upper bits are all zero.
2290 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2291 KnownZero |= ~LowBits;
2292
2293 // If the first operand is negative and not all low bits are zero, then
2294 // the upper bits are all one.
2295 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2296 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002297 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002298 }
2299 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002300 break;
Dan Gohman23e8b712008-04-28 17:02:21 +00002301 case ISD::UREM: {
2302 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002303 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002304 if (RA.isPowerOf2()) {
2305 APInt LowBits = (RA - 1);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002306 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2307
2308 // The upper bits are all zero, the lower ones are unchanged.
2309 KnownZero = KnownZero2 | ~LowBits;
2310 KnownOne = KnownOne2 & LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002311 break;
2312 }
2313 }
2314
2315 // Since the result is less than or equal to either operand, any leading
2316 // zero bits in either operand must also exist in the result.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002317 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2318 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002319
2320 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2321 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002322 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002323 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002324 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002325 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002326 case ISD::FrameIndex:
2327 case ISD::TargetFrameIndex:
2328 if (unsigned Align = InferPtrAlignment(Op)) {
2329 // The low bits are known zero if the pointer is aligned.
2330 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002331 break;
Chris Lattner0a9481f2011-02-13 22:25:43 +00002332 }
2333 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002334
Dan Gohmanea859be2007-06-22 14:59:07 +00002335 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002336 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2337 break;
2338 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002339 case ISD::INTRINSIC_WO_CHAIN:
2340 case ISD::INTRINSIC_W_CHAIN:
2341 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002342 // Allow the target to implement this method for its nodes.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002343 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
2344 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002345 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07002346
2347 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002348}
2349
2350/// ComputeNumSignBits - Return the number of times the sign bit of the
2351/// register is replicated into the other bits. We know that at least 1 bit
2352/// is always equal to the sign bit (itself), but other cases can give us
2353/// information. For example, immediately after an "SRA X, 2", we know that
2354/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002355unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002356 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002357 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002358 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002359 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002360 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002361
Dan Gohmanea859be2007-06-22 14:59:07 +00002362 if (Depth == 6)
2363 return 1; // Limit search depth.
2364
2365 switch (Op.getOpcode()) {
2366 default: break;
2367 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002368 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002369 return VTBits-Tmp+1;
2370 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002372 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002373
Dan Gohmanea859be2007-06-22 14:59:07 +00002374 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002375 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002376 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002377 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002378
Dan Gohmanea859be2007-06-22 14:59:07 +00002379 case ISD::SIGN_EXTEND:
Jack Carter3af4d252013-09-09 22:02:08 +00002380 Tmp =
2381 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002382 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002383
Dan Gohmanea859be2007-06-22 14:59:07 +00002384 case ISD::SIGN_EXTEND_INREG:
2385 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002386 Tmp =
2387 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002388 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002389
Dan Gohmanea859be2007-06-22 14:59:07 +00002390 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2391 return std::max(Tmp, Tmp2);
2392
2393 case ISD::SRA:
2394 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2395 // SRA X, C -> adds C sign bits.
2396 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002397 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002398 if (Tmp > VTBits) Tmp = VTBits;
2399 }
2400 return Tmp;
2401 case ISD::SHL:
2402 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2403 // shl destroys sign bits.
2404 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002405 if (C->getZExtValue() >= VTBits || // Bad shift.
2406 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2407 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002408 }
2409 break;
2410 case ISD::AND:
2411 case ISD::OR:
2412 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002413 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002414 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002415 if (Tmp != 1) {
2416 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2417 FirstAnswer = std::min(Tmp, Tmp2);
2418 // We computed what we know about the sign bits as our first
2419 // answer. Now proceed to the generic code that uses
Stephen Hinesdce4a402014-05-29 02:49:00 -07002420 // computeKnownBits, and pick whichever answer is better.
Dan Gohmana332f172008-05-23 02:28:01 +00002421 }
2422 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002423
2424 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002425 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002426 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002427 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002428 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002429
2430 case ISD::SADDO:
2431 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002432 case ISD::SSUBO:
2433 case ISD::USUBO:
2434 case ISD::SMULO:
2435 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002436 if (Op.getResNo() != 1)
2437 break;
Duncan Sands03228082008-11-23 15:47:28 +00002438 // The boolean result conforms to getBooleanContents. Fall through.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002439 // If setcc returns 0/-1, all bits are sign bits.
2440 // We know that we have an integer-based boolean since these operations
2441 // are only available for integer.
2442 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2443 TargetLowering::ZeroOrNegativeOneBooleanContent)
2444 return VTBits;
2445 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002446 case ISD::SETCC:
2447 // If setcc returns 0/-1, all bits are sign bits.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07002448 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002449 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002450 return VTBits;
2451 break;
2452 case ISD::ROTL:
2453 case ISD::ROTR:
2454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002455 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002456
Dan Gohmanea859be2007-06-22 14:59:07 +00002457 // Handle rotate right by N like a rotate left by 32-N.
2458 if (Op.getOpcode() == ISD::ROTR)
2459 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2460
2461 // If we aren't rotating out all of the known-in sign bits, return the
2462 // number that are left. This handles rotl(sext(x), 1) for example.
2463 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2464 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2465 }
2466 break;
2467 case ISD::ADD:
2468 // Add can have at most one carry bit. Thus we know that the output
2469 // is, at worst, one more bit than the inputs.
2470 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2471 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002472
Dan Gohmanea859be2007-06-22 14:59:07 +00002473 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002474 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002475 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002476 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002477 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002478
Dan Gohmanea859be2007-06-22 14:59:07 +00002479 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2480 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002481 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002482 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002483
Dan Gohmanea859be2007-06-22 14:59:07 +00002484 // If we are subtracting one from a positive number, there is no carry
2485 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002486 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002487 return Tmp;
2488 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002489
Dan Gohmanea859be2007-06-22 14:59:07 +00002490 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2491 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002492 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002493
Dan Gohmanea859be2007-06-22 14:59:07 +00002494 case ISD::SUB:
2495 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2496 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002497
Dan Gohmanea859be2007-06-22 14:59:07 +00002498 // Handle NEG.
2499 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002500 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002501 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002502 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002503 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2504 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002505 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002506 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002507
Dan Gohmanea859be2007-06-22 14:59:07 +00002508 // If the input is known to be positive (the sign bit is known clear),
2509 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002510 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002511 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002512
Dan Gohmanea859be2007-06-22 14:59:07 +00002513 // Otherwise, we treat this like a SUB.
2514 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002515
Dan Gohmanea859be2007-06-22 14:59:07 +00002516 // Sub can have at most one carry bit. Thus we know that the output
2517 // is, at worst, one more bit than the inputs.
2518 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2519 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002520 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002521 case ISD::TRUNCATE:
2522 // FIXME: it's tricky to do anything useful for this, but it is an important
2523 // case for targets like X86.
2524 break;
2525 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002526
Nadav Rotem77451752013-03-20 22:53:44 +00002527 // If we are looking at the loaded value of the SDNode.
2528 if (Op.getResNo() == 0) {
2529 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2530 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2531 unsigned ExtType = LD->getExtensionType();
2532 switch (ExtType) {
2533 default: break;
2534 case ISD::SEXTLOAD: // '17' bits known
2535 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2536 return VTBits-Tmp+1;
2537 case ISD::ZEXTLOAD: // '16' bits known
2538 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2539 return VTBits-Tmp;
2540 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002541 }
2542 }
2543
2544 // Allow the target to implement this method for its nodes.
2545 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002546 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002547 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2548 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002549 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002550 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002551 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002552
Dan Gohmanea859be2007-06-22 14:59:07 +00002553 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2554 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002555 APInt KnownZero, KnownOne;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002556 computeKnownBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002557
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002558 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002559 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002560 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002561 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002562 Mask = KnownOne;
2563 } else {
2564 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002565 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002566 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002567
Dan Gohmanea859be2007-06-22 14:59:07 +00002568 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2569 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002570 Mask = ~Mask;
2571 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002572 // Return # leading zeros. We use 'min' here in case Val was zero before
2573 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002574 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002575}
2576
Chris Lattner0a9481f2011-02-13 22:25:43 +00002577/// isBaseWithConstantOffset - Return true if the specified operand is an
2578/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2579/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2580/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002581/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002582bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2583 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2584 !isa<ConstantSDNode>(Op.getOperand(1)))
2585 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002586
2587 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002588 !MaskedValueIsZero(Op.getOperand(0),
2589 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2590 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002591
Chris Lattner0a9481f2011-02-13 22:25:43 +00002592 return true;
2593}
2594
2595
Dan Gohman8d44b282009-09-03 20:34:31 +00002596bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2597 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002598 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002599 return true;
2600
2601 // If the value is a constant, we can obviously see if it is a NaN or not.
2602 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2603 return !C->getValueAPF().isNaN();
2604
2605 // TODO: Recognize more cases here.
2606
2607 return false;
2608}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002609
Dan Gohmane8326932010-02-24 06:52:40 +00002610bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2611 // If the value is a constant, we can obviously see if it is a zero or not.
2612 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2613 return !C->isZero();
2614
2615 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002616 switch (Op.getOpcode()) {
2617 default: break;
2618 case ISD::OR:
2619 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2620 return !C->isNullValue();
2621 break;
2622 }
Dan Gohmane8326932010-02-24 06:52:40 +00002623
2624 return false;
2625}
2626
2627bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2628 // Check the obvious case.
2629 if (A == B) return true;
2630
2631 // For for negative and positive zero.
2632 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2633 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2634 if (CA->isZero() && CB->isZero()) return true;
2635
2636 // Otherwise they may not be equal.
2637 return false;
2638}
2639
Chris Lattnerc3aae252005-01-07 07:46:32 +00002640/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002641///
Andrew Trickac6d9be2013-05-25 02:42:55 +00002642SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002643 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07002644 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
2645 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002646 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002647 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002648
Jack Carter3af4d252013-09-09 22:02:08 +00002649 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2650 DL.getDebugLoc(), getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002651 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002652
Stephen Hines37ed9c12014-12-01 14:51:49 -08002653 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00002654 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002655}
2656
Andrew Trickac6d9be2013-05-25 02:42:55 +00002657SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002658 EVT VT, SDValue Operand) {
Stephen Hines36b56882014-04-23 16:57:46 -07002659 // Constant fold unary operations with an integer constant operand. Even
2660 // opaque constant will be folded, because the folding of unary operations
2661 // doesn't create new constants with different values. Nevertheless, the
2662 // opaque flag is preserved during folding to prevent future folding with
2663 // other constants.
Gabor Greifba36cb52008-08-28 21:40:38 +00002664 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002665 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002666 switch (Opcode) {
2667 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002668 case ISD::SIGN_EXTEND:
Stephen Hines36b56882014-04-23 16:57:46 -07002669 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT,
2670 C->isTargetOpcode(), C->isOpaque());
Chris Lattner4ed11b42005-09-02 00:17:32 +00002671 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002672 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002673 case ISD::TRUNCATE:
Stephen Hines36b56882014-04-23 16:57:46 -07002674 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT,
2675 C->isTargetOpcode(), C->isOpaque());
Dale Johannesen73328d12007-09-19 23:55:34 +00002676 case ISD::UINT_TO_FP:
2677 case ISD::SINT_TO_FP: {
Tim Northover0a29cb02013-01-22 09:46:31 +00002678 APFloat apf(EVTToAPFloatSemantics(VT),
2679 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002680 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002681 Opcode==ISD::SINT_TO_FP,
2682 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002683 return getConstantFP(apf, VT);
2684 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002685 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002686 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover0a29cb02013-01-22 09:46:31 +00002687 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002688 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover0a29cb02013-01-22 09:46:31 +00002689 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002690 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002691 case ISD::BSWAP:
Stephen Hines36b56882014-04-23 16:57:46 -07002692 return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(),
2693 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002694 case ISD::CTPOP:
Stephen Hines36b56882014-04-23 16:57:46 -07002695 return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(),
2696 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002697 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002698 case ISD::CTLZ_ZERO_UNDEF:
Stephen Hines36b56882014-04-23 16:57:46 -07002699 return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(),
2700 C->isOpaque());
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002701 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002702 case ISD::CTTZ_ZERO_UNDEF:
Stephen Hines36b56882014-04-23 16:57:46 -07002703 return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(),
2704 C->isOpaque());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002705 }
2706 }
2707
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002708 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002709 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002710 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002711 switch (Opcode) {
2712 case ISD::FNEG:
2713 V.changeSign();
2714 return getConstantFP(V, VT);
2715 case ISD::FABS:
2716 V.clearSign();
2717 return getConstantFP(V, VT);
2718 case ISD::FCEIL: {
2719 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2720 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002721 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002722 break;
2723 }
2724 case ISD::FTRUNC: {
2725 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2726 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002727 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002728 break;
2729 }
2730 case ISD::FFLOOR: {
2731 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2732 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002733 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002734 break;
2735 }
2736 case ISD::FP_EXTEND: {
2737 bool ignored;
2738 // This can return overflow, underflow, or inexact; we don't care.
2739 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00002740 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigande669c932012-10-29 18:35:49 +00002741 APFloat::rmNearestTiesToEven, &ignored);
2742 return getConstantFP(V, VT);
2743 }
2744 case ISD::FP_TO_SINT:
2745 case ISD::FP_TO_UINT: {
2746 integerPart x[2];
2747 bool ignored;
Stephen Hines37ed9c12014-12-01 14:51:49 -08002748 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
Ulrich Weigande669c932012-10-29 18:35:49 +00002749 // FIXME need to be more flexible about rounding mode.
2750 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2751 Opcode==ISD::FP_TO_SINT,
2752 APFloat::rmTowardZero, &ignored);
2753 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002754 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002755 APInt api(VT.getSizeInBits(), x);
2756 return getConstant(api, VT);
2757 }
2758 case ISD::BITCAST:
2759 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2760 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2761 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2762 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2763 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002764 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002765 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002766
Stephen Hines37ed9c12014-12-01 14:51:49 -08002767 // Constant fold unary operations with a vector integer operand.
2768 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand.getNode())) {
2769 if (BV->isConstant()) {
2770 switch (Opcode) {
2771 default:
2772 // FIXME: Entirely reasonable to perform folding of other unary
2773 // operations here as the need arises.
2774 break;
2775 case ISD::UINT_TO_FP:
2776 case ISD::SINT_TO_FP: {
2777 SmallVector<SDValue, 8> Ops;
2778 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
2779 SDValue OpN = BV->getOperand(i);
2780 // Let the above scalar folding handle the conversion of each
2781 // element.
2782 OpN = getNode(ISD::SINT_TO_FP, DL, VT.getVectorElementType(),
2783 OpN);
2784 Ops.push_back(OpN);
2785 }
2786 return getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
2787 }
2788 }
2789 }
2790 }
2791
Gabor Greifba36cb52008-08-28 21:40:38 +00002792 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002793 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002794 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002795 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002796 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002797 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002798 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002799 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002800 assert(VT.isFloatingPoint() &&
2801 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002802 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002803 assert((!VT.isVector() ||
2804 VT.getVectorNumElements() ==
2805 Operand.getValueType().getVectorNumElements()) &&
2806 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002807 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002808 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002809 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002810 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002811 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002812 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002813 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002814 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2815 "Invalid sext node, dst < src!");
2816 assert((!VT.isVector() ||
2817 VT.getVectorNumElements() ==
2818 Operand.getValueType().getVectorNumElements()) &&
2819 "Vector element count mismatch!");
Nadav Rotem0c8607b2013-01-20 08:35:56 +00002820 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2821 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2822 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002823 // sext(undef) = 0, because the top bits will all be the same.
2824 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002825 break;
2826 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002827 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002828 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002829 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002830 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2831 "Invalid zext node, dst < src!");
2832 assert((!VT.isVector() ||
2833 VT.getVectorNumElements() ==
2834 Operand.getValueType().getVectorNumElements()) &&
2835 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002836 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002837 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002838 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002839 else if (OpOpcode == ISD::UNDEF)
2840 // zext(undef) = 0, because the top bits will be zero.
2841 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002842 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002843 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002844 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002845 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002846 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002847 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2848 "Invalid anyext node, dst < src!");
2849 assert((!VT.isVector() ||
2850 VT.getVectorNumElements() ==
2851 Operand.getValueType().getVectorNumElements()) &&
2852 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002853
Dan Gohman9e86a732010-06-18 00:08:30 +00002854 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2855 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002856 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002857 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002858 else if (OpOpcode == ISD::UNDEF)
2859 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002860
2861 // (ext (trunx x)) -> x
2862 if (OpOpcode == ISD::TRUNCATE) {
2863 SDValue OpOp = Operand.getNode()->getOperand(0);
2864 if (OpOp.getValueType() == VT)
2865 return OpOp;
2866 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002867 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002868 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002869 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002870 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002871 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002872 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2873 "Invalid truncate node, src < dst!");
2874 assert((!VT.isVector() ||
2875 VT.getVectorNumElements() ==
2876 Operand.getValueType().getVectorNumElements()) &&
2877 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002878 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002879 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002880 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2881 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002882 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002883 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2884 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002885 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002886 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002887 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002888 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002889 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002890 if (OpOpcode == ISD::UNDEF)
2891 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002892 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002893 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002894 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002895 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002896 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002897 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002898 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2899 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002900 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002901 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002902 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002903 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002904 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002905 (VT.getVectorElementType() == Operand.getValueType() ||
2906 (VT.getVectorElementType().isInteger() &&
2907 Operand.getValueType().isInteger() &&
2908 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002909 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002910 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002911 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002912 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2913 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2914 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2915 Operand.getConstantOperandVal(1) == 0 &&
2916 Operand.getOperand(0).getValueType() == VT)
2917 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002918 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002919 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002920 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002921 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002922 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002923 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002924 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002925 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002926 break;
2927 case ISD::FABS:
2928 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002929 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002930 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002931 }
2932
Chris Lattner43247a12005-08-25 19:12:10 +00002933 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002934 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002935 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002936 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002937 SDValue Ops[1] = { Operand };
Stephen Hinesdce4a402014-05-29 02:49:00 -07002938 AddNodeIDNode(ID, Opcode, VTs, Ops);
2939 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002940 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002941 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002942
Jack Carter3af4d252013-09-09 22:02:08 +00002943 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2944 DL.getDebugLoc(), VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002945 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002946 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00002947 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2948 DL.getDebugLoc(), VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002949 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002950
Stephen Hines37ed9c12014-12-01 14:51:49 -08002951 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00002952 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002953}
2954
Benjamin Kramer49693102013-02-04 15:19:18 +00002955SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2956 SDNode *Cst1, SDNode *Cst2) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002957 // If the opcode is a target-specific ISD node, there's nothing we can
2958 // do here and the operand rules may not line up with the below, so
2959 // bail early.
2960 if (Opcode >= ISD::BUILTIN_OP_END)
2961 return SDValue();
2962
Benjamin Kramer49693102013-02-04 15:19:18 +00002963 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2964 SmallVector<SDValue, 4> Outputs;
2965 EVT SVT = VT.getScalarType();
Bill Wendling688d1c42008-09-24 10:16:24 +00002966
Benjamin Kramer49693102013-02-04 15:19:18 +00002967 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2968 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
Stephen Hines36b56882014-04-23 16:57:46 -07002969 if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque()))
2970 return SDValue();
2971
2972 if (Scalar1 && Scalar2)
Benjamin Kramer49693102013-02-04 15:19:18 +00002973 // Scalar instruction.
2974 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
Stephen Hines36b56882014-04-23 16:57:46 -07002975 else {
Benjamin Kramer49693102013-02-04 15:19:18 +00002976 // For vectors extract each constant element into Inputs so we can constant
2977 // fold them individually.
2978 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2979 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2980 if (!BV1 || !BV2)
2981 return SDValue();
2982
2983 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2984
2985 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2986 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2987 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2988 if (!V1 || !V2) // Not a constant, bail.
2989 return SDValue();
2990
Stephen Hines36b56882014-04-23 16:57:46 -07002991 if (V1->isOpaque() || V2->isOpaque())
2992 return SDValue();
2993
Benjamin Kramer49693102013-02-04 15:19:18 +00002994 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2995 // FIXME: This is valid and could be handled by truncating the APInts.
2996 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2997 return SDValue();
2998
2999 Inputs.push_back(std::make_pair(V1, V2));
3000 }
Bill Wendling688d1c42008-09-24 10:16:24 +00003001 }
3002
Benjamin Kramer49693102013-02-04 15:19:18 +00003003 // We have a number of constant values, constant fold them element by element.
3004 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
3005 const APInt &C1 = Inputs[I].first->getAPIntValue();
3006 const APInt &C2 = Inputs[I].second->getAPIntValue();
3007
3008 switch (Opcode) {
3009 case ISD::ADD:
3010 Outputs.push_back(getConstant(C1 + C2, SVT));
3011 break;
3012 case ISD::SUB:
3013 Outputs.push_back(getConstant(C1 - C2, SVT));
3014 break;
3015 case ISD::MUL:
3016 Outputs.push_back(getConstant(C1 * C2, SVT));
3017 break;
3018 case ISD::UDIV:
3019 if (!C2.getBoolValue())
3020 return SDValue();
3021 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
3022 break;
3023 case ISD::UREM:
3024 if (!C2.getBoolValue())
3025 return SDValue();
3026 Outputs.push_back(getConstant(C1.urem(C2), SVT));
3027 break;
3028 case ISD::SDIV:
3029 if (!C2.getBoolValue())
3030 return SDValue();
3031 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
3032 break;
3033 case ISD::SREM:
3034 if (!C2.getBoolValue())
3035 return SDValue();
3036 Outputs.push_back(getConstant(C1.srem(C2), SVT));
3037 break;
3038 case ISD::AND:
3039 Outputs.push_back(getConstant(C1 & C2, SVT));
3040 break;
3041 case ISD::OR:
3042 Outputs.push_back(getConstant(C1 | C2, SVT));
3043 break;
3044 case ISD::XOR:
3045 Outputs.push_back(getConstant(C1 ^ C2, SVT));
3046 break;
3047 case ISD::SHL:
3048 Outputs.push_back(getConstant(C1 << C2, SVT));
3049 break;
3050 case ISD::SRL:
3051 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
3052 break;
3053 case ISD::SRA:
3054 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
3055 break;
3056 case ISD::ROTL:
3057 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
3058 break;
3059 case ISD::ROTR:
3060 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
3061 break;
3062 default:
3063 return SDValue();
3064 }
3065 }
3066
Stephen Hinesdce4a402014-05-29 02:49:00 -07003067 assert((Scalar1 && Scalar2) || (VT.getVectorNumElements() == Outputs.size() &&
3068 "Expected a scalar or vector!"));
3069
Benjamin Kramer49693102013-02-04 15:19:18 +00003070 // Handle the scalar case first.
Stephen Hinesdce4a402014-05-29 02:49:00 -07003071 if (!VT.isVector())
Benjamin Kramer49693102013-02-04 15:19:18 +00003072 return Outputs.back();
3073
Stephen Hinesdce4a402014-05-29 02:49:00 -07003074 // We may have a vector type but a scalar result. Create a splat.
3075 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3076
3077 // Build a big vector out of the scalar elements we generated.
3078 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);
Bill Wendling688d1c42008-09-24 10:16:24 +00003079}
3080
Andrew Trickac6d9be2013-05-25 02:42:55 +00003081SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003082 SDValue N2, bool nuw, bool nsw, bool exact) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003083 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
3084 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00003085 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003086 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00003087 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00003088 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3089 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003090 // Fold trivial token factors.
3091 if (N1.getOpcode() == ISD::EntryToken) return N2;
3092 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00003093 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00003094 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003095 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00003096 // Concat of UNDEFs is UNDEF.
3097 if (N1.getOpcode() == ISD::UNDEF &&
3098 N2.getOpcode() == ISD::UNDEF)
3099 return getUNDEF(VT);
3100
Dan Gohman7f8613e2008-08-14 20:04:46 +00003101 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3102 // one big BUILD_VECTOR.
3103 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3104 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003105 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3106 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003107 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07003108 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman7f8613e2008-08-14 20:04:46 +00003109 }
3110 break;
Chris Lattner76365122005-01-16 02:23:22 +00003111 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00003112 assert(VT.isInteger() && "This operator does not apply to FP types!");
3113 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003114 N1.getValueType() == VT && "Binary operator types must match!");
3115 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3116 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00003117 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003118 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00003119 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3120 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003121 break;
Chris Lattner76365122005-01-16 02:23:22 +00003122 case ISD::OR:
3123 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00003124 case ISD::ADD:
3125 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00003126 assert(VT.isInteger() && "This operator does not apply to FP types!");
3127 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003128 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00003129 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3130 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00003131 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003132 return N1;
3133 break;
Chris Lattner76365122005-01-16 02:23:22 +00003134 case ISD::UDIV:
3135 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00003136 case ISD::MULHU:
3137 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00003138 case ISD::MUL:
3139 case ISD::SDIV:
3140 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00003141 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00003142 assert(N1.getValueType() == N2.getValueType() &&
3143 N1.getValueType() == VT && "Binary operator types must match!");
3144 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003145 case ISD::FADD:
3146 case ISD::FSUB:
3147 case ISD::FMUL:
3148 case ISD::FDIV:
3149 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003150 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00003151 if (Opcode == ISD::FADD) {
3152 // 0+x --> x
3153 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
3154 if (CFP->getValueAPF().isZero())
3155 return N2;
3156 // x+0 --> x
3157 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3158 if (CFP->getValueAPF().isZero())
3159 return N1;
3160 } else if (Opcode == ISD::FSUB) {
3161 // x-0 --> x
3162 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
3163 if (CFP->getValueAPF().isZero())
3164 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00003165 } else if (Opcode == ISD::FMUL) {
3166 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
3167 SDValue V = N2;
3168
3169 // If the first operand isn't the constant, try the second
3170 if (!CFP) {
3171 CFP = dyn_cast<ConstantFPSDNode>(N2);
3172 V = N1;
3173 }
3174
3175 if (CFP) {
3176 // 0*x --> 0
3177 if (CFP->isZero())
3178 return SDValue(CFP,0);
3179 // 1*x --> x
3180 if (CFP->isExactlyValue(1.0))
3181 return V;
3182 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00003183 }
Dan Gohman760f86f2009-01-22 21:58:43 +00003184 }
Dale Johannesen78995512010-05-15 18:38:02 +00003185 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00003186 assert(N1.getValueType() == N2.getValueType() &&
3187 N1.getValueType() == VT && "Binary operator types must match!");
3188 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003189 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3190 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003191 N1.getValueType().isFloatingPoint() &&
3192 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00003193 "Invalid FCOPYSIGN!");
3194 break;
Chris Lattner76365122005-01-16 02:23:22 +00003195 case ISD::SHL:
3196 case ISD::SRA:
3197 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00003198 case ISD::ROTL:
3199 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00003200 assert(VT == N1.getValueType() &&
3201 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003202 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00003203 "Shifts only work on integers");
Michael Liaoa6b20ce2013-03-01 18:40:30 +00003204 assert((!VT.isVector() || VT == N2.getValueType()) &&
3205 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere0751182011-02-13 19:09:16 +00003206 // Verify that the shift amount VT is bit enough to hold valid shift
3207 // amounts. This catches things like trying to shift an i1024 value by an
3208 // i8, which is easy to fall into in generic code that uses
3209 // TLI.getShiftAmount().
3210 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00003211 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00003212 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00003213
3214 // Always fold shifts of i1 values so the code generator doesn't need to
3215 // handle them. Since we know the size of the shift has to be less than the
3216 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00003218 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00003219 if (N2C && N2C->isNullValue())
3220 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00003221 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00003222 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003223 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003224 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003225 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003226 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00003227 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003228 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003229 "type is vector!");
3230 assert((!EVT.isVector() ||
3231 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3232 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003233 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00003234 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003235 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00003236 break;
3237 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003238 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003239 assert(VT.isFloatingPoint() &&
3240 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003241 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00003242 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003243 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00003244 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00003245 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003246 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00003247 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003248 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003249 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003250 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00003251 assert(!EVT.isVector() &&
3252 "AssertSExt/AssertZExt type should be the vector element type "
3253 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003254 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00003255 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003256 break;
3257 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003258 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003259 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003260 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003261 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003262 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00003263 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003264 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003265 "type is vector!");
3266 assert((!EVT.isVector() ||
3267 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3268 "Vector element counts must match in SIGN_EXTEND_INREG");
3269 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003270 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00003271
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003272 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00003273 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003274 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00003275 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00003276 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00003277 return getConstant(Val, VT);
3278 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003279 break;
3280 }
3281 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00003282 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3283 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00003284 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003285
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003286 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3287 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00003288 if (N2C &&
3289 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003290 N1.getNumOperands() > 0) {
3291 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00003292 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003293 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003294 N1.getOperand(N2C->getZExtValue() / Factor),
3295 getConstant(N2C->getZExtValue() % Factor,
3296 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003297 }
3298
3299 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3300 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00003301 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3302 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00003303
3304 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00003305 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00003306 // are promoted and implicitly truncated, and the result implicitly
3307 // extended. Make that explicit here.
3308 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00003309
Bob Wilsonb1303d02009-04-13 22:05:19 +00003310 return Elt;
3311 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003312
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003313 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3314 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00003315 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00003316 // If the indices are the same, return the inserted element else
3317 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00003318 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00003319 SDValue N1Op2 = N1.getOperand(2);
3320 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3321
3322 if (N1Op2C && N2C) {
3323 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3324 if (VT == N1.getOperand(1).getValueType())
3325 return N1.getOperand(1);
3326 else
3327 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3328 }
3329
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003330 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00003331 }
Dan Gohman6ab64222008-08-13 21:51:37 +00003332 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003333 break;
3334 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003335 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00003336 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3337 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00003338 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00003339 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00003340
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003341 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3342 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00003343 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003344 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003345 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00003346
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003347 // EXTRACT_ELEMENT of a constant int is also very common.
3348 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003349 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003350 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00003351 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3352 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003353 }
3354 break;
David Greene91585092011-01-26 15:38:49 +00003355 case ISD::EXTRACT_SUBVECTOR: {
3356 SDValue Index = N2;
3357 if (VT.isSimple() && N1.getValueType().isSimple()) {
3358 assert(VT.isVector() && N1.getValueType().isVector() &&
3359 "Extract subvector VTs must be a vectors!");
Jack Carter3af4d252013-09-09 22:02:08 +00003360 assert(VT.getVectorElementType() ==
3361 N1.getValueType().getVectorElementType() &&
David Greene91585092011-01-26 15:38:49 +00003362 "Extract subvector VTs must have the same element type!");
Craig Topper0ff11902013-08-15 02:44:19 +00003363 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greene91585092011-01-26 15:38:49 +00003364 "Extract subvector must be from larger vector to smaller vector!");
3365
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003366 if (isa<ConstantSDNode>(Index.getNode())) {
3367 assert((VT.getVectorNumElements() +
3368 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003369 <= N1.getValueType().getVectorNumElements())
3370 && "Extract subvector overflow!");
3371 }
3372
3373 // Trivial extraction.
Craig Topper0ff11902013-08-15 02:44:19 +00003374 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greene91585092011-01-26 15:38:49 +00003375 return N1;
3376 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003377 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003378 }
David Greene91585092011-01-26 15:38:49 +00003379 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003380
Benjamin Kramer49693102013-02-04 15:19:18 +00003381 // Perform trivial constant folding.
3382 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3383 if (SV.getNode()) return SV;
3384
3385 // Canonicalize constant to RHS if commutative.
3386 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3387 std::swap(N1C, N2C);
3388 std::swap(N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003389 }
3390
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003391 // Constant fold FP operations.
Stephen Hines37ed9c12014-12-01 14:51:49 -08003392 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
Gabor Greifba36cb52008-08-28 21:40:38 +00003393 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3394 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003395 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003396 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer49693102013-02-04 15:19:18 +00003397 // Canonicalize constant to RHS if commutative.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003398 std::swap(N1CFP, N2CFP);
3399 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003400 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003401 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3402 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003403 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003404 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003405 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003406 if (!HasFPExceptions || s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003407 return getConstantFP(V1, VT);
3408 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003409 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003410 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003411 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003412 return getConstantFP(V1, VT);
3413 break;
3414 case ISD::FMUL:
3415 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003416 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003417 return getConstantFP(V1, VT);
3418 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003419 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003420 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003421 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3422 s!=APFloat::opDivByZero)) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003423 return getConstantFP(V1, VT);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003424 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003425 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003426 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003427 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003428 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3429 s!=APFloat::opDivByZero)) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003430 return getConstantFP(V1, VT);
Stephen Hines37ed9c12014-12-01 14:51:49 -08003431 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003432 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003433 case ISD::FCOPYSIGN:
3434 V1.copySign(V2);
3435 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003436 default: break;
3437 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003438 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003439
3440 if (Opcode == ISD::FP_ROUND) {
3441 APFloat V = N1CFP->getValueAPF(); // make copy
3442 bool ignored;
3443 // This can return overflow, underflow, or inexact; we don't care.
3444 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00003445 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson06886aa2012-04-10 22:46:53 +00003446 APFloat::rmNearestTiesToEven, &ignored);
3447 return getConstantFP(V, VT);
3448 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003449 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003450
Chris Lattner62b57722006-04-20 05:39:12 +00003451 // Canonicalize an UNDEF to the RHS, even over a constant.
3452 if (N1.getOpcode() == ISD::UNDEF) {
3453 if (isCommutativeBinOp(Opcode)) {
3454 std::swap(N1, N2);
3455 } else {
3456 switch (Opcode) {
3457 case ISD::FP_ROUND_INREG:
3458 case ISD::SIGN_EXTEND_INREG:
3459 case ISD::SUB:
3460 case ISD::FSUB:
3461 case ISD::FDIV:
3462 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003463 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003464 return N1; // fold op(undef, arg2) -> undef
3465 case ISD::UDIV:
3466 case ISD::SDIV:
3467 case ISD::UREM:
3468 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003469 case ISD::SRL:
3470 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003471 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003472 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3473 // For vectors, we can't easily build an all zero vector, just return
3474 // the LHS.
3475 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003476 }
3477 }
3478 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003479
3480 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003481 if (N2.getOpcode() == ISD::UNDEF) {
3482 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003483 case ISD::XOR:
3484 if (N1.getOpcode() == ISD::UNDEF)
3485 // Handle undef ^ undef -> 0 special case. This is a common
3486 // idiom (misuse).
3487 return getConstant(0, VT);
3488 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003489 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003490 case ISD::ADDC:
3491 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003492 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003493 case ISD::UDIV:
3494 case ISD::SDIV:
3495 case ISD::UREM:
3496 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003497 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003498 case ISD::FADD:
3499 case ISD::FSUB:
3500 case ISD::FMUL:
3501 case ISD::FDIV:
3502 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003503 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003504 return N2;
3505 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003506 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003507 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003508 case ISD::SRL:
3509 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003510 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003511 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3512 // For vectors, we can't easily build an all zero vector, just return
3513 // the LHS.
3514 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003515 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003516 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003517 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003518 // For vectors, we can't easily build an all one vector, just return
3519 // the LHS.
3520 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003521 case ISD::SRA:
3522 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003523 }
3524 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003525
Chris Lattner27e9b412005-05-11 18:57:39 +00003526 // Memoize this node if possible.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003527 BinarySDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003528 SDVTList VTs = getVTList(VT);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003529 const bool BinOpHasFlags = isBinOpWithFlags(Opcode);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003530 if (VT != MVT::Glue) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003531 SDValue Ops[] = {N1, N2};
Jim Laskey583bd472006-10-27 23:46:08 +00003532 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003533 AddNodeIDNode(ID, Opcode, VTs, Ops);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003534 if (BinOpHasFlags)
3535 AddBinaryNodeIDCustom(ID, Opcode, nuw, nsw, exact);
Stephen Hinesdce4a402014-05-29 02:49:00 -07003536 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003538 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003539
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003540 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
3541
Chris Lattnera5682852006-08-07 23:03:03 +00003542 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003543 } else {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003544
3545 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, nuw, nsw, exact);
Chris Lattner27e9b412005-05-11 18:57:39 +00003546 }
3547
Stephen Hines37ed9c12014-12-01 14:51:49 -08003548 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003549 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003550}
3551
Andrew Trickac6d9be2013-05-25 02:42:55 +00003552SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003553 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003554 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003555 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003556 switch (Opcode) {
Owen Anderson58dcd202013-05-09 22:27:13 +00003557 case ISD::FMA: {
3558 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3559 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3560 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3561 if (N1CFP && N2CFP && N3CFP) {
3562 APFloat V1 = N1CFP->getValueAPF();
3563 const APFloat &V2 = N2CFP->getValueAPF();
3564 const APFloat &V3 = N3CFP->getValueAPF();
3565 APFloat::opStatus s =
3566 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3567 if (s != APFloat::opInvalidOp)
3568 return getConstantFP(V1, VT);
3569 }
3570 break;
3571 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00003572 case ISD::CONCAT_VECTORS:
3573 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3574 // one big BUILD_VECTOR.
3575 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3576 N2.getOpcode() == ISD::BUILD_VECTOR &&
3577 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003578 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3579 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003580 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3581 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Stephen Hinesdce4a402014-05-29 02:49:00 -07003582 return getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
Dan Gohman7f8613e2008-08-14 20:04:46 +00003583 }
3584 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003585 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003586 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003587 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003588 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003589 break;
3590 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003591 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003592 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003593 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003594 return N2; // select true, X, Y -> X
3595 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003596 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003597
3598 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003599 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003600 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003601 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003602 case ISD::INSERT_SUBVECTOR: {
3603 SDValue Index = N3;
3604 if (VT.isSimple() && N1.getValueType().isSimple()
3605 && N2.getValueType().isSimple()) {
3606 assert(VT.isVector() && N1.getValueType().isVector() &&
3607 N2.getValueType().isVector() &&
3608 "Insert subvector VTs must be a vectors");
3609 assert(VT == N1.getValueType() &&
3610 "Dest and insert subvector source types must match!");
Craig Topper0ff11902013-08-15 02:44:19 +00003611 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenecfe33c42011-01-26 19:13:22 +00003612 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003613 if (isa<ConstantSDNode>(Index.getNode())) {
3614 assert((N2.getValueType().getVectorNumElements() +
3615 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003616 <= VT.getVectorNumElements())
3617 && "Insert subvector overflow!");
3618 }
3619
3620 // Trivial insertion.
Craig Topper0ff11902013-08-15 02:44:19 +00003621 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenecfe33c42011-01-26 19:13:22 +00003622 return N2;
3623 }
3624 break;
3625 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003626 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003627 // Fold bit_convert nodes from a type to themselves.
3628 if (N1.getValueType() == VT)
3629 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003630 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003631 }
3632
Chris Lattner43247a12005-08-25 19:12:10 +00003633 // Memoize node if it doesn't produce a flag.
3634 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003635 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003636 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003637 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003638 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003639 AddNodeIDNode(ID, Opcode, VTs, Ops);
3640 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003641 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003642 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003643
Jack Carter3af4d252013-09-09 22:02:08 +00003644 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3645 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003646 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003647 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003648 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3649 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003650 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003651
Stephen Hines37ed9c12014-12-01 14:51:49 -08003652 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003653 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003654}
3655
Andrew Trickac6d9be2013-05-25 02:42:55 +00003656SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003657 SDValue N1, SDValue N2, SDValue N3,
3658 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003659 SDValue Ops[] = { N1, N2, N3, N4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003660 return getNode(Opcode, DL, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003661}
3662
Andrew Trickac6d9be2013-05-25 02:42:55 +00003663SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003664 SDValue N1, SDValue N2, SDValue N3,
3665 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003666 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07003667 return getNode(Opcode, DL, VT, Ops);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003668}
3669
Dan Gohman98ca4f22009-08-05 01:29:28 +00003670/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3671/// the incoming stack arguments to be loaded from the stack.
3672SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3673 SmallVector<SDValue, 8> ArgChains;
3674
3675 // Include the original chain at the beginning of the list. When this is
3676 // used by target LowerCall hooks, this helps legalize find the
3677 // CALLSEQ_BEGIN node.
3678 ArgChains.push_back(Chain);
3679
3680 // Add a chain value for each stack argument.
3681 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3682 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3683 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3684 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3685 if (FI->getIndex() < 0)
3686 ArgChains.push_back(SDValue(L, 1));
3687
3688 // Build a tokenfactor for all the chains.
Stephen Hinesdce4a402014-05-29 02:49:00 -07003689 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003690}
3691
Dan Gohman707e0182008-04-12 04:36:06 +00003692/// getMemsetValue - Vectorized representation of the memset value
3693/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003694static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003695 SDLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003696 assert(Value.getOpcode() != ISD::UNDEF);
3697
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003698 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003699 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003700 assert(C->getAPIntValue().getBitWidth() == 8);
3701 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands83ec4b62008-06-06 12:08:01 +00003702 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003703 return DAG.getConstant(Val, VT);
Tim Northover0a29cb02013-01-22 09:46:31 +00003704 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003705 }
Evan Chengf0df0312008-05-15 08:39:06 +00003706
Dale Johannesen0f502f62009-02-03 22:26:09 +00003707 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003708 if (NumBits > 8) {
3709 // Use a multiplication with 0x010101... to extend the input to the
3710 // required length.
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003711 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003712 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003713 }
3714
3715 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003716}
3717
Dan Gohman707e0182008-04-12 04:36:06 +00003718/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3719/// used when a memcpy is turned into a memset when the source is a constant
3720/// string ptr.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003721static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003722 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003723 // Handle vector with all elements zero.
3724 if (Str.empty()) {
3725 if (VT.isInteger())
3726 return DAG.getConstant(0, VT);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07003727 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
Evan Cheng255f20f2010-04-01 06:04:33 +00003728 return DAG.getConstantFP(0.0, VT);
3729 else if (VT.isVector()) {
3730 unsigned NumElts = VT.getVectorNumElements();
3731 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003732 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003733 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3734 EltVT, NumElts)));
3735 } else
3736 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003737 }
3738
Duncan Sands83ec4b62008-06-06 12:08:01 +00003739 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Cheng4ff23d02013-01-10 22:13:27 +00003740 unsigned NumVTBits = VT.getSizeInBits();
3741 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattner18c7f802012-02-05 02:29:43 +00003742 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3743
Evan Cheng4ff23d02013-01-10 22:13:27 +00003744 APInt Val(NumVTBits, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003745 if (TLI.isLittleEndian()) {
3746 for (unsigned i = 0; i != NumBytes; ++i)
3747 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3748 } else {
3749 for (unsigned i = 0; i != NumBytes; ++i)
3750 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003751 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003752
Stephen Hines36b56882014-04-23 16:57:46 -07003753 // If the "cost" of materializing the integer immediate is less than the cost
3754 // of a load, then it is cost effective to turn the load into the immediate.
3755 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3756 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
Evan Cheng376642e2012-12-10 23:21:26 +00003757 return DAG.getConstant(Val, VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07003758 return SDValue(nullptr, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003759}
3760
Scott Michelfdc40a02009-02-17 22:15:04 +00003761/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003762///
Andrew Trickdd0fb012013-05-25 03:08:10 +00003763static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman707e0182008-04-12 04:36:06 +00003764 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003765 EVT VT = Base.getValueType();
Andrew Trickdd0fb012013-05-25 03:08:10 +00003766 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003767 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003768}
3769
Evan Chengf0df0312008-05-15 08:39:06 +00003770/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3771///
Chris Lattner18c7f802012-02-05 02:29:43 +00003772static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003773 unsigned SrcDelta = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -07003774 GlobalAddressSDNode *G = nullptr;
Evan Chengf0df0312008-05-15 08:39:06 +00003775 if (Src.getOpcode() == ISD::GlobalAddress)
3776 G = cast<GlobalAddressSDNode>(Src);
3777 else if (Src.getOpcode() == ISD::ADD &&
3778 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3779 Src.getOperand(1).getOpcode() == ISD::Constant) {
3780 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003781 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003782 }
3783 if (!G)
3784 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003785
Chris Lattner18c7f802012-02-05 02:29:43 +00003786 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003787}
Dan Gohman707e0182008-04-12 04:36:06 +00003788
Evan Cheng255f20f2010-04-01 06:04:33 +00003789/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3790/// to replace the memset / memcpy. Return true if the number of memory ops
3791/// is below the threshold. It returns the types of the sequence of
3792/// memory ops to perform memset / memcpy by reference.
3793static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003794 unsigned Limit, uint64_t Size,
3795 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003796 bool IsMemset,
3797 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003798 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003799 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003800 SelectionDAG &DAG,
3801 const TargetLowering &TLI) {
3802 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3803 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003804 // If 'SrcAlign' is zero, that means the memory operation does not need to
3805 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3806 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3807 // is the specified alignment of the memory operation. If it is zero, that
3808 // means it's possible to change the alignment of the destination.
3809 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3810 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003811 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003812 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003813 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003814
Chris Lattneracee6472010-03-07 07:45:08 +00003815 if (VT == MVT::Other) {
Stephen Hines36b56882014-04-23 16:57:46 -07003816 unsigned AS = 0;
3817 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) ||
Stephen Hines37ed9c12014-12-01 14:51:49 -08003818 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003819 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003820 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003821 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 case 0: VT = MVT::i64; break;
3823 case 4: VT = MVT::i32; break;
3824 case 2: VT = MVT::i16; break;
3825 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003826 }
3827 }
3828
Owen Anderson766b5ef2009-08-11 21:59:30 +00003829 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003830 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003831 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003832 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003833
Duncan Sands8e4eb092008-06-08 20:54:56 +00003834 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003835 VT = LVT;
3836 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003837
Dan Gohman707e0182008-04-12 04:36:06 +00003838 unsigned NumMemOps = 0;
3839 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003840 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003841 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003842 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003843 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003844 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003845
3846 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003847 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003848 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003849 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003850 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003851 Found = true;
3852 else if (NewVT == MVT::i64 &&
3853 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003854 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003855 // i64 is usually not legal on 32-bit targets, but f64 may be.
3856 NewVT = MVT::f64;
3857 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003858 }
Evan Cheng376642e2012-12-10 23:21:26 +00003859 }
3860
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003861 if (!Found) {
3862 do {
3863 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3864 if (NewVT == MVT::i8)
3865 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003866 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003867 }
3868 NewVTSize = NewVT.getSizeInBits() / 8;
3869
Evan Cheng376642e2012-12-10 23:21:26 +00003870 // If the new VT cannot cover all of the remaining bits, then consider
3871 // issuing a (or a pair of) unaligned and overlapping load / store.
3872 // FIXME: Only does this for 64-bit or more since we don't have proper
3873 // cost model for unaligned load / store.
3874 bool Fast;
Stephen Hines36b56882014-04-23 16:57:46 -07003875 unsigned AS = 0;
Evan Chenga16e49d2012-12-12 20:43:23 +00003876 if (NumMemOps && AllowOverlap &&
3877 VTSize >= 8 && NewVTSize < Size &&
Stephen Hines37ed9c12014-12-01 14:51:49 -08003878 TLI.allowsMisalignedMemoryAccesses(VT, AS, DstAlign, &Fast) && Fast)
Evan Cheng376642e2012-12-10 23:21:26 +00003879 VTSize = Size;
3880 else {
3881 VT = NewVT;
3882 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003883 }
Dan Gohman707e0182008-04-12 04:36:06 +00003884 }
Dan Gohman707e0182008-04-12 04:36:06 +00003885
Evan Chenga16e49d2012-12-12 20:43:23 +00003886 if (++NumMemOps > Limit)
3887 return false;
3888
Dan Gohman707e0182008-04-12 04:36:06 +00003889 MemOps.push_back(VT);
3890 Size -= VTSize;
3891 }
3892
3893 return true;
3894}
3895
Andrew Trickac6d9be2013-05-25 02:42:55 +00003896static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003897 SDValue Chain, SDValue Dst,
3898 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003899 unsigned Align, bool isVol,
3900 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003901 MachinePointerInfo DstPtrInfo,
3902 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003903 // Turn a memcpy of undef to nop.
3904 if (Src.getOpcode() == ISD::UNDEF)
3905 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003906
Dan Gohman21323f32008-05-29 19:42:22 +00003907 // Expand memcpy to a series of load and store ops if the size operand falls
3908 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003909 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3910 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003911 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003912 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003913 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003914 MachineFunction &MF = DAG.getMachineFunction();
3915 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003916 bool OptSize =
Bill Wendling831737d2012-12-30 10:32:01 +00003917 MF.getFunction()->getAttributes().
3918 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003919 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3920 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3921 DstAlignCanChange = true;
3922 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3923 if (Align > SrcAlign)
3924 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003925 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003926 bool CopyFromStr = isMemSrcFromString(Src, Str);
3927 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003928 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003929
Evan Cheng94107ba2010-04-01 18:19:11 +00003930 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003931 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003932 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00003933 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003934 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003935
Evan Cheng255f20f2010-04-01 06:04:33 +00003936 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003937 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003938 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hames2d95e432013-01-31 20:23:43 +00003939
3940 // Don't promote to an alignment that would require dynamic stack
Stephen Lin155615d2013-07-08 00:37:03 +00003941 // realignment.
Stephen Hines37ed9c12014-12-01 14:51:49 -08003942 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Lang Hames2d95e432013-01-31 20:23:43 +00003943 if (!TRI->needsStackRealignment(MF))
3944 while (NewAlign > Align &&
3945 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3946 NewAlign /= 2;
3947
Evan Cheng255f20f2010-04-01 06:04:33 +00003948 if (NewAlign > Align) {
3949 // Give the stack frame object a larger alignment if needed.
3950 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3951 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3952 Align = NewAlign;
3953 }
3954 }
Dan Gohman707e0182008-04-12 04:36:06 +00003955
Dan Gohman475871a2008-07-27 21:46:04 +00003956 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003957 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003958 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003959 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003960 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003961 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003962 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003963
Evan Cheng376642e2012-12-10 23:21:26 +00003964 if (VTSize > Size) {
3965 // Issuing an unaligned load / store pair that overlaps with the previous
3966 // pair. Adjust the offset accordingly.
3967 assert(i == NumMemOps-1 && i != 0);
3968 SrcOff -= VTSize - Size;
3969 DstOff -= VTSize - Size;
3970 }
3971
Evan Cheng255f20f2010-04-01 06:04:33 +00003972 if (CopyFromStr &&
3973 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003974 // It's unlikely a store of a vector immediate can be done in a single
3975 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003976 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003977 // FIXME: Handle other cases where store of vector immediate is done in
3978 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003979 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003980 if (Value.getNode())
3981 Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003982 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng376642e2012-12-10 23:21:26 +00003983 DstPtrInfo.getWithOffset(DstOff), isVol,
3984 false, Align);
3985 }
3986
3987 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003988 // The type might not be legal for the target. This should only happen
3989 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003990 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3991 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003992 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003993 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003994 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003995 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003996 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003997 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Stephen Hines37ed9c12014-12-01 14:51:49 -08003998 false, MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003999 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004000 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004001 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4002 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00004003 }
4004 OutChains.push_back(Store);
4005 SrcOff += VTSize;
4006 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00004007 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00004008 }
4009
Stephen Hinesdce4a402014-05-29 02:49:00 -07004010 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman707e0182008-04-12 04:36:06 +00004011}
4012
Andrew Trickac6d9be2013-05-25 02:42:55 +00004013static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00004014 SDValue Chain, SDValue Dst,
4015 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004016 unsigned Align, bool isVol,
4017 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004018 MachinePointerInfo DstPtrInfo,
4019 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00004020 // Turn a memmove of undef to nop.
4021 if (Src.getOpcode() == ISD::UNDEF)
4022 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00004023
4024 // Expand memmove to a series of load and store ops if the size operand falls
4025 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00004026 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004027 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00004028 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00004029 MachineFunction &MF = DAG.getMachineFunction();
4030 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00004031 bool OptSize = MF.getFunction()->getAttributes().
4032 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004033 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4034 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4035 DstAlignCanChange = true;
4036 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4037 if (Align > SrcAlign)
4038 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00004039 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004040
Evan Cheng94107ba2010-04-01 18:19:11 +00004041 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00004042 (DstAlignCanChange ? 0 : Align), SrcAlign,
4043 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00004044 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00004045
Evan Cheng255f20f2010-04-01 06:04:33 +00004046 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004047 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00004048 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00004049 if (NewAlign > Align) {
4050 // Give the stack frame object a larger alignment if needed.
4051 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4052 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4053 Align = NewAlign;
4054 }
4055 }
Dan Gohman21323f32008-05-29 19:42:22 +00004056
Evan Cheng255f20f2010-04-01 06:04:33 +00004057 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004058 SmallVector<SDValue, 8> LoadValues;
4059 SmallVector<SDValue, 8> LoadChains;
4060 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00004061 unsigned NumMemOps = MemOps.size();
4062 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004063 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00004064 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00004065 SDValue Value;
Dan Gohman21323f32008-05-29 19:42:22 +00004066
Dale Johannesen0f502f62009-02-03 22:26:09 +00004067 Value = DAG.getLoad(VT, dl, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004068 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004069 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004070 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00004071 LoadValues.push_back(Value);
4072 LoadChains.push_back(Value.getValue(1));
4073 SrcOff += VTSize;
4074 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07004075 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
Dan Gohman21323f32008-05-29 19:42:22 +00004076 OutChains.clear();
4077 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004078 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00004079 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00004080 SDValue Store;
Dan Gohman21323f32008-05-29 19:42:22 +00004081
Dale Johannesen0f502f62009-02-03 22:26:09 +00004082 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Trickdd0fb012013-05-25 03:08:10 +00004083 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004084 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00004085 OutChains.push_back(Store);
4086 DstOff += VTSize;
4087 }
4088
Stephen Hinesdce4a402014-05-29 02:49:00 -07004089 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman21323f32008-05-29 19:42:22 +00004090}
4091
Serge Pavlov496f0242013-09-17 16:24:42 +00004092/// \brief Lower the call to 'memset' intrinsic function into a series of store
4093/// operations.
4094///
4095/// \param DAG Selection DAG where lowered code is placed.
4096/// \param dl Link to corresponding IR location.
4097/// \param Chain Control flow dependency.
4098/// \param Dst Pointer to destination memory location.
4099/// \param Src Value of byte to write into the memory.
4100/// \param Size Number of bytes to write.
4101/// \param Align Alignment of the destination in bytes.
4102/// \param isVol True if destination is volatile.
4103/// \param DstPtrInfo IR information on the memory pointer.
4104/// \returns New head in the control flow, if lowering was successful, empty
4105/// SDValue otherwise.
4106///
4107/// The function tries to replace 'llvm.memset' intrinsic with several store
4108/// operations and value calculation code. This is usually profitable for small
4109/// memory size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004110static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00004111 SDValue Chain, SDValue Dst,
4112 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004113 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004114 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00004115 // Turn a memset of undef to nop.
4116 if (Src.getOpcode() == ISD::UNDEF)
4117 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00004118
4119 // Expand memset to a series of load/store ops if the size operand
4120 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00004121 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004122 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00004123 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00004124 MachineFunction &MF = DAG.getMachineFunction();
4125 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00004126 bool OptSize = MF.getFunction()->getAttributes().
4127 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00004128 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4129 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4130 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00004131 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00004132 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00004133 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00004134 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00004135 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00004136 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00004137
Evan Cheng255f20f2010-04-01 06:04:33 +00004138 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00004139 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00004140 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00004141 if (NewAlign > Align) {
4142 // Give the stack frame object a larger alignment if needed.
4143 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4144 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4145 Align = NewAlign;
4146 }
4147 }
4148
Dan Gohman475871a2008-07-27 21:46:04 +00004149 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00004150 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00004151 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00004152
4153 // Find the largest store and generate the bit pattern for it.
4154 EVT LargestVT = MemOps[0];
4155 for (unsigned i = 1; i < NumMemOps; i++)
4156 if (MemOps[i].bitsGT(LargestVT))
4157 LargestVT = MemOps[i];
4158 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4159
Dan Gohman707e0182008-04-12 04:36:06 +00004160 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00004161 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00004162 unsigned VTSize = VT.getSizeInBits() / 8;
4163 if (VTSize > Size) {
4164 // Issuing an unaligned load / store pair that overlaps with the previous
4165 // pair. Adjust the offset accordingly.
4166 assert(i == NumMemOps-1 && i != 0);
4167 DstOff -= VTSize - Size;
4168 }
Benjamin Kramer80220362011-01-02 19:57:05 +00004169
4170 // If this store is smaller than the largest store see whether we can get
4171 // the smaller value for free with a truncate.
4172 SDValue Value = MemSetValue;
4173 if (VT.bitsLT(LargestVT)) {
4174 if (!LargestVT.isVector() && !VT.isVector() &&
4175 TLI.isTruncateFree(LargestVT, VT))
4176 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4177 else
4178 Value = getMemsetValue(Src, VT, DAG, dl);
4179 }
4180 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004181 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00004182 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00004183 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00004184 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00004185 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00004186 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00004187 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00004188 }
4189
Stephen Hinesdce4a402014-05-29 02:49:00 -07004190 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
Dan Gohman707e0182008-04-12 04:36:06 +00004191}
4192
Andrew Trickac6d9be2013-05-25 02:42:55 +00004193SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004194 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004195 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004196 MachinePointerInfo DstPtrInfo,
4197 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004198 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004199
4200 // Check to see if we should lower the memcpy to loads and stores first.
4201 // For cases within the target-specified limits, this is the best choice.
4202 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4203 if (ConstantSize) {
4204 // Memcpy with size zero? Just return the original chain.
4205 if (ConstantSize->isNullValue())
4206 return Chain;
4207
Evan Cheng255f20f2010-04-01 06:04:33 +00004208 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4209 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00004210 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004211 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004212 return Result;
4213 }
4214
4215 // Then check to see if we should lower the memcpy with target-specific
4216 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004217 SDValue Result =
Stephen Hines37ed9c12014-12-01 14:51:49 -08004218 TSI->EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
4219 isVol, AlwaysInline, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004220 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004221 return Result;
4222
4223 // If we really need inline code and the target declined to provide it,
4224 // use a (potentially long) sequence of loads and stores.
4225 if (AlwaysInline) {
4226 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004227 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004228 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004229 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00004230 }
4231
Dan Gohman7b55d362010-04-05 20:24:08 +00004232 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4233 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4234 // respect volatile, so they may do things like read or write memory
4235 // beyond the given memory regions. But fixing this isn't easy, and most
4236 // people don't care.
4237
Dan Gohman707e0182008-04-12 04:36:06 +00004238 // Emit a library call.
4239 TargetLowering::ArgListTy Args;
4240 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004241 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004242 Entry.Node = Dst; Args.push_back(Entry);
4243 Entry.Node = Src; Args.push_back(Entry);
4244 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004245 // FIXME: pass in SDLoc
Stephen Hinesdce4a402014-05-29 02:49:00 -07004246 TargetLowering::CallLoweringInfo CLI(*this);
4247 CLI.setDebugLoc(dl).setChain(Chain)
4248 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4249 Type::getVoidTy(*getContext()),
4250 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004251 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004252 .setDiscardResult();
Bill Wendlingba54bca2013-06-19 21:36:55 +00004253 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004254
Dan Gohman707e0182008-04-12 04:36:06 +00004255 return CallResult.second;
4256}
4257
Andrew Trickac6d9be2013-05-25 02:42:55 +00004258SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004259 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004260 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004261 MachinePointerInfo DstPtrInfo,
4262 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004263 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004264
Dan Gohman21323f32008-05-29 19:42:22 +00004265 // Check to see if we should lower the memmove to loads and stores first.
4266 // For cases within the target-specified limits, this is the best choice.
4267 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4268 if (ConstantSize) {
4269 // Memmove with size zero? Just return the original chain.
4270 if (ConstantSize->isNullValue())
4271 return Chain;
4272
Dan Gohman475871a2008-07-27 21:46:04 +00004273 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00004274 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004275 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004276 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004277 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00004278 return Result;
4279 }
Dan Gohman707e0182008-04-12 04:36:06 +00004280
4281 // Then check to see if we should lower the memmove with target-specific
4282 // code. If the target chooses to do this, this is the next best.
Stephen Hines37ed9c12014-12-01 14:51:49 -08004283 SDValue Result = TSI->EmitTargetCodeForMemmove(
4284 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004285 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004286 return Result;
4287
Mon P Wang01f0e852010-04-06 08:27:51 +00004288 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4289 // not be safe. See memcpy above for more details.
4290
Dan Gohman707e0182008-04-12 04:36:06 +00004291 // Emit a library call.
4292 TargetLowering::ArgListTy Args;
4293 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004294 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004295 Entry.Node = Dst; Args.push_back(Entry);
4296 Entry.Node = Src; Args.push_back(Entry);
4297 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004298 // FIXME: pass in SDLoc
Stephen Hinesdce4a402014-05-29 02:49:00 -07004299 TargetLowering::CallLoweringInfo CLI(*this);
4300 CLI.setDebugLoc(dl).setChain(Chain)
4301 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4302 Type::getVoidTy(*getContext()),
4303 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004304 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004305 .setDiscardResult();
Bill Wendlingba54bca2013-06-19 21:36:55 +00004306 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004307
Dan Gohman707e0182008-04-12 04:36:06 +00004308 return CallResult.second;
4309}
4310
Andrew Trickac6d9be2013-05-25 02:42:55 +00004311SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004312 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004313 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004314 MachinePointerInfo DstPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004315 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004316
4317 // Check to see if we should lower the memset to stores first.
4318 // For cases within the target-specified limits, this is the best choice.
4319 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4320 if (ConstantSize) {
4321 // Memset with size zero? Just return the original chain.
4322 if (ConstantSize->isNullValue())
4323 return Chain;
4324
Mon P Wang20adc9d2010-04-04 03:10:48 +00004325 SDValue Result =
4326 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00004327 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00004328
Gabor Greifba36cb52008-08-28 21:40:38 +00004329 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004330 return Result;
4331 }
4332
4333 // Then check to see if we should lower the memset with target-specific
4334 // code. If the target chooses to do this, this is the next best.
Stephen Hines37ed9c12014-12-01 14:51:49 -08004335 SDValue Result = TSI->EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src,
4336 Size, Align, isVol, DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004337 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004338 return Result;
4339
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004340 // Emit a library call.
Bill Wendlingba54bca2013-06-19 21:36:55 +00004341 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004342 TargetLowering::ArgListTy Args;
4343 TargetLowering::ArgListEntry Entry;
4344 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4345 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004346 Entry.Node = Src;
Stephen Hines37ed9c12014-12-01 14:51:49 -08004347 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004348 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004349 Entry.Node = Size;
4350 Entry.Ty = IntPtrTy;
Dan Gohman707e0182008-04-12 04:36:06 +00004351 Args.push_back(Entry);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004352
Stephen Hinesdce4a402014-05-29 02:49:00 -07004353 // FIXME: pass in SDLoc
4354 TargetLowering::CallLoweringInfo CLI(*this);
4355 CLI.setDebugLoc(dl).setChain(Chain)
4356 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4357 Type::getVoidTy(*getContext()),
4358 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004359 TLI->getPointerTy()), std::move(Args), 0)
Stephen Hinesdce4a402014-05-29 02:49:00 -07004360 .setDiscardResult();
4361
4362 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Dan Gohman707e0182008-04-12 04:36:06 +00004363 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004364}
4365
Andrew Trickac6d9be2013-05-25 02:42:55 +00004366SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004367 SDVTList VTList, ArrayRef<SDValue> Ops,
Amara Emerson268c7432013-09-26 12:22:36 +00004368 MachineMemOperand *MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004369 AtomicOrdering SuccessOrdering,
4370 AtomicOrdering FailureOrdering,
Amara Emerson268c7432013-09-26 12:22:36 +00004371 SynchronizationScope SynchScope) {
4372 FoldingSetNodeID ID;
4373 ID.AddInteger(MemVT.getRawBits());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004374 AddNodeIDNode(ID, Opcode, VTList, Ops);
Amara Emerson268c7432013-09-26 12:22:36 +00004375 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004376 void* IP = nullptr;
Amara Emerson268c7432013-09-26 12:22:36 +00004377 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4378 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4379 return SDValue(E, 0);
4380 }
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004381
4382 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4383 // SDNode doesn't have access to it. This memory will be "leaked" when
4384 // the node is deallocated, but recovered when the allocator is released.
4385 // If the number of operands is less than 5 we use AtomicSDNode's internal
4386 // storage.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004387 unsigned NumOps = Ops.size();
4388 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps)
4389 : nullptr;
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004390
Amara Emerson268c7432013-09-26 12:22:36 +00004391 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4392 dl.getDebugLoc(), VTList, MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004393 Ops.data(), DynOps, NumOps, MMO,
Stephen Hines36b56882014-04-23 16:57:46 -07004394 SuccessOrdering, FailureOrdering,
4395 SynchScope);
Amara Emerson268c7432013-09-26 12:22:36 +00004396 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004397 InsertNode(N);
Amara Emerson268c7432013-09-26 12:22:36 +00004398 return SDValue(N, 0);
4399}
4400
4401SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004402 SDVTList VTList, ArrayRef<SDValue> Ops,
Stephen Hines36b56882014-04-23 16:57:46 -07004403 MachineMemOperand *MMO,
4404 AtomicOrdering Ordering,
4405 SynchronizationScope SynchScope) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07004406 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
Stephen Hines36b56882014-04-23 16:57:46 -07004407 Ordering, SynchScope);
4408}
4409
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004410SDValue SelectionDAG::getAtomicCmpSwap(
4411 unsigned Opcode, SDLoc dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4412 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4413 unsigned Alignment, AtomicOrdering SuccessOrdering,
4414 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4415 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4416 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4417 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4418
Dan Gohmanc76909a2009-09-25 20:36:54 +00004419 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4420 Alignment = getEVTAlignment(MemVT);
4421
Evan Chengff89dcb2009-10-18 18:16:27 +00004422 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004423
Eli Friedman981a0102011-09-07 02:23:42 +00004424 // FIXME: Volatile isn't really correct; we should keep track of atomic
4425 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004426 unsigned Flags = MachineMemOperand::MOVolatile;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004427 Flags |= MachineMemOperand::MOLoad;
4428 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004429
4430 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004431 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004432
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004433 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4434 SuccessOrdering, FailureOrdering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004435}
4436
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004437SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, SDLoc dl, EVT MemVT,
4438 SDVTList VTs, SDValue Chain, SDValue Ptr,
4439 SDValue Cmp, SDValue Swp,
4440 MachineMemOperand *MMO,
4441 AtomicOrdering SuccessOrdering,
4442 AtomicOrdering FailureOrdering,
4443 SynchronizationScope SynchScope) {
4444 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4445 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
Dale Johannesene8c17332009-01-29 00:47:48 +00004446 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4447
Dale Johannesene8c17332009-01-29 00:47:48 +00004448 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07004449 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4450 SuccessOrdering, FailureOrdering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004451}
4452
Andrew Trickac6d9be2013-05-25 02:42:55 +00004453SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004454 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004455 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004456 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004457 unsigned Alignment,
4458 AtomicOrdering Ordering,
4459 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004460 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4461 Alignment = getEVTAlignment(MemVT);
4462
Evan Chengff89dcb2009-10-18 18:16:27 +00004463 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004464 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004465 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004466 // For now, atomics are considered to be volatile always, and they are
4467 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004468 // FIXME: Volatile isn't really correct; we should keep track of atomic
4469 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004470 unsigned Flags = MachineMemOperand::MOVolatile;
4471 if (Opcode != ISD::ATOMIC_STORE)
4472 Flags |= MachineMemOperand::MOLoad;
4473 if (Opcode != ISD::ATOMIC_LOAD)
4474 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004475
4476 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004477 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004478 MemVT.getStoreSize(), Alignment);
4479
Eli Friedman55ba8162011-07-29 03:05:32 +00004480 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4481 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004482}
4483
Andrew Trickac6d9be2013-05-25 02:42:55 +00004484SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004485 SDValue Chain,
4486 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004487 MachineMemOperand *MMO,
4488 AtomicOrdering Ordering,
4489 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004490 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4491 Opcode == ISD::ATOMIC_LOAD_SUB ||
4492 Opcode == ISD::ATOMIC_LOAD_AND ||
4493 Opcode == ISD::ATOMIC_LOAD_OR ||
4494 Opcode == ISD::ATOMIC_LOAD_XOR ||
4495 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004496 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004497 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004498 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004499 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004500 Opcode == ISD::ATOMIC_SWAP ||
4501 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004502 "Invalid Atomic Op");
4503
Owen Andersone50ed302009-08-10 22:56:29 +00004504 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004505
Eli Friedman327236c2011-08-24 20:50:09 +00004506 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4507 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004508 SDValue Ops[] = {Chain, Ptr, Val};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004509 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004510}
4511
Andrew Trickac6d9be2013-05-25 02:42:55 +00004512SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004513 EVT VT, SDValue Chain,
4514 SDValue Ptr,
4515 MachineMemOperand *MMO,
4516 AtomicOrdering Ordering,
4517 SynchronizationScope SynchScope) {
4518 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4519
4520 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman327236c2011-08-24 20:50:09 +00004521 SDValue Ops[] = {Chain, Ptr};
Stephen Hinesdce4a402014-05-29 02:49:00 -07004522 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004523}
4524
Duncan Sands4bdcb612008-07-02 17:40:58 +00004525/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004526SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, SDLoc dl) {
4527 if (Ops.size() == 1)
Dale Johannesen54c94522009-02-02 20:47:48 +00004528 return Ops[0];
4529
Owen Andersone50ed302009-08-10 22:56:29 +00004530 SmallVector<EVT, 4> VTs;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004531 VTs.reserve(Ops.size());
4532 for (unsigned i = 0; i < Ops.size(); ++i)
Dale Johannesen54c94522009-02-02 20:47:48 +00004533 VTs.push_back(Ops[i].getValueType());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004534 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004535}
4536
4537SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004538SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004539 ArrayRef<SDValue> Ops,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004540 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004541 unsigned Align, bool Vol,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004542 bool ReadMem, bool WriteMem, unsigned Size) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004543 if (Align == 0) // Ensure that codegen never sees alignment 0
4544 Align = getEVTAlignment(MemVT);
4545
4546 MachineFunction &MF = getMachineFunction();
4547 unsigned Flags = 0;
4548 if (WriteMem)
4549 Flags |= MachineMemOperand::MOStore;
4550 if (ReadMem)
4551 Flags |= MachineMemOperand::MOLoad;
4552 if (Vol)
4553 Flags |= MachineMemOperand::MOVolatile;
Stephen Hines37ed9c12014-12-01 14:51:49 -08004554 if (!Size)
4555 Size = MemVT.getStoreSize();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004556 MachineMemOperand *MMO =
Stephen Hines37ed9c12014-12-01 14:51:49 -08004557 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004558
Stephen Hinesdce4a402014-05-29 02:49:00 -07004559 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004560}
4561
4562SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004563SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004564 ArrayRef<SDValue> Ops, EVT MemVT,
4565 MachineMemOperand *MMO) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004566 assert((Opcode == ISD::INTRINSIC_VOID ||
4567 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004568 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004569 Opcode == ISD::LIFETIME_START ||
4570 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004571 (Opcode <= INT_MAX &&
4572 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4573 "Opcode is not a memory-accessing opcode!");
4574
Dale Johannesene8c17332009-01-29 00:47:48 +00004575 // Memoize the node unless it returns a flag.
4576 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004577 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004578 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004579 AddNodeIDNode(ID, Opcode, VTList, Ops);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004580 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004581 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004582 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4583 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004584 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004585 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004586
Jack Carter3af4d252013-09-09 22:02:08 +00004587 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4588 dl.getDebugLoc(), VTList, Ops,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004589 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004590 CSEMap.InsertNode(N, IP);
4591 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004592 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4593 dl.getDebugLoc(), VTList, Ops,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004594 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004595 }
Stephen Hines37ed9c12014-12-01 14:51:49 -08004596 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004597 return SDValue(N, 0);
4598}
4599
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004600/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4601/// MachinePointerInfo record from it. This is particularly useful because the
4602/// code generator has many cases where it doesn't bother passing in a
4603/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4604static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4605 // If this is FI+Offset, we can model it.
4606 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4607 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4608
4609 // If this is (FI+Offset1)+Offset2, we can model it.
4610 if (Ptr.getOpcode() != ISD::ADD ||
4611 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4612 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4613 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004614
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004615 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4616 return MachinePointerInfo::getFixedStack(FI, Offset+
4617 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4618}
4619
4620/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4621/// MachinePointerInfo record from it. This is particularly useful because the
4622/// code generator has many cases where it doesn't bother passing in a
4623/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4624static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4625 // If the 'Offset' value isn't a constant, we can't handle this.
4626 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4627 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4628 if (OffsetOp.getOpcode() == ISD::UNDEF)
4629 return InferPointerInfo(Ptr);
4630 return MachinePointerInfo();
4631}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004632
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004633
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004634SDValue
4635SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004636 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004637 SDValue Ptr, SDValue Offset,
4638 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004639 bool isVolatile, bool isNonTemporal, bool isInvariant,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004640 unsigned Alignment, const AAMDNodes &AAInfo,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004641 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004642 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004643 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004644 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4645 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004646
Dan Gohmanc76909a2009-09-25 20:36:54 +00004647 unsigned Flags = MachineMemOperand::MOLoad;
4648 if (isVolatile)
4649 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004650 if (isNonTemporal)
4651 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004652 if (isInvariant)
4653 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004654
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004655 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4656 // clients.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004657 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004658 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004659
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004660 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004661 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004662 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004663 AAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004664 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004665}
4666
4667SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004668SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004669 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004670 SDValue Ptr, SDValue Offset, EVT MemVT,
4671 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004672 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004673 ExtType = ISD::NON_EXTLOAD;
4674 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004675 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004676 } else {
4677 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004678 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4679 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004680 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004681 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004682 assert(VT.isVector() == MemVT.isVector() &&
4683 "Cannot use trunc store to convert to or from a vector!");
4684 assert((!VT.isVector() ||
4685 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4686 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004687 }
4688
4689 bool Indexed = AM != ISD::UNINDEXED;
4690 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4691 "Unindexed load with an offset!");
4692
4693 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004694 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004695 SDValue Ops[] = { Chain, Ptr, Offset };
4696 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004697 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004698 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004699 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004700 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004701 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004702 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004703 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004704 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4705 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004706 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004707 }
Jack Carter3af4d252013-09-09 22:02:08 +00004708 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4709 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman95531882010-03-18 18:49:47 +00004710 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004711 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004712 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004713 return SDValue(N, 0);
4714}
4715
Andrew Trickac6d9be2013-05-25 02:42:55 +00004716SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004717 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004718 MachinePointerInfo PtrInfo,
4719 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004720 bool isInvariant, unsigned Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004721 const AAMDNodes &AAInfo,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004722 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004723 SDValue Undef = getUNDEF(Ptr.getValueType());
4724 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004725 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004726 AAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004727}
Dale Johannesene8c17332009-01-29 00:47:48 +00004728
Richard Sandiford66589dc2013-10-28 11:17:59 +00004729SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4730 SDValue Chain, SDValue Ptr,
4731 MachineMemOperand *MMO) {
4732 SDValue Undef = getUNDEF(Ptr.getValueType());
4733 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4734 VT, MMO);
4735}
4736
Andrew Trickac6d9be2013-05-25 02:42:55 +00004737SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004738 SDValue Chain, SDValue Ptr,
4739 MachinePointerInfo PtrInfo, EVT MemVT,
4740 bool isVolatile, bool isNonTemporal,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004741 bool isInvariant, unsigned Alignment,
4742 const AAMDNodes &AAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004743 SDValue Undef = getUNDEF(Ptr.getValueType());
4744 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004745 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
4746 Alignment, AAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004747}
4748
4749
Richard Sandiford66589dc2013-10-28 11:17:59 +00004750SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4751 SDValue Chain, SDValue Ptr, EVT MemVT,
4752 MachineMemOperand *MMO) {
4753 SDValue Undef = getUNDEF(Ptr.getValueType());
4754 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4755 MemVT, MMO);
4756}
4757
Dan Gohman475871a2008-07-27 21:46:04 +00004758SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004759SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004760 SDValue Offset, ISD::MemIndexedMode AM) {
4761 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4762 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4763 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004764 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004765 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004766 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004767 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004768}
4769
Andrew Trickac6d9be2013-05-25 02:42:55 +00004770SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004771 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004772 bool isVolatile, bool isNonTemporal,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004773 unsigned Alignment, const AAMDNodes &AAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004774 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004775 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004776 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004777 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004778
Dan Gohmanc76909a2009-09-25 20:36:54 +00004779 unsigned Flags = MachineMemOperand::MOStore;
4780 if (isVolatile)
4781 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004782 if (isNonTemporal)
4783 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004784
Stephen Hinesdce4a402014-05-29 02:49:00 -07004785 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004786 PtrInfo = InferPointerInfo(Ptr);
4787
4788 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004789 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004790 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004791 Val.getValueType().getStoreSize(), Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004792 AAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004793
4794 return getStore(Chain, dl, Val, Ptr, MMO);
4795}
4796
Andrew Trickac6d9be2013-05-25 02:42:55 +00004797SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004798 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004799 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004800 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004801 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004802 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004803 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004804 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4805 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004806 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004807 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004808 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004809 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004810 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004811 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004812 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4813 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004814 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004815 }
Jack Carter3af4d252013-09-09 22:02:08 +00004816 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4817 dl.getDebugLoc(), VTs,
4818 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004819 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004820 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004821 return SDValue(N, 0);
4822}
4823
Andrew Trickac6d9be2013-05-25 02:42:55 +00004824SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004825 SDValue Ptr, MachinePointerInfo PtrInfo,
4826 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004827 unsigned Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004828 const AAMDNodes &AAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004829 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004830 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004831 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4832 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004833
Dan Gohmanc76909a2009-09-25 20:36:54 +00004834 unsigned Flags = MachineMemOperand::MOStore;
4835 if (isVolatile)
4836 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004837 if (isNonTemporal)
4838 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004839
Stephen Hinesdce4a402014-05-29 02:49:00 -07004840 if (PtrInfo.V.isNull())
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004841 PtrInfo = InferPointerInfo(Ptr);
4842
4843 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004844 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004845 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -08004846 AAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004847
4848 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4849}
4850
Andrew Trickac6d9be2013-05-25 02:42:55 +00004851SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004852 SDValue Ptr, EVT SVT,
4853 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004854 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004855
Michael Ilseman06b96902012-09-10 16:56:31 +00004856 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004857 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004858 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004859 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004860
Dan Gohman2e141d72009-12-14 23:40:38 +00004861 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4862 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004863 assert(VT.isInteger() == SVT.isInteger() &&
4864 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004865 assert(VT.isVector() == SVT.isVector() &&
4866 "Cannot use trunc store to convert to or from a vector!");
4867 assert((!VT.isVector() ||
4868 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4869 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004870
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004872 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004873 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4874 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004875 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004876 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004877 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004878 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004879 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004880 void *IP = nullptr;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004881 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4882 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004883 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004884 }
Jack Carter3af4d252013-09-09 22:02:08 +00004885 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4886 dl.getDebugLoc(), VTs,
4887 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004888 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004889 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004890 return SDValue(N, 0);
4891}
4892
Dan Gohman475871a2008-07-27 21:46:04 +00004893SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004894SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004895 SDValue Offset, ISD::MemIndexedMode AM) {
4896 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4897 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4898 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004899 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004900 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4901 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004902 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00004903 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004904 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004905 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Stephen Hinesdce4a402014-05-29 02:49:00 -07004906 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004907 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004908 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004909
Jack Carter3af4d252013-09-09 22:02:08 +00004910 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4911 dl.getDebugLoc(), VTs, AM,
Dan Gohman95531882010-03-18 18:49:47 +00004912 ST->isTruncatingStore(),
4913 ST->getMemoryVT(),
4914 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004915 CSEMap.InsertNode(N, IP);
Stephen Hines37ed9c12014-12-01 14:51:49 -08004916 InsertNode(N);
Dale Johannesene8c17332009-01-29 00:47:48 +00004917 return SDValue(N, 0);
4918}
4919
Andrew Trickac6d9be2013-05-25 02:42:55 +00004920SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004921 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004922 SDValue SV,
4923 unsigned Align) {
4924 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
Stephen Hinesdce4a402014-05-29 02:49:00 -07004925 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004926}
4927
Andrew Trickac6d9be2013-05-25 02:42:55 +00004928SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004929 ArrayRef<SDUse> Ops) {
4930 switch (Ops.size()) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004931 case 0: return getNode(Opcode, DL, VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07004932 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
Bill Wendling7ade28c2009-01-28 22:17:52 +00004933 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4934 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004935 default: break;
4936 }
4937
Dan Gohman475871a2008-07-27 21:46:04 +00004938 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004939 // the regular getNode logic.
Stephen Hinesdce4a402014-05-29 02:49:00 -07004940 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
4941 return getNode(Opcode, DL, VT, NewOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004942}
4943
Andrew Trickac6d9be2013-05-25 02:42:55 +00004944SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Stephen Hinesdce4a402014-05-29 02:49:00 -07004945 ArrayRef<SDValue> Ops) {
4946 unsigned NumOps = Ops.size();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004947 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004948 case 0: return getNode(Opcode, DL, VT);
4949 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4950 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4951 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004952 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004953 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004954
Chris Lattneref847df2005-04-09 03:27:28 +00004955 switch (Opcode) {
4956 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004957 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004958 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004959 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4960 "LHS and RHS of condition must have same type!");
4961 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4962 "True and False arms of SelectCC must have same type!");
4963 assert(Ops[2].getValueType() == VT &&
4964 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004965 break;
4966 }
4967 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004968 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004969 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4970 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004971 break;
4972 }
Chris Lattneref847df2005-04-09 03:27:28 +00004973 }
4974
Chris Lattner385328c2005-05-14 07:42:29 +00004975 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004976 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004977 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004978
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004979 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004980 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07004981 AddNodeIDNode(ID, Opcode, VTs, Ops);
4982 void *IP = nullptr;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004983
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004984 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004985 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004986
Jack Carter3af4d252013-09-09 22:02:08 +00004987 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07004988 VTs, Ops);
Chris Lattnera5682852006-08-07 23:03:03 +00004989 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004990 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004991 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07004992 VTs, Ops);
Chris Lattner43247a12005-08-25 19:12:10 +00004993 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004994
Stephen Hines37ed9c12014-12-01 14:51:49 -08004995 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004996 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004997}
4998
Andrew Trickac6d9be2013-05-25 02:42:55 +00004999SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005000 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5001 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
Scott Michelfdc40a02009-02-17 22:15:04 +00005002}
5003
Andrew Trickac6d9be2013-05-25 02:42:55 +00005004SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005005 ArrayRef<SDValue> Ops) {
Chris Lattnerbe384162006-08-16 22:57:46 +00005006 if (VTList.NumVTs == 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -07005007 return getNode(Opcode, DL, VTList.VTs[0], Ops);
Chris Lattner89c34632005-05-14 06:20:26 +00005008
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00005009#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00005010 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00005011 // FIXME: figure out how to safely handle things like
5012 // int foo(int x) { return 1 << (x & 255); }
5013 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00005014 case ISD::SRA_PARTS:
5015 case ISD::SRL_PARTS:
5016 case ISD::SHL_PARTS:
5017 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005018 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00005019 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00005020 else if (N3.getOpcode() == ISD::AND)
5021 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5022 // If the and is only masking out bits that cannot effect the shift,
5023 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00005024 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00005025 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00005026 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00005027 }
5028 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00005029 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00005030#endif
Chris Lattner89c34632005-05-14 06:20:26 +00005031
Chris Lattner43247a12005-08-25 19:12:10 +00005032 // Memoize the node unless it returns a flag.
5033 SDNode *N;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005034 unsigned NumOps = Ops.size();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005035 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00005036 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005037 AddNodeIDNode(ID, Opcode, VTList, Ops);
5038 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005039 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00005040 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005041
Dan Gohman0e5f1302008-07-07 23:02:41 +00005042 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00005043 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5044 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005045 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00005046 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5047 DL.getDebugLoc(), VTList, Ops[0],
5048 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005049 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00005050 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5051 DL.getDebugLoc(), VTList, Ops[0],
5052 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005053 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00005054 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005055 VTList, Ops);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005056 }
Chris Lattnera5682852006-08-07 23:03:03 +00005057 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00005058 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00005059 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00005060 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
5061 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005062 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00005063 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
5064 DL.getDebugLoc(), VTList, Ops[0],
5065 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005066 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00005067 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
5068 DL.getDebugLoc(), VTList, Ops[0],
5069 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005070 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00005071 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005072 VTList, Ops);
Dan Gohman0e5f1302008-07-07 23:02:41 +00005073 }
Chris Lattner43247a12005-08-25 19:12:10 +00005074 }
Stephen Hines37ed9c12014-12-01 14:51:49 -08005075 InsertNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00005076 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00005077}
5078
Andrew Trickac6d9be2013-05-25 02:42:55 +00005079SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08005080 return getNode(Opcode, DL, VTList, None);
Dan Gohman08ce9762007-10-08 15:49:58 +00005081}
5082
Andrew Trickac6d9be2013-05-25 02:42:55 +00005083SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005084 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00005085 SDValue Ops[] = { N1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005086 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005087}
5088
Andrew Trickac6d9be2013-05-25 02:42:55 +00005089SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005090 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00005091 SDValue Ops[] = { N1, N2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005092 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005093}
5094
Andrew Trickac6d9be2013-05-25 02:42:55 +00005095SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005096 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005097 SDValue Ops[] = { N1, N2, N3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005098 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005099}
5100
Andrew Trickac6d9be2013-05-25 02:42:55 +00005101SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005102 SDValue N1, SDValue N2, SDValue N3,
5103 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00005104 SDValue Ops[] = { N1, N2, N3, N4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005105 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005106}
5107
Andrew Trickac6d9be2013-05-25 02:42:55 +00005108SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00005109 SDValue N1, SDValue N2, SDValue N3,
5110 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00005111 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005112 return getNode(Opcode, DL, VTList, Ops);
Dan Gohman08ce9762007-10-08 15:49:58 +00005113}
5114
Owen Andersone50ed302009-08-10 22:56:29 +00005115SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00005116 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00005117}
5118
Owen Andersone50ed302009-08-10 22:56:29 +00005119SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005120 FoldingSetNodeID ID;
5121 ID.AddInteger(2U);
5122 ID.AddInteger(VT1.getRawBits());
5123 ID.AddInteger(VT2.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005124
Stephen Hinesdce4a402014-05-29 02:49:00 -07005125 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005126 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005127 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005128 EVT *Array = Allocator.Allocate<EVT>(2);
5129 Array[0] = VT1;
5130 Array[1] = VT2;
5131 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5132 VTListMap.InsertNode(Result, IP);
5133 }
5134 return Result->getSDVTList();
Chris Lattnera3255112005-11-08 23:30:28 +00005135}
Dan Gohmane8be6c62008-07-17 19:10:17 +00005136
Owen Andersone50ed302009-08-10 22:56:29 +00005137SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005138 FoldingSetNodeID ID;
5139 ID.AddInteger(3U);
5140 ID.AddInteger(VT1.getRawBits());
5141 ID.AddInteger(VT2.getRawBits());
5142 ID.AddInteger(VT3.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005143
Stephen Hinesdce4a402014-05-29 02:49:00 -07005144 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005145 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005146 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005147 EVT *Array = Allocator.Allocate<EVT>(3);
5148 Array[0] = VT1;
5149 Array[1] = VT2;
5150 Array[2] = VT3;
5151 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5152 VTListMap.InsertNode(Result, IP);
5153 }
5154 return Result->getSDVTList();
Chris Lattner70046e92006-08-15 17:46:01 +00005155}
5156
Owen Andersone50ed302009-08-10 22:56:29 +00005157SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005158 FoldingSetNodeID ID;
5159 ID.AddInteger(4U);
5160 ID.AddInteger(VT1.getRawBits());
5161 ID.AddInteger(VT2.getRawBits());
5162 ID.AddInteger(VT3.getRawBits());
5163 ID.AddInteger(VT4.getRawBits());
Bill Wendling13d6d442008-12-01 23:28:22 +00005164
Stephen Hinesdce4a402014-05-29 02:49:00 -07005165 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005166 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005167 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005168 EVT *Array = Allocator.Allocate<EVT>(4);
5169 Array[0] = VT1;
5170 Array[1] = VT2;
5171 Array[2] = VT3;
5172 Array[3] = VT4;
5173 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5174 VTListMap.InsertNode(Result, IP);
5175 }
5176 return Result->getSDVTList();
Bill Wendling13d6d442008-12-01 23:28:22 +00005177}
5178
Stephen Hinesdce4a402014-05-29 02:49:00 -07005179SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5180 unsigned NumVTs = VTs.size();
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005181 FoldingSetNodeID ID;
5182 ID.AddInteger(NumVTs);
5183 for (unsigned index = 0; index < NumVTs; index++) {
5184 ID.AddInteger(VTs[index].getRawBits());
Chris Lattner70046e92006-08-15 17:46:01 +00005185 }
5186
Stephen Hinesdce4a402014-05-29 02:49:00 -07005187 void *IP = nullptr;
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005188 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005189 if (!Result) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005190 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005191 std::copy(VTs.begin(), VTs.end(), Array);
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005192 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5193 VTListMap.InsertNode(Result, IP);
Chris Lattner70046e92006-08-15 17:46:01 +00005194 }
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005195 return Result->getSDVTList();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00005196}
5197
5198
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005199/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5200/// specified operands. If the resultant node already exists in the DAG,
5201/// this does not modify the specified node, instead it returns the node that
5202/// already exists. If the resultant node does not exist in the DAG, the
5203/// input node is returned. As a degenerate case, if you specify the same
5204/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00005205SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005206 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005207
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005208 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00005209 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005210
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005211 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005212 void *InsertPos = nullptr;
Chris Lattnera5682852006-08-07 23:03:03 +00005213 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005214 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005215
Dan Gohman79acd2b2008-07-21 22:38:59 +00005216 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005217 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005218 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005219 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005220
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005221 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005222 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00005223
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005224 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005225 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005226 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005227}
5228
Dan Gohman027657d2010-06-18 15:30:29 +00005229SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005230 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005231
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005232 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005233 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00005234 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00005235
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005236 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005237 void *InsertPos = nullptr;
Chris Lattnera5682852006-08-07 23:03:03 +00005238 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005239 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005240
Dan Gohman79acd2b2008-07-21 22:38:59 +00005241 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005242 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005243 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005244 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005245
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005246 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005247 if (N->OperandList[0] != Op1)
5248 N->OperandList[0].set(Op1);
5249 if (N->OperandList[1] != Op2)
5250 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005251
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005252 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005253 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005254 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005255}
5256
Dan Gohman027657d2010-06-18 15:30:29 +00005257SDNode *SelectionDAG::
5258UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005259 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005260 return UpdateNodeOperands(N, Ops);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005261}
5262
Dan Gohman027657d2010-06-18 15:30:29 +00005263SDNode *SelectionDAG::
5264UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005265 SDValue Op3, SDValue Op4) {
5266 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005267 return UpdateNodeOperands(N, Ops);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005268}
5269
Dan Gohman027657d2010-06-18 15:30:29 +00005270SDNode *SelectionDAG::
5271UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005272 SDValue Op3, SDValue Op4, SDValue Op5) {
5273 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005274 return UpdateNodeOperands(N, Ops);
Chris Lattner809ec112006-01-28 10:09:25 +00005275}
5276
Dan Gohman027657d2010-06-18 15:30:29 +00005277SDNode *SelectionDAG::
Stephen Hinesdce4a402014-05-29 02:49:00 -07005278UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5279 unsigned NumOps = Ops.size();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005280 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005281 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005282
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005283 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005284 bool AnyChange = false;
5285 for (unsigned i = 0; i != NumOps; ++i) {
5286 if (Ops[i] != N->getOperand(i)) {
5287 AnyChange = true;
5288 break;
5289 }
5290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005291
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005292 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00005293 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005294
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005295 // See if the modified node already exists.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005296 void *InsertPos = nullptr;
5297 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005298 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005299
Dan Gohman7ceda162008-05-02 00:05:03 +00005300 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005301 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005302 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005303 InsertPos = nullptr;
Scott Michelfdc40a02009-02-17 22:15:04 +00005304
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005305 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005306 for (unsigned i = 0; i != NumOps; ++i)
5307 if (N->OperandList[i] != Ops[i])
5308 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005309
5310 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005311 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005312 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005313}
5314
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005315/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00005316/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005317void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005318 // Unlike the code in MorphNodeTo that does this, we don't need to
5319 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00005320 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5321 SDUse &Use = *I++;
5322 Use.set(SDValue());
5323 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00005324}
Chris Lattner149c58c2005-08-16 18:17:10 +00005325
Dan Gohmane8be6c62008-07-17 19:10:17 +00005326/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5327/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005328///
Dan Gohmane8be6c62008-07-17 19:10:17 +00005329SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005330 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005331 SDVTList VTs = getVTList(VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005332 return SelectNodeTo(N, MachineOpc, VTs, None);
Chris Lattner7651fa42005-08-24 23:00:29 +00005333}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005334
Dan Gohmane8be6c62008-07-17 19:10:17 +00005335SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005336 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005337 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005338 SDValue Ops[] = { Op1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005339 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005340}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005341
Dan Gohmane8be6c62008-07-17 19:10:17 +00005342SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005343 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005344 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005345 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005346 SDValue Ops[] = { Op1, Op2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005347 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005348}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005349
Dan Gohmane8be6c62008-07-17 19:10:17 +00005350SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005351 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005352 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005353 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005354 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005355 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner149c58c2005-08-16 18:17:10 +00005356}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00005357
Dan Gohmane8be6c62008-07-17 19:10:17 +00005358SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005359 EVT VT, ArrayRef<SDValue> Ops) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005360 SDVTList VTs = getVTList(VT);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005361 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005362}
5363
Dan Gohmane8be6c62008-07-17 19:10:17 +00005364SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005365 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005366 SDVTList VTs = getVTList(VT1, VT2);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005367 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005368}
5369
Dan Gohmane8be6c62008-07-17 19:10:17 +00005370SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005371 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005372 SDVTList VTs = getVTList(VT1, VT2);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005373 return SelectNodeTo(N, MachineOpc, VTs, None);
Dan Gohmancd920d92008-07-02 23:23:19 +00005374}
5375
Dan Gohmane8be6c62008-07-17 19:10:17 +00005376SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005377 EVT VT1, EVT VT2, EVT VT3,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005378 ArrayRef<SDValue> Ops) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005379 SDVTList VTs = getVTList(VT1, VT2, VT3);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005380 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005381}
5382
Bill Wendling13d6d442008-12-01 23:28:22 +00005383SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005384 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005385 ArrayRef<SDValue> Ops) {
Bill Wendling13d6d442008-12-01 23:28:22 +00005386 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005387 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling13d6d442008-12-01 23:28:22 +00005388}
5389
Scott Michelfdc40a02009-02-17 22:15:04 +00005390SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005391 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005392 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005393 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005394 SDValue Ops[] = { Op1 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005395 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005396}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005397
Scott Michelfdc40a02009-02-17 22:15:04 +00005398SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005399 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005400 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005401 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005402 SDValue Ops[] = { Op1, Op2 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005403 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005404}
5405
Dan Gohmane8be6c62008-07-17 19:10:17 +00005406SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005407 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005408 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005409 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005410 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005411 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005412 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Dan Gohmancd920d92008-07-02 23:23:19 +00005413}
5414
Dan Gohmane8be6c62008-07-17 19:10:17 +00005415SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005416 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005417 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005418 SDValue Op3) {
5419 SDVTList VTs = getVTList(VT1, VT2, VT3);
5420 SDValue Ops[] = { Op1, Op2, Op3 };
Stephen Hinesdce4a402014-05-29 02:49:00 -07005421 return SelectNodeTo(N, MachineOpc, VTs, Ops);
Bill Wendling13d6d442008-12-01 23:28:22 +00005422}
5423
5424SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005425 SDVTList VTs,ArrayRef<SDValue> Ops) {
5426 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
Chris Lattner5857e0a2010-02-23 23:01:35 +00005427 // Reset the NodeID to -1.
5428 N->setNodeId(-1);
5429 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005430}
5431
Andrew Trickac6d9be2013-05-25 02:42:55 +00005432/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel0508d042011-12-15 18:21:18 +00005433/// the line number information on the merged node since it is not possible to
5434/// preserve the information that operation is associated with multiple lines.
5435/// This will make the debugger working better at -O0, were there is a higher
5436/// probability having other instructions associated with that line.
5437///
Andrew Trickac6d9be2013-05-25 02:42:55 +00005438/// For IROrder, we keep the smaller of the two
5439SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel0508d042011-12-15 18:21:18 +00005440 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005441 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5442 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel0508d042011-12-15 18:21:18 +00005443 N->setDebugLoc(DebugLoc());
5444 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005445 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5446 N->setIROrder(Order);
Devang Patel0508d042011-12-15 18:21:18 +00005447 return N;
5448}
5449
Chris Lattner21221e32010-02-28 21:36:14 +00005450/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005451/// return type, opcode, and operands.
5452///
5453/// Note that MorphNodeTo returns the resultant node. If there is already a
5454/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickac6d9be2013-05-25 02:42:55 +00005455/// the current one. Note that the SDLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005456///
5457/// Using MorphNodeTo is faster than creating a new node and swapping it in
5458/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005459/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005460/// the node's users.
5461///
Stephen Hines37ed9c12014-12-01 14:51:49 -08005462/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5463/// As a consequence it isn't appropriate to use from within the DAG combiner or
5464/// the legalizer which maintain worklists that would need to be updated when
5465/// deleting things.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005466SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Stephen Hinesdce4a402014-05-29 02:49:00 -07005467 SDVTList VTs, ArrayRef<SDValue> Ops) {
5468 unsigned NumOps = Ops.size();
Dan Gohmancd920d92008-07-02 23:23:19 +00005469 // If an identical node already exists, use it.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005470 void *IP = nullptr;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005471 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005472 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005473 AddNodeIDNode(ID, Opc, VTs, Ops);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005474 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005475 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohmane8be6c62008-07-17 19:10:17 +00005476 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005477
Dan Gohman095cc292008-09-13 01:54:27 +00005478 if (!RemoveNodeFromCSEMaps(N))
Stephen Hinesdce4a402014-05-29 02:49:00 -07005479 IP = nullptr;
Chris Lattner67612a12007-02-04 02:32:44 +00005480
Dan Gohmane8be6c62008-07-17 19:10:17 +00005481 // Start the morphing.
5482 N->NodeType = Opc;
5483 N->ValueList = VTs.VTs;
5484 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005485
Dan Gohmane8be6c62008-07-17 19:10:17 +00005486 // Clear the operands list, updating used nodes to remove this from their
5487 // use list. Keep track of any operands that become dead as a result.
5488 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005489 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5490 SDUse &Use = *I++;
5491 SDNode *Used = Use.getNode();
5492 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005493 if (Used->use_empty())
5494 DeadNodeSet.insert(Used);
5495 }
5496
Dan Gohmanc76909a2009-09-25 20:36:54 +00005497 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5498 // Initialize the memory references information.
Stephen Hinesdce4a402014-05-29 02:49:00 -07005499 MN->setMemRefs(nullptr, nullptr);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005500 // If NumOps is larger than the # of operands we can have in a
5501 // MachineSDNode, reallocate the operand list.
5502 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5503 if (MN->OperandsNeedDelete)
5504 delete[] MN->OperandList;
5505 if (NumOps > array_lengthof(MN->LocalOperands))
5506 // We're creating a final node that will live unmorphed for the
5507 // remainder of the current SelectionDAG iteration, so we can allocate
5508 // the operands directly out of a pool with no recycling metadata.
5509 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Stephen Hinesdce4a402014-05-29 02:49:00 -07005510 Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005511 else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005512 MN->InitOperands(MN->LocalOperands, Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005513 MN->OperandsNeedDelete = false;
5514 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005515 MN->InitOperands(MN->OperandList, Ops.data(), NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005516 } else {
5517 // If NumOps is larger than the # of operands we currently have, reallocate
5518 // the operand list.
5519 if (NumOps > N->NumOperands) {
5520 if (N->OperandsNeedDelete)
5521 delete[] N->OperandList;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005522 N->InitOperands(new SDUse[NumOps], Ops.data(), NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005523 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005524 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07005525 N->InitOperands(N->OperandList, Ops.data(), NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005526 }
5527
5528 // Delete any nodes that are still dead after adding the uses for the
5529 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005530 if (!DeadNodeSet.empty()) {
5531 SmallVector<SDNode *, 16> DeadNodes;
Stephen Hines37ed9c12014-12-01 14:51:49 -08005532 for (SDNode *N : DeadNodeSet)
5533 if (N->use_empty())
5534 DeadNodes.push_back(N);
Chris Lattner7d892d62010-03-01 07:43:08 +00005535 RemoveDeadNodes(DeadNodes);
5536 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005537
Dan Gohmane8be6c62008-07-17 19:10:17 +00005538 if (IP)
5539 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005540 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005541}
5542
Chris Lattner0fb094f2005-11-19 01:44:53 +00005543
Dan Gohman602b0c82009-09-25 18:54:59 +00005544/// getMachineNode - These are used for target selectors to create a new node
5545/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005546///
Dan Gohman602b0c82009-09-25 18:54:59 +00005547/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005548/// node of the specified opcode and operands, it returns that node instead of
5549/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005550MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005551SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005552 SDVTList VTs = getVTList(VT);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005553 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005554}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005555
Dan Gohmanc81b7832009-10-10 01:29:16 +00005556MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005557SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005558 SDVTList VTs = getVTList(VT);
5559 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005560 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005561}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005562
Dan Gohmanc81b7832009-10-10 01:29:16 +00005563MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005564SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005565 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005566 SDVTList VTs = getVTList(VT);
5567 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005568 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005569}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005570
Dan Gohmanc81b7832009-10-10 01:29:16 +00005571MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005572SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005573 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005574 SDVTList VTs = getVTList(VT);
5575 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005576 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005577}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005578
Dan Gohmanc81b7832009-10-10 01:29:16 +00005579MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005580SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liao2a8bea72013-04-19 22:22:57 +00005581 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005582 SDVTList VTs = getVTList(VT);
Michael Liao2a8bea72013-04-19 22:22:57 +00005583 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005584}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005585
Dan Gohmanc81b7832009-10-10 01:29:16 +00005586MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005587SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005588 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005589 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005590}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005591
Dan Gohmanc81b7832009-10-10 01:29:16 +00005592MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005593SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005594 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005595 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005596 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005597 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005598}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005599
Dan Gohmanc81b7832009-10-10 01:29:16 +00005600MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005601SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005602 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005603 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005604 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005605 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005606}
5607
Dan Gohmanc81b7832009-10-10 01:29:16 +00005608MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005609SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005610 EVT VT1, EVT VT2, SDValue Op1,
5611 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005612 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005613 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005614 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005615}
5616
Dan Gohmanc81b7832009-10-10 01:29:16 +00005617MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005618SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005619 EVT VT1, EVT VT2,
Michael Liao2a8bea72013-04-19 22:22:57 +00005620 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005621 SDVTList VTs = getVTList(VT1, VT2);
Michael Liao2a8bea72013-04-19 22:22:57 +00005622 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005623}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005624
Dan Gohmanc81b7832009-10-10 01:29:16 +00005625MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005626SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005627 EVT VT1, EVT VT2, EVT VT3,
5628 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005629 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005630 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005631 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005632}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005633
Dan Gohmanc81b7832009-10-10 01:29:16 +00005634MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005635SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005636 EVT VT1, EVT VT2, EVT VT3,
5637 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005638 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005639 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005640 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Cheng6ae46c42006-02-09 07:15:23 +00005641}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005642
Dan Gohmanc81b7832009-10-10 01:29:16 +00005643MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005644SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005645 EVT VT1, EVT VT2, EVT VT3,
Michael Liao2a8bea72013-04-19 22:22:57 +00005646 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005647 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liao2a8bea72013-04-19 22:22:57 +00005648 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005649}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005650
Dan Gohmanc81b7832009-10-10 01:29:16 +00005651MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005652SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005653 EVT VT2, EVT VT3, EVT VT4,
Michael Liao2a8bea72013-04-19 22:22:57 +00005654 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005655 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liao2a8bea72013-04-19 22:22:57 +00005656 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005657}
5658
Dan Gohmanc81b7832009-10-10 01:29:16 +00005659MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005660SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramer3853f742013-03-07 20:33:29 +00005661 ArrayRef<EVT> ResultTys,
Michael Liao2a8bea72013-04-19 22:22:57 +00005662 ArrayRef<SDValue> Ops) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07005663 SDVTList VTs = getVTList(ResultTys);
Michael Liao2a8bea72013-04-19 22:22:57 +00005664 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005665}
5666
Dan Gohmanc81b7832009-10-10 01:29:16 +00005667MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005668SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liao2a8bea72013-04-19 22:22:57 +00005669 ArrayRef<SDValue> OpsArray) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005670 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005671 MachineSDNode *N;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005672 void *IP = nullptr;
Michael Liao2a8bea72013-04-19 22:22:57 +00005673 const SDValue *Ops = OpsArray.data();
5674 unsigned NumOps = OpsArray.size();
Dan Gohmanc76909a2009-09-25 20:36:54 +00005675
5676 if (DoCSE) {
5677 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005678 AddNodeIDNode(ID, ~Opcode, VTs, OpsArray);
5679 IP = nullptr;
Devang Patel0508d042011-12-15 18:21:18 +00005680 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005681 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel0508d042011-12-15 18:21:18 +00005682 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005683 }
5684
5685 // Allocate a new MachineSDNode.
Jack Carter3af4d252013-09-09 22:02:08 +00005686 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5687 DL.getDebugLoc(), VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005688
5689 // Initialize the operands list.
5690 if (NumOps > array_lengthof(N->LocalOperands))
5691 // We're creating a final node that will live unmorphed for the
5692 // remainder of the current SelectionDAG iteration, so we can allocate
5693 // the operands directly out of a pool with no recycling metadata.
5694 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5695 Ops, NumOps);
5696 else
5697 N->InitOperands(N->LocalOperands, Ops, NumOps);
5698 N->OperandsNeedDelete = false;
5699
5700 if (DoCSE)
5701 CSEMap.InsertNode(N, IP);
5702
Stephen Hines37ed9c12014-12-01 14:51:49 -08005703 InsertNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005704 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005705}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005706
Dan Gohman6a402dc2009-08-19 18:16:17 +00005707/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005708/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005709SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005710SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohman6a402dc2009-08-19 18:16:17 +00005711 SDValue Operand) {
5712 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005713 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005714 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005715 return SDValue(Subreg, 0);
5716}
5717
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005718/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005719/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005720SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005721SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005722 SDValue Operand, SDValue Subreg) {
5723 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005724 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005725 VT, Operand, Subreg, SRIdxVal);
5726 return SDValue(Result, 0);
5727}
5728
Evan Cheng08b11732008-03-22 01:55:50 +00005729/// getNodeIfExists - Get the specified node if it's already available, or
5730/// else return NULL.
5731SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07005732 ArrayRef<SDValue> Ops, bool nuw, bool nsw,
5733 bool exact) {
5734 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005735 FoldingSetNodeID ID;
Stephen Hinesdce4a402014-05-29 02:49:00 -07005736 AddNodeIDNode(ID, Opcode, VTList, Ops);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07005737 if (isBinOpWithFlags(Opcode))
5738 AddBinaryNodeIDCustom(ID, nuw, nsw, exact);
Stephen Hinesdce4a402014-05-29 02:49:00 -07005739 void *IP = nullptr;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005740 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005741 return E;
5742 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07005743 return nullptr;
Evan Cheng08b11732008-03-22 01:55:50 +00005744}
5745
Evan Cheng31441b72010-03-29 20:48:30 +00005746/// getDbgValue - Creates a SDDbgValue node.
5747///
Stephen Hinesdce4a402014-05-29 02:49:00 -07005748/// SDNode
Stephen Hines37ed9c12014-12-01 14:51:49 -08005749SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
5750 unsigned R, bool IsIndirect, uint64_t Off,
5751 DebugLoc DL, unsigned O) {
5752 return new (Allocator) SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
Evan Cheng31441b72010-03-29 20:48:30 +00005753}
5754
Stephen Hinesdce4a402014-05-29 02:49:00 -07005755/// Constant
Stephen Hines37ed9c12014-12-01 14:51:49 -08005756SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
5757 const Value *C, uint64_t Off,
5758 DebugLoc DL, unsigned O) {
5759 return new (Allocator) SDDbgValue(Var, Expr, C, Off, DL, O);
Evan Cheng31441b72010-03-29 20:48:30 +00005760}
5761
Stephen Hinesdce4a402014-05-29 02:49:00 -07005762/// FrameIndex
Stephen Hines37ed9c12014-12-01 14:51:49 -08005763SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
5764 unsigned FI, uint64_t Off,
5765 DebugLoc DL, unsigned O) {
5766 return new (Allocator) SDDbgValue(Var, Expr, FI, Off, DL, O);
Evan Cheng31441b72010-03-29 20:48:30 +00005767}
5768
Dan Gohmana72d2a22010-03-03 21:33:37 +00005769namespace {
5770
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005771/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005772/// pointed to by a use iterator is deleted, increment the use iterator
5773/// so that it doesn't dangle.
5774///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005775class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005776 SDNode::use_iterator &UI;
5777 SDNode::use_iterator &UE;
5778
Stephen Hines36b56882014-04-23 16:57:46 -07005779 void NodeDeleted(SDNode *N, SDNode *E) override {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005780 // Increment the iterator as needed.
5781 while (UI != UE && N == *UI)
5782 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005783 }
5784
5785public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005786 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005787 SDNode::use_iterator &ui,
5788 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005789 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005790};
5791
5792}
5793
Evan Cheng99157a02006-08-07 22:13:29 +00005794/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005795/// This can cause recursive merging of nodes in the DAG.
5796///
Chris Lattner11d049c2008-02-03 03:35:22 +00005797/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005798///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005799void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005800 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005801 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005802 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005803 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005804
Dan Gohman39946102009-01-25 16:29:12 +00005805 // Iterate over all the existing uses of From. New uses will be added
5806 // to the beginning of the use list, which we avoid visiting.
5807 // This specifically avoids visiting uses of From that arise while the
5808 // replacement is happening, because any such uses would be the result
5809 // of CSE: If an existing node looks like From after one of its operands
5810 // is replaced by To, we don't want to replace of all its users with To
5811 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005812 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005813 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005814 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005815 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005816
Chris Lattner8b8749f2005-08-17 19:00:20 +00005817 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005818 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005819
Dan Gohman39946102009-01-25 16:29:12 +00005820 // A user can appear in a use list multiple times, and when this
5821 // happens the uses are usually next to each other in the list.
5822 // To help reduce the number of CSE recomputations, process all
5823 // the uses of this user that we can find this way.
5824 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005825 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005826 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005827 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005828 } while (UI != UE && *UI == User);
5829
5830 // Now that we have modified User, add it back to the CSE maps. If it
5831 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005832 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005833 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005834
5835 // If we just RAUW'd the root, take note.
5836 if (FromN == getRoot())
5837 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005838}
5839
5840/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5841/// This can cause recursive merging of nodes in the DAG.
5842///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005843/// This version assumes that for each value of From, there is a
5844/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005845///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005846void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005847#ifndef NDEBUG
5848 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5849 assert((!From->hasAnyUseOfValue(i) ||
5850 From->getValueType(i) == To->getValueType(i)) &&
5851 "Cannot use this version of ReplaceAllUsesWith!");
5852#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005853
5854 // Handle the trivial case.
5855 if (From == To)
5856 return;
5857
Dan Gohmandbe664a2009-01-19 21:44:21 +00005858 // Iterate over just the existing users of From. See the comments in
5859 // the ReplaceAllUsesWith above.
5860 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005861 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005862 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005863 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005864
Chris Lattner8b52f212005-08-26 18:36:28 +00005865 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005866 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005867
Dan Gohman39946102009-01-25 16:29:12 +00005868 // A user can appear in a use list multiple times, and when this
5869 // happens the uses are usually next to each other in the list.
5870 // To help reduce the number of CSE recomputations, process all
5871 // the uses of this user that we can find this way.
5872 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005873 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005874 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005875 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005876 } while (UI != UE && *UI == User);
5877
5878 // Now that we have modified User, add it back to the CSE maps. If it
5879 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005880 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005881 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005882
5883 // If we just RAUW'd the root, take note.
5884 if (From == getRoot().getNode())
5885 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005886}
5887
Chris Lattner8b52f212005-08-26 18:36:28 +00005888/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5889/// This can cause recursive merging of nodes in the DAG.
5890///
5891/// This version can replace From with any result values. To must match the
5892/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005893void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005894 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005895 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005896
Dan Gohmandbe664a2009-01-19 21:44:21 +00005897 // Iterate over just the existing users of From. See the comments in
5898 // the ReplaceAllUsesWith above.
5899 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005900 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005901 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005902 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005903
Chris Lattner7b2880c2005-08-24 22:44:39 +00005904 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005905 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005906
Dan Gohman39946102009-01-25 16:29:12 +00005907 // A user can appear in a use list multiple times, and when this
5908 // happens the uses are usually next to each other in the list.
5909 // To help reduce the number of CSE recomputations, process all
5910 // the uses of this user that we can find this way.
5911 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005912 SDUse &Use = UI.getUse();
5913 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005914 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005915 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005916 } while (UI != UE && *UI == User);
5917
5918 // Now that we have modified User, add it back to the CSE maps. If it
5919 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005920 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005921 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005922
5923 // If we just RAUW'd the root, take note.
5924 if (From == getRoot().getNode())
5925 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005926}
5927
Chris Lattner012f2412006-02-17 21:58:01 +00005928/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005929/// uses of other values produced by From.getNode() alone. The Deleted
5930/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005931void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005932 // Handle the really simple, really trivial case efficiently.
5933 if (From == To) return;
5934
Chris Lattner012f2412006-02-17 21:58:01 +00005935 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005936 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005937 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005938 return;
5939 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005940
Dan Gohman39946102009-01-25 16:29:12 +00005941 // Iterate over just the existing users of From. See the comments in
5942 // the ReplaceAllUsesWith above.
5943 SDNode::use_iterator UI = From.getNode()->use_begin(),
5944 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005945 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005946 while (UI != UE) {
5947 SDNode *User = *UI;
5948 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005949
Dan Gohman39946102009-01-25 16:29:12 +00005950 // A user can appear in a use list multiple times, and when this
5951 // happens the uses are usually next to each other in the list.
5952 // To help reduce the number of CSE recomputations, process all
5953 // the uses of this user that we can find this way.
5954 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005955 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005956
5957 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005958 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005959 ++UI;
5960 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005961 }
Dan Gohman39946102009-01-25 16:29:12 +00005962
5963 // If this node hasn't been modified yet, it's still in the CSE maps,
5964 // so remove its old self from the CSE maps.
5965 if (!UserRemovedFromCSEMaps) {
5966 RemoveNodeFromCSEMaps(User);
5967 UserRemovedFromCSEMaps = true;
5968 }
5969
5970 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005971 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005972 } while (UI != UE && *UI == User);
5973
5974 // We are iterating over all uses of the From node, so if a use
5975 // doesn't use the specific value, no changes are made.
5976 if (!UserRemovedFromCSEMaps)
5977 continue;
5978
Chris Lattner01d029b2007-10-15 06:10:22 +00005979 // Now that we have modified User, add it back to the CSE maps. If it
5980 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005981 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005982 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005983
5984 // If we just RAUW'd the root, take note.
5985 if (From == getRoot())
5986 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005987}
5988
Dan Gohman39946102009-01-25 16:29:12 +00005989namespace {
5990 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5991 /// to record information about a use.
5992 struct UseMemo {
5993 SDNode *User;
5994 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005995 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005996 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005997
5998 /// operator< - Sort Memos by User.
5999 bool operator<(const UseMemo &L, const UseMemo &R) {
6000 return (intptr_t)L.User < (intptr_t)R.User;
6001 }
Dan Gohman39946102009-01-25 16:29:12 +00006002}
6003
Dan Gohmane8be6c62008-07-17 19:10:17 +00006004/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00006005/// uses of other values produced by From.getNode() alone. The same value
6006/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00006007/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00006008void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6009 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006010 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00006011 // Handle the simple, trivial case efficiently.
6012 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006013 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00006014
Dan Gohman39946102009-01-25 16:29:12 +00006015 // Read up all the uses and make records of them. This helps
6016 // processing new uses that are introduced during the
6017 // replacement process.
6018 SmallVector<UseMemo, 4> Uses;
6019 for (unsigned i = 0; i != Num; ++i) {
6020 unsigned FromResNo = From[i].getResNo();
6021 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00006022 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00006023 E = FromNode->use_end(); UI != E; ++UI) {
6024 SDUse &Use = UI.getUse();
6025 if (Use.getResNo() == FromResNo) {
6026 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00006027 Uses.push_back(Memo);
6028 }
Dan Gohmane7852d02009-01-26 04:35:06 +00006029 }
Dan Gohman39946102009-01-25 16:29:12 +00006030 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00006031
Dan Gohmane7852d02009-01-26 04:35:06 +00006032 // Sort the uses, so that all the uses from a given User are together.
6033 std::sort(Uses.begin(), Uses.end());
6034
Dan Gohman39946102009-01-25 16:29:12 +00006035 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6036 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00006037 // We know that this user uses some value of From. If it is the right
6038 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00006039 SDNode *User = Uses[UseIndex].User;
6040
6041 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00006042 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00006043
Dan Gohmane7852d02009-01-26 04:35:06 +00006044 // The Uses array is sorted, so all the uses for a given User
6045 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00006046 // To help reduce the number of CSE recomputations, process all
6047 // the uses of this user that we can find this way.
6048 do {
6049 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00006050 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00006051 ++UseIndex;
6052
Dan Gohmane7852d02009-01-26 04:35:06 +00006053 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00006054 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6055
Dan Gohmane8be6c62008-07-17 19:10:17 +00006056 // 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 Lattner012f2412006-02-17 21:58:01 +00006059 }
6060}
6061
Evan Chenge6f35d82006-08-01 08:20:41 +00006062/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00006063/// based on their topological order. It returns the maximum id and a vector
6064/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00006065unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00006066
Dan Gohmanf06c8352008-09-30 18:30:35 +00006067 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00006068
Dan Gohmanf06c8352008-09-30 18:30:35 +00006069 // SortedPos tracks the progress of the algorithm. Nodes before it are
6070 // sorted, nodes after it are unsorted. When the algorithm completes
6071 // it is at the end of the list.
6072 allnodes_iterator SortedPos = allnodes_begin();
6073
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006074 // Visit all the nodes. Move nodes with no operands to the front of
6075 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00006076 // operand count. Before we do this, the Node Id fields of the nodes
6077 // may contain arbitrary values. After, the Node Id fields for nodes
6078 // before SortedPos will contain the topological sort index, and the
6079 // Node Id fields for nodes At SortedPos and after will contain the
6080 // count of outstanding operands.
6081 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6082 SDNode *N = I++;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006083 checkForCycles(N, this);
Dan Gohmanf06c8352008-09-30 18:30:35 +00006084 unsigned Degree = N->getNumOperands();
6085 if (Degree == 0) {
6086 // A node with no uses, add it to the result array immediately.
6087 N->setNodeId(DAGSize++);
6088 allnodes_iterator Q = N;
6089 if (Q != SortedPos)
6090 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00006091 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006092 ++SortedPos;
6093 } else {
6094 // Temporarily use the Node Id as scratch space for the degree count.
6095 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00006096 }
6097 }
6098
Chad Rosierf496dea2012-05-21 17:13:41 +00006099 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00006100 // such that by the time the end is reached all nodes will be sorted.
6101 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
6102 SDNode *N = I;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006103 checkForCycles(N, this);
David Greene221925e2010-01-20 00:59:23 +00006104 // N is in sorted position, so all its uses have one less operand
6105 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00006106 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6107 UI != UE; ++UI) {
6108 SDNode *P = *UI;
6109 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00006110 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006111 --Degree;
6112 if (Degree == 0) {
6113 // All of P's operands are sorted, so P may sorted now.
6114 P->setNodeId(DAGSize++);
6115 if (P != SortedPos)
6116 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00006117 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006118 ++SortedPos;
6119 } else {
6120 // Update P's outstanding operand count.
6121 P->setNodeId(Degree);
6122 }
6123 }
David Greene221925e2010-01-20 00:59:23 +00006124 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00006125#ifndef NDEBUG
6126 SDNode *S = ++I;
6127 dbgs() << "Overran sorted position:\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006128 S->dumprFull(this); dbgs() << "\n";
6129 dbgs() << "Checking if this is due to cycles\n";
6130 checkForCycles(this, true);
David Greene39143702010-02-09 23:03:05 +00006131#endif
Stephen Hinesdce4a402014-05-29 02:49:00 -07006132 llvm_unreachable(nullptr);
David Greene221925e2010-01-20 00:59:23 +00006133 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00006134 }
6135
6136 assert(SortedPos == AllNodes.end() &&
6137 "Topological sort incomplete!");
6138 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6139 "First node in topological sort is not the entry token!");
6140 assert(AllNodes.front().getNodeId() == 0 &&
6141 "First node in topological sort has non-zero id!");
6142 assert(AllNodes.front().getNumOperands() == 0 &&
6143 "First node in topological sort has operands!");
6144 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6145 "Last node in topologic sort has unexpected id!");
6146 assert(AllNodes.back().use_empty() &&
6147 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006148 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006149 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00006150}
6151
Evan Chengbfcb3052010-03-25 01:38:16 +00006152/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6153/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00006154void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08006155 if (SD) {
6156 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
Evan Chengbfcb3052010-03-25 01:38:16 +00006157 SD->setHasDebugValue(true);
Stephen Hines37ed9c12014-12-01 14:51:49 -08006158 }
6159 DbgInfo->add(DB, SD, isParameter);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00006160}
Evan Cheng091cba12006-07-27 06:39:06 +00006161
Devang Patela2e868d2011-01-25 23:27:42 +00006162/// TransferDbgValues - Transfer SDDbgValues.
6163void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6164 if (From == To || !From.getNode()->getHasDebugValue())
6165 return;
6166 SDNode *FromNode = From.getNode();
6167 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006168 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00006169 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006170 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00006171 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00006172 SDDbgValue *Dbg = *I;
6173 if (Dbg->getKind() == SDDbgValue::SDNODE) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08006174 SDDbgValue *Clone =
6175 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6176 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6177 Dbg->getDebugLoc(), Dbg->getOrder());
Devang Patela778f5c2011-02-18 22:43:42 +00006178 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00006179 }
6180 }
Craig Topperf22fd3f2013-07-03 05:11:49 +00006181 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patela778f5c2011-02-18 22:43:42 +00006182 E = ClonedDVs.end(); I != E; ++I)
6183 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00006184}
6185
Jim Laskey58b968b2005-08-17 20:08:02 +00006186//===----------------------------------------------------------------------===//
6187// SDNode Class
6188//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00006189
Chris Lattner48b85922007-02-04 02:41:42 +00006190HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00006191 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00006192}
6193
Andrew Trickac6d9be2013-05-25 02:42:55 +00006194GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6195 DebugLoc DL, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006196 EVT VT, int64_t o, unsigned char TF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006197 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00006198 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00006199}
Chris Lattner48b85922007-02-04 02:41:42 +00006200
Matt Arsenault59d3ae62013-11-15 01:34:59 +00006201AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6202 SDValue X, unsigned SrcAS,
6203 unsigned DestAS)
6204 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6205 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6206
Andrew Trickac6d9be2013-05-25 02:42:55 +00006207MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6208 EVT memvt, MachineMemOperand *mmo)
6209 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006210 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006211 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006212 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00006213 assert(isNonTemporal() == MMO->isNonTemporal() &&
6214 "Non-temporal encoding error!");
Stephen Hines37ed9c12014-12-01 14:51:49 -08006215 // We check here that the size of the memory operand fits within the size of
6216 // the MMO. This is because the MMO might indicate only a possible address
6217 // range instead of specifying the affected memory addresses precisely.
6218 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00006219}
6220
Andrew Trickac6d9be2013-05-25 02:42:55 +00006221MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Stephen Hinesdce4a402014-05-29 02:49:00 -07006222 ArrayRef<SDValue> Ops, EVT memvt, MachineMemOperand *mmo)
6223 : SDNode(Opc, Order, dl, VTs, Ops),
Dan Gohmanc76909a2009-09-25 20:36:54 +00006224 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006225 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006226 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006227 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
Stephen Hines37ed9c12014-12-01 14:51:49 -08006228 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00006229}
6230
Jim Laskey583bd472006-10-27 23:46:08 +00006231/// Profile - Gather unique data for the node.
6232///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00006233void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00006234 AddNodeIDNode(ID, this);
6235}
6236
Owen Andersond8110fb2009-08-25 22:27:22 +00006237namespace {
6238 struct EVTArray {
6239 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006240
Owen Andersond8110fb2009-08-25 22:27:22 +00006241 EVTArray() {
6242 VTs.reserve(MVT::LAST_VALUETYPE);
6243 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6244 VTs.push_back(MVT((MVT::SimpleValueType)i));
6245 }
6246 };
6247}
6248
Owen Andersone50ed302009-08-10 22:56:29 +00006249static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00006250static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00006251static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00006252
Chris Lattnera3255112005-11-08 23:30:28 +00006253/// getValueTypeList - Return a pointer to the specified value type.
6254///
Owen Andersone50ed302009-08-10 22:56:29 +00006255const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006256 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00006257 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00006258 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00006259 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00006260 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00006261 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00006262 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00006263 }
Chris Lattnera3255112005-11-08 23:30:28 +00006264}
Duncan Sandsaf47b112007-10-16 09:56:48 +00006265
Chris Lattner5c884562005-01-12 18:37:47 +00006266/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6267/// indicated value. This method ignores uses of other values defined by this
6268/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00006269bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00006270 assert(Value < getNumValues() && "Bad value!");
6271
Roman Levensteindc1adac2008-04-07 10:06:32 +00006272 // TODO: Only iterate over uses of a given value of the node
6273 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00006274 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00006275 if (NUses == 0)
6276 return false;
6277 --NUses;
6278 }
Chris Lattner5c884562005-01-12 18:37:47 +00006279 }
6280
6281 // Found exactly the right number of uses?
6282 return NUses == 0;
6283}
6284
6285
Evan Cheng33d55952007-08-02 05:29:38 +00006286/// hasAnyUseOfValue - Return true if there are any use of the indicated
6287/// value. This method ignores uses of other values defined by this operation.
6288bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6289 assert(Value < getNumValues() && "Bad value!");
6290
Dan Gohman1373c1c2008-07-09 22:39:01 +00006291 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00006292 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00006293 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00006294
6295 return false;
6296}
6297
6298
Dan Gohman2a629952008-07-27 18:06:42 +00006299/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00006300///
Dan Gohman2a629952008-07-27 18:06:42 +00006301bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00006302 bool Seen = false;
6303 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006304 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00006305 if (User == this)
6306 Seen = true;
6307 else
6308 return false;
6309 }
6310
6311 return Seen;
6312}
6313
Evan Chenge6e97e62006-11-03 07:31:32 +00006314/// isOperand - Return true if this node is an operand of N.
6315///
Dan Gohman475871a2008-07-27 21:46:04 +00006316bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00006317 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6318 if (*this == N->getOperand(i))
6319 return true;
6320 return false;
6321}
6322
Evan Cheng917be682008-03-04 00:41:45 +00006323bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006324 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00006325 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006326 return true;
6327 return false;
6328}
Evan Cheng4ee62112006-02-05 06:29:23 +00006329
Chris Lattner572dee72008-01-16 05:49:24 +00006330/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00006331/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006332/// side-effecting instructions on any chain path. In practice, this looks
6333/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006334/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00006335bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00006336 unsigned Depth) const {
6337 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006338
Chris Lattner572dee72008-01-16 05:49:24 +00006339 // Don't search too deeply, we just want to be able to see through
6340 // TokenFactor's etc.
6341 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006342
Chris Lattner572dee72008-01-16 05:49:24 +00006343 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006344 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00006345 if (getOpcode() == ISD::TokenFactor) {
6346 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006347 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6348 return false;
6349 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00006350 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006351
Chris Lattner572dee72008-01-16 05:49:24 +00006352 // Loads don't have side effects, look through them.
6353 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6354 if (!Ld->isVolatile())
6355 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6356 }
6357 return false;
6358}
6359
Lang Hames944520f2011-07-07 04:31:51 +00006360/// hasPredecessor - Return true if N is a predecessor of this node.
6361/// N is either an operand of this node, or can be reached by recursively
6362/// traversing up the operands.
6363/// NOTE: This is an expensive method. Use it carefully.
6364bool SDNode::hasPredecessor(const SDNode *N) const {
6365 SmallPtrSet<const SDNode *, 32> Visited;
6366 SmallVector<const SDNode *, 16> Worklist;
6367 return hasPredecessorHelper(N, Visited, Worklist);
6368}
Dan Gohman6688d612009-10-28 03:44:30 +00006369
Craig Toppera0ec3f92013-07-14 04:42:23 +00006370bool
6371SDNode::hasPredecessorHelper(const SDNode *N,
Stephen Hines37ed9c12014-12-01 14:51:49 -08006372 SmallPtrSetImpl<const SDNode *> &Visited,
Craig Toppera0ec3f92013-07-14 04:42:23 +00006373 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames944520f2011-07-07 04:31:51 +00006374 if (Visited.empty()) {
6375 Worklist.push_back(this);
6376 } else {
6377 // Take a look in the visited set. If we've already encountered this node
6378 // we needn't search further.
6379 if (Visited.count(N))
6380 return true;
6381 }
6382
6383 // Haven't visited N yet. Continue the search.
6384 while (!Worklist.empty()) {
6385 const SDNode *M = Worklist.pop_back_val();
6386 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6387 SDNode *Op = M->getOperand(i).getNode();
Stephen Hines37ed9c12014-12-01 14:51:49 -08006388 if (Visited.insert(Op).second)
Dan Gohman6688d612009-10-28 03:44:30 +00006389 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006390 if (Op == N)
6391 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006392 }
Lang Hames944520f2011-07-07 04:31:51 +00006393 }
Dan Gohman6688d612009-10-28 03:44:30 +00006394
6395 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006396}
6397
Evan Chengc5484282006-10-04 00:56:09 +00006398uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6399 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006400 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006401}
6402
Mon P Wangcd6e7252009-11-30 02:42:02 +00006403SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6404 assert(N->getNumValues() == 1 &&
6405 "Can't unroll a vector with multiple results!");
6406
6407 EVT VT = N->getValueType(0);
6408 unsigned NE = VT.getVectorNumElements();
6409 EVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006410 SDLoc dl(N);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006411
6412 SmallVector<SDValue, 8> Scalars;
6413 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6414
6415 // If ResNE is 0, fully unroll the vector op.
6416 if (ResNE == 0)
6417 ResNE = NE;
6418 else if (NE > ResNE)
6419 NE = ResNE;
6420
6421 unsigned i;
6422 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006423 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006424 SDValue Operand = N->getOperand(j);
6425 EVT OperandVT = Operand.getValueType();
6426 if (OperandVT.isVector()) {
6427 // A vector operand; extract a single element.
6428 EVT OperandEltVT = OperandVT.getVectorElementType();
6429 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6430 OperandEltVT,
6431 Operand,
Tom Stellard425b76c2013-08-05 22:22:01 +00006432 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006433 } else {
6434 // A scalar operand; just use it as is.
6435 Operands[j] = Operand;
6436 }
6437 }
6438
6439 switch (N->getOpcode()) {
6440 default:
Stephen Hinesdce4a402014-05-29 02:49:00 -07006441 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006442 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006443 case ISD::VSELECT:
Stephen Hinesdce4a402014-05-29 02:49:00 -07006444 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
Nadav Rotemaec58612011-09-13 19:17:42 +00006445 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006446 case ISD::SHL:
6447 case ISD::SRA:
6448 case ISD::SRL:
6449 case ISD::ROTL:
6450 case ISD::ROTR:
6451 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter3af4d252013-09-09 22:02:08 +00006452 getShiftAmountOperand(Operands[0].getValueType(),
6453 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006454 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006455 case ISD::SIGN_EXTEND_INREG:
6456 case ISD::FP_ROUND_INREG: {
6457 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6458 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6459 Operands[0],
6460 getValueType(ExtVT)));
6461 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006462 }
6463 }
6464
6465 for (; i < ResNE; ++i)
6466 Scalars.push_back(getUNDEF(EltVT));
6467
6468 return getNode(ISD::BUILD_VECTOR, dl,
Stephen Hinesdce4a402014-05-29 02:49:00 -07006469 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006470}
6471
Evan Cheng64fa4a92009-12-09 01:36:00 +00006472
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006473/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6474/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006475/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006476bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006477 unsigned Bytes, int Dist) const {
6478 if (LD->getChain() != Base->getChain())
6479 return false;
6480 EVT VT = LD->getValueType(0);
6481 if (VT.getSizeInBits() / 8 != Bytes)
6482 return false;
6483
6484 SDValue Loc = LD->getOperand(1);
6485 SDValue BaseLoc = Base->getOperand(1);
6486 if (Loc.getOpcode() == ISD::FrameIndex) {
6487 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6488 return false;
6489 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6490 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6491 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6492 int FS = MFI->getObjectSize(FI);
6493 int BFS = MFI->getObjectSize(BFI);
6494 if (FS != BFS || FS != (int)Bytes) return false;
6495 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6496 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006497
6498 // Handle X+C
6499 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6500 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6501 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006502
Stephen Hinesdce4a402014-05-29 02:49:00 -07006503 const GlobalValue *GV1 = nullptr;
6504 const GlobalValue *GV2 = nullptr;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006505 int64_t Offset1 = 0;
6506 int64_t Offset2 = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006507 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6508 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Cheng64fa4a92009-12-09 01:36:00 +00006509 if (isGA1 && isGA2 && GV1 == GV2)
6510 return Offset1 == (Offset2 + Dist*Bytes);
6511 return false;
6512}
6513
6514
Evan Chengf2dc5c72009-12-09 01:04:59 +00006515/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6516/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006517unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006518 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006519 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006520 int64_t GVOffset = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006521 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaulte6e81122013-11-16 20:50:54 +00006522 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006523 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Stephen Hinesdce4a402014-05-29 02:49:00 -07006524 llvm::computeKnownBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
6525 TLI->getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006526 unsigned AlignBits = KnownZero.countTrailingOnes();
6527 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6528 if (Align)
6529 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006530 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006531
Evan Chengf2dc5c72009-12-09 01:04:59 +00006532 // If this is a direct reference to a stack slot, use information about the
6533 // stack slot's alignment.
6534 int FrameIdx = 1 << 31;
6535 int64_t FrameOffset = 0;
6536 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6537 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006538 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006539 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006540 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006541 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6542 FrameOffset = Ptr.getConstantOperandVal(1);
6543 }
6544
6545 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006546 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006547 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6548 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006549 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006550 }
6551
6552 return 0;
6553}
6554
Bill Wendlingee287ca2013-11-22 05:17:44 +00006555/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6556/// which is split (or expanded) into two not necessarily identical pieces.
6557std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6558 // Currently all types are split in half.
6559 EVT LoVT, HiVT;
6560 if (!VT.isVector()) {
6561 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6562 } else {
6563 unsigned NumElements = VT.getVectorNumElements();
6564 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6565 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6566 NumElements/2);
6567 }
6568 return std::make_pair(LoVT, HiVT);
6569}
6570
6571/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6572/// low/high part.
6573std::pair<SDValue, SDValue>
6574SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6575 const EVT &HiVT) {
6576 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6577 N.getValueType().getVectorNumElements() &&
6578 "More vector elements requested than available!");
6579 SDValue Lo, Hi;
6580 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6581 getConstant(0, TLI->getVectorIdxTy()));
6582 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6583 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6584 return std::make_pair(Lo, Hi);
6585}
6586
Stephen Hinesdce4a402014-05-29 02:49:00 -07006587void SelectionDAG::ExtractVectorElements(SDValue Op,
6588 SmallVectorImpl<SDValue> &Args,
6589 unsigned Start, unsigned Count) {
6590 EVT VT = Op.getValueType();
6591 if (Count == 0)
6592 Count = VT.getVectorNumElements();
6593
6594 EVT EltVT = VT.getVectorElementType();
6595 EVT IdxTy = TLI->getVectorIdxTy();
6596 SDLoc SL(Op);
6597 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
6598 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
6599 Op, getConstant(i, IdxTy)));
6600 }
6601}
6602
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006603// getAddressSpace - Return the address space this GlobalAddress belongs to.
6604unsigned GlobalAddressSDNode::getAddressSpace() const {
6605 return getGlobal()->getType()->getAddressSpace();
6606}
6607
6608
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006609Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006610 if (isMachineConstantPoolEntry())
6611 return Val.MachineCPVal->getType();
6612 return Val.ConstVal->getType();
6613}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006614
Bob Wilson24e338e2009-03-02 23:24:16 +00006615bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6616 APInt &SplatUndef,
6617 unsigned &SplatBitSize,
6618 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006619 unsigned MinSplatBits,
Stephen Hines36b56882014-04-23 16:57:46 -07006620 bool isBigEndian) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006621 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006622 assert(VT.isVector() && "Expected a vector type");
6623 unsigned sz = VT.getSizeInBits();
6624 if (MinSplatBits > sz)
6625 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006626
Bob Wilson24e338e2009-03-02 23:24:16 +00006627 SplatValue = APInt(sz, 0);
6628 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006629
Bob Wilson24e338e2009-03-02 23:24:16 +00006630 // Get the bits. Bits with undefined values (when the corresponding element
6631 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6632 // in SplatValue. If any of the values are not constant, give up and return
6633 // false.
6634 unsigned int nOps = getNumOperands();
6635 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6636 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006637
6638 for (unsigned j = 0; j < nOps; ++j) {
6639 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006640 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006641 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006642
Bob Wilson24e338e2009-03-02 23:24:16 +00006643 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006644 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006645 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006646 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006647 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006648 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006649 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006650 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006651 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006652 }
6653
Bob Wilson24e338e2009-03-02 23:24:16 +00006654 // The build_vector is all constants or undefs. Find the smallest element
6655 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006656
Bob Wilson24e338e2009-03-02 23:24:16 +00006657 HasAnyUndefs = (SplatUndef != 0);
6658 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006659
Bob Wilson24e338e2009-03-02 23:24:16 +00006660 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006661 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6662 APInt LowValue = SplatValue.trunc(HalfSize);
6663 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6664 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006665
Bob Wilson24e338e2009-03-02 23:24:16 +00006666 // If the two halves do not match (ignoring undef bits), stop here.
6667 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6668 MinSplatBits > HalfSize)
6669 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006670
Bob Wilson24e338e2009-03-02 23:24:16 +00006671 SplatValue = HighValue | LowValue;
6672 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006673
Bob Wilson24e338e2009-03-02 23:24:16 +00006674 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006675 }
6676
Bob Wilson24e338e2009-03-02 23:24:16 +00006677 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006678 return true;
6679}
Nate Begeman9008ca62009-04-27 18:41:29 +00006680
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006681SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
6682 if (UndefElements) {
6683 UndefElements->clear();
6684 UndefElements->resize(getNumOperands());
6685 }
6686 SDValue Splatted;
6687 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6688 SDValue Op = getOperand(i);
6689 if (Op.getOpcode() == ISD::UNDEF) {
6690 if (UndefElements)
6691 (*UndefElements)[i] = true;
6692 } else if (!Splatted) {
6693 Splatted = Op;
6694 } else if (Splatted != Op) {
6695 return SDValue();
6696 }
6697 }
Stephen Hines36b56882014-04-23 16:57:46 -07006698
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006699 if (!Splatted) {
6700 assert(getOperand(0).getOpcode() == ISD::UNDEF &&
6701 "Can only have a splat without a constant for all undefs.");
6702 return getOperand(0);
6703 }
Stephen Hines36b56882014-04-23 16:57:46 -07006704
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006705 return Splatted;
6706}
6707
6708ConstantSDNode *
6709BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
6710 return dyn_cast_or_null<ConstantSDNode>(
6711 getSplatValue(UndefElements).getNode());
6712}
6713
6714ConstantFPSDNode *
6715BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
6716 return dyn_cast_or_null<ConstantFPSDNode>(
6717 getSplatValue(UndefElements).getNode());
Stephen Hines36b56882014-04-23 16:57:46 -07006718}
6719
6720bool BuildVectorSDNode::isConstant() const {
6721 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
6722 unsigned Opc = getOperand(i).getOpcode();
6723 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
6724 return false;
6725 }
6726 return true;
6727}
6728
Owen Andersone50ed302009-08-10 22:56:29 +00006729bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006730 // Find the first non-undef value in the shuffle mask.
6731 unsigned i, e;
6732 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6733 /* search */;
6734
Nate Begemana6415752009-04-29 18:13:31 +00006735 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006736
Nate Begeman5a5ca152009-04-29 05:20:52 +00006737 // Make sure all remaining elements are either undef or the same as the first
6738 // non-undef value.
6739 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006740 if (Mask[i] >= 0 && Mask[i] != Idx)
6741 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006742 return true;
6743}
David Greenecf495bc2010-01-20 20:13:31 +00006744
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006745#ifndef NDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006746static void checkForCyclesHelper(const SDNode *N,
Stephen Hines37ed9c12014-12-01 14:51:49 -08006747 SmallPtrSetImpl<const SDNode*> &Visited,
6748 SmallPtrSetImpl<const SDNode*> &Checked,
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006749 const llvm::SelectionDAG *DAG) {
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006750 // If this node has already been checked, don't check it again.
6751 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006752 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006753
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006754 // If a node has already been visited on this depth-first walk, reject it as
6755 // a cycle.
Stephen Hines37ed9c12014-12-01 14:51:49 -08006756 if (!Visited.insert(N).second) {
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006757 errs() << "Detected cycle in SelectionDAG\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006758 dbgs() << "Offending node:\n";
6759 N->dumprFull(DAG); dbgs() << "\n";
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006760 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006761 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006762
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006763 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006764 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006765
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006766 Checked.insert(N);
6767 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006768}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006769#endif
David Greenecf495bc2010-01-20 20:13:31 +00006770
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006771void llvm::checkForCycles(const llvm::SDNode *N,
6772 const llvm::SelectionDAG *DAG,
6773 bool force) {
6774#ifndef NDEBUG
6775 bool check = force;
David Greenecf495bc2010-01-20 20:13:31 +00006776#ifdef XDEBUG
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006777 check = true;
6778#endif // XDEBUG
6779 if (check) {
6780 assert(N && "Checking nonexistent SDNode");
6781 SmallPtrSet<const SDNode*, 32> visited;
6782 SmallPtrSet<const SDNode*, 32> checked;
6783 checkForCyclesHelper(N, visited, checked, DAG);
6784 }
6785#endif // !NDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006786}
6787
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07006788void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
6789 checkForCycles(DAG->getRoot().getNode(), DAG, force);
David Greenecf495bc2010-01-20 20:13:31 +00006790}